update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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 # 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')
33
34 import gi
35 gi.require_version('RwDts', '1.0')
36 gi.require_version('RwPkgMgmtYang', '1.0')
37 from gi.repository import (
38 RwDts as rwdts,
39 RwPkgMgmtYang,
40 )
41 from rift.tasklets.rwpkgmgr.proxy import filesystem
42
43 import rift.tasklets.rwpkgmgr.publisher as pkg_publisher
44 import rift.tasklets.rwpkgmgr.rpc as rpc
45 import rift.test.dts
46 from rift.mano.utils.project import ManoProject, DEFAULT_PROJECT
47
48 TEST_STRING = "foobar"
49
50
51 class MockPublisher(object):
52 def __init__(self, uid):
53 self.assert_uid = uid
54
55 @asyncio.coroutine
56 def register_downloader(self, *args):
57 return self.assert_uid
58
59
60 class MockProject(ManoProject):
61 def __init__(self, log, uid=None):
62 super().__init__(log, name=DEFAULT_PROJECT)
63 self.job_handler = MockPublisher(uid)
64
65
66 class MockTasklet:
67 def __init__(self, log, uid=None):
68 self.log = log
69 self.projects = {}
70 project = MockProject(self.log,
71 uid=uid)
72 project.publisher = None
73 self.projects[project.name] = project
74
75
76 class TestCase(rift.test.dts.AbstractDTSTest):
77 @classmethod
78 def configure_schema(cls):
79 return RwPkgMgmtYang.get_schema()
80
81 @classmethod
82 def configure_timeout(cls):
83 return 240
84
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)
89
90 def tearDown(self):
91 super().tearDown()
92
93 def create_mock_package(self, project):
94 uid = str(uuid.uuid4())
95 path = os.path.join(
96 os.getenv('RIFT_VAR_ROOT'),
97 "launchpad/packages/vnfd",
98 project,
99 uid)
100
101 asset_path = os.path.join(path, "icons")
102
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()
106
107 return uid, path
108
109 @rift.test.dts.async_test
110 def test_endpoint_discovery(self):
111 """
112 Verifies the following:
113 The endpoint RPC returns a URL
114 """
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()
118
119 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageEndpoint.from_dict({
120 "package_type": "VNFD",
121 "package_id": "BLAHID",
122 "project_name": DEFAULT_PROJECT})
123
124 rpc_out = yield from self.dts.query_rpc(
125 "I,/get-package-endpoint",
126 rwdts.XactFlag.TRACE,
127 ip)
128
129 for itr in rpc_out:
130 result = yield from itr
131 assert result.result.endpoint == 'https://127.0.0.1:8008/mano/api/package/vnfd/{}/BLAHID'.format(DEFAULT_PROJECT)
132
133 @rift.test.dts.async_test
134 def test_schema_rpc(self):
135 """
136 Verifies the following:
137 The schema RPC return the schema structure
138 """
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()
142
143 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageSchema.from_dict({
144 "package_type": "VNFD",
145 "project_name": DEFAULT_PROJECT})
146
147 rpc_out = yield from self.dts.query_rpc(
148 "I,/get-package-schema",
149 rwdts.XactFlag.TRACE,
150 ip)
151
152 for itr in rpc_out:
153 result = yield from itr
154 assert "charms" in result.result.schema
155
156 @rift.test.dts.async_test
157 def test_file_proxy_rpc(self):
158 """
159 1. The file RPC returns a valid UUID thro' DTS
160 """
161 assert_uid = str(uuid.uuid4())
162
163 uid, path = self.create_mock_package(DEFAULT_PROJECT)
164
165 proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
166 endpoint = rpc.PackageOperationsRpcHandler(
167 self.log,
168 self.dts,
169 self.loop,
170 proxy,
171 MockTasklet(self.log, uid=assert_uid))
172 yield from endpoint.register()
173
174 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileAdd.from_dict({
175 "package_type": "VNFD",
176 "package_id": uid,
177 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
178 "package_path": "script/rift-shell",
179 "project_name": DEFAULT_PROJECT})
180
181 rpc_out = yield from self.dts.query_rpc(
182 "I,/rw-pkg-mgmt:package-file-add",
183 rwdts.XactFlag.TRACE,
184 ip)
185
186 for itr in rpc_out:
187 result = yield from itr
188 assert result.result.task_id == assert_uid
189
190 shutil.rmtree(path)
191
192 @rift.test.dts.async_test
193 def test_file_add_workflow(self):
194 """
195 Integration test:
196 1. Verify the end to end flow of package ADD (NO MOCKS)
197 """
198 uid, path = self.create_mock_package(DEFAULT_PROJECT)
199
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(
206 self.log,
207 self.dts,
208 self.loop,
209 proxy,
210 tasklet)
211
212 yield from publisher.register()
213 yield from endpoint.register()
214
215 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileAdd.from_dict({
216 "package_type": "VNFD",
217 "package_id": uid,
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"})
222
223 rpc_out = yield from self.dts.query_rpc(
224 "I,/rw-pkg-mgmt:package-file-add",
225 rwdts.XactFlag.TRACE,
226 ip)
227
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"
234
235 shutil.rmtree(path)
236
237
238 @rift.test.dts.async_test
239 def test_file_delete_workflow(self):
240 """
241 Integration test:
242 1. Verify the end to end flow of package ADD (NO MOCKS)
243 """
244 uid, path = self.create_mock_package(DEFAULT_PROJECT)
245
246 proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
247 endpoint = rpc.PackageDeleteOperationsRpcHandler(
248 self.log,
249 self.dts,
250 self.loop,
251 proxy)
252
253 yield from endpoint.register()
254
255 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileDelete.from_dict({
256 "package_type": "VNFD",
257 "package_id": uid,
258 "package_path": "logo.png",
259 "vnfd_file_type": "ICONS",
260 "project_name": DEFAULT_PROJECT})
261
262 assert os.path.isfile(os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path))
263
264 rpc_out = yield from self.dts.query_rpc(
265 "I,/rw-pkg-mgmt:package-file-delete",
266 rwdts.XactFlag.TRACE,
267 ip)
268
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))
271
272 shutil.rmtree(path)
273
274 def main():
275 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
276
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()
281 if args.no_runner:
282 runner = None
283
284 logging.basicConfig(format='TEST %(message)s')
285 logging.getLogger().setLevel(logging.DEBUG)
286
287
288 unittest.main(testRunner=runner, argv=[sys.argv[0]] + unittest_args)
289
290 if __name__ == '__main__':
291 main()