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=7606b4aa7eb8230dfe8334b391d1261c2a6dded3;hb=HEAD;hpb=552a601c25f23e892ff4ffec580c5d472b29c6ba diff --git a/n2vc/tests/unit/test_n2vc_juju_conn.py b/n2vc/tests/unit/test_n2vc_juju_conn.py index 7606b4a..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,7 +483,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_id": None, "application": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", - }, + } ] vnf_count = "" vdu_count = "" @@ -505,9 +519,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): 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", @@ -516,7 +528,7 @@ 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 = "" @@ -639,9 +651,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): self, ): charm_level = "vdu-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/mgmtVM", @@ -654,7 +664,7 @@ 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 = "2" vdu_count = "0" @@ -671,9 +681,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): 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", - } + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} vca_records = [ { "target_element": "vnf/vnf1/mgmtvVM", @@ -686,7 +694,7 @@ 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 = "2" vdu_count = "0" @@ -705,9 +713,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): self, ): charm_level = "vdu-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/mgmtVM", @@ -720,7 +726,7 @@ 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 = "2" vdu_count = "0" @@ -737,9 +743,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): 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", - } + vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"} vca_records = [ { "target_element": "vnf/vnf1/mgmtVM", @@ -752,7 +756,7 @@ 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 = "2" vdu_count = "0" @@ -839,9 +843,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [ { @@ -855,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": { @@ -893,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) @@ -903,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": { @@ -934,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 = [ { @@ -950,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": { @@ -988,9 +986,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "charm_name": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } expected_result = [ { @@ -1004,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) @@ -1039,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" @@ -1095,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" @@ -1146,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( @@ -1246,16 +1238,13 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "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": {}, - } + 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" @@ -1304,9 +1293,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase): "ee_descriptor_id": "", "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898", }, - ], - }, - }, + ] + } + } } mock_vnf_count_and_record = MagicMock() db_vnfr = { @@ -1348,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 = {} @@ -1384,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 = {} @@ -1423,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 = {} @@ -1441,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, + )