+class TestInstantiateN2VC(TestBaseNS):
+ async def setUp(self):
+ await super().setUp()
+ self.db_nsr = yaml.safe_load(descriptors.db_nsrs_text)[0]
+ self.db_vnfr = yaml.safe_load(descriptors.db_vnfrs_text)[0]
+ self.vca_index = 1
+ self.my_ns._write_configuration_status = Mock()
+
+ async def call_instantiate_N2VC(self):
+ logging_text = "N2VC Instantiation"
+ config_descriptor = {"config-access": {"ssh-access": {"default-user": "admin"}}}
+ base_folder = {"pkg-dir": "", "folder": "~"}
+ stage = ["Stage", "Message"]
+
+ await self.my_ns.instantiate_N2VC(
+ logging_text=logging_text,
+ vca_index=self.vca_index,
+ nsi_id="nsi_id",
+ db_nsr=self.db_nsr,
+ db_vnfr=self.db_vnfr,
+ vdu_id=None,
+ kdu_name=None,
+ vdu_index=None,
+ kdu_index=None,
+ config_descriptor=config_descriptor,
+ deploy_params={},
+ base_folder=base_folder,
+ nslcmop_id="nslcmop_id",
+ stage=stage,
+ vca_type="native_charm",
+ vca_name="vca_name",
+ ee_config_descriptor={},
+ )
+
+ def check_config_status(self, expected_status):
+ self.my_ns._write_configuration_status.assert_called_with(
+ nsr_id=self.db_nsr["_id"], vca_index=self.vca_index, status=expected_status
+ )
+
+ async def call_ns_add_relation(self):
+ ee_relation = EERelation(
+ {
+ "nsr-id": self.db_nsr["_id"],
+ "vdu-profile-id": None,
+ "kdu-resource-profile-id": None,
+ "vnf-profile-id": "hackfest_vnf1",
+ "execution-environment-ref": "f48163a6-c807-47bc-9682-f72caef5af85.alf-c-ab",
+ "endpoint": "127.0.0.1",
+ }
+ )
+
+ relation = Relation("relation-name", ee_relation, ee_relation)
+ cached_vnfrs = {"hackfest_vnf1": self.db_vnfr}
+
+ return await self.my_ns._add_relation(
+ relation=relation,
+ vca_type="native_charm",
+ db_nsr=self.db_nsr,
+ cached_vnfds={},
+ cached_vnfrs=cached_vnfrs,
+ )
+
+ async def test_add_relation_ok(self):
+ await self.call_instantiate_N2VC()
+ self.check_config_status(expected_status="READY")
+
+ async def test_add_relation_returns_false_raises_exception(self):
+ self.my_ns._add_vca_relations = asynctest.CoroutineMock(return_value=False)
+
+ with self.assertRaises(LcmException) as exception:
+ await self.call_instantiate_N2VC()
+
+ exception_msg = "Relations could not be added to VCA."
+ self.assertTrue(exception_msg in str(exception.exception))
+ self.check_config_status(expected_status="BROKEN")
+
+ async def test_add_relation_raises_lcm_exception(self):
+ exception_msg = "Relations FAILED"
+ self.my_ns._add_vca_relations = asynctest.CoroutineMock(
+ side_effect=LcmException(exception_msg)
+ )
+
+ with self.assertRaises(LcmException) as exception:
+ await self.call_instantiate_N2VC()
+
+ self.assertTrue(exception_msg in str(exception.exception))
+ self.check_config_status(expected_status="BROKEN")
+
+ async def test_n2vc_add_relation_fails_raises_exception(self):
+ exception_msg = "N2VC failed to add relations"
+ self.my_ns.n2vc.add_relation = asynctest.CoroutineMock(
+ side_effect=N2VCException(exception_msg)
+ )
+ with self.assertRaises(LcmException) as exception:
+ await self.call_ns_add_relation()
+ self.assertTrue(exception_msg in str(exception.exception))
+
+ async def test_n2vc_add_relation_ok_returns_true(self):
+ self.my_ns.n2vc.add_relation = asynctest.CoroutineMock(return_value=None)
+ self.assertTrue(await self.call_ns_add_relation())
+
+
+class TestGetVNFRelations(TestBaseNS):
+ async def setUp(self):
+ await super().setUp()
+ self.db_nsd = yaml.safe_load(descriptors.db_nsds_text)[0]
+
+ def test_ns_charm_vca_returns_empty_relations(self):
+ ns_charm_vca = {"member-vnf-index": None, "target_element": "ns"}
+ nsr_id = self.db_nsd["id"]
+ deployed_vca = DeployedVCA(nsr_id, ns_charm_vca)
+
+ expected_relations = []
+ self.assertEqual(
+ expected_relations,
+ self.my_ns._get_vnf_relations(
+ nsr_id=nsr_id, nsd=self.db_nsd, vca=deployed_vca, cached_vnfds={}
+ ),
+ )
+
+ def test_vnf_returns_relation(self):
+ vnf_vca = {
+ "member-vnf-index": "1",
+ "target_element": "vnf/0",
+ "ee_descriptor_id": "simple-ee",
+ "vdu_id": "mgmtVM",
+ }
+ nsr_id = self.db_nsd["id"]
+ deployed_vca = DeployedVCA(nsr_id, vnf_vca)
+
+ provider_dict = {
+ "nsr-id": nsr_id,
+ "vnf-profile-id": "1",
+ "vdu-profile-id": "mgmtVM",
+ "kdu-resource-profile-id": None,
+ "execution-environment-ref": "simple-ee",
+ "endpoint": "interface",
+ }
+
+ requirer_dict = {
+ "nsr-id": nsr_id,
+ "vnf-profile-id": "1",
+ "vdu-profile-id": "dataVM",
+ "kdu-resource-profile-id": None,
+ "execution-environment-ref": "simple-ee",
+ "endpoint": "interface",
+ }
+
+ provider = EERelation(provider_dict)
+ requirer = EERelation(requirer_dict)
+ relation = Relation("relation", provider, requirer)
+
+ relations_found = self.my_ns._get_vnf_relations(
+ nsr_id=nsr_id, nsd=self.db_nsd, vca=deployed_vca, cached_vnfds={}
+ )
+
+ self.assertEqual(1, len(relations_found))
+ self.assertEqual(relation, relations_found[0])
+
+