141 - Support for Package Management in SO
[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 package_path = os.path.join(path, "pong_vnfd")
70
71 os.makedirs(package_path)
72 open(os.path.join(path, "pong_vnfd.xml"), "wb").close()
73 open(os.path.join(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 "package_path": "icons/rift-shell"})
186
187 rpc_out = yield from self.dts.query_rpc(
188 "I,/rw-pkg-mgmt:package-file-add",
189 rwdts.XactFlag.TRACE,
190 ip)
191
192 yield from asyncio.sleep(5, loop=self.loop)
193 filepath = os.path.join(path, ip.package_path)
194 assert os.path.isfile(filepath)
195 mode = oct(os.stat(filepath)[stat.ST_MODE])
196 assert str(mode) == "0o100664"
197
198 shutil.rmtree(path)
199
200
201 @rift.test.dts.async_test
202 def test_file_delete_workflow(self):
203 """
204 Integration test:
205 1. Verify the end to end flow of package ADD (NO MOCKS)
206 """
207 uid, path = self.create_mock_package()
208
209 proxy = filesystem.FileSystemProxy(self.loop, self.log)
210 endpoint = rpc.PackageDeleteOperationsRpcHandler(
211 self.log,
212 self.dts,
213 self.loop,
214 proxy)
215
216 yield from endpoint.register()
217
218 ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileDelete.from_dict({
219 "package_type": "VNFD",
220 "package_id": uid,
221 "package_path": "logo.png"})
222
223 assert os.path.isfile(os.path.join(path, ip.package_path))
224
225 rpc_out = yield from self.dts.query_rpc(
226 "I,/rw-pkg-mgmt:package-file-delete",
227 rwdts.XactFlag.TRACE,
228 ip)
229
230 yield from asyncio.sleep(5, loop=self.loop)
231 assert not os.path.isfile(os.path.join(path, ip.package_path))
232
233 shutil.rmtree(path)
234
235 def main():
236 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
237
238 parser = argparse.ArgumentParser()
239 parser.add_argument('-v', '--verbose', action='store_true')
240 parser.add_argument('-n', '--no-runner', action='store_true')
241 args, unittest_args = parser.parse_known_args()
242 if args.no_runner:
243 runner = None
244
245 logging.basicConfig(format='TEST %(message)s')
246 logging.getLogger().setLevel(logging.DEBUG)
247
248
249 unittest.main(testRunner=runner, argv=[sys.argv[0]] + unittest_args)
250
251 if __name__ == '__main__':
252 main()