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.
31 gi
.require_version('RwDts', '1.0')
32 gi
.require_version('RwPkgMgmtYang', '1.0')
33 from gi
.repository
import (
37 from rift
.tasklets
.rwpkgmgr
.proxy
import filesystem
39 import rift
.tasklets
.rwpkgmgr
.publisher
as pkg_publisher
40 import rift
.tasklets
.rwpkgmgr
.rpc
as rpc
42 from rift
.mano
.utils
.project
import ManoProject
, DEFAULT_PROJECT
44 TEST_STRING
= "foobar"
47 class MockPublisher(object):
48 def __init__(self
, uid
):
52 def register_downloader(self
, *args
):
53 return self
.assert_uid
56 class MockProject(ManoProject
):
57 def __init__(self
, log
, uid
=None):
58 super().__init
__(log
, name
=DEFAULT_PROJECT
)
59 self
.job_handler
= MockPublisher(uid
)
63 def __init__(self
, log
, uid
=None):
66 project
= MockProject(self
.log
,
68 project
.publisher
= None
69 self
.projects
[project
.name
] = project
72 class TestCase(rift
.test
.dts
.AbstractDTSTest
):
74 def configure_schema(cls
):
75 return RwPkgMgmtYang
.get_schema()
78 def configure_timeout(cls
):
81 def configure_test(self
, loop
, test_id
):
82 self
.log
.debug("STARTING - %s", test_id
)
83 self
.tinfo
= self
.new_tinfo(str(test_id
))
84 self
.dts
= rift
.tasklets
.DTS(self
.tinfo
, self
.schema
, self
.loop
)
89 def create_mock_package(self
):
90 uid
= str(uuid
.uuid4())
92 os
.getenv('RIFT_ARTIFACTS'),
93 "launchpad/packages/vnfd",
96 package_path
= os
.path
.join(path
, "pong_vnfd")
98 os
.makedirs(package_path
)
99 open(os
.path
.join(path
, "pong_vnfd.xml"), "wb").close()
100 open(os
.path
.join(path
, "logo.png"), "wb").close()
104 @rift.test
.dts
.async_test
105 def test_endpoint_discovery(self
):
107 Verifies the following:
108 The endpoint RPC returns a URL
110 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
)
111 endpoint
= rpc
.EndpointDiscoveryRpcHandler(self
.log
, self
.dts
, self
.loop
, proxy
)
112 yield from endpoint
.register()
114 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_GetPackageEndpoint
.from_dict({
115 "package_type": "VNFD",
116 "package_id": "BLAHID",
117 "project_name": DEFAULT_PROJECT
})
119 rpc_out
= yield from self
.dts
.query_rpc(
120 "I,/get-package-endpoint",
121 rwdts
.XactFlag
.TRACE
,
125 result
= yield from itr
126 assert result
.result
.endpoint
== 'https://127.0.0.1:4567/api/package/vnfd/BLAHID'
128 @rift.test
.dts
.async_test
129 def test_schema_rpc(self
):
131 Verifies the following:
132 The schema RPC return the schema structure
134 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
)
135 endpoint
= rpc
.SchemaRpcHandler(self
.log
, self
.dts
, self
.loop
, proxy
)
136 yield from endpoint
.register()
138 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_GetPackageSchema
.from_dict({
139 "package_type": "VNFD",
140 "project_name": DEFAULT_PROJECT
})
142 rpc_out
= yield from self
.dts
.query_rpc(
143 "I,/get-package-schema",
144 rwdts
.XactFlag
.TRACE
,
148 result
= yield from itr
149 assert "charms" in result
.result
.schema
151 @rift.test
.dts
.async_test
152 def test_file_proxy_rpc(self
):
154 1. The file RPC returns a valid UUID thro' DTS
156 assert_uid
= str(uuid
.uuid4())
158 uid
, path
= self
.create_mock_package()
160 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
)
161 endpoint
= rpc
.PackageOperationsRpcHandler(
166 MockTasklet(self
.log
, uid
=assert_uid
))
167 yield from endpoint
.register()
169 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileAdd
.from_dict({
170 "package_type": "VNFD",
172 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
173 "package_path": "script/rift-shell",
174 "project_name": DEFAULT_PROJECT
})
176 rpc_out
= yield from self
.dts
.query_rpc(
177 "I,/rw-pkg-mgmt:package-file-add",
178 rwdts
.XactFlag
.TRACE
,
182 result
= yield from itr
183 assert result
.result
.task_id
== assert_uid
187 @rift.test
.dts
.async_test
188 def test_file_add_workflow(self
):
191 1. Verify the end to end flow of package ADD (NO MOCKS)
193 uid
, path
= self
.create_mock_package()
195 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
)
196 tasklet
= MockTasklet(self
.log
, uid
=uid
)
197 project
= tasklet
.projects
[DEFAULT_PROJECT
]
198 publisher
= pkg_publisher
.DownloadStatusPublisher(self
.log
, self
.dts
, self
.loop
, project
)
199 project
.job_handler
= publisher
200 endpoint
= rpc
.PackageOperationsRpcHandler(
207 yield from publisher
.register()
208 yield from endpoint
.register()
210 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileAdd
.from_dict({
211 "package_type": "VNFD",
213 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
214 "package_path": "icons/rift-shell",
215 "project_name": DEFAULT_PROJECT
})
217 rpc_out
= yield from self
.dts
.query_rpc(
218 "I,/rw-pkg-mgmt:package-file-add",
219 rwdts
.XactFlag
.TRACE
,
222 yield from asyncio
.sleep(5, loop
=self
.loop
)
223 filepath
= os
.path
.join(path
, ip
.package_path
)
224 self
.log
.debug("Filepath: {}".format(filepath
))
225 assert os
.path
.isfile(filepath
)
226 mode
= oct(os
.stat(filepath
)[stat
.ST_MODE
])
227 assert str(mode
) == "0o100664"
232 @rift.test
.dts
.async_test
233 def test_file_delete_workflow(self
):
236 1. Verify the end to end flow of package ADD (NO MOCKS)
238 uid
, path
= self
.create_mock_package()
240 proxy
= filesystem
.FileSystemProxy(self
.loop
, self
.log
)
241 endpoint
= rpc
.PackageDeleteOperationsRpcHandler(
247 yield from endpoint
.register()
249 ip
= RwPkgMgmtYang
.YangInput_RwPkgMgmt_PackageFileDelete
.from_dict({
250 "package_type": "VNFD",
252 "package_path": "logo.png",
253 "project_name": DEFAULT_PROJECT
})
255 assert os
.path
.isfile(os
.path
.join(path
, ip
.package_path
))
257 rpc_out
= yield from self
.dts
.query_rpc(
258 "I,/rw-pkg-mgmt:package-file-delete",
259 rwdts
.XactFlag
.TRACE
,
262 yield from asyncio
.sleep(5, loop
=self
.loop
)
263 assert not os
.path
.isfile(os
.path
.join(path
, ip
.package_path
))
268 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
270 parser
= argparse
.ArgumentParser()
271 parser
.add_argument('-v', '--verbose', action
='store_true')
272 parser
.add_argument('-n', '--no-runner', action
='store_true')
273 args
, unittest_args
= parser
.parse_known_args()
277 logging
.basicConfig(format
='TEST %(message)s')
278 logging
.getLogger().setLevel(logging
.DEBUG
)
281 unittest
.main(testRunner
=runner
, argv
=[sys
.argv
[0]] + unittest_args
)
283 if __name__
== '__main__':