5e248a2bf3282e0fdd9f9dee14bb9bce29e90a61
[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 osm_lcm.data_utils.vnfd import find_software_version
31 from osm_lcm.lcm_utils import check_juju_bundle_existence, get_charm_artifact_path
32 from osm_lcm.lcm_utils import LcmException
33 from uuid import uuid4
34 from unittest.mock import Mock, patch
35
36 from osm_lcm.tests import test_db_descriptors as descriptors
37
38 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
39
40 """ Perform unittests using asynctest of osm_lcm.ns module
41 It allows, if some testing ENV are supplied, testing without mocking some external libraries for debugging:
42 OSMLCMTEST_NS_PUBKEY: public ssh-key returned by N2VC to inject to VMs
43 OSMLCMTEST_NS_NAME: change name of NS
44 OSMLCMTEST_PACKAGES_PATH: path where the vnf-packages are stored (de-compressed), each one on a 'vnfd_id' folder
45 OSMLCMTEST_NS_IPADDRESS: IP address where emulated VMs are reached. Comma separate list
46 OSMLCMTEST_RO_VIMID: VIM id of RO target vim IP. Obtain it with openmano datcenter-list on RO container
47 OSMLCMTEST_VCA_NOMOCK: Do no mock the VCA, N2VC library, for debugging it
48 OSMLCMTEST_RO_NOMOCK: Do no mock the ROClient library, for debugging it
49 OSMLCMTEST_DB_NOMOCK: Do no mock the database library, for debugging it
50 OSMLCMTEST_FS_NOMOCK: Do no mock the File Storage library, for debugging it
51 OSMLCMTEST_LOGGING_NOMOCK: Do no mock the logging
52 OSMLCM_VCA_XXX: configuration of N2VC
53 OSMLCM_RO_XXX: configuration of RO
54 """
55
56 lcm_config = {
57 "global": {"loglevel": "DEBUG"},
58 "timeout": {},
59 "VCA": { # TODO replace with os.get_env to get other configurations
60 "host": getenv("OSMLCM_VCA_HOST", "vca"),
61 "port": getenv("OSMLCM_VCA_PORT", 17070),
62 "user": getenv("OSMLCM_VCA_USER", "admin"),
63 "secret": getenv("OSMLCM_VCA_SECRET", "vca"),
64 "public_key": getenv("OSMLCM_VCA_PUBKEY", None),
65 "ca_cert": getenv("OSMLCM_VCA_CACERT", None),
66 "apiproxy": getenv("OSMLCM_VCA_APIPROXY", "192.168.1.1"),
67 },
68 "ro_config": {
69 "uri": "http://{}:{}/openmano".format(
70 getenv("OSMLCM_RO_HOST", "ro"), getenv("OSMLCM_RO_PORT", "9090")
71 ),
72 "tenant": getenv("OSMLCM_RO_TENANT", "osm"),
73 "logger_name": "lcm.ROclient",
74 "loglevel": "DEBUG",
75 "ng": True,
76 },
77 }
78
79
80 class TestMyNS(asynctest.TestCase):
81 async def _n2vc_DeployCharms(
82 self,
83 model_name,
84 application_name,
85 vnfd,
86 charm_path,
87 params={},
88 machine_spec={},
89 callback=None,
90 *callback_args
91 ):
92 if callback:
93 for status, message in (
94 ("maintenance", "installing sofwware"),
95 ("active", "Ready!"),
96 ):
97 # call callback after some time
98 asyncio.sleep(5, loop=self.loop)
99 callback(model_name, application_name, status, message, *callback_args)
100
101 @staticmethod
102 def _n2vc_FormatApplicationName(*args):
103 num_calls = 0
104 while True:
105 yield "app_name-{}".format(num_calls)
106 num_calls += 1
107
108 def _n2vc_CreateExecutionEnvironment(
109 self, namespace, reuse_ee_id, db_dict, *args, **kwargs
110 ):
111 k_list = namespace.split(".")
112 ee_id = k_list[1] + "."
113 if len(k_list) >= 2:
114 for k in k_list[2:4]:
115 ee_id += k[:8]
116 else:
117 ee_id += "_NS_"
118 return ee_id, {}
119
120 def _ro_status(self, *args, **kwargs):
121 print("Args > {}".format(args))
122 print("kwargs > {}".format(kwargs))
123 if args:
124 if "update" in args:
125 ro_ns_desc = yaml.load(
126 descriptors.ro_update_action_text, Loader=yaml.Loader
127 )
128 while True:
129 yield ro_ns_desc
130 if kwargs.get("delete"):
131 ro_ns_desc = yaml.load(
132 descriptors.ro_delete_action_text, Loader=yaml.Loader
133 )
134 while True:
135 yield ro_ns_desc
136
137 ro_ns_desc = yaml.load(descriptors.ro_ns_text, Loader=yaml.Loader)
138
139 # if ip address provided, replace descriptor
140 ip_addresses = getenv("OSMLCMTEST_NS_IPADDRESS", "")
141 if ip_addresses:
142 ip_addresses_list = ip_addresses.split(",")
143 for vnf in ro_ns_desc["vnfs"]:
144 if not ip_addresses_list:
145 break
146 vnf["ip_address"] = ip_addresses_list[0]
147 for vm in vnf["vms"]:
148 if not ip_addresses_list:
149 break
150 vm["ip_address"] = ip_addresses_list.pop(0)
151
152 while True:
153 yield ro_ns_desc
154 for net in ro_ns_desc["nets"]:
155 if net["status"] != "ACTIVE":
156 net["status"] = "ACTIVE"
157 break
158 else:
159 for vnf in ro_ns_desc["vnfs"]:
160 for vm in vnf["vms"]:
161 if vm["status"] != "ACTIVE":
162 vm["status"] = "ACTIVE"
163 break
164
165 def _ro_deploy(self, *args, **kwargs):
166 return {"action_id": args[1]["action_id"], "nsr_id": args[0], "status": "ok"}
167
168 def _return_uuid(self, *args, **kwargs):
169 return str(uuid4())
170
171 async def setUp(self):
172
173 # Mock DB
174 if not getenv("OSMLCMTEST_DB_NOMOCK"):
175 # Cleanup singleton Database instance
176 Database.instance = None
177
178 self.db = Database({"database": {"driver": "memory"}}).instance.db
179 self.db.create_list(
180 "vnfds", yaml.load(descriptors.db_vnfds_text, Loader=yaml.Loader)
181 )
182 self.db.create_list(
183 "vnfds_revisions",
184 yaml.load(descriptors.db_vnfds_revisions_text, Loader=yaml.Loader),
185 )
186 self.db.create_list(
187 "nsds", yaml.load(descriptors.db_nsds_text, Loader=yaml.Loader)
188 )
189 self.db.create_list(
190 "nsrs", yaml.load(descriptors.db_nsrs_text, Loader=yaml.Loader)
191 )
192 self.db.create_list(
193 "vim_accounts",
194 yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader),
195 )
196 self.db.create_list(
197 "k8sclusters",
198 yaml.load(descriptors.db_k8sclusters_text, Loader=yaml.Loader),
199 )
200 self.db.create_list(
201 "nslcmops", yaml.load(descriptors.db_nslcmops_text, Loader=yaml.Loader)
202 )
203 self.db.create_list(
204 "vnfrs", yaml.load(descriptors.db_vnfrs_text, Loader=yaml.Loader)
205 )
206 self.db_vim_accounts = yaml.load(
207 descriptors.db_vim_accounts_text, Loader=yaml.Loader
208 )
209
210 # Mock kafka
211 self.msg = asynctest.Mock(MsgKafka())
212
213 # Mock filesystem
214 if not getenv("OSMLCMTEST_FS_NOMOCK"):
215 self.fs = asynctest.Mock(
216 Filesystem({"storage": {"driver": "local", "path": "/"}}).instance.fs
217 )
218 self.fs.get_params.return_value = {
219 "path": getenv("OSMLCMTEST_PACKAGES_PATH", "./test/temp/packages")
220 }
221 self.fs.file_open = asynctest.mock_open()
222 # self.fs.file_open.return_value.__enter__.return_value = asynctest.MagicMock() # called on a python "with"
223 # self.fs.file_open.return_value.__enter__.return_value.read.return_value = "" # empty file
224
225 # Mock TaskRegistry
226 self.lcm_tasks = asynctest.Mock(TaskRegistry())
227 self.lcm_tasks.lock_HA.return_value = True
228 self.lcm_tasks.waitfor_related_HA.return_value = None
229 self.lcm_tasks.lookfor_related.return_value = ("", [])
230
231 # Mock VCA - K8s
232 if not getenv("OSMLCMTEST_VCA_K8s_NOMOCK"):
233 ns.K8sJujuConnector = asynctest.MagicMock(ns.K8sJujuConnector)
234 ns.K8sHelmConnector = asynctest.MagicMock(ns.K8sHelmConnector)
235 ns.K8sHelm3Connector = asynctest.MagicMock(ns.K8sHelm3Connector)
236
237 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
238 ns.N2VCJujuConnector = asynctest.MagicMock(ns.N2VCJujuConnector)
239 ns.LCMHelmConn = asynctest.MagicMock(ns.LCMHelmConn)
240
241 # Create NsLCM class
242 self.my_ns = ns.NsLcm(self.msg, self.lcm_tasks, lcm_config, self.loop)
243 self.my_ns.fs = self.fs
244 self.my_ns.db = self.db
245 self.my_ns._wait_dependent_n2vc = asynctest.CoroutineMock()
246
247 # Mock logging
248 if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
249 self.my_ns.logger = asynctest.Mock(self.my_ns.logger)
250
251 # Mock VCA - N2VC
252 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
253 pub_key = getenv("OSMLCMTEST_NS_PUBKEY", "ssh-rsa test-pub-key t@osm.com")
254 # self.my_ns.n2vc = asynctest.Mock(N2VC())
255 self.my_ns.n2vc.GetPublicKey.return_value = getenv(
256 "OSMLCM_VCA_PUBKEY", "public_key"
257 )
258 # allow several versions of n2vc
259 self.my_ns.n2vc.FormatApplicationName = asynctest.Mock(
260 side_effect=self._n2vc_FormatApplicationName()
261 )
262 self.my_ns.n2vc.DeployCharms = asynctest.CoroutineMock(
263 side_effect=self._n2vc_DeployCharms
264 )
265 self.my_ns.n2vc.create_execution_environment = asynctest.CoroutineMock(
266 side_effect=self._n2vc_CreateExecutionEnvironment
267 )
268 self.my_ns.n2vc.install_configuration_sw = asynctest.CoroutineMock(
269 return_value=pub_key
270 )
271 self.my_ns.n2vc.get_ee_ssh_public__key = asynctest.CoroutineMock(
272 return_value=pub_key
273 )
274 self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock(
275 side_effect=self._return_uuid
276 )
277 self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock(
278 side_effect=self._return_uuid
279 )
280 self.my_ns.n2vc.GetPrimitiveStatus = asynctest.CoroutineMock(
281 return_value="completed"
282 )
283 self.my_ns.n2vc.GetPrimitiveOutput = asynctest.CoroutineMock(
284 return_value={"result": "ok", "pubkey": pub_key}
285 )
286 self.my_ns.n2vc.delete_execution_environment = asynctest.CoroutineMock(
287 return_value=None
288 )
289 self.my_ns.n2vc.get_public_key = asynctest.CoroutineMock(
290 return_value=getenv("OSMLCM_VCA_PUBKEY", "public_key")
291 )
292 self.my_ns.n2vc.delete_namespace = asynctest.CoroutineMock(
293 return_value=None
294 )
295
296 # Mock RO
297 if not getenv("OSMLCMTEST_RO_NOMOCK"):
298 self.my_ns.RO = asynctest.Mock(
299 NgRoClient(self.loop, **lcm_config["ro_config"])
300 )
301 # TODO first time should be empty list, following should return a dict
302 # self.my_ns.RO.get_list = asynctest.CoroutineMock(self.my_ns.RO.get_list, return_value=[])
303 self.my_ns.RO.deploy = asynctest.CoroutineMock(
304 self.my_ns.RO.deploy, side_effect=self._ro_deploy
305 )
306 # self.my_ns.RO.status = asynctest.CoroutineMock(self.my_ns.RO.status, side_effect=self._ro_status)
307 # self.my_ns.RO.create_action = asynctest.CoroutineMock(self.my_ns.RO.create_action,
308 # return_value={"vm-id": {"vim_result": 200,
309 # "description": "done"}})
310 self.my_ns.RO.delete = asynctest.CoroutineMock(self.my_ns.RO.delete)
311
312 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
313 # async def test_instantiate(self):
314 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
315 # nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
316 # # print("Test instantiate started")
317
318 # # delete deployed information of database
319 # if not getenv("OSMLCMTEST_DB_NOMOCK"):
320 # if self.db.get_list("nsrs")[0]["_admin"].get("deployed"):
321 # del self.db.get_list("nsrs")[0]["_admin"]["deployed"]
322 # for db_vnfr in self.db.get_list("vnfrs"):
323 # db_vnfr.pop("ip_address", None)
324 # for db_vdur in db_vnfr["vdur"]:
325 # db_vdur.pop("ip_address", None)
326 # db_vdur.pop("mac_address", None)
327 # if getenv("OSMLCMTEST_RO_VIMID"):
328 # self.db.get_list("vim_accounts")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
329 # if getenv("OSMLCMTEST_RO_VIMID"):
330 # self.db.get_list("nsrs")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
331
332 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
333
334 # self.msg.aiowrite.assert_called_once_with("ns", "instantiated",
335 # {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
336 # "operationState": "COMPLETED"},
337 # loop=self.loop)
338 # self.lcm_tasks.lock_HA.assert_called_once_with('ns', 'nslcmops', nslcmop_id)
339 # if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
340 # self.assertTrue(self.my_ns.logger.debug.called, "Debug method not called")
341 # self.my_ns.logger.error.assert_not_called()
342 # self.my_ns.logger.exception().assert_not_called()
343
344 # if not getenv("OSMLCMTEST_DB_NOMOCK"):
345 # self.assertTrue(self.db.set_one.called, "db.set_one not called")
346 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
347 # db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
348 # self.assertEqual(db_nsr["_admin"].get("nsState"), "INSTANTIATED", "Not instantiated")
349 # for vnfr in db_vnfrs_list:
350 # self.assertEqual(vnfr["_admin"].get("nsState"), "INSTANTIATED", "Not instantiated")
351
352 # if not getenv("OSMLCMTEST_VCA_NOMOCK"):
353 # # check intial-primitives called
354 # self.assertTrue(self.my_ns.n2vc.exec_primitive.called,
355 # "Exec primitive not called for initial config primitive")
356 # for _call in self.my_ns.n2vc.exec_primitive.call_args_list:
357 # self.assertIn(_call[1]["primitive_name"], ("config", "touch"),
358 # "called exec primitive with a primitive different than config or touch")
359
360 # # TODO add more checks of called methods
361 # # TODO add a terminate
362
363 # async def test_instantiate_ee_list(self):
364 # # Using modern IM where configuration is in the new format of execution_environment_list
365 # ee_descriptor_id = "charm_simple"
366 # non_used_initial_primitive = {
367 # "name": "not_to_be_called",
368 # "seq": 3,
369 # "execution-environment-ref": "not_used_ee"
370 # }
371 # ee_list = [
372 # {
373 # "id": ee_descriptor_id,
374 # "juju": {"charm": "simple"},
375
376 # },
377 # ]
378
379 # self.db.set_one(
380 # "vnfds",
381 # q_filter={"_id": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"},
382 # update_dict={"vnf-configuration.0.execution-environment-list": ee_list,
383 # "vnf-configuration.0.initial-config-primitive.0.execution-environment-ref": ee_descriptor_id,
384 # "vnf-configuration.0.initial-config-primitive.1.execution-environment-ref": ee_descriptor_id,
385 # "vnf-configuration.0.initial-config-primitive.2": non_used_initial_primitive,
386 # "vnf-configuration.0.config-primitive.0.execution-environment-ref": ee_descriptor_id,
387 # "vnf-configuration.0.config-primitive.0.execution-environment-primitive": "touch_charm",
388 # },
389 # unset={"vnf-configuration.juju": None})
390 # await self.test_instantiate()
391 # # this will check that the initial-congig-primitive 'not_to_be_called' is not called
392
393 # Test scale() and related methods
394 @asynctest.fail_on(active_handles=True) # all async tasks must be completed
395 async def test_scale(self):
396 # print("Test scale started")
397
398 # TODO: Add more higher-lever tests here, for example:
399 # scale-out/scale-in operations with success/error result
400
401 # Test scale() with missing 'scaleVnfData', should return operationState = 'FAILED'
402 nsr_id = descriptors.test_ids["TEST-A"]["ns"]
403 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
404 await self.my_ns.scale(nsr_id, nslcmop_id)
405 expected_value = "FAILED"
406 return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get(
407 "operationState"
408 )
409 self.assertEqual(return_value, expected_value)
410 # print("scale_result: {}".format(self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status")))
411
412 # Test scale() for native kdu
413 # this also includes testing _scale_kdu()
414 nsr_id = descriptors.test_ids["TEST-NATIVE-KDU"]["ns"]
415 nslcmop_id = descriptors.test_ids["TEST-NATIVE-KDU"]["instantiate"]
416
417 self.my_ns.k8sclusterjuju.scale = asynctest.mock.CoroutineMock()
418 self.my_ns.k8sclusterjuju.exec_primitive = asynctest.mock.CoroutineMock()
419 self.my_ns.k8sclusterjuju.get_scale_count = asynctest.mock.CoroutineMock(
420 return_value=1
421 )
422 await self.my_ns.scale(nsr_id, nslcmop_id)
423 expected_value = "COMPLETED"
424 return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get(
425 "operationState"
426 )
427 self.assertEqual(return_value, expected_value)
428 self.my_ns.k8sclusterjuju.scale.assert_called_once()
429
430 # Test scale() for native kdu with 2 resource
431 nsr_id = descriptors.test_ids["TEST-NATIVE-KDU-2"]["ns"]
432 nslcmop_id = descriptors.test_ids["TEST-NATIVE-KDU-2"]["instantiate"]
433
434 self.my_ns.k8sclusterjuju.get_scale_count.return_value = 2
435 await self.my_ns.scale(nsr_id, nslcmop_id)
436 expected_value = "COMPLETED"
437 return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get(
438 "operationState"
439 )
440 self.assertEqual(return_value, expected_value)
441 self.my_ns.k8sclusterjuju.scale.assert_called()
442
443 async def test_vca_status_refresh(self):
444 nsr_id = descriptors.test_ids["TEST-A"]["ns"]
445 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
446 await self.my_ns.vca_status_refresh(nsr_id, nslcmop_id)
447 expected_value = dict()
448 return_value = dict()
449 vnf_descriptors = self.db.get_list("vnfds")
450 for i, _ in enumerate(vnf_descriptors):
451 for j, value in enumerate(vnf_descriptors[i]["df"]):
452 if "lcm-operations-configuration" in vnf_descriptors[i]["df"][j]:
453 if (
454 "day1-2"
455 in value["lcm-operations-configuration"][
456 "operate-vnf-op-config"
457 ]
458 ):
459 for k, v in enumerate(
460 value["lcm-operations-configuration"][
461 "operate-vnf-op-config"
462 ]["day1-2"]
463 ):
464 if (
465 v.get("execution-environment-list")
466 and "juju" in v["execution-environment-list"][k]
467 ):
468 expected_value = self.db.get_list("nsrs")[i][
469 "vcaStatus"
470 ]
471 await self.my_ns._on_update_n2vc_db(
472 "nsrs", {"_id": nsr_id}, "_admin.deployed.VCA.0", {}
473 )
474 return_value = self.db.get_list("nsrs")[i]["vcaStatus"]
475 self.assertEqual(return_value, expected_value)
476
477 # Test _retry_or_skip_suboperation()
478 # Expected result:
479 # - if a suboperation's 'operationState' is marked as 'COMPLETED', SUBOPERATION_STATUS_SKIP is expected
480 # - if marked as anything but 'COMPLETED', the suboperation index is expected
481 def test_scale_retry_or_skip_suboperation(self):
482 # Load an alternative 'nslcmops' YAML for this test
483 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
484 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
485 op_index = 2
486 # Test when 'operationState' is 'COMPLETED'
487 db_nslcmop["_admin"]["operations"][op_index]["operationState"] = "COMPLETED"
488 return_value = self.my_ns._retry_or_skip_suboperation(db_nslcmop, op_index)
489 expected_value = self.my_ns.SUBOPERATION_STATUS_SKIP
490 self.assertEqual(return_value, expected_value)
491 # Test when 'operationState' is not 'COMPLETED'
492 db_nslcmop["_admin"]["operations"][op_index]["operationState"] = None
493 return_value = self.my_ns._retry_or_skip_suboperation(db_nslcmop, op_index)
494 expected_value = op_index
495 self.assertEqual(return_value, expected_value)
496
497 # Test _find_suboperation()
498 # Expected result: index of the found sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if not found
499 def test_scale_find_suboperation(self):
500 # Load an alternative 'nslcmops' YAML for this test
501 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
502 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
503 # Find this sub-operation
504 op_index = 2
505 vnf_index = db_nslcmop["_admin"]["operations"][op_index]["member_vnf_index"]
506 primitive = db_nslcmop["_admin"]["operations"][op_index]["primitive"]
507 primitive_params = db_nslcmop["_admin"]["operations"][op_index][
508 "primitive_params"
509 ]
510 match = {
511 "member_vnf_index": vnf_index,
512 "primitive": primitive,
513 "primitive_params": primitive_params,
514 }
515 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
516 self.assertEqual(found_op_index, op_index)
517 # Test with not-matching params
518 match = {
519 "member_vnf_index": vnf_index,
520 "primitive": "",
521 "primitive_params": primitive_params,
522 }
523 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
524 self.assertEqual(found_op_index, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
525 # Test with None
526 match = None
527 found_op_index = self.my_ns._find_suboperation(db_nslcmop, match)
528 self.assertEqual(found_op_index, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
529
530 # Test _update_suboperation_status()
531 def test_scale_update_suboperation_status(self):
532 self.db.set_one = asynctest.Mock()
533 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
534 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
535 op_index = 0
536 # Force the initial values to be distinct from the updated ones
537 q_filter = {"_id": db_nslcmop["_id"]}
538 # Test to change 'operationState' and 'detailed-status'
539 operationState = "COMPLETED"
540 detailed_status = "Done"
541 expected_update_dict = {
542 "_admin.operations.0.operationState": operationState,
543 "_admin.operations.0.detailed-status": detailed_status,
544 }
545 self.my_ns._update_suboperation_status(
546 db_nslcmop, op_index, operationState, detailed_status
547 )
548 self.db.set_one.assert_called_once_with(
549 "nslcmops",
550 q_filter=q_filter,
551 update_dict=expected_update_dict,
552 fail_on_empty=False,
553 )
554
555 def test_scale_add_suboperation(self):
556 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
557 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
558 vnf_index = "1"
559 num_ops_before = len(db_nslcmop.get("_admin", {}).get("operations", [])) - 1
560 vdu_id = None
561 vdu_count_index = None
562 vdu_name = None
563 primitive = "touch"
564 mapped_primitive_params = {
565 "parameter": [
566 {
567 "data-type": "STRING",
568 "name": "filename",
569 "default-value": "<touch_filename2>",
570 }
571 ],
572 "name": "touch",
573 }
574 operationState = "PROCESSING"
575 detailed_status = "In progress"
576 operationType = "PRE-SCALE"
577 # Add a 'pre-scale' suboperation
578 op_index_after = self.my_ns._add_suboperation(
579 db_nslcmop,
580 vnf_index,
581 vdu_id,
582 vdu_count_index,
583 vdu_name,
584 primitive,
585 mapped_primitive_params,
586 operationState,
587 detailed_status,
588 operationType,
589 )
590 self.assertEqual(op_index_after, num_ops_before + 1)
591
592 # Delete all suboperations and add the same operation again
593 del db_nslcmop["_admin"]["operations"]
594 op_index_zero = self.my_ns._add_suboperation(
595 db_nslcmop,
596 vnf_index,
597 vdu_id,
598 vdu_count_index,
599 vdu_name,
600 primitive,
601 mapped_primitive_params,
602 operationState,
603 detailed_status,
604 operationType,
605 )
606 self.assertEqual(op_index_zero, 0)
607
608 # Add a 'RO' suboperation
609 RO_nsr_id = "1234567890"
610 RO_scaling_info = [
611 {
612 "type": "create",
613 "count": 1,
614 "member-vnf-index": "1",
615 "osm_vdu_id": "dataVM",
616 }
617 ]
618 op_index = self.my_ns._add_suboperation(
619 db_nslcmop,
620 vnf_index,
621 vdu_id,
622 vdu_count_index,
623 vdu_name,
624 primitive,
625 mapped_primitive_params,
626 operationState,
627 detailed_status,
628 operationType,
629 RO_nsr_id,
630 RO_scaling_info,
631 )
632 db_RO_nsr_id = db_nslcmop["_admin"]["operations"][op_index]["RO_nsr_id"]
633 self.assertEqual(op_index, 1)
634 self.assertEqual(RO_nsr_id, db_RO_nsr_id)
635
636 # Try to add an invalid suboperation, should return SUBOPERATION_STATUS_NOT_FOUND
637 op_index_invalid = self.my_ns._add_suboperation(
638 None, None, None, None, None, None, None, None, None, None, None
639 )
640 self.assertEqual(op_index_invalid, self.my_ns.SUBOPERATION_STATUS_NOT_FOUND)
641
642 # Test _check_or_add_scale_suboperation() and _check_or_add_scale_suboperation_RO()
643 # check the possible return values:
644 # - SUBOPERATION_STATUS_NEW: This is a new sub-operation
645 # - op_index (non-negative number): This is an existing sub-operation, operationState != 'COMPLETED'
646 # - SUBOPERATION_STATUS_SKIP: This is an existing sub-operation, operationState == 'COMPLETED'
647 def test_scale_check_or_add_scale_suboperation(self):
648 nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
649 db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
650 operationType = "PRE-SCALE"
651 vnf_index = "1"
652 primitive = "touch"
653 primitive_params = {
654 "parameter": [
655 {
656 "data-type": "STRING",
657 "name": "filename",
658 "default-value": "<touch_filename2>",
659 }
660 ],
661 "name": "touch",
662 }
663
664 # Delete all sub-operations to be sure this is a new sub-operation
665 del db_nslcmop["_admin"]["operations"]
666
667 # Add a new sub-operation
668 # For new sub-operations, operationState is set to 'PROCESSING' by default
669 op_index_new = self.my_ns._check_or_add_scale_suboperation(
670 db_nslcmop, vnf_index, primitive, primitive_params, operationType
671 )
672 self.assertEqual(op_index_new, self.my_ns.SUBOPERATION_STATUS_NEW)
673
674 # Use the same parameters again to match the already added sub-operation
675 # which has status 'PROCESSING' (!= 'COMPLETED') by default
676 # The expected return value is a non-negative number
677 op_index_existing = self.my_ns._check_or_add_scale_suboperation(
678 db_nslcmop, vnf_index, primitive, primitive_params, operationType
679 )
680 self.assertTrue(op_index_existing >= 0)
681
682 # Change operationState 'manually' for this sub-operation
683 db_nslcmop["_admin"]["operations"][op_index_existing][
684 "operationState"
685 ] = "COMPLETED"
686 # Then use the same parameters again to match the already added sub-operation,
687 # which now has status 'COMPLETED'
688 # The expected return value is SUBOPERATION_STATUS_SKIP
689 op_index_skip = self.my_ns._check_or_add_scale_suboperation(
690 db_nslcmop, vnf_index, primitive, primitive_params, operationType
691 )
692 self.assertEqual(op_index_skip, self.my_ns.SUBOPERATION_STATUS_SKIP)
693
694 # RO sub-operation test:
695 # Repeat tests for the very similar _check_or_add_scale_suboperation_RO(),
696 RO_nsr_id = "1234567890"
697 RO_scaling_info = [
698 {
699 "type": "create",
700 "count": 1,
701 "member-vnf-index": "1",
702 "osm_vdu_id": "dataVM",
703 }
704 ]
705 op_index_new_RO = self.my_ns._check_or_add_scale_suboperation(
706 db_nslcmop, vnf_index, None, None, "SCALE-RO", RO_nsr_id, RO_scaling_info
707 )
708 self.assertEqual(op_index_new_RO, self.my_ns.SUBOPERATION_STATUS_NEW)
709
710 # Use the same parameters again to match the already added RO sub-operation
711 op_index_existing_RO = self.my_ns._check_or_add_scale_suboperation(
712 db_nslcmop, vnf_index, None, None, "SCALE-RO", RO_nsr_id, RO_scaling_info
713 )
714 self.assertTrue(op_index_existing_RO >= 0)
715
716 # Change operationState 'manually' for this RO sub-operation
717 db_nslcmop["_admin"]["operations"][op_index_existing_RO][
718 "operationState"
719 ] = "COMPLETED"
720 # Then use the same parameters again to match the already added sub-operation,
721 # which now has status 'COMPLETED'
722 # The expected return value is SUBOPERATION_STATUS_SKIP
723 op_index_skip_RO = self.my_ns._check_or_add_scale_suboperation(
724 db_nslcmop, vnf_index, None, None, "SCALE-RO", RO_nsr_id, RO_scaling_info
725 )
726 self.assertEqual(op_index_skip_RO, self.my_ns.SUBOPERATION_STATUS_SKIP)
727
728 async def test_deploy_kdus(self):
729 nsr_id = descriptors.test_ids["TEST-KDU"]["ns"]
730 nslcmop_id = descriptors.test_ids["TEST-KDU"]["instantiate"]
731 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
732 db_vnfr = self.db.get_one(
733 "vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "multikdu"}
734 )
735 db_vnfrs = {"multikdu": db_vnfr}
736 db_vnfd = self.db.get_one("vnfds", {"_id": db_vnfr["vnfd-id"]})
737 db_vnfds = [db_vnfd]
738 task_register = {}
739 logging_text = "KDU"
740 self.my_ns.k8sclusterhelm3.generate_kdu_instance_name = asynctest.mock.Mock()
741 self.my_ns.k8sclusterhelm3.generate_kdu_instance_name.return_value = "k8s_id"
742 self.my_ns.k8sclusterhelm3.install = asynctest.CoroutineMock()
743 self.my_ns.k8sclusterhelm3.synchronize_repos = asynctest.CoroutineMock(
744 return_value=("", "")
745 )
746 self.my_ns.k8sclusterhelm3.get_services = asynctest.CoroutineMock(
747 return_value=([])
748 )
749 await self.my_ns.deploy_kdus(
750 logging_text, nsr_id, nslcmop_id, db_vnfrs, db_vnfds, task_register
751 )
752 await asyncio.wait(list(task_register.keys()), timeout=100)
753 db_nsr = self.db.get_list("nsrs")[1]
754 self.assertIn(
755 "K8s",
756 db_nsr["_admin"]["deployed"],
757 "K8s entry not created at '_admin.deployed'",
758 )
759 self.assertIsInstance(
760 db_nsr["_admin"]["deployed"]["K8s"], list, "K8s entry is not of type list"
761 )
762 self.assertEqual(
763 len(db_nsr["_admin"]["deployed"]["K8s"]), 2, "K8s entry is not of type list"
764 )
765 k8s_instace_info = {
766 "kdu-instance": "k8s_id",
767 "k8scluster-uuid": "73d96432-d692-40d2-8440-e0c73aee209c",
768 "k8scluster-type": "helm-chart-v3",
769 "kdu-name": "ldap",
770 "member-vnf-index": "multikdu",
771 "namespace": None,
772 "kdu-deployment-name": None,
773 }
774
775 nsr_result = copy.deepcopy(db_nsr["_admin"]["deployed"]["K8s"][0])
776 nsr_kdu_model_result = nsr_result.pop("kdu-model")
777 expected_kdu_model = "stable/openldap:1.2.1"
778 self.assertEqual(nsr_result, k8s_instace_info)
779 self.assertTrue(
780 nsr_kdu_model_result in expected_kdu_model
781 or expected_kdu_model in nsr_kdu_model_result
782 )
783 nsr_result = copy.deepcopy(db_nsr["_admin"]["deployed"]["K8s"][1])
784 nsr_kdu_model_result = nsr_result.pop("kdu-model")
785 k8s_instace_info["kdu-name"] = "mongo"
786 expected_kdu_model = "stable/mongodb"
787 self.assertEqual(nsr_result, k8s_instace_info)
788 self.assertTrue(
789 nsr_kdu_model_result in expected_kdu_model
790 or expected_kdu_model in nsr_kdu_model_result
791 )
792
793 # Test remove_vnf() and related methods
794 @asynctest.fail_on(active_handles=True) # all async tasks must be completed
795 async def test_remove_vnf(self):
796 # Test REMOVE_VNF
797 nsr_id = descriptors.test_ids["TEST-UPDATE"]["ns"]
798 nslcmop_id = descriptors.test_ids["TEST-UPDATE"]["removeVnf"]
799 vnf_instance_id = descriptors.test_ids["TEST-UPDATE"]["vnf"]
800 mock_wait_ng_ro = asynctest.CoroutineMock()
801 with patch("osm_lcm.ns.NsLcm._wait_ng_ro", mock_wait_ng_ro):
802 await self.my_ns.update(nsr_id, nslcmop_id)
803 expected_value = "COMPLETED"
804 return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get(
805 "operationState"
806 )
807 self.assertEqual(return_value, expected_value)
808 with self.assertRaises(Exception) as context:
809 self.db.get_one("vnfrs", {"_id": vnf_instance_id})
810 self.assertTrue("database exception Not found entry with filter" in str(context.exception))
811
812 # async def test_instantiate_pdu(self):
813 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
814 # nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
815 # # Modify vnfd/vnfr to change KDU for PDU. Adding keys that NBI will already set
816 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"},
817 # update_dict={"ip-address": "10.205.1.46",
818 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
819 # "vdur.0.pdu-type": "PDU-TYPE-1",
820 # "vdur.0.ip-address": "10.205.1.46",
821 # },
822 # unset={"vdur.status": None})
823 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "2"},
824 # update_dict={"ip-address": "10.205.1.47",
825 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
826 # "vdur.0.pdu-type": "PDU-TYPE-1",
827 # "vdur.0.ip-address": "10.205.1.47",
828 # },
829 # unset={"vdur.status": None})
830
831 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
832 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
833 # self.assertEqual(db_nsr.get("nsState"), "READY", str(db_nsr.get("errorDescription ")))
834 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
835 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
836 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
837 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
838
839 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
840 # async def test_terminate_without_configuration(self):
841 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
842 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
843 # # set instantiation task as completed
844 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
845 # update_dict={"operationState": "COMPLETED"})
846 # self.db.set_one("nsrs", {"_id": nsr_id},
847 # update_dict={"_admin.deployed.VCA.0": None, "_admin.deployed.VCA.1": None})
848
849 # await self.my_ns.terminate(nsr_id, nslcmop_id)
850 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
851 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
852 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
853 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
854 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
855 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
856 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
857 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
858 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
859 # db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
860 # for vnfr in db_vnfrs_list:
861 # self.assertEqual(vnfr["_admin"].get("nsState"), "NOT_INSTANTIATED", "Not instantiated")
862
863 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
864 # async def test_terminate_primitive(self):
865 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
866 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
867 # # set instantiation task as completed
868 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
869 # update_dict={"operationState": "COMPLETED"})
870
871 # # modify vnfd descriptor to include terminate_primitive
872 # terminate_primitive = [{
873 # "name": "touch",
874 # "parameter": [{"name": "filename", "value": "terminate_filename"}],
875 # "seq": '1'
876 # }]
877 # db_vnfr = self.db.get_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"})
878 # self.db.set_one("vnfds", {"_id": db_vnfr["vnfd-id"]},
879 # {"vnf-configuration.0.terminate-config-primitive": terminate_primitive})
880
881 # await self.my_ns.terminate(nsr_id, nslcmop_id)
882 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
883 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
884 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
885 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
886 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
887 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
888 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
889 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
890 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
891
892 # Test update method
893
894 async def test_update(self):
895
896 nsr_id = descriptors.test_ids["TEST-A"]["ns"]
897 nslcmop_id = descriptors.test_ids["TEST-A"]["update"]
898 vnfr_id = "6421c7c9-d865-4fb4-9a13-d4275d243e01"
899 vnfd_id = "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"
900
901 def mock_reset():
902 mock_charm_hash.reset_mock()
903 mock_juju_bundle.reset_mock()
904 fs.sync.reset_mock()
905 mock_charm_upgrade.reset_mock()
906 mock_software_version.reset_mock()
907
908 with self.subTest(
909 i=1,
910 t="Update type: CHANGE_VNFPKG, latest_vnfd revision changed,"
911 "Charm package changed, sw-version is not changed.",
912 ):
913
914 self.db.set_one(
915 "vnfds",
916 q_filter={"_id": vnfd_id},
917 update_dict={"_admin.revision": 3, "kdu": []},
918 )
919
920 self.db.set_one(
921 "vnfds_revisions",
922 q_filter={"_id": vnfd_id + ":1"},
923 update_dict={"_admin.revision": 1, "kdu": []},
924 )
925
926 self.db.set_one(
927 "vnfrs", q_filter={"_id": vnfr_id}, update_dict={"revision": 1}
928 )
929
930 mock_charm_hash = Mock(autospec=True)
931 mock_charm_hash.return_value = True
932
933 mock_juju_bundle = Mock(return_value=None)
934
935 mock_software_version = Mock(autospec=True)
936 mock_software_version.side_effect = ["1.0", "1.0"]
937
938 mock_charm_upgrade = asynctest.Mock(autospec=True)
939 task = asyncio.Future()
940 task.set_result(("COMPLETED", "some_output"))
941 mock_charm_upgrade.return_value = task
942
943 fs = Mock(autospec=True)
944 fs.path.__add__ = Mock()
945 fs.path.side_effect = ["/", "/", "/", "/"]
946 fs.sync.side_effect = [None, None]
947
948 instance = self.my_ns
949
950 expected_operation_state = "COMPLETED"
951 expected_operation_error = ""
952 expected_vnfr_revision = 3
953 expected_ns_state = "INSTANTIATED"
954 expected_ns_operational_state = "running"
955
956 with patch.object(instance, "fs", fs), patch(
957 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
958 ), patch("osm_lcm.ns.NsLcm._ns_charm_upgrade", mock_charm_upgrade), patch(
959 "osm_lcm.data_utils.vnfd.find_software_version", mock_software_version
960 ), patch(
961 "osm_lcm.lcm_utils.check_juju_bundle_existence", mock_juju_bundle
962 ):
963
964 await instance.update(nsr_id, nslcmop_id)
965 return_operation_state = self.db.get_one(
966 "nslcmops", {"_id": nslcmop_id}
967 ).get("operationState")
968 return_operation_error = self.db.get_one(
969 "nslcmops", {"_id": nslcmop_id}
970 ).get("errorMessage")
971 return_ns_operational_state = self.db.get_one(
972 "nsrs", {"_id": nsr_id}
973 ).get("operational-status")
974
975 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
976 "revision"
977 )
978
979 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
980 "nsState"
981 )
982
983 mock_charm_hash.assert_called_with(
984 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:1/hackfest_3charmed_vnfd/charms/simple",
985 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:3/hackfest_3charmed_vnfd/charms/simple",
986 )
987
988 self.assertEqual(fs.sync.call_count, 2)
989 self.assertEqual(return_ns_state, expected_ns_state)
990 self.assertEqual(return_operation_state, expected_operation_state)
991 self.assertEqual(return_operation_error, expected_operation_error)
992 self.assertEqual(
993 return_ns_operational_state, expected_ns_operational_state
994 )
995 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
996
997 mock_reset()
998
999 with self.subTest(
1000 i=2, t="Update type: CHANGE_VNFPKG, latest_vnfd revision not changed"
1001 ):
1002
1003 self.db.set_one(
1004 "vnfds", q_filter={"_id": vnfd_id}, update_dict={"_admin.revision": 1}
1005 )
1006
1007 self.db.set_one(
1008 "vnfrs", q_filter={"_id": vnfr_id}, update_dict={"revision": 1}
1009 )
1010
1011 mock_charm_hash = Mock(autospec=True)
1012 mock_charm_hash.return_value = True
1013
1014 mock_juju_bundle = Mock(return_value=None)
1015 mock_software_version = Mock(autospec=True)
1016
1017 mock_charm_upgrade = asynctest.Mock(autospec=True)
1018 task = asyncio.Future()
1019 task.set_result(("COMPLETED", "some_output"))
1020 mock_charm_upgrade.return_value = task
1021
1022 fs = Mock(autospec=True)
1023 fs.path.__add__ = Mock()
1024 fs.path.side_effect = ["/", "/", "/", "/"]
1025 fs.sync.side_effect = [None, None]
1026
1027 instance = self.my_ns
1028
1029 expected_operation_state = "COMPLETED"
1030 expected_operation_error = ""
1031 expected_vnfr_revision = 1
1032 expected_ns_state = "INSTANTIATED"
1033 expected_ns_operational_state = "running"
1034
1035 with patch.object(instance, "fs", fs), patch(
1036 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
1037 ), patch("osm_lcm.ns.NsLcm._ns_charm_upgrade", mock_charm_upgrade), patch(
1038 "osm_lcm.lcm_utils.check_juju_bundle_existence", mock_juju_bundle
1039 ):
1040
1041 await instance.update(nsr_id, nslcmop_id)
1042
1043 return_operation_state = self.db.get_one(
1044 "nslcmops", {"_id": nslcmop_id}
1045 ).get("operationState")
1046
1047 return_operation_error = self.db.get_one(
1048 "nslcmops", {"_id": nslcmop_id}
1049 ).get("errorMessage")
1050
1051 return_ns_operational_state = self.db.get_one(
1052 "nsrs", {"_id": nsr_id}
1053 ).get("operational-status")
1054
1055 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
1056 "nsState"
1057 )
1058
1059 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
1060 "revision"
1061 )
1062
1063 mock_charm_hash.assert_not_called()
1064 mock_software_version.assert_not_called()
1065 mock_juju_bundle.assert_not_called()
1066 mock_charm_upgrade.assert_not_called()
1067 fs.sync.assert_not_called()
1068
1069 self.assertEqual(return_ns_state, expected_ns_state)
1070 self.assertEqual(return_operation_state, expected_operation_state)
1071 self.assertEqual(return_operation_error, expected_operation_error)
1072 self.assertEqual(
1073 return_ns_operational_state, expected_ns_operational_state
1074 )
1075 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
1076
1077 mock_reset()
1078
1079 with self.subTest(
1080 i=3,
1081 t="Update type: CHANGE_VNFPKG, latest_vnfd revision changed, "
1082 "Charm package is not changed, sw-version is not changed.",
1083 ):
1084
1085 self.db.set_one(
1086 "vnfds", q_filter={"_id": vnfd_id}, update_dict={"_admin.revision": 3}
1087 )
1088
1089 self.db.set_one(
1090 "vnfds_revisions",
1091 q_filter={"_id": vnfd_id + ":1"},
1092 update_dict={"_admin.revision": 1},
1093 )
1094
1095 self.db.set_one(
1096 "vnfrs", q_filter={"_id": vnfr_id}, update_dict={"revision": 1}
1097 )
1098
1099 mock_charm_hash = Mock(autospec=True)
1100 mock_charm_hash.return_value = False
1101
1102 mock_juju_bundle = Mock(return_value=None)
1103
1104 mock_software_version = Mock(autospec=True)
1105
1106 mock_charm_upgrade = asynctest.Mock(autospec=True)
1107 task = asyncio.Future()
1108 task.set_result(("COMPLETED", "some_output"))
1109 mock_charm_upgrade.return_value = task
1110 mock_software_version.side_effect = ["1.0", "1.0"]
1111
1112 fs = Mock(autospec=True)
1113 fs.path.__add__ = Mock()
1114 fs.path.side_effect = ["/", "/", "/", "/"]
1115 fs.sync.side_effect = [None, None]
1116
1117 instance = self.my_ns
1118
1119 expected_operation_state = "COMPLETED"
1120 expected_operation_error = ""
1121 expected_vnfr_revision = 3
1122 expected_ns_state = "INSTANTIATED"
1123 expected_ns_operational_state = "running"
1124
1125 with patch.object(instance, "fs", fs), patch(
1126 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
1127 ), patch("osm_lcm.ns.NsLcm._ns_charm_upgrade", mock_charm_upgrade), patch(
1128 "osm_lcm.lcm_utils.check_juju_bundle_existence", mock_juju_bundle
1129 ):
1130
1131 await instance.update(nsr_id, nslcmop_id)
1132
1133 return_operation_state = self.db.get_one(
1134 "nslcmops", {"_id": nslcmop_id}
1135 ).get("operationState")
1136
1137 return_operation_error = self.db.get_one(
1138 "nslcmops", {"_id": nslcmop_id}
1139 ).get("errorMessage")
1140
1141 return_ns_operational_state = self.db.get_one(
1142 "nsrs", {"_id": nsr_id}
1143 ).get("operational-status")
1144
1145 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
1146 "revision"
1147 )
1148
1149 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
1150 "nsState"
1151 )
1152
1153 mock_charm_hash.assert_called_with(
1154 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:1/hackfest_3charmed_vnfd/charms/simple",
1155 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:3/hackfest_3charmed_vnfd/charms/simple",
1156 )
1157
1158 self.assertEqual(fs.sync.call_count, 2)
1159 self.assertEqual(mock_charm_hash.call_count, 1)
1160
1161 mock_juju_bundle.assert_not_called()
1162 mock_charm_upgrade.assert_not_called()
1163
1164 self.assertEqual(return_ns_state, expected_ns_state)
1165 self.assertEqual(return_operation_state, expected_operation_state)
1166 self.assertEqual(return_operation_error, expected_operation_error)
1167 self.assertEqual(
1168 return_ns_operational_state, expected_ns_operational_state
1169 )
1170 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
1171
1172 mock_reset()
1173
1174 with self.subTest(
1175 i=4,
1176 t="Update type: CHANGE_VNFPKG, latest_vnfd revision changed, "
1177 "Charm package exists, sw-version changed.",
1178 ):
1179
1180 self.db.set_one(
1181 "vnfds",
1182 q_filter={"_id": vnfd_id},
1183 update_dict={"_admin.revision": 3, "software-version": "3.0"},
1184 )
1185
1186 self.db.set_one(
1187 "vnfds_revisions",
1188 q_filter={"_id": vnfd_id + ":1"},
1189 update_dict={"_admin.revision": 1},
1190 )
1191
1192 self.db.set_one(
1193 "vnfrs",
1194 q_filter={"_id": vnfr_id},
1195 update_dict={"revision": 1},
1196 )
1197
1198 mock_charm_hash = Mock(autospec=True)
1199 mock_charm_hash.return_value = False
1200
1201 mock_juju_bundle = Mock(return_value=None)
1202
1203 mock_charm_upgrade = asynctest.Mock(autospec=True)
1204 task = asyncio.Future()
1205 task.set_result(("COMPLETED", "some_output"))
1206 mock_charm_upgrade.return_value = task
1207
1208 mock_charm_artifact = Mock(autospec=True)
1209 mock_charm_artifact.side_effect = [
1210 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:1/hackfest_3charmed_vnfd/charms/simple",
1211 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77/hackfest_3charmed_vnfd/charms/simple",
1212 ]
1213
1214 fs = Mock(autospec=True)
1215 fs.path.__add__ = Mock()
1216 fs.path.side_effect = ["/", "/", "/", "/"]
1217 fs.sync.side_effect = [None, None]
1218
1219 instance = self.my_ns
1220
1221 expected_operation_state = "FAILED"
1222 expected_operation_error = "FAILED Checking if existing VNF has charm: Software version change is not supported as VNF instance 6421c7c9-d865-4fb4-9a13-d4275d243e01 has charm."
1223 expected_vnfr_revision = 1
1224 expected_ns_state = "INSTANTIATED"
1225 expected_ns_operational_state = "running"
1226
1227 with patch.object(instance, "fs", fs), patch(
1228 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
1229 ), patch("osm_lcm.ns.NsLcm._ns_charm_upgrade", mock_charm_upgrade), patch(
1230 "osm_lcm.lcm_utils.get_charm_artifact_path", mock_charm_artifact
1231 ):
1232
1233 await instance.update(nsr_id, nslcmop_id)
1234
1235 return_operation_state = self.db.get_one(
1236 "nslcmops", {"_id": nslcmop_id}
1237 ).get("operationState")
1238
1239 return_operation_error = self.db.get_one(
1240 "nslcmops", {"_id": nslcmop_id}
1241 ).get("errorMessage")
1242
1243 return_ns_operational_state = self.db.get_one(
1244 "nsrs", {"_id": nsr_id}
1245 ).get("operational-status")
1246
1247 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
1248 "revision"
1249 )
1250
1251 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
1252 "nsState"
1253 )
1254
1255 self.assertEqual(fs.sync.call_count, 2)
1256 mock_charm_hash.assert_not_called()
1257
1258 mock_juju_bundle.assert_not_called()
1259 mock_charm_upgrade.assert_not_called()
1260
1261 self.assertEqual(return_ns_state, expected_ns_state)
1262 self.assertEqual(return_operation_state, expected_operation_state)
1263 self.assertEqual(return_operation_error, expected_operation_error)
1264 self.assertEqual(
1265 return_ns_operational_state, expected_ns_operational_state
1266 )
1267 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
1268
1269 mock_reset()
1270
1271 with self.subTest(
1272 i=5,
1273 t="Update type: CHANGE_VNFPKG, latest_vnfd revision changed,"
1274 "Charm package exists, sw-version not changed, juju-bundle exists",
1275 ):
1276
1277 self.db.set_one(
1278 "vnfds",
1279 q_filter={"_id": vnfd_id},
1280 update_dict={
1281 "_admin.revision": 3,
1282 "software-version": "1.0",
1283 "kdu.0.juju-bundle": "stable/native-kdu",
1284 },
1285 )
1286
1287 self.db.set_one(
1288 "vnfds_revisions",
1289 q_filter={"_id": vnfd_id + ":1"},
1290 update_dict={
1291 "_admin.revision": 1,
1292 "software-version": "1.0",
1293 "kdu.0.juju-bundle": "stable/native-kdu",
1294 },
1295 )
1296
1297 self.db.set_one(
1298 "vnfrs", q_filter={"_id": vnfr_id}, update_dict={"revision": 1}
1299 )
1300
1301 mock_charm_hash = Mock(autospec=True)
1302 mock_charm_hash.return_value = True
1303
1304 mock_charm_artifact = Mock(autospec=True)
1305 mock_charm_artifact.side_effect = [
1306 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:1/hackfest_3charmed_vnfd/charms/simple",
1307 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77/hackfest_3charmed_vnfd/charms/simple",
1308 ]
1309
1310 fs = Mock(autospec=True)
1311 fs.path.__add__ = Mock()
1312 fs.path.side_effect = ["/", "/", "/", "/"]
1313 fs.sync.side_effect = [None, None]
1314
1315 instance = self.my_ns
1316
1317 expected_operation_state = "FAILED"
1318 expected_operation_error = "FAILED Checking whether VNF uses juju bundle: Charm upgrade is not supported for the instance which uses juju-bundle: stable/native-kdu"
1319 expected_vnfr_revision = 1
1320 expected_ns_state = "INSTANTIATED"
1321 expected_ns_operational_state = "running"
1322
1323 with patch.object(instance, "fs", fs), patch(
1324 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
1325 ), patch("osm_lcm.lcm_utils.get_charm_artifact_path", mock_charm_artifact):
1326
1327 await instance.update(nsr_id, nslcmop_id)
1328
1329 return_operation_state = self.db.get_one(
1330 "nslcmops", {"_id": nslcmop_id}
1331 ).get("operationState")
1332
1333 return_operation_error = self.db.get_one(
1334 "nslcmops", {"_id": nslcmop_id}
1335 ).get("errorMessage")
1336
1337 return_ns_operational_state = self.db.get_one(
1338 "nsrs", {"_id": nsr_id}
1339 ).get("operational-status")
1340
1341 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
1342 "revision"
1343 )
1344
1345 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
1346 "nsState"
1347 )
1348
1349 self.assertEqual(fs.sync.call_count, 2)
1350 self.assertEqual(mock_charm_hash.call_count, 1)
1351 self.assertEqual(mock_charm_hash.call_count, 1)
1352
1353 mock_charm_upgrade.assert_not_called()
1354
1355 self.assertEqual(return_ns_state, expected_ns_state)
1356 self.assertEqual(return_operation_state, expected_operation_state)
1357 self.assertEqual(return_operation_error, expected_operation_error)
1358 self.assertEqual(
1359 return_ns_operational_state, expected_ns_operational_state
1360 )
1361 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
1362
1363 mock_reset()
1364
1365 with self.subTest(
1366 i=6,
1367 t="Update type: CHANGE_VNFPKG, latest_vnfd revision changed,"
1368 "Charm package exists, sw-version not changed, charm-upgrade failed",
1369 ):
1370
1371 self.db.set_one(
1372 "vnfds",
1373 q_filter={"_id": vnfd_id},
1374 update_dict={
1375 "_admin.revision": 3,
1376 "software-version": "1.0",
1377 "kdu": [],
1378 },
1379 )
1380
1381 self.db.set_one(
1382 "vnfds_revisions",
1383 q_filter={"_id": vnfd_id + ":1"},
1384 update_dict={
1385 "_admin.revision": 1,
1386 "software-version": "1.0",
1387 "kdu": [],
1388 },
1389 )
1390
1391 self.db.set_one(
1392 "vnfrs", q_filter={"_id": vnfr_id}, update_dict={"revision": 1}
1393 )
1394
1395 mock_charm_hash = Mock(autospec=True)
1396 mock_charm_hash.return_value = True
1397
1398 mock_charm_upgrade = asynctest.Mock(autospec=True)
1399 task = asyncio.Future()
1400 task.set_result(("FAILED", "some_error"))
1401 mock_charm_upgrade.return_value = task
1402
1403 mock_charm_artifact = Mock(autospec=True)
1404 mock_charm_artifact.side_effect = [
1405 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:1/hackfest_3charmed_vnfd/charms/simple",
1406 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77/hackfest_3charmed_vnfd/charms/simple",
1407 ]
1408
1409 fs = Mock(autospec=True)
1410 fs.path.__add__ = Mock()
1411 fs.path.side_effect = ["/", "/", "/", "/"]
1412 fs.sync.side_effect = [None, None]
1413
1414 instance = self.my_ns
1415
1416 expected_operation_state = "FAILED"
1417 expected_operation_error = "some_error"
1418 expected_vnfr_revision = 1
1419 expected_ns_state = "INSTANTIATED"
1420 expected_ns_operational_state = "running"
1421
1422 with patch.object(instance, "fs", fs), patch(
1423 "osm_lcm.lcm_utils.LcmBase.check_charm_hash_changed", mock_charm_hash
1424 ), patch("osm_lcm.ns.NsLcm._ns_charm_upgrade", mock_charm_upgrade):
1425
1426 await instance.update(nsr_id, nslcmop_id)
1427
1428 return_operation_state = self.db.get_one(
1429 "nslcmops", {"_id": nslcmop_id}
1430 ).get("operationState")
1431
1432 return_operation_error = self.db.get_one(
1433 "nslcmops", {"_id": nslcmop_id}
1434 ).get("errorMessage")
1435
1436 return_ns_operational_state = self.db.get_one(
1437 "nsrs", {"_id": nsr_id}
1438 ).get("operational-status")
1439
1440 return_vnfr_revision = self.db.get_one("vnfrs", {"_id": vnfr_id}).get(
1441 "revision"
1442 )
1443
1444 return_ns_state = self.db.get_one("nsrs", {"_id": nsr_id}).get(
1445 "nsState"
1446 )
1447
1448 self.assertEqual(fs.sync.call_count, 2)
1449 self.assertEqual(mock_charm_hash.call_count, 1)
1450 self.assertEqual(mock_charm_upgrade.call_count, 1)
1451
1452 self.assertEqual(return_ns_state, expected_ns_state)
1453 self.assertEqual(return_operation_state, expected_operation_state)
1454 self.assertEqual(return_operation_error, expected_operation_error)
1455 self.assertEqual(
1456 return_ns_operational_state, expected_ns_operational_state
1457 )
1458 self.assertEqual(return_vnfr_revision, expected_vnfr_revision)
1459
1460 mock_reset()
1461
1462 def test_ns_update_helper_methods(self):
1463 def mock_reset():
1464 fs.mock_reset()
1465 mock_path.mock_reset()
1466 mock_checksumdir.mock_reset()
1467
1468 with self.subTest(
1469 i=1, t="Find software version, VNFD does not have have software version"
1470 ):
1471 # Testing method find_software_version
1472
1473 db_vnfd = self.db.get_one(
1474 "vnfds", {"_id": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"}
1475 )
1476 expected_result = "1.0"
1477 result = find_software_version(db_vnfd)
1478 self.assertEqual(
1479 result, expected_result, "Default sw version should be 1.0"
1480 )
1481
1482 with self.subTest(
1483 i=2, t="Find software version, VNFD includes software version"
1484 ):
1485 # Testing method find_software_version
1486
1487 db_vnfd = self.db.get_one(
1488 "vnfds", {"_id": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"}
1489 )
1490 db_vnfd["software-version"] = "3.1"
1491 expected_result = "3.1"
1492 result = find_software_version(db_vnfd)
1493 self.assertEqual(result, expected_result, "VNFD software version is wrong")
1494
1495 with self.subTest(i=3, t="Check charm hash, Hash has did not change"):
1496 # Testing method check_charm_hash_changed
1497
1498 current_path, target_path = "/tmp/charm1", "/tmp/charm1"
1499 fs = Mock(autospec=True)
1500 fs.path.__add__ = Mock()
1501 fs.path.side_effect = ["/", "/", "/", "/"]
1502
1503 mock_path = Mock(autospec=True)
1504 mock_path.exists.side_effect = [True, True]
1505
1506 mock_checksumdir = Mock(autospec=True)
1507 mock_checksumdir.dirhash.side_effect = ["hash_value", "hash_value"]
1508
1509 instance = self.my_ns
1510 expected_result = False
1511
1512 with patch.object(instance, "fs", fs), patch(
1513 "checksumdir.dirhash", mock_checksumdir.dirhash
1514 ), patch("os.path.exists", mock_path.exists):
1515
1516 result = instance.check_charm_hash_changed(current_path, target_path)
1517 self.assertEqual(
1518 result, expected_result, "Wrong charm hash control value"
1519 )
1520 self.assertEqual(mock_path.exists.call_count, 2)
1521 self.assertEqual(mock_checksumdir.dirhash.call_count, 2)
1522
1523 mock_reset()
1524
1525 with self.subTest(i=4, t="Check charm hash, Hash has changed"):
1526 # Testing method check_charm_hash_changed
1527
1528 current_path, target_path = "/tmp/charm1", "/tmp/charm2"
1529 fs = Mock(autospec=True)
1530 fs.path.__add__ = Mock()
1531 fs.path.side_effect = ["/", "/", "/", "/"]
1532
1533 mock_path = Mock(autospec=True)
1534 mock_path.exists.side_effect = [True, True]
1535
1536 mock_checksumdir = Mock(autospec=True)
1537 mock_checksumdir.dirhash.side_effect = ["hash_value", "another_hash_value"]
1538
1539 instance = self.my_ns
1540 expected_result = True
1541
1542 with patch.object(instance, "fs", fs), patch(
1543 "checksumdir.dirhash", mock_checksumdir.dirhash
1544 ), patch("os.path.exists", mock_path.exists):
1545
1546 result = instance.check_charm_hash_changed(current_path, target_path)
1547 self.assertEqual(
1548 result, expected_result, "Wrong charm hash control value"
1549 )
1550 self.assertEqual(mock_path.exists.call_count, 2)
1551 self.assertEqual(mock_checksumdir.dirhash.call_count, 2)
1552
1553 mock_reset()
1554
1555 with self.subTest(i=5, t="Check charm hash, Charm path does not exists"):
1556 # Testing method check_charm_hash_changed
1557
1558 current_path, target_path = "/tmp/charm1", "/tmp/charm2"
1559 fs = Mock(autospec=True)
1560 fs.path.__add__ = Mock()
1561 fs.path.side_effect = ["/", "/", "/", "/"]
1562
1563 mock_path = Mock(autospec=True)
1564 mock_path.exists.side_effect = [True, False]
1565
1566 mock_checksumdir = Mock(autospec=True)
1567 mock_checksumdir.dirhash.side_effect = ["hash_value", "hash_value"]
1568
1569 instance = self.my_ns
1570
1571 with patch.object(instance, "fs", fs), patch(
1572 "checksumdir.dirhash", mock_checksumdir.dirhash
1573 ), patch("os.path.exists", mock_path.exists):
1574
1575 with self.assertRaises(LcmException):
1576
1577 instance.check_charm_hash_changed(current_path, target_path)
1578 self.assertEqual(mock_path.exists.call_count, 2)
1579 self.assertEqual(mock_checksumdir.dirhash.call_count, 0)
1580
1581 mock_reset()
1582
1583 with self.subTest(i=6, t="Check juju bundle existence"):
1584 # Testing method check_juju_bundle_existence
1585
1586 test_vnfd1 = self.db.get_one(
1587 "vnfds", {"_id": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77"}
1588 )
1589 test_vnfd2 = self.db.get_one(
1590 "vnfds", {"_id": "d96b1cdf-5ad6-49f7-bf65-907ada989293"}
1591 )
1592
1593 expected_result = None
1594 result = check_juju_bundle_existence(test_vnfd1)
1595 self.assertEqual(result, expected_result, "Wrong juju bundle name")
1596
1597 expected_result = "stable/native-kdu"
1598 result = check_juju_bundle_existence(test_vnfd2)
1599 self.assertEqual(result, expected_result, "Wrong juju bundle name")
1600
1601 with self.subTest(i=7, t="Check charm artifacts"):
1602 # Testing method check_juju_bundle_existence
1603
1604 base_folder = {
1605 "folder": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77",
1606 "pkg-dir": "hackfest_3charmed_vnfd",
1607 }
1608 charm_name = "simple"
1609 charm_type = "lxc_proxy_charm"
1610 revision = 3
1611
1612 expected_result = "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77:3/hackfest_3charmed_vnfd/charms/simple"
1613 result = get_charm_artifact_path(
1614 base_folder, charm_name, charm_type, revision
1615 )
1616 self.assertEqual(result, expected_result, "Wrong charm artifact path")
1617
1618 # SOL004 packages
1619 base_folder = {
1620 "folder": "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77",
1621 }
1622 charm_name = "basic"
1623 charm_type = ""
1624 revision = ""
1625
1626 expected_result = (
1627 "7637bcf8-cf14-42dc-ad70-c66fcf1e6e77/Scripts/helm-charts/basic"
1628 )
1629 result = get_charm_artifact_path(
1630 base_folder, charm_name, charm_type, revision
1631 )
1632 self.assertEqual(result, expected_result, "Wrong charm artifact path")
1633
1634
1635 if __name__ == "__main__":
1636 asynctest.main()