Fixing Bug 2181 Application name creation for Helm chart 93/12593/2
authorGulsum Atici <gulsum.atici@canonical.com>
Mon, 17 Oct 2022 11:40:39 +0000 (14:40 +0300)
committerGulsum Atici <gulsum.atici@canonical.com>
Mon, 17 Oct 2022 11:48:47 +0000 (14:48 +0300)
Change-Id: Id0a83e2b5fee1dbb4c4109f7974b073fc7c3abd7
Signed-off-by: Gulsum Atici <gulsum.atici@canonical.com>
n2vc/n2vc_juju_conn.py
n2vc/tests/unit/test_n2vc_juju_conn.py

index da09fdb..64e338c 100644 (file)
@@ -1288,6 +1288,23 @@ class N2VCJujuConnector(N2VCConnector):
         )
         return application_name
 
+    @staticmethod
+    def _get_vca_record(search_key: str, vca_records: list, vdu_id: str) -> dict:
+        """Get the correct VCA record dict depending on the search key
+
+        Args:
+            search_key  (str):      keyword to find the correct VCA record
+            vca_records (list):     All VCA records as list
+            vdu_id  (str):          VDU ID
+
+        Returns:
+            vca_record  (dict):     Dictionary which includes the correct VCA record
+
+        """
+        return next(
+            filter(lambda record: record[search_key] == vdu_id, vca_records), {}
+        )
+
     @staticmethod
     def _generate_application_name(
         charm_level: str,
@@ -1355,9 +1372,16 @@ class N2VCJujuConnector(N2VCConnector):
             # to first 12 characters.
             if not vdu_id:
                 raise N2VCException(message="vdu-id should be provided.")
-            vca_record = next(
-                filter(lambda record: record["vdu_id"] == vdu_id, vca_records), {}
+
+            vca_record = N2VCJujuConnector._get_vca_record(
+                "vdu_id", vca_records, vdu_id
             )
+
+            if not vca_record:
+                vca_record = N2VCJujuConnector._get_vca_record(
+                    "kdu_name", vca_records, vdu_id
+                )
+
             application_name = (
                 vca_record["ee_descriptor_id"][:12]
                 + "-"
index 8324006..7606b4a 100644 (file)
@@ -533,7 +533,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         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",
@@ -556,9 +556,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",
@@ -583,14 +583,68 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         self.assertEqual(application_name, expected_result)
         self.assertLess(len(application_name), 50)
 
-    def test_generate_application_name_vdu_charm_vdu_id_none(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": "mgmtVM",
+                "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",
+            },
+            {
+                "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,
@@ -604,6 +658,38 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         ]
         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(
@@ -615,6 +701,74 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 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"
@@ -1077,7 +1231,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                         {
                             "target_element": "vnf/openldap/kdu/ldap",
                             "member-vnf-index": "openldap",
-                            "vdu_id": "ldap",
+                            "vdu_id": None,
                             "kdu_name": "ldap",
                             "vdu_count_index": 0,
                             "operational-status": "init",
@@ -1129,9 +1283,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",