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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # For those usages not covered by the Apache License, Version 2.0 please
15 # contact: alfonso.tiernosepulveda@telefonica.com
19 import asynctest
# pip3 install asynctest --user
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
32 from osm_lcm
.tests
import test_db_descriptors
as descriptors
34 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
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
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"),
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",
77 class TestMyNS(asynctest
.TestCase
):
79 async def _n2vc_DeployCharms(self
, model_name
, application_name
, vnfd
, charm_path
, params
={}, machine_spec
={},
80 callback
=None, *callback_args
):
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
)
88 def _n2vc_FormatApplicationName(*args
):
91 yield "app_name-{}".format(num_calls
)
94 def _n2vc_CreateExecutionEnvironment(self
, namespace
, reuse_ee_id
, db_dict
, *args
, **kwargs
):
95 k_list
= namespace
.split(".")
96 ee_id
= k_list
[1] + "."
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
)
112 ro_ns_desc
= yaml
.load(descriptors
.ro_ns_text
, Loader
=yaml
.Loader
)
114 # if ip address provided, replace descriptor
115 ip_addresses
= getenv("OSMLCMTEST_NS_IPADDRESS", "")
117 ip_addresses_list
= ip_addresses
.split(",")
118 for vnf
in ro_ns_desc
["vnfs"]:
119 if not ip_addresses_list
:
121 vnf
["ip_address"] = ip_addresses_list
[0]
122 for vm
in vnf
["vms"]:
123 if not ip_addresses_list
:
125 vm
["ip_address"] = ip_addresses_list
.pop(0)
129 for net
in ro_ns_desc
["nets"]:
130 if net
["status"] != "ACTIVE":
131 net
["status"] = "ACTIVE"
134 for vnf
in ro_ns_desc
["vnfs"]:
135 for vm
in vnf
["vms"]:
136 if vm
["status"] != "ACTIVE":
137 vm
["status"] = "ACTIVE"
140 def _ro_deploy(self
, *args
, **kwargs
):
142 'action_id': args
[1]["action_id"],
147 def _return_uuid(self
, *args
, **kwargs
):
150 async def setUp(self
):
153 if not getenv("OSMLCMTEST_DB_NOMOCK"):
154 # Cleanup singleton Database instance
155 Database
.instance
= None
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
)
172 self
.msg
= asynctest
.Mock(MsgKafka())
175 if not getenv("OSMLCMTEST_FS_NOMOCK"):
176 self
.fs
= asynctest
.Mock(Filesystem({
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
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
= ("", [])
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
)
199 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
200 ns
.N2VCJujuConnector
= asynctest
.MagicMock(ns
.N2VCJujuConnector
)
201 ns
.LCMHelmConn
= asynctest
.MagicMock(ns
.LCMHelmConn
)
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
_n
2vc
= asynctest
.CoroutineMock()
210 if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
211 self
.my_ns
.logger
= asynctest
.Mock(self
.my_ns
.logger
)
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
._n
2vc
_FormatApplicationName
())
220 self
.my_ns
.n2vc
.DeployCharms
= asynctest
.CoroutineMock(side_effect
=self
._n
2vc
_DeployCharms
)
221 self
.my_ns
.n2vc
.create_execution_environment
= asynctest
.CoroutineMock(
222 side_effect
=self
._n
2vc
_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",
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)
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
)
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")
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")
267 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
269 # self.msg.aiowrite.assert_called_once_with("ns", "instantiated",
270 # {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id,
271 # "operationState": "COMPLETED"},
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()
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")
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")
295 # # TODO add more checks of called methods
296 # # TODO add a terminate
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",
304 # "execution-environment-ref": "not_used_ee"
308 # "id": ee_descriptor_id,
309 # "juju": {"charm": "simple"},
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",
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
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")
333 # TODO: Add more higher-lever tests here, for example:
334 # scale-out/scale-in operations with success/error result
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")))
345 # Test _retry_or_skip_suboperation()
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
})
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
)
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
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']
377 'member_vnf_index': vnf_index
,
378 'primitive': primitive
,
379 'primitive_params': primitive_params
,
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
385 'member_vnf_index': vnf_index
,
387 'primitive_params': primitive_params
,
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
)
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
)
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
})
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
,
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
,
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
})
418 num_ops_before
= len(db_nslcmop
.get('_admin', {}).get('operations', [])) - 1
420 vdu_count_index
= None
423 mapped_primitive_params
= {'parameter':
424 [{'data-type': 'STRING',
426 'default-value': '<touch_filename2>'}],
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)
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)
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
)
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,
459 self
.assertEqual(op_index_invalid
, self
.my_ns
.SUBOPERATION_STATUS_NOT_FOUND
)
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'
472 primitive_params
= {'parameter':
473 [{'data-type': 'STRING',
475 'default-value': '<touch_filename2>'}],
478 # Delete all sub-operations to be sure this is a new sub-operation
479 del db_nslcmop
['_admin']['operations']
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
)
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)
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
)
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
)
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)
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
)
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"]})
535 self
.my_ns
.k8sclusterhelm3
.generate_kdu_instance_name
= asynctest
.mock
.Mock()
536 self
.my_ns
.k8sclusterhelm3
.generate_kdu_instance_name
.return_value
= "k8s_id"
537 self
.my_ns
.k8sclusterhelm3
.install
= asynctest
.CoroutineMock()
538 self
.my_ns
.k8sclusterhelm3
.synchronize_repos
= asynctest
.CoroutineMock(return_value
=("", ""))
539 self
.my_ns
.k8sclusterhelm3
.get_services
= asynctest
.CoroutineMock(return_value
=([]))
540 await self
.my_ns
.deploy_kdus(logging_text
, nsr_id
, nslcmop_id
, db_vnfrs
, db_vnfds
, task_register
)
541 await asyncio
.wait(list(task_register
.keys()), timeout
=100)
542 db_nsr
= self
.db
.get_list("nsrs")[1]
543 self
.assertIn("K8s", db_nsr
["_admin"]["deployed"], "K8s entry not created at '_admin.deployed'")
544 self
.assertIsInstance(db_nsr
["_admin"]["deployed"]["K8s"], list, "K8s entry is not of type list")
545 self
.assertEqual(len(db_nsr
["_admin"]["deployed"]["K8s"]), 2, "K8s entry is not of type list")
546 k8s_instace_info
= {"kdu-instance": "k8s_id", "k8scluster-uuid": "73d96432-d692-40d2-8440-e0c73aee209c",
547 "k8scluster-type": "helm-chart-v3",
549 "member-vnf-index": "multikdu",
552 nsr_result
= copy
.deepcopy(db_nsr
["_admin"]["deployed"]["K8s"][0])
553 nsr_kdu_model_result
= nsr_result
.pop("kdu-model")
554 expected_kdu_model
= "stable/openldap:1.2.1"
555 self
.assertEqual(nsr_result
, k8s_instace_info
)
557 nsr_kdu_model_result
in expected_kdu_model
or expected_kdu_model
in nsr_kdu_model_result
559 nsr_result
= copy
.deepcopy(db_nsr
["_admin"]["deployed"]["K8s"][1])
560 nsr_kdu_model_result
= nsr_result
.pop("kdu-model")
561 k8s_instace_info
["kdu-name"] = "mongo"
562 expected_kdu_model
= "stable/mongodb"
563 self
.assertEqual(nsr_result
, k8s_instace_info
)
565 nsr_kdu_model_result
in expected_kdu_model
or expected_kdu_model
in nsr_kdu_model_result
568 # async def test_instantiate_pdu(self):
569 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
570 # nslcmop_id = descriptors.test_ids["TEST-A"]["instantiate"]
571 # # Modify vnfd/vnfr to change KDU for PDU. Adding keys that NBI will already set
572 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"},
573 # update_dict={"ip-address": "10.205.1.46",
574 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
575 # "vdur.0.pdu-type": "PDU-TYPE-1",
576 # "vdur.0.ip-address": "10.205.1.46",
578 # unset={"vdur.status": None})
579 # self.db.set_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "2"},
580 # update_dict={"ip-address": "10.205.1.47",
581 # "vdur.0.pdu-id": "53e1ec21-2464-451e-a8dc-6e311d45b2c8",
582 # "vdur.0.pdu-type": "PDU-TYPE-1",
583 # "vdur.0.ip-address": "10.205.1.47",
585 # unset={"vdur.status": None})
587 # await self.my_ns.instantiate(nsr_id, nslcmop_id)
588 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
589 # self.assertEqual(db_nsr.get("nsState"), "READY", str(db_nsr.get("errorDescription ")))
590 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
591 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
592 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
593 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
595 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
596 # async def test_terminate_without_configuration(self):
597 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
598 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
599 # # set instantiation task as completed
600 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
601 # update_dict={"operationState": "COMPLETED"})
602 # self.db.set_one("nsrs", {"_id": nsr_id},
603 # update_dict={"_admin.deployed.VCA.0": None, "_admin.deployed.VCA.1": None})
605 # await self.my_ns.terminate(nsr_id, nslcmop_id)
606 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
607 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
608 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
609 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
610 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
611 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
612 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
613 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
614 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
615 # db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
616 # for vnfr in db_vnfrs_list:
617 # self.assertEqual(vnfr["_admin"].get("nsState"), "NOT_INSTANTIATED", "Not instantiated")
619 # @asynctest.fail_on(active_handles=True) # all async tasks must be completed
620 # async def test_terminate_primitive(self):
621 # nsr_id = descriptors.test_ids["TEST-A"]["ns"]
622 # nslcmop_id = descriptors.test_ids["TEST-A"]["terminate"]
623 # # set instantiation task as completed
624 # self.db.set_list("nslcmops", {"nsInstanceId": nsr_id, "_id.ne": nslcmop_id},
625 # update_dict={"operationState": "COMPLETED"})
627 # # modify vnfd descriptor to include terminate_primitive
628 # terminate_primitive = [{
630 # "parameter": [{"name": "filename", "value": "terminate_filename"}],
633 # db_vnfr = self.db.get_one("vnfrs", {"nsr-id-ref": nsr_id, "member-vnf-index-ref": "1"})
634 # self.db.set_one("vnfds", {"_id": db_vnfr["vnfd-id"]},
635 # {"vnf-configuration.0.terminate-config-primitive": terminate_primitive})
637 # await self.my_ns.terminate(nsr_id, nslcmop_id)
638 # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
639 # self.assertEqual(db_nslcmop.get("operationState"), 'COMPLETED', db_nslcmop.get("detailed-status"))
640 # db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
641 # self.assertEqual(db_nsr.get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
642 # self.assertEqual(db_nsr["_admin"].get("nsState"), "NOT_INSTANTIATED", str(db_nsr.get("errorDescription ")))
643 # self.assertEqual(db_nsr.get("currentOperation"), "IDLE", "currentOperation different than 'IDLE'")
644 # self.assertEqual(db_nsr.get("currentOperationID"), None, "currentOperationID different than None")
645 # self.assertEqual(db_nsr.get("errorDescription "), None, "errorDescription different than None")
646 # self.assertEqual(db_nsr.get("errorDetail"), None, "errorDetail different than None")
649 if __name__
== '__main__':