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 # Setting RIFT_VAR_ROOT if not already set for unit test execution
31 if "RIFT_VAR_ROOT" not in os
.environ
:
32 os
.environ
['RIFT_VAR_ROOT'] = os
.path
.join(os
.environ
['RIFT_INSTALL'], 'var/rift/unittest')
35 gi
.require_version('RwDts', '1.0')
36 gi
.require_version('RwPkgMgmtYang', '1.0')
37 from gi
.repository
import (
41 from rift
.tasklets
.rwpkgmgr
.proxy
import filesystem
43 import rift
.tasklets
.rwpkgmgr
.publisher
as pkg_publisher
44 import rift
.tasklets
.rwpkgmgr
.rpc
as rpc
46 from rift
.mano
.utils
.project
import ManoProject
, DEFAULT_PROJECT
48 TEST_STRING
= "foobar"
51 class MockPublisher(object):
52 def __init__(self
, uid
):
56 def register_downloader(self
, *args
):
57 return self
.assert_uid
60 class MockProject(ManoProject
):
61 def __init__(self
, log
, uid
=None):
62 super().__init
__(log
, name
=DEFAULT_PROJECT
)
63 self
.job_handler
= MockPublisher(uid
)
67 def __init__(self
, log
, uid
=None):
70 project
= MockProject(self
.log
,
72 project
.publisher
= None
73 self
.projects
[project
.name
] = project
76 class TestCase(rift
.test
.dts
.AbstractDTSTest
):
78 def configure_schema(cls
):
79 return RwPkgMgmtYang
.get_schema()
82 def configure_timeout(cls
):
85 def configure_test(self
, loop
, test_id
):
86 self
.log
.debug("STARTING - %s", test_id
)
87 self
.tinfo
= self
.new_tinfo(str(test_id
))
88 self
.dts
= rift
.tasklets
.DTS(self
.tinfo
, self
.schema
, self
.loop
)
93 def create_mock_package(self
, project
):
94 uid
= str(uuid
.uuid4())
96 os
.getenv('RIFT_VAR_ROOT'),
97 "launchpad/packages/vnfd",
101 asset_path
= os
.path
.join(path
, "icons")
103 os
.makedirs(asset_path
)
104 open(os
.path
.join(path
, "pong_vnfd.xml"), "wb").close()
105 open(os
.path
.join(asset_path
, "logo.png"), "wb").close()
109 @rift.test
.dts
.async_test
110 def test_endpoint_discovery(self
):
112 Verifies the following:
113 The endpoint RPC returns a URL
115 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
, self
.dts
)
116 endpoint
= rpc
.EndpointDiscoveryRpcHandler(self
.log
, self
.dts
, self
.loop
, proxy
)
117 yield from endpoint
.register()
119 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_GetPackageEndpoint
.from_dict({
120 "package_type": "VNFD",
121 "package_id": "BLAHID",
122 "project_name": DEFAULT_PROJECT
})
124 rpc_out
= yield from self
.dts
.query_rpc(
125 "I,/get-package-endpoint",
126 rwdts
.XactFlag
.TRACE
,
130 result
= yield from itr
131 assert result
.result
.endpoint
== 'https://127.0.0.1:8008/mano/api/package/vnfd/{}/BLAHID'.format(DEFAULT_PROJECT
)
133 @rift.test
.dts
.async_test
134 def test_schema_rpc(self
):
136 Verifies the following:
137 The schema RPC return the schema structure
139 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
, self
.dts
)
140 endpoint
= rpc
.SchemaRpcHandler(self
.log
, self
.dts
, self
.loop
, proxy
)
141 yield from endpoint
.register()
143 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_GetPackageSchema
.from_dict({
144 "package_type": "VNFD",
145 "project_name": DEFAULT_PROJECT
})
147 rpc_out
= yield from self
.dts
.query_rpc(
148 "I,/get-package-schema",
149 rwdts
.XactFlag
.TRACE
,
153 result
= yield from itr
154 assert "charms" in result
.result
.schema
156 @rift.test
.dts
.async_test
157 def test_file_proxy_rpc(self
):
159 1. The file RPC returns a valid UUID thro' DTS
161 assert_uid
= str(uuid
.uuid4())
163 uid
, path
= self
.create_mock_package(DEFAULT_PROJECT
)
165 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
, self
.dts
)
166 endpoint
= rpc
.PackageOperationsRpcHandler(
171 MockTasklet(self
.log
, uid
=assert_uid
))
172 yield from endpoint
.register()
174 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileAdd
.from_dict({
175 "package_type": "VNFD",
177 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
178 "package_path": "script/rift-shell",
179 "project_name": DEFAULT_PROJECT
})
181 rpc_out
= yield from self
.dts
.query_rpc(
182 "I,/rw-pkg-mgmt:package-file-add",
183 rwdts
.XactFlag
.TRACE
,
187 result
= yield from itr
188 assert result
.result
.task_id
== assert_uid
192 @rift.test
.dts
.async_test
193 def test_file_add_workflow(self
):
196 1. Verify the end to end flow of package ADD (NO MOCKS)
198 uid
, path
= self
.create_mock_package(DEFAULT_PROJECT
)
200 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
, self
.dts
)
201 tasklet
= MockTasklet(self
.log
, uid
=uid
)
202 project
= tasklet
.projects
[DEFAULT_PROJECT
]
203 publisher
= pkg_publisher
.DownloadStatusPublisher(self
.log
, self
.dts
, self
.loop
, project
)
204 project
.job_handler
= publisher
205 endpoint
= rpc
.PackageOperationsRpcHandler(
212 yield from publisher
.register()
213 yield from endpoint
.register()
215 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileAdd
.from_dict({
216 "package_type": "VNFD",
218 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
219 "project_name": DEFAULT_PROJECT
,
220 "vnfd_file_type": "ICONS",
221 "package_path": "rift-shell"})
223 rpc_out
= yield from self
.dts
.query_rpc(
224 "I,/rw-pkg-mgmt:package-file-add",
225 rwdts
.XactFlag
.TRACE
,
228 yield from asyncio
.sleep(5, loop
=self
.loop
)
229 filepath
= os
.path
.join(path
, ip
.vnfd_file_type
.lower(), ip
.package_path
)
230 self
.log
.debug("Filepath: {}".format(filepath
))
231 assert os
.path
.isfile(filepath
)
232 mode
= oct(os
.stat(filepath
)[stat
.ST_MODE
])
233 assert str(mode
) == "0o100664"
238 @rift.test
.dts
.async_test
239 def test_file_delete_workflow(self
):
242 1. Verify the end to end flow of package ADD (NO MOCKS)
244 uid
, path
= self
.create_mock_package(DEFAULT_PROJECT
)
246 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
, self
.dts
)
247 endpoint
= rpc
.PackageDeleteOperationsRpcHandler(
253 yield from endpoint
.register()
255 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileDelete
.from_dict({
256 "package_type": "VNFD",
258 "package_path": "logo.png",
259 "vnfd_file_type": "ICONS",
260 "project_name": DEFAULT_PROJECT
})
262 assert os
.path
.isfile(os
.path
.join(path
, ip
.vnfd_file_type
.lower(), ip
.package_path
))
264 rpc_out
= yield from self
.dts
.query_rpc(
265 "I,/rw-pkg-mgmt:package-file-delete",
266 rwdts
.XactFlag
.TRACE
,
269 yield from asyncio
.sleep(5, loop
=self
.loop
)
270 assert not os
.path
.isfile(os
.path
.join(path
, ip
.vnfd_file_type
.lower(), ip
.package_path
))
275 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
277 parser
= argparse
.ArgumentParser()
278 parser
.add_argument('-v', '--verbose', action
='store_true')
279 parser
.add_argument('-n', '--no-runner', action
='store_true')
280 args
, unittest_args
= parser
.parse_known_args()
284 logging
.basicConfig(format
='TEST %(message)s')
285 logging
.getLogger().setLevel(logging
.DEBUG
)
288 unittest
.main(testRunner
=runner
, argv
=[sys
.argv
[0]] + unittest_args
)
290 if __name__
== '__main__':