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
23 from osm_lcm
import ns
24 from osm_common
.dbmemory
import DbMemory
25 from osm_common
.msgkafka
import MsgKafka
26 from osm_common
.fslocal
import FsLocal
27 from osm_lcm
.lcm_utils
import TaskRegistry
28 # from osm_lcm.ROclient import ROClient
29 from uuid
import uuid4
30 # from asynctest.mock import patch
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
54 "VCA": { # TODO replace with os.get_env to get other configurations
55 "host": getenv("OSMLCM_VCA_HOST", "vca"),
56 "port": getenv("OSMLCM_VCA_PORT", 17070),
57 "user": getenv("OSMLCM_VCA_USER", "admin"),
58 "secret": getenv("OSMLCM_VCA_SECRET", "vca"),
59 "public_key": getenv("OSMLCM_VCA_PUBKEY", None),
60 'ca_cert': getenv("OSMLCM_VCA_CACERT", None)
63 "endpoint_url": "http://{}:{}/openmano".format(getenv("OSMLCM_RO_HOST", "ro"),
64 getenv("OSMLCM_RO_PORT", "9090")),
65 "tenant": getenv("OSMLCM_RO_TENANT", "osm"),
66 "logger_name": "lcm.ROclient",
72 class TestMyNS(asynctest
.TestCase
):
74 async def _n2vc_DeployCharms(self
, model_name
, application_name
, vnfd
, charm_path
, params
={}, machine_spec
={},
75 callback
=None, *callback_args
):
77 for status
, message
in (("maintenance", "installing sofwware"), ("active", "Ready!")):
78 # call callback after some time
79 asyncio
.sleep(5, loop
=self
.loop
)
80 callback(model_name
, application_name
, status
, message
, *callback_args
)
83 def _n2vc_FormatApplicationName(*args
):
86 yield "app_name-{}".format(num_calls
)
89 def _n2vc_CreateExecutionEnvironment(self
, namespace
, reuse_ee_id
, db_dict
):
90 k_list
= namespace
.split(".")
91 ee_id
= k_list
[1] + "."
99 def _ro_show(self
, *args
, **kwargs
):
100 if kwargs
.get("delete"):
101 ro_ns_desc
= yaml
.load(descriptors
.ro_delete_action_text
, Loader
=yaml
.Loader
)
105 ro_ns_desc
= yaml
.load(descriptors
.ro_ns_text
, Loader
=yaml
.Loader
)
107 # if ip address provided, replace descriptor
108 ip_addresses
= getenv("OSMLCMTEST_NS_IPADDRESS", "")
110 ip_addresses_list
= ip_addresses
.split(",")
111 for vnf
in ro_ns_desc
["vnfs"]:
112 if not ip_addresses_list
:
114 vnf
["ip_address"] = ip_addresses_list
[0]
115 for vm
in vnf
["vms"]:
116 if not ip_addresses_list
:
118 vm
["ip_address"] = ip_addresses_list
.pop(0)
122 for net
in ro_ns_desc
["nets"]:
123 if net
["status"] != "ACTIVE":
124 net
["status"] = "ACTIVE"
127 for vnf
in ro_ns_desc
["vnfs"]:
128 for vm
in vnf
["vms"]:
129 if vm
["status"] != "ACTIVE":
130 vm
["status"] = "ACTIVE"
133 def _ro_create(self
, *args
, **kwargs
):
135 yield {"uuid": str(uuid4())}
137 def _return_uuid(self
, *args
, **kwargs
):
140 async def setUp(self
):
143 if not getenv("OSMLCMTEST_DB_NOMOCK"):
145 self
.db
.create_list("vnfds", yaml
.load(descriptors
.db_vnfds_text
, Loader
=yaml
.Loader
))
146 self
.db
.create_list("nsds", yaml
.load(descriptors
.db_nsds_text
, Loader
=yaml
.Loader
))
147 self
.db
.create_list("nsrs", yaml
.load(descriptors
.db_nsrs_text
, Loader
=yaml
.Loader
))
148 self
.db
.create_list("vim_accounts", yaml
.load(descriptors
.db_vim_accounts_text
, Loader
=yaml
.Loader
))
149 self
.db
.create_list("k8sclusters", yaml
.load(descriptors
.db_k8sclusters_text
, Loader
=yaml
.Loader
))
150 self
.db
.create_list("nslcmops", yaml
.load(descriptors
.db_nslcmops_text
, Loader
=yaml
.Loader
))
151 self
.db
.create_list("vnfrs", yaml
.load(descriptors
.db_vnfrs_text
, Loader
=yaml
.Loader
))
152 self
.db_vim_accounts
= yaml
.load(descriptors
.db_vim_accounts_text
, Loader
=yaml
.Loader
)
155 self
.msg
= asynctest
.Mock(MsgKafka())
158 if not getenv("OSMLCMTEST_FS_NOMOCK"):
159 self
.fs
= asynctest
.Mock(FsLocal())
160 self
.fs
.get_params
.return_value
= {"path": getenv("OSMLCMTEST_PACKAGES_PATH", "./test/temp/packages")}
161 self
.fs
.file_open
= asynctest
.mock_open()
162 # self.fs.file_open.return_value.__enter__.return_value = asynctest.MagicMock() # called on a python "with"
163 # self.fs.file_open.return_value.__enter__.return_value.read.return_value = "" # empty file
166 self
.lcm_tasks
= asynctest
.Mock(TaskRegistry())
167 self
.lcm_tasks
.lock_HA
.return_value
= True
168 self
.lcm_tasks
.waitfor_related_HA
.return_value
= None
169 self
.lcm_tasks
.lookfor_related
.return_value
= ("", [])
172 if not getenv("OSMLCMTEST_VCA_K8s_NOMOCK"):
173 ns
.K8sJujuConnector
= asynctest
.MagicMock(ns
.K8sJujuConnector
)
174 ns
.K8sHelmConnector
= asynctest
.MagicMock(ns
.K8sHelmConnector
)
176 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
177 ns
.N2VCJujuConnector
= asynctest
.MagicMock(ns
.N2VCJujuConnector
)
180 self
.my_ns
= ns
.NsLcm(self
.db
, self
.msg
, self
.fs
, self
.lcm_tasks
, lcm_config
, self
.loop
)
181 self
.my_ns
._wait
_dependent
_n
2vc
= asynctest
.CoroutineMock()
184 if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
185 self
.my_ns
.logger
= asynctest
.Mock(self
.my_ns
.logger
)
188 if not getenv("OSMLCMTEST_VCA_NOMOCK"):
189 pub_key
= getenv("OSMLCMTEST_NS_PUBKEY", "ssh-rsa test-pub-key t@osm.com")
190 # self.my_ns.n2vc = asynctest.Mock(N2VC())
191 self
.my_ns
.n2vc
.GetPublicKey
.return_value
= getenv("OSMLCM_VCA_PUBKEY", "public_key")
192 # allow several versions of n2vc
193 self
.my_ns
.n2vc
.FormatApplicationName
= asynctest
.Mock(side_effect
=self
._n
2vc
_FormatApplicationName
())
194 self
.my_ns
.n2vc
.DeployCharms
= asynctest
.CoroutineMock(side_effect
=self
._n
2vc
_DeployCharms
)
195 self
.my_ns
.n2vc
.create_execution_environment
= asynctest
.CoroutineMock(
196 side_effect
=self
._n
2vc
_CreateExecutionEnvironment
)
197 self
.my_ns
.n2vc
.install_configuration_sw
= asynctest
.CoroutineMock(return_value
=pub_key
)
198 self
.my_ns
.n2vc
.get_ee_ssh_public__key
= asynctest
.CoroutineMock(return_value
=pub_key
)
199 self
.my_ns
.n2vc
.exec_primitive
= asynctest
.CoroutineMock(side_effect
=self
._return
_uuid
)
200 self
.my_ns
.n2vc
.GetPrimitiveStatus
= asynctest
.CoroutineMock(return_value
="completed")
201 self
.my_ns
.n2vc
.GetPrimitiveOutput
= asynctest
.CoroutineMock(return_value
={"result": "ok",
203 self
.my_ns
.n2vc
.get_public_key
= asynctest
.CoroutineMock(
204 return_value
=getenv("OSMLCM_VCA_PUBKEY", "public_key"))
205 self
.my_ns
.n2vc
.delete_namespace
= asynctest
.CoroutineMock(return_value
=None)
208 if not getenv("OSMLCMTEST_RO_NOMOCK"):
209 # self.my_ns.RO = asynctest.Mock(ROclient.ROClient(self.loop, **lcm_config["ro_config"]))
210 # TODO first time should be empty list, following should return a dict
211 self
.my_ns
.RO
.get_list
= asynctest
.CoroutineMock(self
.my_ns
.RO
.get_list
, return_value
=[])
212 self
.my_ns
.RO
.create
= asynctest
.CoroutineMock(self
.my_ns
.RO
.create
, side_effect
=self
._ro
_create
())
213 self
.my_ns
.RO
.show
= asynctest
.CoroutineMock(self
.my_ns
.RO
.show
, side_effect
=self
._ro
_show
())
214 self
.my_ns
.RO
.create_action
= asynctest
.CoroutineMock(self
.my_ns
.RO
.create_action
,
215 return_value
={"vm-id": {"vim_result": 200,
216 "description": "done"}})
217 self
.my_ns
.RO
.delete
= asynctest
.CoroutineMock(self
.my_ns
.RO
.delete
, return_value
={"action_id": "del"})
218 # self.my_ns.wait_vm_up_insert_key_ro = asynctest.CoroutineMock(return_value="ip-address")
220 @asynctest.fail_on(active_handles
=True) # all async tasks must be completed
221 async def test_instantiate(self
):
222 self
.db
.set_one
= asynctest
.Mock()
223 nsr_id
= descriptors
.test_ids
["TEST-A"]["ns"]
224 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
225 # print("Test instantiate started")
227 # delete deployed information of database
228 if not getenv("OSMLCMTEST_DB_NOMOCK"):
229 if self
.db
.get_list("nsrs")[0]["_admin"].get("deployed"):
230 del self
.db
.get_list("nsrs")[0]["_admin"]["deployed"]
231 for db_vnfr
in self
.db
.get_list("vnfrs"):
232 db_vnfr
.pop("ip_address", None)
233 for db_vdur
in db_vnfr
["vdur"]:
234 db_vdur
.pop("ip_address", None)
235 db_vdur
.pop("mac_address", None)
236 if getenv("OSMLCMTEST_RO_VIMID"):
237 self
.db
.get_list("vim_accounts")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
238 if getenv("OSMLCMTEST_RO_VIMID"):
239 self
.db
.get_list("nsrs")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID")
241 await self
.my_ns
.instantiate(nsr_id
, nslcmop_id
)
243 # print("instantiate_result: {}".format(self.db.get_one("nslcmops",
244 # {"_id": nslcmop_id}).get("detailed-status")))
246 self
.msg
.aiowrite
.assert_called_once_with("ns", "instantiated",
247 {"nsr_id": nsr_id
, "nslcmop_id": nslcmop_id
,
248 "operationState": "COMPLETED"},
250 self
.lcm_tasks
.lock_HA
.assert_called_once_with('ns', 'nslcmops', nslcmop_id
)
251 if not getenv("OSMLCMTEST_LOGGING_NOMOCK"):
252 self
.assertTrue(self
.my_ns
.logger
.debug
.called
, "Debug method not called")
253 self
.my_ns
.logger
.error
.assert_not_called()
254 self
.my_ns
.logger
.exception().assert_not_called()
256 if not getenv("OSMLCMTEST_DB_NOMOCK"):
257 self
.assertTrue(self
.db
.set_one
.called
, "db.set_one not called")
259 # TODO add more checks of called methods
260 # TODO add a terminate
262 def test_ns_params_2_RO(self
):
263 vim
= self
.db
.get_list("vim_accounts")[0]
265 ro_vim_id
= vim
["_admin"]["deployed"]["RO"]
266 ns_params
= {"vimAccountId": vim_id
}
267 mgmt_interface
= {"cp": "cp"}
268 vdu
= [{"id": "vdu_id", "interface": [{"external-connection-point-ref": "cp"}]}]
270 "1": {"vdu": vdu
, "mgmt-interface": mgmt_interface
},
271 "2": {"vdu": vdu
, "mgmt-interface": mgmt_interface
, "vnf-configuration": None},
272 "3": {"vdu": vdu
, "mgmt-interface": mgmt_interface
, "vnf-configuration": {"config-access": None}},
273 "4": {"vdu": vdu
, "mgmt-interface": mgmt_interface
,
274 "vnf-configuration": {"config-access": {"ssh-access": None}}},
275 "5": {"vdu": vdu
, "mgmt-interface": mgmt_interface
,
276 "vnf-configuration": {"config-access": {"ssh-access": {"required": True, "default_user": "U"}}}},
278 nsd
= {"constituent-vnfd": []}
279 for k
in vnfd_dict
.keys():
280 nsd
["constituent-vnfd"].append({"vnfd-id-ref": k
, "member-vnf-index": k
})
282 n2vc_key_list
= ["key"]
283 ro_ns_params
= self
.my_ns
.ns_params_2_RO(ns_params
, nsd
, vnfd_dict
, n2vc_key_list
)
284 ro_params_expected
= {'wim_account': None, "datacenter": ro_vim_id
,
285 "vnfs": {"5": {"vdus": {"vdu_id": {"mgmt_keys": n2vc_key_list
}}}}}
286 self
.assertEqual(ro_ns_params
, ro_params_expected
)
288 # Test scale() and related methods
289 @asynctest.fail_on(active_handles
=True) # all async tasks must be completed
290 async def test_scale(self
):
291 # print("Test scale started")
293 # TODO: Add more higher-lever tests here, for example:
294 # scale-out/scale-in operations with success/error result
296 # Test scale() with missing 'scaleVnfData', should return operationState = 'FAILED'
297 nsr_id
= descriptors
.test_ids
["TEST-A"]["ns"]
298 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
299 await self
.my_ns
.scale(nsr_id
, nslcmop_id
)
300 expected_value
= 'FAILED'
301 return_value
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
}).get("operationState")
302 self
.assertEqual(return_value
, expected_value
)
303 # print("scale_result: {}".format(self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status")))
305 # Test _reintent_or_skip_suboperation()
307 # - if a suboperation's 'operationState' is marked as 'COMPLETED', SUBOPERATION_STATUS_SKIP is expected
308 # - if marked as anything but 'COMPLETED', the suboperation index is expected
309 def test_scale_reintent_or_skip_suboperation(self
):
310 # Load an alternative 'nslcmops' YAML for this test
311 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
312 db_nslcmop
= self
.db
.get_one('nslcmops', {"_id": nslcmop_id
})
314 # Test when 'operationState' is 'COMPLETED'
315 db_nslcmop
['_admin']['operations'][op_index
]['operationState'] = 'COMPLETED'
316 return_value
= self
.my_ns
._reintent
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
317 expected_value
= self
.my_ns
.SUBOPERATION_STATUS_SKIP
318 self
.assertEqual(return_value
, expected_value
)
319 # Test when 'operationState' is not 'COMPLETED'
320 db_nslcmop
['_admin']['operations'][op_index
]['operationState'] = None
321 return_value
= self
.my_ns
._reintent
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
322 expected_value
= op_index
323 self
.assertEqual(return_value
, expected_value
)
325 # Test _find_suboperation()
326 # Expected result: index of the found sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if not found
327 def test_scale_find_suboperation(self
):
328 # Load an alternative 'nslcmops' YAML for this test
329 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
330 db_nslcmop
= self
.db
.get_one('nslcmops', {"_id": nslcmop_id
})
331 # Find this sub-operation
333 vnf_index
= db_nslcmop
['_admin']['operations'][op_index
]['member_vnf_index']
334 primitive
= db_nslcmop
['_admin']['operations'][op_index
]['primitive']
335 primitive_params
= db_nslcmop
['_admin']['operations'][op_index
]['primitive_params']
337 'member_vnf_index': vnf_index
,
338 'primitive': primitive
,
339 'primitive_params': primitive_params
,
341 found_op_index
= self
.my_ns
._find
_suboperation
(db_nslcmop
, match
)
342 self
.assertEqual(found_op_index
, op_index
)
343 # Test with not-matching params
345 'member_vnf_index': vnf_index
,
347 'primitive_params': primitive_params
,
349 found_op_index
= self
.my_ns
._find
_suboperation
(db_nslcmop
, match
)
350 self
.assertEqual(found_op_index
, self
.my_ns
.SUBOPERATION_STATUS_NOT_FOUND
)
353 found_op_index
= self
.my_ns
._find
_suboperation
(db_nslcmop
, match
)
354 self
.assertEqual(found_op_index
, self
.my_ns
.SUBOPERATION_STATUS_NOT_FOUND
)
356 # Test _update_suboperation_status()
357 def test_scale_update_suboperation_status(self
):
358 self
.db
.set_one
= asynctest
.Mock()
359 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
360 db_nslcmop
= self
.db
.get_one('nslcmops', {"_id": nslcmop_id
})
362 # Force the initial values to be distinct from the updated ones
363 q_filter
= {"_id": db_nslcmop
["_id"]}
364 # Test to change 'operationState' and 'detailed-status'
365 operationState
= 'COMPLETED'
366 detailed_status
= 'Done'
367 expected_update_dict
= {'_admin.operations.0.operationState': operationState
,
368 '_admin.operations.0.detailed-status': detailed_status
,
370 self
.my_ns
._update
_suboperation
_status
(db_nslcmop
, op_index
, operationState
, detailed_status
)
371 self
.db
.set_one
.assert_called_once_with("nslcmops", q_filter
=q_filter
, update_dict
=expected_update_dict
,
374 def test_scale_add_suboperation(self
):
375 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
376 db_nslcmop
= self
.db
.get_one('nslcmops', {"_id": nslcmop_id
})
378 num_ops_before
= len(db_nslcmop
.get('_admin', {}).get('operations', [])) - 1
380 vdu_count_index
= None
383 mapped_primitive_params
= {'parameter':
384 [{'data-type': 'STRING',
386 'default-value': '<touch_filename2>'}],
388 operationState
= 'PROCESSING'
389 detailed_status
= 'In progress'
390 operationType
= 'PRE-SCALE'
391 # Add a 'pre-scale' suboperation
392 op_index_after
= self
.my_ns
._add
_suboperation
(db_nslcmop
, vnf_index
, vdu_id
, vdu_count_index
,
393 vdu_name
, primitive
, mapped_primitive_params
,
394 operationState
, detailed_status
, operationType
)
395 self
.assertEqual(op_index_after
, num_ops_before
+ 1)
397 # Delete all suboperations and add the same operation again
398 del db_nslcmop
['_admin']['operations']
399 op_index_zero
= self
.my_ns
._add
_suboperation
(db_nslcmop
, vnf_index
, vdu_id
, vdu_count_index
,
400 vdu_name
, primitive
, mapped_primitive_params
,
401 operationState
, detailed_status
, operationType
)
402 self
.assertEqual(op_index_zero
, 0)
404 # Add a 'RO' suboperation
405 RO_nsr_id
= '1234567890'
406 RO_scaling_info
= [{'type': 'create', 'count': 1, 'member-vnf-index': '1', 'osm_vdu_id': 'dataVM'}]
407 op_index
= self
.my_ns
._add
_suboperation
(db_nslcmop
, vnf_index
, vdu_id
, vdu_count_index
,
408 vdu_name
, primitive
, mapped_primitive_params
,
409 operationState
, detailed_status
, operationType
,
410 RO_nsr_id
, RO_scaling_info
)
411 db_RO_nsr_id
= db_nslcmop
['_admin']['operations'][op_index
]['RO_nsr_id']
412 self
.assertEqual(op_index
, 1)
413 self
.assertEqual(RO_nsr_id
, db_RO_nsr_id
)
415 # Try to add an invalid suboperation, should return SUBOPERATION_STATUS_NOT_FOUND
416 op_index_invalid
= self
.my_ns
._add
_suboperation
(None, None, None, None, None,
419 self
.assertEqual(op_index_invalid
, self
.my_ns
.SUBOPERATION_STATUS_NOT_FOUND
)
421 # Test _check_or_add_scale_suboperation() and _check_or_add_scale_suboperation_RO()
422 # check the possible return values:
423 # - SUBOPERATION_STATUS_NEW: This is a new sub-operation
424 # - op_index (non-negative number): This is an existing sub-operation, operationState != 'COMPLETED'
425 # - SUBOPERATION_STATUS_SKIP: This is an existing sub-operation, operationState == 'COMPLETED'
426 def test_scale_check_or_add_scale_suboperation(self
):
427 nslcmop_id
= descriptors
.test_ids
["TEST-A"]["instantiate"]
428 db_nslcmop
= self
.db
.get_one('nslcmops', {"_id": nslcmop_id
})
429 operationType
= 'PRE-SCALE'
432 primitive_params
= {'parameter':
433 [{'data-type': 'STRING',
435 'default-value': '<touch_filename2>'}],
438 # Delete all sub-operations to be sure this is a new sub-operation
439 del db_nslcmop
['_admin']['operations']
441 # Add a new sub-operation
442 # For new sub-operations, operationState is set to 'PROCESSING' by default
443 op_index_new
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
444 db_nslcmop
, vnf_index
, primitive
, primitive_params
, operationType
)
445 self
.assertEqual(op_index_new
, self
.my_ns
.SUBOPERATION_STATUS_NEW
)
447 # Use the same parameters again to match the already added sub-operation
448 # which has status 'PROCESSING' (!= 'COMPLETED') by default
449 # The expected return value is a non-negative number
450 op_index_existing
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
451 db_nslcmop
, vnf_index
, primitive
, primitive_params
, operationType
)
452 self
.assertTrue(op_index_existing
>= 0)
454 # Change operationState 'manually' for this sub-operation
455 db_nslcmop
['_admin']['operations'][op_index_existing
]['operationState'] = 'COMPLETED'
456 # Then use the same parameters again to match the already added sub-operation,
457 # which now has status 'COMPLETED'
458 # The expected return value is SUBOPERATION_STATUS_SKIP
459 op_index_skip
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
460 db_nslcmop
, vnf_index
, primitive
, primitive_params
, operationType
)
461 self
.assertEqual(op_index_skip
, self
.my_ns
.SUBOPERATION_STATUS_SKIP
)
463 # RO sub-operation test:
464 # Repeat tests for the very similar _check_or_add_scale_suboperation_RO(),
465 RO_nsr_id
= '1234567890'
466 RO_scaling_info
= [{'type': 'create', 'count': 1, 'member-vnf-index': '1', 'osm_vdu_id': 'dataVM'}]
467 op_index_new_RO
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
468 db_nslcmop
, vnf_index
, None, None, 'SCALE-RO', RO_nsr_id
, RO_scaling_info
)
469 self
.assertEqual(op_index_new_RO
, self
.my_ns
.SUBOPERATION_STATUS_NEW
)
471 # Use the same parameters again to match the already added RO sub-operation
472 op_index_existing_RO
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
473 db_nslcmop
, vnf_index
, None, None, 'SCALE-RO', RO_nsr_id
, RO_scaling_info
)
474 self
.assertTrue(op_index_existing_RO
>= 0)
476 # Change operationState 'manually' for this RO sub-operation
477 db_nslcmop
['_admin']['operations'][op_index_existing_RO
]['operationState'] = 'COMPLETED'
478 # Then use the same parameters again to match the already added sub-operation,
479 # which now has status 'COMPLETED'
480 # The expected return value is SUBOPERATION_STATUS_SKIP
481 op_index_skip_RO
= self
.my_ns
._check
_or
_add
_scale
_suboperation
(
482 db_nslcmop
, vnf_index
, None, None, 'SCALE-RO', RO_nsr_id
, RO_scaling_info
)
483 self
.assertEqual(op_index_skip_RO
, self
.my_ns
.SUBOPERATION_STATUS_SKIP
)
485 async def test_deploy_kdus(self
):
486 nsr_id
= descriptors
.test_ids
["TEST-KDU"]["ns"]
487 # nslcmop_id = descriptors.test_ids["TEST-KDU"]["instantiate"]
488 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
489 db_vnfr
= self
.db
.get_one("vnfrs", {"nsr-id-ref": nsr_id
, "member-vnf-index-ref": "multikdu"})
490 db_vnfrs
= {"multikdu": db_vnfr
}
491 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
492 db_vnfds
= {db_vnfd
["_id"]: db_vnfd
}
494 self
.my_ns
.k8sclusterhelm
.install
= asynctest
.CoroutineMock(return_value
="k8s_id")
495 await self
.my_ns
.deploy_kdus(logging_text
, nsr_id
, db_nsr
, db_vnfrs
, db_vnfds
)
496 db_nsr
= self
.db
.get_list("nsrs")[1]
497 self
.assertIn("K8s", db_nsr
["_admin"]["deployed"], "K8s entry not created at '_admin.deployed'")
498 self
.assertIsInstance(db_nsr
["_admin"]["deployed"]["K8s"], list, "K8s entry is not of type list")
499 self
.assertEqual(len(db_nsr
["_admin"]["deployed"]["K8s"]), 2, "K8s entry is not of type list")
500 k8s_instace_info
= {"kdu-instance": "k8s_id", "k8scluster-uuid": "73d96432-d692-40d2-8440-e0c73aee209c",
501 "k8scluster-type": "chart",
502 "kdu-name": "ldap", "kdu-model": "stable/openldap:1.2.1"}
504 self
.assertEqual(db_nsr
["_admin"]["deployed"]["K8s"][0], k8s_instace_info
)
505 k8s_instace_info
["kdu-name"] = "mongo"
506 k8s_instace_info
["kdu-model"] = "stable/mongodb"
507 self
.assertEqual(db_nsr
["_admin"]["deployed"]["K8s"][1], k8s_instace_info
)
510 if __name__
== '__main__':