[RIFT 16414, 16415, 16416] Unittest fixes to conform to the new package structure...
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / test / utest_filesystem_proxy_dts.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18
19 import asyncio
20 import argparse
21 import logging
22 import os
23 import shutil
24 import stat
25 import sys
26 import unittest
27 import uuid
28 import xmlrunner
29
30 import gi
31 gi.require_version('RwDts', '1.0')
32 gi.require_version('RwPkgMgmtYang', '1.0')
33 from gi.repository import (
34 RwDts as rwdts,
35 RwPkgMgmtYang,
36 )
37 from rift.tasklets.rwpkgmgr.proxy import filesystem
38
39 import rift.tasklets.rwpkgmgr.publisher as pkg_publisher
40 import rift.tasklets.rwpkgmgr.rpc as rpc
41 import rift.test.dts
42
43 TEST_STRING = "foobar"
44
45 class TestCase(rift.test.dts.AbstractDTSTest):
46 @classmethod
47 def configure_schema(cls):
48 return RwPkgMgmtYang.get_schema()
49
50 @classmethod
51 def configure_timeout(cls):
52 return 240
53
54 def configure_test(self, loop, test_id):
55 self.log.debug("STARTING - %s", test_id)
56 self.tinfo = self.new_tinfo(str(test_id))
57 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
58
59 def tearDown(self):
60 super().tearDown()
61
62 def create_mock_package(self):
63 uid = str(uuid.uuid4())
64 path = os.path.join(
65 os.getenv('RIFT_ARTIFACTS'),
66 "launchpad/packages/vnfd",
67 uid)
68
69 asset_path = os.path.join(path, "icons")
70
71 os.makedirs(asset_path)
72 open(os.path.join(path, "pong_vnfd.xml"), "wb").close()
73 open(os.path.join(asset_path, "logo.png"), "wb").close()
74
75 return uid, path
76
77 @rift.test.dts.async_test
78 def test_endpoint_discovery(self):
79 """
80 Verifies the following:
81 The endpoint RPC returns a URL
82 """
83 proxy = filesystem.FileSystemProxy(self.loop, self.log)
84 endpoint = rpc.EndpointDiscoveryRpcHandler(self.log, self.dts, self.loop, proxy)
85 yield from endpoint.register()
86
87 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageEndpoint.from_dict({
88 "package_type": "VNFD",
89 "package_id": "BLAHID"})
90
91 rpc_out = yield from self.dts.query_rpc(
92 "I,/get-package-endpoint",
93 rwdts.XactFlag.TRACE,
94 ip)
95
96 for itr in rpc_out:
97 result = yield from itr
98 assert result.result.endpoint == 'https://127.0.0.1:4567/api/package/vnfd/BLAHID'
99
100 @rift.test.dts.async_test
101 def test_schema_rpc(self):
102 """
103 Verifies the following:
104 The schema RPC return the schema structure
105 """
106 proxy = filesystem.FileSystemProxy(self.loop, self.log)
107 endpoint = rpc.SchemaRpcHandler(self.log, self.dts, self.loop, proxy)
108 yield from endpoint.register()
109
110 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageSchema.from_dict({
111 "package_type": "VNFD"})
112
113 rpc_out = yield from self.dts.query_rpc(
114 "I,/get-package-schema",
115 rwdts.XactFlag.TRACE,
116 ip)
117
118 for itr in rpc_out:
119 result = yield from itr
120 assert "charms" in result.result.schema
121
122 @rift.test.dts.async_test
123 def test_file_proxy_rpc(self):
124 """
125 1. The file RPC returns a valid UUID thro' DTS
126 """
127 assert_uid = str(uuid.uuid4())
128 class MockPublisher:
129 @asyncio.coroutine
130 def register_downloader(self, *args):
131 return assert_uid
132
133 uid, path = self.create_mock_package()
134
135 proxy = filesystem.FileSystemProxy(self.loop, self.log)
136 endpoint = rpc.PackageOperationsRpcHandler(
137 self.log,
138 self.dts,
139 self.loop,
140 proxy,
141 MockPublisher())
142 yield from endpoint.register()
143
144 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileAdd.from_dict({
145 "package_type": "VNFD",
146 "package_id": uid,
147 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
148 "package_path": "script/rift-shell"})
149
150 rpc_out = yield from self.dts.query_rpc(
151 "I,/rw-pkg-mgmt:package-file-add",
152 rwdts.XactFlag.TRACE,
153 ip)
154
155 for itr in rpc_out:
156 result = yield from itr
157 assert result.result.task_id == assert_uid
158
159 shutil.rmtree(path)
160
161 @rift.test.dts.async_test
162 def test_file_add_workflow(self):
163 """
164 Integration test:
165 1. Verify the end to end flow of package ADD (NO MOCKS)
166 """
167 uid, path = self.create_mock_package()
168
169 proxy = filesystem.FileSystemProxy(self.loop, self.log)
170 publisher = pkg_publisher.DownloadStatusPublisher(self.log, self.dts, self.loop)
171 endpoint = rpc.PackageOperationsRpcHandler(
172 self.log,
173 self.dts,
174 self.loop,
175 proxy,
176 publisher)
177
178 yield from publisher.register()
179 yield from endpoint.register()
180
181 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileAdd.from_dict({
182 "package_type": "VNFD",
183 "package_id": uid,
184 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
185 "vnfd_file_type": "ICONS",
186 "package_path": "rift-shell"})
187
188 rpc_out = yield from self.dts.query_rpc(
189 "I,/rw-pkg-mgmt:package-file-add",
190 rwdts.XactFlag.TRACE,
191 ip)
192
193 yield from asyncio.sleep(5, loop=self.loop)
194 filepath = os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path)
195 assert os.path.isfile(filepath)
196 mode = oct(os.stat(filepath)[stat.ST_MODE])
197 assert str(mode) == "0o100664"
198
199 shutil.rmtree(path)
200
201
202 @rift.test.dts.async_test
203 def test_file_delete_workflow(self):
204 """
205 Integration test:
206 1. Verify the end to end flow of package ADD (NO MOCKS)
207 """
208 uid, path = self.create_mock_package()
209
210 proxy = filesystem.FileSystemProxy(self.loop, self.log)
211 endpoint = rpc.PackageDeleteOperationsRpcHandler(
212 self.log,
213 self.dts,
214 self.loop,
215 proxy)
216
217 yield from endpoint.register()
218
219 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileDelete.from_dict({
220 "package_type": "VNFD",
221 "package_id": uid,
222 "vnfd_file_type": "ICONS",
223 "package_path": "logo.png"})
224
225 assert os.path.isfile(os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path))
226
227 rpc_out = yield from self.dts.query_rpc(
228 "I,/rw-pkg-mgmt:package-file-delete",
229 rwdts.XactFlag.TRACE,
230 ip)
231
232 yield from asyncio.sleep(5, loop=self.loop)
233 assert not os.path.isfile(os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path))
234
235 shutil.rmtree(path)
236
237 def main():
238 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
239
240 parser = argparse.ArgumentParser()
241 parser.add_argument('-v', '--verbose', action='store_true')
242 parser.add_argument('-n', '--no-runner', action='store_true')
243 args, unittest_args = parser.parse_known_args()
244 if args.no_runner:
245 runner = None
246
247 logging.basicConfig(format='TEST %(message)s')
248 logging.getLogger().setLevel(logging.DEBUG)
249
250
251 unittest.main(testRunner=runner, argv=[sys.argv[0]] + unittest_args)
252
253 if __name__ == '__main__':
254 main()