X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Ftests%2Ftest_ns.py;h=548ba5725a9a48dbe7e3a6bf87056cb96a9c891e;hb=c231a87c94ba0cbd807b6b44e13d5f30747f04af;hp=486dd860917e64252ef8a9649a8337fefc2ad339;hpb=951169e7b758103e5bbefff01e5439ff6248f90f;p=osm%2FLCM.git diff --git a/osm_lcm/tests/test_ns.py b/osm_lcm/tests/test_ns.py index 486dd86..548ba57 100644 --- a/osm_lcm/tests/test_ns.py +++ b/osm_lcm/tests/test_ns.py @@ -12,7 +12,7 @@ # under the License. # # For those usages not covered by the Apache License, Version 2.0 please -# contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com +# contact: alfonso.tiernosepulveda@telefonica.com ## @@ -22,12 +22,14 @@ import yaml # import logging from os import getenv from osm_lcm.ns import NsLcm -from osm_common.dbmongo import DbMongo +from osm_common.dbmemory import DbMemory from osm_common.msgkafka import MsgKafka from osm_common.fslocal import FsLocal from osm_lcm.lcm_utils import TaskRegistry from n2vc.vnf import N2VC +# from n2vc.k8s_helm_conn import K8sHelmConnector from uuid import uuid4 +from asynctest.mock import patch from osm_lcm.tests import test_db_descriptors as descriptors @@ -49,61 +51,28 @@ It allows, if some testing ENV are supplied, testing without mocking some extern OSMLCM_RO_XXX: configuration of RO """ - -vca_config = { # TODO replace with os.get_env to get other configurations - "host": getenv("OSMLCM_VCA_HOST", "vca"), - "port": getenv("OSMLCM_VCA_PORT", 17070), - "user": getenv("OSMLCM_VCA_USER", "admin"), - "secret": getenv("OSMLCM_VCA_SECRET", "vca"), - "pubkey": getenv("OSMLCM_VCA_PUBKEY", None), - 'cacert': getenv("OSMLCM_VCA_CACERT", None) -} - -ro_config = { - "endpoint_url": "http://{}:{}/openmano".format(getenv("OSMLCM_RO_HOST", "ro"), getenv("OSMLCM_RO_PORT", "9090")), - "tenant": getenv("OSMLCM_RO_TENANT", "osm"), - "logger_name": "lcm.ROclient", - "loglevel": "DEBUG", +lcm_config = { + "timeout": {}, + "VCA": { # TODO replace with os.get_env to get other configurations + "host": getenv("OSMLCM_VCA_HOST", "vca"), + "port": getenv("OSMLCM_VCA_PORT", 17070), + "user": getenv("OSMLCM_VCA_USER", "admin"), + "secret": getenv("OSMLCM_VCA_SECRET", "vca"), + "public_key": getenv("OSMLCM_VCA_PUBKEY", None), + 'ca_cert': getenv("OSMLCM_VCA_CACERT", None) + }, + "ro_config": { + "endpoint_url": "http://{}:{}/openmano".format(getenv("OSMLCM_RO_HOST", "ro"), + getenv("OSMLCM_RO_PORT", "9090")), + "tenant": getenv("OSMLCM_RO_TENANT", "osm"), + "logger_name": "lcm.ROclient", + "loglevel": "DEBUG", + } } class TestMyNS(asynctest.TestCase): - def _db_get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): - if table not in self.db_content: - self.assertTrue(False, "db.get_one called with table={}".format(table)) - for db_item in self.db_content[table]: - if db_item["_id"] == q_filter["_id"]: - return db_item - else: - self.assertTrue(False, "db.get_one, table={}, not found _id={}".format(table, q_filter["_id"])) - - def _db_get_list(self, table, q_filter=None): - if table not in self.db_content: - self.assertTrue(False, "db.get_list called with table={} not found".format(table)) - return self.db_content[table] - - def _db_set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None): - db_item = self._db_get_one(table, q_filter, fail_on_empty=fail_on_empty) - for k, v in update_dict.items(): - db_nested = db_item - k_list = k.split(".") - for k_nested in k_list[0:-1]: - if isinstance(db_nested, list): - db_nested = db_nested[int(k_nested)] - else: - if k_nested not in db_nested: - db_nested[k_nested] = {} - db_nested = db_nested[k_nested] - k_nested = k_list[-1] - if isinstance(db_nested, list): - if int(k_nested) < len(db_nested): - db_nested[int(k_nested)] = v - else: - db_nested.insert(int(k_nested), v) - else: - db_nested[k_nested] = v - async def _n2vc_DeployCharms(self, model_name, application_name, vnfd, charm_path, params={}, machine_spec={}, callback=None, *callback_args): if callback: @@ -119,7 +88,7 @@ class TestMyNS(asynctest.TestCase): yield "app_name-{}".format(num_calls) num_calls += 1 - def _n2vc_CreateExecutionEnvironment(self, namespace): + def _n2vc_CreateExecutionEnvironment(self, namespace, reuse_ee_id, db_dict): k_list = namespace.split(".") ee_id = k_list[1] + "." if len(k_list) >= 2: @@ -127,7 +96,7 @@ class TestMyNS(asynctest.TestCase): ee_id += k[:8] else: ee_id += "_NS_" - return ee_id + return ee_id, {} def _ro_show(self, *args, **kwargs): ro_ns_desc = yaml.load(descriptors.db_ro_ns_text, Loader=yaml.Loader) @@ -165,20 +134,20 @@ class TestMyNS(asynctest.TestCase): def _return_uuid(self, *args, **kwargs): return str(uuid4()) - async def setUp(self): + @patch("osm_lcm.ns.N2VCJujuConnector") + @patch("osm_lcm.ns.K8sHelmConnector") + async def setUp(self, k8s_mock, n2vc_mock): # Mock DB if not getenv("OSMLCMTEST_DB_NOMOCK"): - self.db = asynctest.Mock(DbMongo()) - self.db.get_one.side_effect = self._db_get_one - self.db.get_list.side_effect = self._db_get_list - self.db.set_one.side_effect = self._db_set_one - self.db_content = { - "nsrs": yaml.load(descriptors.db_nsrs_text, Loader=yaml.Loader), - "nslcmops": yaml.load(descriptors.db_nslcmops_text, Loader=yaml.Loader), - "vnfrs": yaml.load(descriptors.db_vnfrs_text, Loader=yaml.Loader), - "vnfds": yaml.load(descriptors.db_vnfds_text, Loader=yaml.Loader), - "vim_accounts": yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader), - } + self.db = DbMemory() + self.db.create_list("vnfds", yaml.load(descriptors.db_vnfds_text, Loader=yaml.Loader)) + self.db.create_list("nsds", yaml.load(descriptors.db_nsds_text, Loader=yaml.Loader)) + self.db.create_list("nsrs", yaml.load(descriptors.db_nsrs_text, Loader=yaml.Loader)) + self.db.create_list("vim_accounts", yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader)) + self.db.create_list("k8sclusters", yaml.load(descriptors.db_k8sclusters_text, Loader=yaml.Loader)) + self.db.create_list("nslcmops", yaml.load(descriptors.db_nslcmops_text, Loader=yaml.Loader)) + self.db.create_list("vnfrs", yaml.load(descriptors.db_vnfrs_text, Loader=yaml.Loader)) + self.db_vim_accounts = yaml.load(descriptors.db_vim_accounts_text, Loader=yaml.Loader) # Mock kafka @@ -199,7 +168,8 @@ class TestMyNS(asynctest.TestCase): self.lcm_tasks.lookfor_related.return_value = ("", []) # Create NsLCM class - self.my_ns = NsLcm(self.db, self.msg, self.fs, self.lcm_tasks, ro_config, vca_config, self.loop) + self.my_ns = NsLcm(self.db, self.msg, self.fs, self.lcm_tasks, lcm_config, self.loop) + self.my_ns._wait_dependent_n2vc = asynctest.CoroutineMock() # Mock logging if not getenv("OSMLCMTEST_LOGGING_NOMOCK"): @@ -213,46 +183,59 @@ class TestMyNS(asynctest.TestCase): # allow several versions of n2vc self.my_ns.n2vc.FormatApplicationName = asynctest.Mock(side_effect=self._n2vc_FormatApplicationName()) self.my_ns.n2vc.DeployCharms = asynctest.CoroutineMock(side_effect=self._n2vc_DeployCharms) - self.my_ns.n2vc.CreateExecutionEnvironment = asynctest.CoroutineMock( + self.my_ns.n2vc.create_execution_environment = asynctest.CoroutineMock( side_effect=self._n2vc_CreateExecutionEnvironment) - self.my_ns.n2vc.InstallConfigurationSW = asynctest.CoroutineMock(return_value=pub_key) - self.my_ns.n2vc.ExecutePrimitive = asynctest.CoroutineMock(side_effect=self._return_uuid) + self.my_ns.n2vc.install_configuration_sw = asynctest.CoroutineMock(return_value=pub_key) + self.my_ns.n2vc.get_ee_ssh_public__key = asynctest.CoroutineMock(return_value=pub_key) + self.my_ns.n2vc.exec_primitive = asynctest.CoroutineMock(side_effect=self._return_uuid) self.my_ns.n2vc.GetPrimitiveStatus = asynctest.CoroutineMock(return_value="completed") self.my_ns.n2vc.GetPrimitiveOutput = asynctest.CoroutineMock(return_value={"result": "ok", "pubkey": pub_key}) + self.my_ns.n2vc.get_public_key = asynctest.CoroutineMock( + return_value=getenv("OSMLCM_VCA_PUBKEY", "public_key")) + + # # Mock VCA - K8s + # if not getenv("OSMLCMTEST_VCA_K8s_NOMOCK"): + # pub_key = getenv("OSMLCMTEST_NS_PUBKEY", "ssh-rsa test-pub-key t@osm.com") + # self.my_ns.k8sclusterhelm = asynctest.Mock(K8sHelmConnector()) # Mock RO if not getenv("OSMLCMTEST_RO_NOMOCK"): - # self.my_ns.RO = asynctest.Mock(ROclient.ROClient(self.loop, **ro_config)) + # self.my_ns.RO = asynctest.Mock(ROclient.ROClient(self.loop, **lcm_config["ro_config"])) # TODO first time should be empty list, following should return a dict self.my_ns.RO.get_list = asynctest.CoroutineMock(self.my_ns.RO.get_list, return_value=[]) self.my_ns.RO.create = asynctest.CoroutineMock(self.my_ns.RO.create, side_effect=self._ro_create()) self.my_ns.RO.show = asynctest.CoroutineMock(self.my_ns.RO.show, side_effect=self._ro_show()) - self.my_ns.RO.create_action = asynctest.CoroutineMock(self.my_ns.RO.create_action) + self.my_ns.RO.create_action = asynctest.CoroutineMock(self.my_ns.RO.create_action, + return_value={"vm-id": {"vim_result": 200, + "description": "done"}}) + # self.my_ns.wait_vm_up_insert_key_ro = asynctest.CoroutineMock(return_value="ip-address") @asynctest.fail_on(active_handles=True) # all async tasks must be completed async def test_instantiate(self): - nsr_id = self.db_content["nsrs"][0]["_id"] - nslcmop_id = self.db_content["nslcmops"][0]["_id"] - print("Test instantiate started") + self.db.set_one = asynctest.Mock() + nsr_id = self.db.get_list("nsrs")[0]["_id"] + nslcmop_id = self.db.get_list("nslcmops")[0]["_id"] + # print("Test instantiate started") # delete deployed information of database if not getenv("OSMLCMTEST_DB_NOMOCK"): - if self.db_content["nsrs"][0]["_admin"].get("deployed"): - del self.db_content["nsrs"][0]["_admin"]["deployed"] - for db_vnfr in self.db_content["vnfrs"]: + if self.db.get_list("nsrs")[0]["_admin"].get("deployed"): + del self.db.get_list("nsrs")[0]["_admin"]["deployed"] + for db_vnfr in self.db.get_list("vnfrs"): db_vnfr.pop("ip_address", None) for db_vdur in db_vnfr["vdur"]: db_vdur.pop("ip_address", None) db_vdur.pop("mac_address", None) if getenv("OSMLCMTEST_RO_VIMID"): - self.db_content["vim_accounts"][0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID") + self.db.get_list("vim_accounts")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID") if getenv("OSMLCMTEST_RO_VIMID"): - self.db_content["nsrs"][0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID") + self.db.get_list("nsrs")[0]["_admin"]["deployed"]["RO"] = getenv("OSMLCMTEST_RO_VIMID") await self.my_ns.instantiate(nsr_id, nslcmop_id) - print("instantiate_result: {}".format(self._db_get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status"))) + # print("instantiate_result: {}".format(self.db.get_one("nslcmops", + # {"_id": nslcmop_id}).get("detailed-status"))) self.msg.aiowrite.assert_called_once_with("ns", "instantiated", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id, @@ -271,7 +254,7 @@ class TestMyNS(asynctest.TestCase): # TODO add a terminate def test_ns_params_2_RO(self): - vim = self._db_get_list("vim_accounts")[0] + vim = self.db.get_list("vim_accounts")[0] vim_id = vim["_id"] ro_vim_id = vim["_admin"]["deployed"]["RO"] ns_params = {"vimAccountId": vim_id} @@ -305,13 +288,13 @@ class TestMyNS(asynctest.TestCase): # scale-out/scale-in operations with success/error result # Test scale() with missing 'scaleVnfData', should return operationState = 'FAILED' - nsr_id = self.db_content["nsrs"][0]["_id"] - nslcmop_id = self.db_content["nslcmops"][0]["_id"] + nsr_id = self.db.get_list("nsrs")[0]["_id"] + nslcmop_id = self.db.get_list("nslcmops")[0]["_id"] await self.my_ns.scale(nsr_id, nslcmop_id) expected_value = 'FAILED' - return_value = self._db_get_one("nslcmops", {"_id": nslcmop_id}).get("operationState") + return_value = self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("operationState") self.assertEqual(return_value, expected_value) - # print("scale_result: {}".format(self._db_get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status"))) + # print("scale_result: {}".format(self.db.get_one("nslcmops", {"_id": nslcmop_id}).get("detailed-status"))) # Test _reintent_or_skip_suboperation() # Expected result: @@ -319,8 +302,7 @@ class TestMyNS(asynctest.TestCase): # - if marked as anything but 'COMPLETED', the suboperation index is expected def test_scale_reintent_or_skip_suboperation(self): # Load an alternative 'nslcmops' YAML for this test - self.db_content['nslcmops'] = yaml.load(descriptors.db_nslcmops_scale_text, Loader=yaml.Loader) - db_nslcmop = self.db_content['nslcmops'][0] + db_nslcmop = self.db.get_list('nslcmops')[0] op_index = 2 # Test when 'operationState' is 'COMPLETED' db_nslcmop['_admin']['operations'][op_index]['operationState'] = 'COMPLETED' @@ -337,8 +319,7 @@ class TestMyNS(asynctest.TestCase): # Expected result: index of the found sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if not found def test_scale_find_suboperation(self): # Load an alternative 'nslcmops' YAML for this test - self.db_content['nslcmops'] = yaml.load(descriptors.db_nslcmops_scale_text, Loader=yaml.Loader) - db_nslcmop = self.db_content['nslcmops'][0] + db_nslcmop = self.db.get_list('nslcmops')[0] # Find this sub-operation op_index = 2 vnf_index = db_nslcmop['_admin']['operations'][op_index]['member_vnf_index'] @@ -366,25 +347,23 @@ class TestMyNS(asynctest.TestCase): # Test _update_suboperation_status() def test_scale_update_suboperation_status(self): - db_nslcmop = self.db_content['nslcmops'][0] + self.db.set_one = asynctest.Mock() + db_nslcmop = self.db.get_list('nslcmops')[0] op_index = 0 # Force the initial values to be distinct from the updated ones - db_nslcmop['_admin']['operations'][op_index]['operationState'] = 'PROCESSING' - db_nslcmop['_admin']['operations'][op_index]['detailed-status'] = 'In progress' + q_filter = {"_id": db_nslcmop["_id"]} # Test to change 'operationState' and 'detailed-status' operationState = 'COMPLETED' detailed_status = 'Done' - self.my_ns._update_suboperation_status( - db_nslcmop, op_index, operationState, detailed_status) - operationState_new = db_nslcmop['_admin']['operations'][op_index]['operationState'] - detailed_status_new = db_nslcmop['_admin']['operations'][op_index]['detailed-status'] - # print("DEBUG: operationState_new={}, detailed_status_new={}".format(operationState_new, detailed_status_new)) - self.assertEqual(operationState, operationState_new) - self.assertEqual(detailed_status, detailed_status_new) - - # Test _add_suboperation() + expected_update_dict = {'_admin.operations.0.operationState': operationState, + '_admin.operations.0.detailed-status': detailed_status, + } + self.my_ns._update_suboperation_status(db_nslcmop, op_index, operationState, detailed_status) + self.db.set_one.assert_called_once_with("nslcmops", q_filter=q_filter, update_dict=expected_update_dict, + fail_on_empty=False) + def test_scale_add_suboperation(self): - db_nslcmop = self.db_content['nslcmops'][0] + db_nslcmop = self.db.get_list('nslcmops')[0] vnf_index = '1' num_ops_before = len(db_nslcmop.get('_admin', {}).get('operations', [])) - 1 vdu_id = None @@ -435,7 +414,7 @@ class TestMyNS(asynctest.TestCase): # - op_index (non-negative number): This is an existing sub-operation, operationState != 'COMPLETED' # - SUBOPERATION_STATUS_SKIP: This is an existing sub-operation, operationState == 'COMPLETED' def test_scale_check_or_add_scale_suboperation(self): - db_nslcmop = self.db_content['nslcmops'][0] + db_nslcmop = self.db.get_list('nslcmops')[0] operationType = 'PRE-SCALE' vnf_index = '1' primitive = 'touch' @@ -492,6 +471,28 @@ class TestMyNS(asynctest.TestCase): db_nslcmop, vnf_index, None, None, 'SCALE-RO', RO_nsr_id, RO_scaling_info) self.assertEqual(op_index_skip_RO, self.my_ns.SUBOPERATION_STATUS_SKIP) + async def test_deploy_kdus(self): + db_nsr = self.db.get_list("nsrs")[1] + db_vnfr = self.db.get_list("vnfrs")[2] + db_vnfrs = {"multikdu": db_vnfr} + nsr_id = db_nsr["_id"] + # nslcmop_id = self.db.get_list("nslcmops")[1]["_id"] + logging_text = "KDU" + self.my_ns.k8sclusterhelm.install = asynctest.CoroutineMock(return_value="k8s_id") + await self.my_ns.deploy_kdus(logging_text, nsr_id, db_nsr, db_vnfrs) + db_nsr = self.db.get_list("nsrs")[1] + self.assertIn("K8s", db_nsr["_admin"]["deployed"], "K8s entry not created at '_admin.deployed'") + self.assertIsInstance(db_nsr["_admin"]["deployed"]["K8s"], list, "K8s entry is not of type list") + self.assertEqual(len(db_nsr["_admin"]["deployed"]["K8s"]), 2, "K8s entry is not of type list") + k8s_instace_info = {"kdu-instance": "k8s_id", "k8scluster-uuid": "73d96432-d692-40d2-8440-e0c73aee209c", + "k8scluster-type": "chart", + "kdu-name": "ldap", "kdu-model": "stable/openldap:1.2.1"} + + self.assertEqual(db_nsr["_admin"]["deployed"]["K8s"][0], k8s_instace_info) + k8s_instace_info["kdu-name"] = "mongo" + k8s_instace_info["kdu-model"] = "stable/mongodb" + self.assertEqual(db_nsr["_admin"]["deployed"]["K8s"][1], k8s_instace_info) + if __name__ == '__main__': asynctest.main()