X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Ftests%2Funit%2Ftest_n2vc_juju_conn.py;h=2475d016d9d7c22564f222d50aec3097fb68a388;hp=ebf36d52e57aab615610f73545751e0fbbbf58db;hb=HEAD;hpb=01244641fdac89ce2afd5490e5c6d2bcf7ad05ae diff --git a/n2vc/tests/unit/test_n2vc_juju_conn.py b/n2vc/tests/unit/test_n2vc_juju_conn.py index ebf36d5..2ce5024 100644 --- a/n2vc/tests/unit/test_n2vc_juju_conn.py +++ b/n2vc/tests/unit/test_n2vc_juju_conn.py @@ -40,10 +40,7 @@ class N2VCJujuConnTestCase(asynctest.TestCase): @asynctest.mock.patch("n2vc.n2vc_juju_conn.get_connection") @asynctest.mock.patch("n2vc.vca.connection_data.base64_to_cacert") def setUp( - self, - mock_base64_to_cacert=None, - mock_get_connection=None, - mock_store=None, + self, mock_base64_to_cacert=None, mock_get_connection=None, mock_store=None ): self.loop = asyncio.get_event_loop() self.db = Mock() @@ -76,7 +73,6 @@ class N2VCJujuConnTestCase(asynctest.TestCase): db=self.db, fs=fslocal.FsLocal(), log=None, - loop=self.loop, on_update_db=None, ) N2VCJujuConnector.get_public_key.assert_not_called() @@ -156,10 +152,7 @@ class K8sProxyCharmsTest(N2VCJujuConnTestCase): "n2vc.n2vc_juju_conn.generate_random_alfanum_string", **{"return_value": "random"} ) - def test_success( - self, - mock_generate_random_alfanum_string, - ): + def test_success(self, mock_generate_random_alfanum_string): self.n2vc.fs.file_exists = MagicMock(create_autospec=True) self.n2vc.fs.file_exists.return_value = True ee_id = self.loop.run_until_complete( @@ -258,14 +251,16 @@ class AddRelationTest(N2VCJujuConnTestCase): self.n2vc.libjuju.get_controller = AsyncMock() self.n2vc.libjuju.consume = AsyncMock() - def test_standard_relation(self): - relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint") - relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint") + def test_standard_relation_same_model_and_controller(self): + relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint1") + relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint2") self.loop.run_until_complete( self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2) ) self.n2vc.libjuju.add_relation.assert_called_once_with( - model_name="model-1", endpoint_1="app1:endpoint", endpoint_2="app2:endpoint" + model_name="model-1", + endpoint_1="app1:endpoint1", + endpoint_2="app2:endpoint2", ) self.n2vc.libjuju.offer.assert_not_called() self.n2vc.libjuju.consume.assert_not_called() @@ -285,6 +280,26 @@ class AddRelationTest(N2VCJujuConnTestCase): "model-2", "app2:endpoint", "saas" ) + def test_cmr_relation_different_controller(self): + self.n2vc._get_libjuju = AsyncMock(return_value=self.n2vc.libjuju) + relation_endpoint_1 = RelationEndpoint( + "model-1.app1.0", "vca-id-1", "endpoint1" + ) + relation_endpoint_2 = RelationEndpoint( + "model-1.app2.1", "vca-id-2", "endpoint2" + ) + offer = Offer("admin/model-1.app1") + self.n2vc.libjuju.offer.return_value = offer + self.n2vc.libjuju.consume.return_value = "saas" + self.loop.run_until_complete( + self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2) + ) + self.n2vc.libjuju.offer.assert_called_once_with(relation_endpoint_1) + self.n2vc.libjuju.consume.assert_called_once() + self.n2vc.libjuju.add_relation.assert_called_once_with( + "model-1", "app2:endpoint2", "saas" + ) + def test_relation_exception(self): relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint") relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint") @@ -381,7 +396,6 @@ class UpgradeCharmTest(N2VCJujuConnTestCase): class GenerateApplicationNameTest(N2VCJujuConnTestCase): - vnf_id = "dbfbd751-3de4-4e68-bd40-ec5ae0a53898" def setUp(self): @@ -469,16 +483,18 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_id": None, "application": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vnf_count = "" vdu_count = "" + vdu_id = None expected_result = "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns" application_name = self.n2vc._generate_application_name( charm_level, vnfrs, vca_records, vnf_count=vnf_count, + vdu_id=vdu_id, vdu_count=vdu_count, ) self.assertEqual(application_name, expected_result) @@ -490,20 +506,20 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): vca_records = [] vnf_count = "" vdu_count = "" + vdu_id = None with self.assertRaises(N2VCException): self.n2vc._generate_application_name( charm_level, vnfrs, vca_records, vnf_count=vnf_count, + vdu_id=vdu_id, vdu_count=vdu_count, ) def test_generate_application_name_vnf_charm(self): charm_level = "vnf-level" - vnfrs = { - "member-vnf-index-ref": "vnf111-xxx-yyy-zzz", - } + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} vca_records = [ { "target_element": "vnf/vnf1", @@ -512,22 +528,24 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vnf_count = "1" vdu_count = "" + vdu_id = None expected_result = "simple-ee-ab-1-vnf111-xxx-y-vnf" application_name = self.n2vc._generate_application_name( charm_level, vnfrs, vca_records, vnf_count=vnf_count, + vdu_id=vdu_id, vdu_count=vdu_count, ) self.assertEqual(application_name, expected_result) self.assertLess(len(application_name), 50) - def test_generate_application_name_vdu_charm(self): + def test_generate_application_name_vdu_charm_kdu_name_in_vca_record_is_none(self): charm_level = "vdu-level" vnfrs = { "member-vnf-index-ref": "vnf111-xxx-yyy-zzz", @@ -540,7 +558,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): { "target_element": "vnf/vnf1/mgmtvm", "member-vnf-index": "vnf111-xxx-yyy-zzz", - "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", + "vdu_id": "mgmtVM", "kdu_name": None, "vdu_count_index": None, "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", @@ -550,9 +568,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, { - "target_element": "vnf/vnf1/datavm", + "target_element": "vnf/vnf1/dataVM", "member-vnf-index": "vnf111-xxx-yyy-zzz", - "vdu_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", + "vdu_id": "dataVM", "kdu_name": None, "vdu_count_index": None, "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", @@ -564,28 +582,36 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): ] vnf_count = "2" vdu_count = "0" + vdu_id = "mgmtVM" expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu" application_name = self.n2vc._generate_application_name( charm_level, vnfrs, vca_records, vnf_count=vnf_count, + vdu_id=vdu_id, vdu_count=vdu_count, ) self.assertEqual(application_name, expected_result) self.assertLess(len(application_name), 50) - def test_generate_application_name_vdu_charm_wrong_vnfrs(self): + def test_generate_application_name_vdu_charm_vdu_id_kdu_name_in_vca_record_are_both_set( + self, + ): charm_level = "vdu-level" vnfrs = { "member-vnf-index-ref": "vnf111-xxx-yyy-zzz", + "vdur": [ + {"_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "mgmtVM"}, + {"_id": "45512ff7-5bdd-4228-911f-c2bee259c44a", "vdu-id-ref": "dataVM"}, + ], } vca_records = [ { - "target_element": "vnf/vnf1/mgmtvm", + "target_element": "vnf/vnf1/mgmtVM", "member-vnf-index": "vnf111-xxx-yyy-zzz", - "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", - "kdu_name": None, + "vdu_id": "mgmtVM", + "kdu_name": "mgmtVM", "vdu_count_index": None, "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", "vdu_name": "mgmtvm", @@ -593,18 +619,160 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, + { + "target_element": "vnf/vnf1/dataVM", + "member-vnf-index": "vnf111-xxx-yyy-zzz", + "vdu_id": "dataVM", + "kdu_name": None, + "vdu_count_index": None, + "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", + "vdu_name": "datavm", + "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-8888-hhh-3333-yyyy-888-hhh-ttt-444", + "charm_name": "", + "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", + }, + ] + vnf_count = "2" + vdu_count = "0" + vdu_id = "mgmtVM" + expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu" + application_name = self.n2vc._generate_application_name( + charm_level, + vnfrs, + vca_records, + vnf_count=vnf_count, + vdu_id=vdu_id, + vdu_count=vdu_count, + ) + self.assertEqual(application_name, expected_result) + self.assertLess(len(application_name), 50) + + def test_generate_application_name_vdu_charm_both_vdu_id_kdu_name_in_vca_record_are_none( + self, + ): + charm_level = "vdu-level" + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} + vca_records = [ + { + "target_element": "vnf/vnf1/mgmtVM", + "member-vnf-index": "vnf111-xxx-yyy-zzz", + "vdu_id": None, + "kdu_name": None, + "vdu_count_index": None, + "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", + "vdu_name": "mgmtvm", + "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", + "charm_name": "", + "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", + } ] vnf_count = "2" vdu_count = "0" + vdu_id = "mgmtVM" with self.assertRaises(KeyError): self.n2vc._generate_application_name( charm_level, vnfrs, vca_records, vnf_count=vnf_count, + vdu_id=vdu_id, vdu_count=vdu_count, ) + def test_generate_application_name_vdu_charm_given_vdu_id_is_none(self): + charm_level = "vdu-level" + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} + vca_records = [ + { + "target_element": "vnf/vnf1/mgmtvVM", + "member-vnf-index": "vnf111-xxx-yyy-zzz", + "vdu_id": None, + "kdu_name": "mgmtVM", + "vdu_count_index": None, + "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", + "vdu_name": "mgmtvm", + "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", + "charm_name": "", + "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", + } + ] + vnf_count = "2" + vdu_count = "0" + vdu_id = None + with self.assertRaises(N2VCException): + self.n2vc._generate_application_name( + charm_level, + vnfrs, + vca_records, + vnf_count=vnf_count, + vdu_id=vdu_id, + vdu_count=vdu_count, + ) + + def test_generate_application_name_vdu_charm_vdu_id_does_not_match_with_the_key_in_vca_record( + self, + ): + charm_level = "vdu-level" + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} + vca_records = [ + { + "target_element": "vnf/vnf1/mgmtVM", + "member-vnf-index": "vnf111-xxx-yyy-zzz", + "vdu_id": None, + "kdu_name": "mgmtVM", + "vdu_count_index": None, + "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", + "vdu_name": "mgmtvm", + "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", + "charm_name": "", + "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", + } + ] + vnf_count = "2" + vdu_count = "0" + vdu_id = "mgmtvm" + with self.assertRaises(KeyError): + self.n2vc._generate_application_name( + charm_level, + vnfrs, + vca_records, + vnf_count=vnf_count, + vdu_id=vdu_id, + vdu_count=vdu_count, + ) + + def test_generate_application_name_vdu_charm_vdu_id_in_vca_record_is_none(self): + charm_level = "vdu-level" + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} + vca_records = [ + { + "target_element": "vnf/vnf1/mgmtVM", + "member-vnf-index": "vnf111-xxx-yyy-zzz", + "vdu_id": None, + "kdu_name": "mgmtVM", + "vdu_count_index": None, + "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", + "vdu_name": "mgmtvm", + "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", + "charm_name": "", + "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", + } + ] + vnf_count = "2" + vdu_count = "0" + vdu_id = "mgmtVM" + expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu" + application_name = self.n2vc._generate_application_name( + charm_level, + vnfrs, + vca_records, + vnf_count=vnf_count, + vdu_id=vdu_id, + vdu_count=vdu_count, + ) + self.assertEqual(application_name, expected_result) + self.assertLess(len(application_name), 50) + def test_get_vnf_count_db_vnfr_ns_charm(self): self.db.get_one.return_value = {"member-vnf-index-ref": "sample-ref"} charm_level = "ns-level" @@ -675,9 +843,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [ { @@ -691,16 +859,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr) self.assertEqual(vca_records, expected_result) def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self): charm_level = "vnf-level" - db_vnfr = { - "member-vnf-index-ref": "vnf222-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"} db_nsr = { "_admin": { "deployed": { @@ -729,9 +895,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [] vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr) @@ -739,9 +905,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): def test_get_vca_records_ns_charm(self): charm_level = "ns-level" - db_vnfr = { - "member-vnf-index-ref": "vnf222-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"} db_nsr = { "_admin": { "deployed": { @@ -770,9 +934,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [ { @@ -786,16 +950,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr) self.assertEqual(vca_records, expected_result) def test_get_vca_records_ns_charm_empty_charm_name(self): charm_level = "ns-level" - db_vnfr = { - "member-vnf-index-ref": "vnf222-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"} db_nsr = { "_admin": { "deployed": { @@ -824,9 +986,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [ { @@ -840,7 +1002,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr) self.assertEqual(vca_records, expected_result) @@ -875,14 +1037,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } mock_vnf_count_and_record = MagicMock() - db_vnfr = { - "member-vnf-index-ref": "vnf111-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} vnf_count = "0" mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr) expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf" @@ -931,14 +1091,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } mock_vnf_count_and_record = MagicMock() - db_vnfr = { - "member-vnf-index-ref": "vnf111-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} vnf_count = "0" mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr) expected_result = "app-vnf-eb3161eec0-z0-random" @@ -982,14 +1140,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } mock_vnf_count_and_record = MagicMock() - db_vnfr = { - "member-vnf-index-ref": "vnf222-xxx-yyy-zzz", - } + db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"} vnf_count = "0" mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr) with patch.object(self.n2vc, "db", self.db), patch.object( @@ -1011,7 +1167,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): { "target_element": "vnf/vnf1/mgmtvm", "member-vnf-index": "vnf111-xxx-yyy-zzz", - "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", + "vdu_id": "mgmtVM", "kdu_name": None, "vdu_count_index": None, "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", @@ -1058,6 +1214,51 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): ) self.db.get_one.assert_called_once() + def test_get_application_name_kdu_charm(self): + namespace = ".82b11965-e580-47c0-9ee0-329f318a305b.1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0.ldap" + self.db.get_one.return_value = { + "_admin": { + "deployed": { + "VCA": [ + { + "target_element": "vnf/openldap/kdu/ldap", + "member-vnf-index": "openldap", + "vdu_id": None, + "kdu_name": "ldap", + "vdu_count_index": 0, + "operational-status": "init", + "detailed-status": "", + "step": "initial-deploy", + "vnfd_id": "openldap_knf", + "vdu_name": None, + "type": "lxc_proxy_charm", + "ee_descriptor_id": "openldap-ee", + "charm_name": "", + "ee_id": "", + "application": "openldap-ee-z0-openldap-vdu", + "model": "82b11965-e580-47c0-9ee0-329f318a305b", + "config_sw_installed": True, + } + ] + } + } + } + mock_vnf_count_and_record = MagicMock() + db_vnfr = {"member-vnf-index-ref": "openldap", "vdur": {}} + vnf_count = "0" + mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr) + expected_result = "openldap-ee-z0-openldap-ldap-vdu" + with patch.object(self.n2vc, "db", self.db), patch.object( + self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record + ): + application_name = self.n2vc._get_application_name(namespace) + self.assertEqual(application_name, expected_result) + self.assertLess(len(application_name), 50) + mock_vnf_count_and_record.assert_called_once_with( + "vdu-level", "1b6a4eb3-4fbf-415e-985c-4aeb3161eec0-0" + ) + self.db.get_one.assert_called_once() + @patch( "n2vc.n2vc_juju_conn.generate_random_alfanum_string", **{"return_value": "random"} @@ -1071,9 +1272,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "deployed": { "VCA": [ { - "target_element": "vnf/vnf1/mgmtvm", + "target_element": "vnf/vnf1/mgmtVM", "member-vnf-index": "vnf111-xxx-yyy-zzz", - "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a", + "vdu_id": "mgmtVM", "kdu_name": None, "vdu_count_index": None, "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898", @@ -1092,9 +1293,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } mock_vnf_count_and_record = MagicMock() db_vnfr = { @@ -1136,10 +1337,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, - ], - }, - }, + } + ] + } + } } mock_vnf_count_and_record = MagicMock() db_vnfr = {} @@ -1172,10 +1373,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, - ], - }, - }, + } + ] + } + } } mock_vnf_count_and_record = MagicMock() db_vnfr = {} @@ -1211,10 +1412,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "vdu_name": "", "ee_descriptor_id": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, - ], - }, - }, + } + ] + } + } } mock_vnf_count_and_record = MagicMock() db_vnfr = {} @@ -1229,3 +1430,66 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): self.assertLess(len(application_name), 50) mock_vnf_count_and_record.assert_called_once_with("ns-level", None) self.db.get_one.assert_called_once() + + +class DeleteExecutionEnvironmentTest(N2VCJujuConnTestCase): + def setUp(self): + super(DeleteExecutionEnvironmentTest, self).setUp() + self.n2vc.libjuju.get_controller = AsyncMock() + self.n2vc.libjuju.destroy_model = AsyncMock() + self.n2vc.libjuju.destroy_application = AsyncMock() + + def test_remove_ee__target_application_exists__model_is_deleted(self): + get_ee_id_components = MagicMock() + get_ee_id_components.return_value = ("my_model", "my_app", None) + model = MagicMock(create_autospec=True) + model.applications = {} + self.n2vc.libjuju.get_model = AsyncMock() + self.n2vc.libjuju.get_model.return_value = model + with patch.object(self.n2vc, "_get_ee_id_components", get_ee_id_components): + self.loop.run_until_complete( + self.n2vc.delete_execution_environment( + "my_ee", application_to_delete="my_app" + ) + ) + self.n2vc.libjuju.destroy_application.assert_called_with( + model_name="my_model", + application_name="my_app", + total_timeout=None, + ) + self.n2vc.libjuju.destroy_model.assert_called_with( + model_name="my_model", + total_timeout=None, + ) + + def test_remove_ee__multiple_applications_exist__model_is_not_deleted(self): + get_ee_id_components = MagicMock() + get_ee_id_components.return_value = ("my_model", "my_app", None) + model = MagicMock(create_autospec=True) + model.applications = {MagicMock(create_autospec=True)} + self.n2vc.libjuju.get_model = AsyncMock() + self.n2vc.libjuju.get_model.return_value = model + with patch.object(self.n2vc, "_get_ee_id_components", get_ee_id_components): + self.loop.run_until_complete( + self.n2vc.delete_execution_environment( + "my_ee", application_to_delete="my_app" + ) + ) + self.n2vc.libjuju.destroy_application.assert_called_with( + model_name="my_model", + application_name="my_app", + total_timeout=None, + ) + self.n2vc.libjuju.destroy_model.assert_not_called() + + def test_remove_ee__target_application_does_not_exist__model_is_deleted(self): + get_ee_id_components = MagicMock() + get_ee_id_components.return_value = ("my_model", "my_app", None) + with patch.object(self.n2vc, "_get_ee_id_components", get_ee_id_components): + self.loop.run_until_complete( + self.n2vc.delete_execution_environment("my_ee") + ) + self.n2vc.libjuju.destroy_model.assert_called_with( + model_name="my_model", + total_timeout=None, + )