feat(sol006): LCM migration to SOL006
[osm/LCM.git] / osm_lcm / tests / test_ns.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
14 # For those usages not covered by the Apache License, Version 2.0 please
15 # contact: alfonso.tiernosepulveda@telefonica.com
16 ##
17
18
19 import asynctest # pip3 install asynctest --user
20 import asyncio
21 import yaml
22 import copy
23 from os import getenv
24 from osm_lcm import ns
25 from osm_common.msgkafka import MsgKafka
26 from osm_lcm.lcm_utils import TaskRegistry
27 from osm_lcm.ng_ro import NgRoClient
28 from osm_lcm.data_utils.database.database import Database
29 from osm_lcm.data_utils.filesystem.filesystem import Filesystem
30 from uuid import uuid4
31
32 from osm_lcm.tests import test_db_descriptors as descriptors
33
34 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
35
36 """ Perform unittests using asynctest of osm_lcm.ns module
37 It allows, if some testing ENV are supplied, testing without mocking some external libraries for debugging:
38 OSMLCMTEST_NS_PUBKEY: public ssh-key returned by N2VC to inject to VMs
39 OSMLCMTEST_NS_NAME: change name of NS
40 OSMLCMTEST_PACKAGES_PATH: path where the vnf-packages are stored (de-compressed), each one on a 'vnfd_id' folder
41 OSMLCMTEST_NS_IPADDRESS: IP address where emulated VMs are reached. Comma separate list
42 OSMLCMTEST_RO_VIMID: VIM id of RO target vim IP. Obtain it with openmano datcenter-list on RO container
43 OSMLCMTEST_VCA_NOMOCK: Do no mock the VCA, N2VC library, for debugging it
44 OSMLCMTEST_RO_NOMOCK: Do no mock the ROClient library, for debugging it
45 OSMLCMTEST_DB_NOMOCK: Do no mock the database library, for debugging it
46 OSMLCMTEST_FS_NOMOCK: Do no mock the File Storage library, for debugging it
47 OSMLCMTEST_LOGGING_NOMOCK: Do no mock the logging
48 OSMLCM_VCA_XXX: configuration of N2VC
49 OSMLCM_RO_XXX: configuration of RO
50 """
51
52 lcm_config = {
53 "global": {
54 "loglevel": "DEBUG"
55 },
56 "timeout": {},
57 "VCA": { # TODO replace with os.get_env to get other configurations
58 "host": getenv("OSMLCM_VCA_HOST", "vca"),
59 "port": getenv("OSMLCM_VCA_PORT", 17070),
60 "user": getenv("OSMLCM_VCA_USER", "admin"),
61 "secret": getenv("OSMLCM_VCA_SECRET", "vca"),
62 "public_key": getenv("OSMLCM_VCA_PUBKEY", None),
63 'ca_cert': getenv("OSMLCM_VCA_CACERT", None),
64 'apiproxy': getenv("OSMLCM_VCA_APIPROXY", "192.168.1.1"),
65 },
66 "ro_config": {
67 "uri": "http://{}:{}/openmano".format(getenv("OSMLCM_RO_HOST", "ro"),
68 getenv("OSMLCM_RO_PORT", "9090")),
69 "tenant": getenv("OSMLCM_RO_TENANT", "osm"),
70 "logger_name": "lcm.ROclient",
71 "loglevel": "DEBUG",
72 "ng": True
73 }
74 }
75
76
77 class TestMyNS(asynctest.TestCase):
78
79 async def _n2vc_DeployCharms(self, model_name, application_name, vnfd, charm_path, params={}, machine_spec={},
80 callback=None, *callback_args):
81 if callback:
82 for status, message in (("maintenance", "installing sofwware"), ("active", "Ready!")):
83 # call callback after some time
84 asyncio.sleep(5, loop=self.loop)
85 callback(model_name, application_name, status, message, *callback_args)
86
87 @staticmethod
88 def _n2vc_FormatApplicationName(*args):
89 num_calls = 0
90 while True:
91 yield "app_name-{}".format(num_calls)
92 num_calls += 1
93
94 def _n2vc_CreateExecutionEnvironment(self, namespace, reuse_ee_id, db_dict, *args, **kwargs):
95 k_list = namespace.split(".")
96 ee_id = k_list[1] + "."
97 if len(k_list) >= 2:
98 for k in k_list[2:4]:
99 ee_id += k[:8]
100 else:
101 ee_id += "_NS_"
102 return ee_id, {}
103
104 def _ro_status(self, *args, **kwargs):
105 print("Args > {}".format(args))
106 print("kwargs > {}".format(kwargs))
107 if kwargs.get("delete"):
108 ro_ns_desc = yaml.load(descriptors.ro_delete_action_text, Loader=yaml.Loader)
109 while True:
110 yield ro_ns_desc
111
112 ro_ns_desc = yaml.load(descriptors.ro_ns_text, Loader=yaml.Loader)
113
114 # if ip address provided, replace descriptor
115 ip_addresses = getenv("OSMLCMTEST_NS_IPADDRESS", "")
116 if ip_addresses:
117 ip_addresses_list = ip_addresses.split(",")
118 for vnf in ro_ns_desc["vnfs"]:
119 if not ip_addresses_list:
120 break
121 vnf["ip_address"] = ip_addresses_list[0]
122 for vm in vnf["vms"]:
123 if not ip_addresses_list:
124 break
125 vm["ip_address"] = ip_addresses_list.pop(0)
126
127 while True:
128 yield ro_ns_desc
129 for net in ro_ns_desc["nets"]:
130 if net["status"] != "ACTIVE":
131 net["status"] = "ACTIVE"
132 break
133 else:
134 for vnf in ro_ns_desc["vnfs"]:
135 for vm in vnf["vms"]:
136 if vm["status"] != "ACTIVE":
137 vm["status"] = "ACTIVE"
138 break
139
140 def _ro_deploy(self, *args, **kwargs):
141 return {
142 'action_id': args[1]["action_id"],
143 'nsr_id': args[0],
144 'status': 'ok'
145 }
146
147 def _return_uuid(self, *args, **kwargs):
148 return str(uuid4())
149
150 async def setUp(self):
151
152 # Mock DB
153 if not getenv("OSMLCMTEST_DB_NOMOCK"):
154 # Cleanup singleton Database instance
155 Database.instance = None
156
157 self.db = Database({
158 "database": {
159 "driver": "memory"
160 }
161 }).instance.db
162 self.db.create_list("vnfds", yaml.load(descriptors.db_vnfds_text, Loader=yaml.Loader))
163 self.db.create_list("nsds", yaml.load(descriptors.db_nsds_text, Loader=yaml.Loader))
164 self.db.create_list("nsrs", yaml.load(descriptors.db_nsrs_text, Loader=yaml.Loader))
165 self.db.create_list("vim_accounts", yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader))
166 self.db.create_list("k8sclusters", yaml.load(descriptors.db_k8sclusters_text, Loader=yaml.Loader))
167 self.db.create_list("nslcmops", yaml.load(descriptors.db_nslcmops_text, Loader=yaml.Loader))
168 self.db.create_list("vnfrs", yaml.load(descriptors.db_vnfrs_text, Loader=yaml.Loader))
169 self.db_vim_accounts = yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader)
170
171 # Mock kafka
172 self.msg = asynctest.Mock(MsgKafka())
173
174 # Mock filesystem
175 if not getenv("OSMLCMTEST_FS_NOMOCK"):
176 self.fs = asynctest.Mock(Filesystem({
177 "storage": {
178 "driver": "local",
179 "path": "/"
180 }
181 }).instance.fs)
182 self.fs.get_params.return_value = {"path": getenv("OSMLCMTEST_PACKAGES_PATH", "./test/temp/packages")}
183 self.fs.file_open = asynctest.mock_open()
184 # self.fs.file_open.return_value.__enter__.return_value = asynctest.MagicMock() # called on a python "with"
185 # self.fs.file_open.return_value.__enter__.return_value.read.return_value = "" # empty file
186
187 # Mock TaskRegistry
188 self.lcm_tasks = asynctest.Mock(TaskRegistry())
189 self.lcm_tasks.lock_HA.return_value = True
190 self.lcm_tasks.waitfor_related_HA.return_value = None
191 self.lcm_tasks.lookfor_related.return_value = ("", [])
192
193 # Mock VCA - K8s
194 if not getenv("OSMLCMTEST_VCA_K8s_NOMOCK"):
195 ns.K8sJujuConnector = asynctest.MagicMock(ns.K8sJujuConnector)
196 ns.K8sHelmConnector = asynctest.MagicMock(ns.K8sHelmConnector)
197 ns.K8sHelm3Connector = asynctest.MagicMock(ns.K8sHelm3Connector)
198
199 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
200 ns.N2VCJujuConnector = asynctest.MagicMock(ns.N2VCJujuConnector)
201 ns.LCMHelmConn = asynctest.MagicMock(ns.LCMHelmConn)
202
203 # Create NsLCM class
204 self.my_ns = ns.NsLcm(self.msg, self.lcm_tasks, lcm_config, self.loop)
205 self.my_ns.fs = self.fs
206 self.my_ns.db = self.db
207 self.my_ns._wait_dependent_n2vc = asynctest.CoroutineMock()
208
209 # Mock logging
210 if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
211 self.my_ns.logger = asynctest.Mock(self.my_ns.logger)
212
213 # Mock VCA - N2VC
214 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
215 pub_key = getenv("OSMLCMTEST_NS_PUBKEY", "ssh-rsa test-pub-key t@osm.com")
216 # self.my_ns.n2vc = asynctest.Mock(N2VC())
217 self.my_ns.n2vc.GetPublicKey.return_value = getenv("OSMLCM_VCA_PUBKEY", "public_key")
218 # allow several versions of n2vc
219 self.my_ns.n2vc.FormatApplicationName = asynctest.Mock(side_effect=self._n2vc_FormatApplicationName())
220 self.my_ns.n2vc.DeployCharms = asynctest.CoroutineMock(side_effect=self._n2vc_DeployCharms)
221 self.my_ns.n2vc.create_execution_environment = asynctest.CoroutineMock(
222 side_effect=self._n2vc_CreateExecutionEnvironment)
223 self.my_ns.n2vc.install_configuration_sw = asynctest.CoroutineMock(return_value=pub_key)
224 self.my_ns.n2vc.get_ee_ssh_public__key = asynctest.CoroutineMock(return_value=pub_key)
225 self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock(side_effect=self._return_uuid)
226 self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock(side_effect=self._return_uuid)
227 self.my_ns.n2vc.GetPrimitiveStatus = asynctest.CoroutineMock(return_value="completed")
228 self.my_ns.n2vc.GetPrimitiveOutput = asynctest.CoroutineMock(return_value={"result": "ok",
229 "pubkey": pub_key})
230 self.my_ns.n2vc.delete_execution_environment = asynctest.CoroutineMock(return_value=None)
231 self.my_ns.n2vc.get_public_key = asynctest.CoroutineMock(
232 return_value=getenv("OSMLCM_VCA_PUBKEY", "public_key"))
233 self.my_ns.n2vc.delete_namespace = asynctest.CoroutineMock(return_value=None)
234
235 # Mock RO
236 if not getenv("OSMLCMTEST_RO_NOMOCK"):
237 self.my_ns.RO = asynctest.Mock(NgRoClient(self.loop, **lcm_config["ro_config"]))
238 # TODO first time should be empty list, following should return a dict
239 # self.my_ns.RO.get_list = asynctest.CoroutineMock(self.my_ns.RO.get_list, return_value=[])
240 self.my_ns.RO.deploy = asynctest.CoroutineMock(self.my_ns.RO.deploy, side_effect=self._ro_deploy)
241 # self.my_ns.RO.status = asynctest.CoroutineMock(self.my_ns.RO.status, side_effect=self._ro_status)
242 # self.my_ns.RO.create_action = asynctest.CoroutineMock(self.my_ns.RO.create_action,
243 # return_value={"vm-id": {"vim_result": 200,
244 # "description": "done"}})
245 self.my_ns.RO.delete = asynctest.CoroutineMock(self.my_ns.RO.delete)
246
247 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
248 # async def test_instantiate(self):
249 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
250 # nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
251 # # print("Test instantiate started")
252
253 # # delete deployed information of database
254 # if not getenv("OSMLCMTEST_DB_NOMOCK"):
255 # if self.db.get_list("nsrs")[0]["_admin"].get("deployed"):
256 # del self.db.get_list("nsrs")[0]["_admin"]["deployed"]
257 # for db_vnfr in self.db.get_list("vnfrs"):
258 # db_vnfr.pop("ip_address", None)
259 # for db_vdur in db_vnfr["vdur"]:
260 # db_vdur.pop("ip_address", None)
261 # db_vdur.pop("mac_address", None)
262 # if getenv("OSMLCMTEST_RO_VIMID"):
263 # self.db.get_list("vim_accounts")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
264 # if getenv("OSMLCMTEST_RO_VIMID"):
265 # self.db.get_list("nsrs")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
266
267 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
268
269 # self.msg.aiowrite.assert_called_once_with("ns", "instantiated",
270 # {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
271 # "operationState": "COMPLETED"},
272 # loop=self.loop)
273 # self.lcm_tasks.lock_HA.assert_called_once_with('ns', 'nslcmops', nslcmop_id)
274 # if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
275 # self.assertTrue(self.my_ns.logger.debug.called, "Debug method not called")
276 # self.my_ns.logger.error.assert_not_called()
277 # self.my_ns.logger.exception().assert_not_called()
278
279 # if not getenv("OSMLCMTEST_DB_NOMOCK"):
280 # self.assertTrue(self.db.set_one.called, "db.set_one not called")
281 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
282 # db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
283 # self.assertEqual(db_nsr["_admin"].get("nsState"), "INSTANTIATED", "Not instantiated")
284 # for vnfr in db_vnfrs_list:
285 # self.assertEqual(vnfr["_admin"].get("nsState"), "INSTANTIATED", "Not instantiated")
286
287 # if not getenv("OSMLCMTEST_VCA_NOMOCK"):
288 # # check intial-primitives called
289 # self.assertTrue(self.my_ns.n2vc.exec_primitive.called,
290 # "Exec primitive not called for initial config primitive")
291 # for _call in self.my_ns.n2vc.exec_primitive.call_args_list:
292 # self.assertIn(_call[1]["primitive_name"], ("config", "touch"),
293 # "called exec primitive with a primitive different than config or touch")
294
295 # # TODO add more checks of called methods
296 # # TODO add a terminate
297
298 # async def test_instantiate_ee_list(self):
299 # # Using modern IM where configuration is in the new format of execution_environment_list
300 # ee_descriptor_id = "charm_simple"
301 # non_used_initial_primitive = {
302 # "name": "not_to_be_called",
303 # "seq": 3,
304 # "execution-environment-ref": "not_used_ee"
305 # }
306 # ee_list = [
307 # {
308 # "id": ee_descriptor_id,
309 # "juju": {"charm": "simple"},
310
311 # },
312 # ]
313
314 # self.db.set_one(
315 # "vnfds",
316 # q_filter={"_id": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"},
317 # update_dict={"vnf-configuration.0.execution-environment-list": ee_list,
318 # "vnf-configuration.0.initial-config-primitive.0.execution-environment-ref": ee_descriptor_id,
319 # "vnf-configuration.0.initial-config-primitive.1.execution-environment-ref": ee_descriptor_id,
320 # "vnf-configuration.0.initial-config-primitive.2": non_used_initial_primitive,
321 # "vnf-configuration.0.config-primitive.0.execution-environment-ref": ee_descriptor_id,
322 # "vnf-configuration.0.config-primitive.0.execution-environment-primitive": "touch_charm",
323 # },
324 # unset={"vnf-configuration.juju": None})
325 # await self.test_instantiate()
326 # # this will check that the initial-congig-primitive 'not_to_be_called' is not called
327
328 # Test scale() and related methods
329 @asynctest.fail_on(active_handles=True) # all async tasks must be completed
330 async def test_scale(self):
331 # print("Test scale started")
332
333 # TODO: Add more higher-lever tests here, for example:
334 # scale-out/scale-in operations with success/error result
335
336 # Test scale() with missing 'scaleVnfData', should return operationState = 'FAILED'
337 nsr_id = descriptors.test_ids["TEST-A"]["ns"]
338 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
339 await self.my_ns.scale(nsr_id, nslcmop_id)
340 expected_value = 'FAILED'
341 return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("operationState")
342 self.assertEqual(return_value, expected_value)
343 # print("scale_result: {}".format(self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status")))
344
345 # Test _retry_or_skip_suboperation()
346 # Expected result:
347 # - if a suboperation's 'operationState' is marked as 'COMPLETED', SUBOPERATION_STATUS_SKIP is expected
348 # - if marked as anything but 'COMPLETED', the suboperation index is expected
349 def test_scale_retry_or_skip_suboperation(self):
350 # Load an alternative 'nslcmops' YAML for this test
351 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
352 db_nslcmop = self.db.get_one('nslcmops', {"_id": nslcmop_id})
353 op_index = 2
354 # Test when 'operationState' is 'COMPLETED'
355 db_nslcmop['_admin']['operations'][op_index]['operationState'] = 'COMPLETED'
356 return_value = self.my_ns._retry_or_skip_suboperation(db_nslcmop, op_index)
357 expected_value = self.my_ns.SUBOPERATION_STATUS_SKIP
358 self.assertEqual(return_value, expected_value)
359 # Test when 'operationState' is not 'COMPLETED'
360 db_nslcmop['_admin']['operations'][op_index]['operationState'] = None
361 return_value = self.my_ns._retry_or_skip_suboperation(db_nslcmop, op_index)
362 expected_value = op_index
363 self.assertEqual(return_value, expected_value)
364
365 # Test _find_suboperation()
366 # Expected result: index of the found sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if not found
367 def test_scale_find_suboperation(self):
368 # Load an alternative 'nslcmops' YAML for this test
369 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
370 db_nslcmop = self.db.get_one('nslcmops', {"_id": nslcmop_id})
371 # Find this sub-operation
372 op_index = 2
373 vnf_index = db_nslcmop['_admin']['operations'][op_index]['member_vnf_index']
374 primitive = db_nslcmop['_admin']['operations'][op_index]['primitive']
375 primitive_params = db_nslcmop['_admin']['operations'][op_index]['primitive_params']
376 match = {
377 'member_vnf_index': vnf_index,
378 'primitive': primitive,
379 'primitive_params': primitive_params,
380 }
381 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
382 self.assertEqual(found_op_index, op_index)
383 # Test with not-matching params
384 match = {
385 'member_vnf_index': vnf_index,
386 'primitive': '',
387 'primitive_params': primitive_params,
388 }
389 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
390 self.assertEqual(found_op_index, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
391 # Test with None
392 match = None
393 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
394 self.assertEqual(found_op_index, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
395
396 # Test _update_suboperation_status()
397 def test_scale_update_suboperation_status(self):
398 self.db.set_one = asynctest.Mock()
399 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
400 db_nslcmop = self.db.get_one('nslcmops', {"_id": nslcmop_id})
401 op_index = 0
402 # Force the initial values to be distinct from the updated ones
403 q_filter = {"_id": db_nslcmop["_id"]}
404 # Test to change 'operationState' and 'detailed-status'
405 operationState = 'COMPLETED'
406 detailed_status = 'Done'
407 expected_update_dict = {'_admin.operations.0.operationState': operationState,
408 '_admin.operations.0.detailed-status': detailed_status,
409 }
410 self.my_ns._update_suboperation_status(db_nslcmop, op_index, operationState, detailed_status)
411 self.db.set_one.assert_called_once_with("nslcmops", q_filter=q_filter, update_dict=expected_update_dict,
412 fail_on_empty=False)
413
414 def test_scale_add_suboperation(self):
415 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
416 db_nslcmop = self.db.get_one('nslcmops', {"_id": nslcmop_id})
417 vnf_index = '1'
418 num_ops_before = len(db_nslcmop.get('_admin', {}).get('operations', [])) - 1
419 vdu_id = None
420 vdu_count_index = None
421 vdu_name = None
422 primitive = 'touch'
423 mapped_primitive_params = {'parameter':
424 [{'data-type': 'STRING',
425 'name': 'filename',
426 'default-value': '<touch_filename2>'}],
427 'name': 'touch'}
428 operationState = 'PROCESSING'
429 detailed_status = 'In progress'
430 operationType = 'PRE-SCALE'
431 # Add a 'pre-scale' suboperation
432 op_index_after = self.my_ns._add_suboperation(db_nslcmop, vnf_index, vdu_id, vdu_count_index,
433 vdu_name, primitive, mapped_primitive_params,
434 operationState, detailed_status, operationType)
435 self.assertEqual(op_index_after, num_ops_before + 1)
436
437 # Delete all suboperations and add the same operation again
438 del db_nslcmop['_admin']['operations']
439 op_index_zero = self.my_ns._add_suboperation(db_nslcmop, vnf_index, vdu_id, vdu_count_index,
440 vdu_name, primitive, mapped_primitive_params,
441 operationState, detailed_status, operationType)
442 self.assertEqual(op_index_zero, 0)
443
444 # Add a 'RO' suboperation
445 RO_nsr_id = '1234567890'
446 RO_scaling_info = [{'type': 'create', 'count': 1, 'member-vnf-index': '1', 'osm_vdu_id': 'dataVM'}]
447 op_index = self.my_ns._add_suboperation(db_nslcmop, vnf_index, vdu_id, vdu_count_index,
448 vdu_name, primitive, mapped_primitive_params,
449 operationState, detailed_status, operationType,
450 RO_nsr_id, RO_scaling_info)
451 db_RO_nsr_id = db_nslcmop['_admin']['operations'][op_index]['RO_nsr_id']
452 self.assertEqual(op_index, 1)
453 self.assertEqual(RO_nsr_id, db_RO_nsr_id)
454
455 # Try to add an invalid suboperation, should return SUBOPERATION_STATUS_NOT_FOUND
456 op_index_invalid = self.my_ns._add_suboperation(None, None, None, None, None,
457 None, None, None,
458 None, None, None)
459 self.assertEqual(op_index_invalid, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
460
461 # Test _check_or_add_scale_suboperation() and _check_or_add_scale_suboperation_RO()
462 # check the possible return values:
463 # - SUBOPERATION_STATUS_NEW: This is a new sub-operation
464 # - op_index (non-negative number): This is an existing sub-operation, operationState != 'COMPLETED'
465 # - SUBOPERATION_STATUS_SKIP: This is an existing sub-operation, operationState == 'COMPLETED'
466 def test_scale_check_or_add_scale_suboperation(self):
467 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
468 db_nslcmop = self.db.get_one('nslcmops', {"_id": nslcmop_id})
469 operationType = 'PRE-SCALE'
470 vnf_index = '1'
471 primitive = 'touch'
472 primitive_params = {'parameter':
473 [{'data-type': 'STRING',
474 'name': 'filename',
475 'default-value': '<touch_filename2>'}],
476 'name': 'touch'}
477
478 # Delete all sub-operations to be sure this is a new sub-operation
479 del db_nslcmop['_admin']['operations']
480
481 # Add a new sub-operation
482 # For new sub-operations, operationState is set to 'PROCESSING' by default
483 op_index_new = self.my_ns._check_or_add_scale_suboperation(
484 db_nslcmop, vnf_index, primitive, primitive_params, operationType)
485 self.assertEqual(op_index_new, self.my_ns.SUBOPERATION_STATUS_NEW)
486
487 # Use the same parameters again to match the already added sub-operation
488 # which has status 'PROCESSING' (!= 'COMPLETED') by default
489 # The expected return value is a non-negative number
490 op_index_existing = self.my_ns._check_or_add_scale_suboperation(
491 db_nslcmop, vnf_index, primitive, primitive_params, operationType)
492 self.assertTrue(op_index_existing >= 0)
493
494 # Change operationState 'manually' for this sub-operation
495 db_nslcmop['_admin']['operations'][op_index_existing]['operationState'] = 'COMPLETED'
496 # Then use the same parameters again to match the already added sub-operation,
497 # which now has status 'COMPLETED'
498 # The expected return value is SUBOPERATION_STATUS_SKIP
499 op_index_skip = self.my_ns._check_or_add_scale_suboperation(
500 db_nslcmop, vnf_index, primitive, primitive_params, operationType)
501 self.assertEqual(op_index_skip, self.my_ns.SUBOPERATION_STATUS_SKIP)
502
503 # RO sub-operation test:
504 # Repeat tests for the very similar _check_or_add_scale_suboperation_RO(),
505 RO_nsr_id = '1234567890'
506 RO_scaling_info = [{'type': 'create', 'count': 1, 'member-vnf-index': '1', 'osm_vdu_id': 'dataVM'}]
507 op_index_new_RO = self.my_ns._check_or_add_scale_suboperation(
508 db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info)
509 self.assertEqual(op_index_new_RO, self.my_ns.SUBOPERATION_STATUS_NEW)
510
511 # Use the same parameters again to match the already added RO sub-operation
512 op_index_existing_RO = self.my_ns._check_or_add_scale_suboperation(
513 db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info)
514 self.assertTrue(op_index_existing_RO >= 0)
515
516 # Change operationState 'manually' for this RO sub-operation
517 db_nslcmop['_admin']['operations'][op_index_existing_RO]['operationState'] = 'COMPLETED'
518 # Then use the same parameters again to match the already added sub-operation,
519 # which now has status 'COMPLETED'
520 # The expected return value is SUBOPERATION_STATUS_SKIP
521 op_index_skip_RO = self.my_ns._check_or_add_scale_suboperation(
522 db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info)
523 self.assertEqual(op_index_skip_RO, self.my_ns.SUBOPERATION_STATUS_SKIP)
524
525 async def test_deploy_kdus(self):
526 nsr_id = descriptors.test_ids["TEST-KDU"]["ns"]
527 nslcmop_id = descriptors.test_ids["TEST-KDU"]["instantiate"]
528 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
529 db_vnfr = self.db.get_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "multikdu"})
530 db_vnfrs = {"multikdu": db_vnfr}
531 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
532 db_vnfds = {db_vnfd["_id"]: db_vnfd}
533 task_register = {}
534 logging_text = "KDU"
535 self.my_ns.k8sclusterhelm3.install = asynctest.CoroutineMock(return_value="k8s_id")
536 self.my_ns.k8sclusterhelm3.synchronize_repos = asynctest.CoroutineMock(return_value=("", ""))
537 self.my_ns.k8sclusterhelm3.get_services = asynctest.CoroutineMock(return_value=([]))
538 await self.my_ns.deploy_kdus(logging_text, nsr_id, nslcmop_id, db_vnfrs, db_vnfds, task_register)
539 await asyncio.wait(list(task_register.keys()), timeout=100)
540 db_nsr = self.db.get_list("nsrs")[1]
541 self.assertIn("K8s", db_nsr["_admin"]["deployed"], "K8s entry not created at '_admin.deployed'")
542 self.assertIsInstance(db_nsr["_admin"]["deployed"]["K8s"], list, "K8s entry is not of type list")
543 self.assertEqual(len(db_nsr["_admin"]["deployed"]["K8s"]), 2, "K8s entry is not of type list")
544 k8s_instace_info = {"kdu-instance": "k8s_id", "k8scluster-uuid": "73d96432-d692-40d2-8440-e0c73aee209c",
545 "k8scluster-type": "helm-chart-v3",
546 "kdu-name": "ldap",
547 "member-vnf-index": "multikdu",
548 "namespace": None}
549
550 nsr_result = copy.deepcopy(db_nsr["_admin"]["deployed"]["K8s"][0])
551 nsr_kdu_model_result = nsr_result.pop("kdu-model")
552 expected_kdu_model = "stable/openldap:1.2.1"
553 self.assertEqual(nsr_result, k8s_instace_info)
554 self.assertTrue(
555 nsr_kdu_model_result in expected_kdu_model or expected_kdu_model in nsr_kdu_model_result
556 )
557 nsr_result = copy.deepcopy(db_nsr["_admin"]["deployed"]["K8s"][1])
558 nsr_kdu_model_result = nsr_result.pop("kdu-model")
559 k8s_instace_info["kdu-name"] = "mongo"
560 expected_kdu_model = "stable/mongodb"
561 self.assertEqual(nsr_result, k8s_instace_info)
562 self.assertTrue(
563 nsr_kdu_model_result in expected_kdu_model or expected_kdu_model in nsr_kdu_model_result
564 )
565
566 # async def test_instantiate_pdu(self):
567 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
568 # nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
569 # # Modify vnfd/vnfr to change KDU for PDU. Adding keys that NBI will already set
570 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"},
571 # update_dict={"ip-address": "10.205.1.46",
572 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
573 # "vdur.0.pdu-type": "PDU-TYPE-1",
574 # "vdur.0.ip-address": "10.205.1.46",
575 # },
576 # unset={"vdur.status": None})
577 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "2"},
578 # update_dict={"ip-address": "10.205.1.47",
579 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
580 # "vdur.0.pdu-type": "PDU-TYPE-1",
581 # "vdur.0.ip-address": "10.205.1.47",
582 # },
583 # unset={"vdur.status": None})
584
585 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
586 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
587 # self.assertEqual(db_nsr.get("nsState"), "READY", str(db_nsr.get("errorDescription ")))
588 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
589 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
590 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
591 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
592
593 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
594 # async def test_terminate_without_configuration(self):
595 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
596 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
597 # # set instantiation task as completed
598 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
599 # update_dict={"operationState": "COMPLETED"})
600 # self.db.set_one("nsrs", {"_id": nsr_id},
601 # update_dict={"_admin.deployed.VCA.0": None, "_admin.deployed.VCA.1": None})
602
603 # await self.my_ns.terminate(nsr_id, nslcmop_id)
604 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
605 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
606 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
607 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
608 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
609 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
610 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
611 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
612 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
613 # db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
614 # for vnfr in db_vnfrs_list:
615 # self.assertEqual(vnfr["_admin"].get("nsState"), "NOT_INSTANTIATED", "Not instantiated")
616
617 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
618 # async def test_terminate_primitive(self):
619 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
620 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
621 # # set instantiation task as completed
622 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
623 # update_dict={"operationState": "COMPLETED"})
624
625 # # modify vnfd descriptor to include terminate_primitive
626 # terminate_primitive = [{
627 # "name": "touch",
628 # "parameter": [{"name": "filename", "value": "terminate_filename"}],
629 # "seq": '1'
630 # }]
631 # db_vnfr = self.db.get_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"})
632 # self.db.set_one("vnfds", {"_id": db_vnfr["vnfd-id"]},
633 # {"vnf-configuration.0.terminate-config-primitive": terminate_primitive})
634
635 # await self.my_ns.terminate(nsr_id, nslcmop_id)
636 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
637 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
638 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
639 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
640 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
641 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
642 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
643 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
644 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
645
646
647 if __name__ == '__main__':
648 asynctest.main()