4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
30 gi
.require_version('RwDts', '1.0')
31 gi
.require_version('RwPkgMgmtYang', '1.0')
32 from gi
.repository
import (
36 import rift
.tasklets
.rwpkgmgr
.downloader
as downloader
37 import rift
.tasklets
.rwpkgmgr
.publisher
as pkg_publisher
39 from rift
.mano
.utils
.project
import ManoProject
, DEFAULT_PROJECT
42 class TestCase(rift
.test
.dts
.AbstractDTSTest
):
44 def configure_schema(cls
):
45 return RwPkgMgmtYang
.get_schema()
48 def configure_timeout(cls
):
51 def configure_test(self
, loop
, test_id
):
52 self
.log
.debug("STARTING - %s", test_id
)
53 self
.tinfo
= self
.new_tinfo(str(test_id
))
54 self
.dts
= rift
.tasklets
.DTS(self
.tinfo
, self
.schema
, self
.loop
)
55 self
.project
= ManoProject(self
.log
, name
=DEFAULT_PROJECT
)
57 self
.job_handler
= pkg_publisher
.DownloadStatusPublisher(self
.log
, self
.dts
,
58 self
.loop
, self
.project
)
64 def get_published_xpaths(self
):
65 published_xpaths
= set()
67 res_iter
= yield from self
.dts
.query_read("D,/rwdts:dts")
69 res
= (yield from i
).result
70 for member
in res
.member
:
71 published_xpaths |
= {reg
.keyspec
for reg
in member
.state
.registration
if reg
.flags
== "publisher"}
73 return published_xpaths
76 def read_xpath(self
, xpath
):
77 itr
= yield from self
.dts
.query_read(xpath
)
81 result
= yield from fut
84 @rift.test
.dts
.async_test
85 def test_download_publisher(self
):
86 yield from self
.job_handler
.register()
87 published_xpaths
= yield from self
.get_published_xpaths()
88 assert self
.job_handler
.xpath() in published_xpaths
90 @rift.test
.dts
.async_test
91 def test_publish(self
):
94 1. Verify if an update on_download_progess & on_download_finished
96 2. Verify if the internal store is updated
98 yield from self
.job_handler
.register()
100 mock_msg
= RwPkgMgmtYang
.DownloadJob
.from_dict({
101 "url": "http://foo/bar",
103 "download_id": str(uuid
.uuid4())})
105 yield from self
.job_handler
._dts
_publisher
(mock_msg
)
106 yield from asyncio
.sleep(5, loop
=self
.loop
)
108 xpath
= self
.project
.add_project("/download-jobs/job[download-id='{}']".
109 format(mock_msg
.download_id
))
110 itr
= yield from self
.dts
.query_read(xpath
)
114 result
= yield from fut
115 result
= result
.result
117 self
.log
.debug("Mock msg: {}".format(mock_msg
))
118 assert result
== mock_msg
121 mock_msg
.url
= "http://bar/foo"
122 yield from self
.job_handler
._dts
_publisher
(mock_msg
)
123 yield from asyncio
.sleep(5, loop
=self
.loop
)
125 itr
= yield from self
.dts
.query_read(xpath
)
129 result
= yield from fut
130 result
= result
.result
131 assert result
== mock_msg
134 @rift.test
.dts
.async_test
135 def test_url_download(self
):
138 Test the updates with download/url.py
140 yield from self
.job_handler
.register()
142 proxy
= mock
.MagicMock()
144 url
= "http://boson.eng.riftio.com/common/unittests/plantuml.jar"
145 url_downloader
= downloader
.PackageFileDownloader(url
, "1", "/", "VNFD", proxy
)
147 download_id
= yield from self
.job_handler
.register_downloader(url_downloader
)
148 assert download_id
is not None
150 # Waiting to be sure that the file is downloaded
151 # From BLR, it sometimes take longer for the file to
156 yield from asyncio
.sleep(5, loop
=self
.loop
)
157 xpath
= self
.project
.add_project("/download-jobs/job[download-id='{}']".
159 result
= yield from self
.read_xpath(xpath
)
160 self
.log
.debug("Test result before complete check - %s", result
)
161 if result
.status
!= "COMPLETED":
162 total_time
= total_time
+ 5
163 if total_time
<= max_time
:
168 assert result
.status
== "COMPLETED"
169 assert len(self
.job_handler
.tasks
) == 0
172 @rift.test
.dts
.async_test
173 def test_cancelled(self
):
176 1. Test the updates with downloader.py
177 2. Verifies if cancel triggers the job status to move to cancelled
179 yield from self
.job_handler
.register()
181 proxy
= mock
.MagicMock()
182 url
= "http://boson.eng.riftio.com/common/unittests/Fedora-x86_64-20-20131211.1-sda-ping.qcow2"
183 url_downloader
= downloader
.PackageFileDownloader(url
, "1", "/", "VNFD", proxy
)
185 download_id
= yield from self
.job_handler
.register_downloader(url_downloader
)
186 assert download_id
is not None
187 xpath
= self
.project
.add_project("/download-jobs/job[download-id='{}']".
190 yield from asyncio
.sleep(10, loop
=self
.loop
)
192 result
= yield from self
.read_xpath(xpath
)
193 self
.log
.debug("Test result before in_progress check - %s", result
)
194 assert result
.status
== "IN_PROGRESS"
196 yield from self
.job_handler
.cancel_download(download_id
)
197 yield from asyncio
.sleep(3, loop
=self
.loop
)
198 result
= yield from self
.read_xpath(xpath
)
199 self
.log
.debug("Test result before cancel check - %s", result
)
200 assert result
.status
== "CANCELLED"
201 assert len(self
.job_handler
.tasks
) == 0
205 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
207 parser
= argparse
.ArgumentParser()
208 parser
.add_argument('-v', '--verbose', action
='store_true')
209 parser
.add_argument('-n', '--no-runner', action
='store_true')
210 args
, unittest_args
= parser
.parse_known_args()
214 TestCase
.log_level
= logging
.DEBUG
if args
.verbose
else logging
.WARN
216 unittest
.main(testRunner
=runner
, argv
=[sys
.argv
[0]] + unittest_args
)
218 if __name__
== '__main__':