Feature 10975: get flavor-id from additionalParams if specified
[osm/RO.git] / NG-RO / osm_ng_ro / tests / test_ns.py
index d1ab0a4..d966a86 100644 (file)
@@ -206,6 +206,25 @@ expected_extra_dict2 = {
         "start": True,
     },
 }
+
+expected_extra_dict3 = {
+    "depends_on": [
+        f"{ns_preffix}:image.0",
+    ],
+    "params": {
+        "affinity_group_list": [],
+        "availability_zone_index": None,
+        "availability_zone_list": None,
+        "cloud_config": None,
+        "description": "without_volumes-VM",
+        "disk_list": [],
+        "flavor_id": "flavor_test",
+        "image_id": f"TASK-{ns_preffix}:image.0",
+        "name": "sample_name-vnf-several-volu-without_volumes-VM-0",
+        "net_list": [],
+        "start": True,
+    },
+}
 tasks_by_target_record_id = {
     "nsrs:th47f48-9870-4169-b758-9732e1ff40f3": {
         "extra_dict": {
@@ -1815,7 +1834,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         target_flavor = {}
         indata = {
             "vnf": [
@@ -1842,7 +1860,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         target_flavor = {
             "no-target-flavor": "here",
         }
@@ -1865,7 +1882,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         expected_result = {
             "find_params": {
                 "flavor_data": {
@@ -1910,7 +1926,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         expected_result = {
             "find_params": {
                 "flavor_data": {
@@ -2067,7 +2082,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         expected_result = {
             "find_params": {
                 "flavor_data": {
@@ -2132,7 +2146,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         kwargs = {
             "db": db,
         }
@@ -2243,7 +2256,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         expected_result = {
             "find_params": {
                 "flavor_data": {
@@ -2308,7 +2320,6 @@ class TestNs(unittest.TestCase):
         self,
         epa_params,
     ):
-
         kwargs = {
             "db": db,
         }
@@ -2429,213 +2440,8 @@ class TestNs(unittest.TestCase):
         self.assertTrue(epa_params.called)
         self.assertDictEqual(result, expected_result)
 
-    def test__ip_profile_to_ro_with_none(self):
-        ip_profile = None
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertIsNone(result)
-
-    def test__ip_profile_to_ro_with_empty_profile(self):
-        ip_profile = {}
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertIsNone(result)
-
-    def test__ip_profile_to_ro_with_wrong_profile(self):
-        ip_profile = {
-            "no-profile": "here",
-        }
-        expected_result = {
-            "ip_version": "IPv4",
-            "subnet_address": None,
-            "gateway_address": None,
-            "dhcp_enabled": False,
-            "dhcp_start_address": None,
-            "dhcp_count": None,
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    def test__ip_profile_to_ro_with_ipv4_profile(self):
-        ip_profile = {
-            "ip-version": "ipv4",
-            "subnet-address": "192.168.0.0/24",
-            "gateway-address": "192.168.0.254",
-            "dhcp-params": {
-                "enabled": True,
-                "start-address": "192.168.0.10",
-                "count": 25,
-            },
-        }
-        expected_result = {
-            "ip_version": "IPv4",
-            "subnet_address": "192.168.0.0/24",
-            "gateway_address": "192.168.0.254",
-            "dhcp_enabled": True,
-            "dhcp_start_address": "192.168.0.10",
-            "dhcp_count": 25,
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    def test__ip_profile_to_ro_with_ipv6_profile(self):
-        ip_profile = {
-            "ip-version": "ipv6",
-            "subnet-address": "2001:0200:0001::/48",
-            "gateway-address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe",
-            "dhcp-params": {
-                "enabled": True,
-                "start-address": "2001:0200:0001::0010",
-                "count": 25,
-            },
-        }
-        expected_result = {
-            "ip_version": "IPv6",
-            "subnet_address": "2001:0200:0001::/48",
-            "gateway_address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe",
-            "dhcp_enabled": True,
-            "dhcp_start_address": "2001:0200:0001::0010",
-            "dhcp_count": 25,
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    def test__ip_profile_to_ro_with_dns_server(self):
-        ip_profile = {
-            "ip-version": "ipv4",
-            "subnet-address": "192.168.0.0/24",
-            "gateway-address": "192.168.0.254",
-            "dhcp-params": {
-                "enabled": True,
-                "start-address": "192.168.0.10",
-                "count": 25,
-            },
-            "dns-server": [
-                {
-                    "address": "8.8.8.8",
-                },
-                {
-                    "address": "1.1.1.1",
-                },
-                {
-                    "address": "1.0.0.1",
-                },
-            ],
-        }
-        expected_result = {
-            "ip_version": "IPv4",
-            "subnet_address": "192.168.0.0/24",
-            "gateway_address": "192.168.0.254",
-            "dhcp_enabled": True,
-            "dhcp_start_address": "192.168.0.10",
-            "dhcp_count": 25,
-            "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1",
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    def test__ip_profile_to_ro_with_security_group(self):
-        ip_profile = {
-            "ip-version": "ipv4",
-            "subnet-address": "192.168.0.0/24",
-            "gateway-address": "192.168.0.254",
-            "dhcp-params": {
-                "enabled": True,
-                "start-address": "192.168.0.10",
-                "count": 25,
-            },
-            "security-group": {
-                "some-security-group": "here",
-            },
-        }
-        expected_result = {
-            "ip_version": "IPv4",
-            "subnet_address": "192.168.0.0/24",
-            "gateway_address": "192.168.0.254",
-            "dhcp_enabled": True,
-            "dhcp_start_address": "192.168.0.10",
-            "dhcp_count": 25,
-            "security_group": {
-                "some-security-group": "here",
-            },
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    def test__ip_profile_to_ro(self):
-        ip_profile = {
-            "ip-version": "ipv4",
-            "subnet-address": "192.168.0.0/24",
-            "gateway-address": "192.168.0.254",
-            "dhcp-params": {
-                "enabled": True,
-                "start-address": "192.168.0.10",
-                "count": 25,
-            },
-            "dns-server": [
-                {
-                    "address": "8.8.8.8",
-                },
-                {
-                    "address": "1.1.1.1",
-                },
-                {
-                    "address": "1.0.0.1",
-                },
-            ],
-            "security-group": {
-                "some-security-group": "here",
-            },
-        }
-        expected_result = {
-            "ip_version": "IPv4",
-            "subnet_address": "192.168.0.0/24",
-            "gateway_address": "192.168.0.254",
-            "dhcp_enabled": True,
-            "dhcp_start_address": "192.168.0.10",
-            "dhcp_count": 25,
-            "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1",
-            "security_group": {
-                "some-security-group": "here",
-            },
-        }
-
-        result = Ns._ip_profile_to_ro(
-            ip_profile=ip_profile,
-        )
-
-        self.assertDictEqual(expected_result, result)
-
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_empty_params(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2645,6 +2451,9 @@ class TestNs(unittest.TestCase):
         }
         vim_info = {
             "provider_network": "some-profile-here",
+            "ip_profile": {
+                "some_ip_profile": "here",
+            },
         }
         target_record_id = ""
         expected_result = {
@@ -2658,10 +2467,6 @@ class TestNs(unittest.TestCase):
             }
         }
 
-        ip_profile_to_ro.return_value = {
-            "some_ip_profile": "here",
-        }
-
         result = Ns._process_net_params(
             target_vld=target_vld,
             indata=indata,
@@ -2670,12 +2475,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertTrue(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_vim_info_sdn(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2706,12 +2508,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertFalse(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_vim_info_sdn_target_vim(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2745,12 +2544,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertFalse(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_vim_network_name(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2778,12 +2574,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertFalse(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_vim_network_id(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2811,12 +2604,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertFalse(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_mgmt_network(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "id": "vld-id",
@@ -2843,12 +2633,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertFalse(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_underlay_eline(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2860,6 +2647,9 @@ class TestNs(unittest.TestCase):
         }
         vim_info = {
             "provider_network": "some-profile-here",
+            "ip_profile": {
+                "some_ip_profile": "here",
+            },
         }
         target_record_id = ""
         expected_result = {
@@ -2873,10 +2663,6 @@ class TestNs(unittest.TestCase):
             }
         }
 
-        ip_profile_to_ro.return_value = {
-            "some_ip_profile": "here",
-        }
-
         result = Ns._process_net_params(
             target_vld=target_vld,
             indata=indata,
@@ -2885,12 +2671,9 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertTrue(ip_profile_to_ro.called)
 
-    @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
     def test__process_net_params_with_underlay_elan(
         self,
-        ip_profile_to_ro,
     ):
         target_vld = {
             "name": "vld-name",
@@ -2902,6 +2685,9 @@ class TestNs(unittest.TestCase):
         }
         vim_info = {
             "provider_network": "some-profile-here",
+            "ip_profile": {
+                "some_ip_profile": "here",
+            },
         }
         target_record_id = ""
         expected_result = {
@@ -2915,10 +2701,6 @@ class TestNs(unittest.TestCase):
             }
         }
 
-        ip_profile_to_ro.return_value = {
-            "some_ip_profile": "here",
-        }
-
         result = Ns._process_net_params(
             target_vld=target_vld,
             indata=indata,
@@ -2927,7 +2709,6 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(expected_result, result)
-        self.assertTrue(ip_profile_to_ro.called)
 
     def test__get_cloud_init_exception(self):
         db_mock = MagicMock(name="database mock")
@@ -3650,7 +3431,8 @@ class TestProcessVduParams(unittest.TestCase):
         self, mock_volume_keeping_required
     ):
         """Find persistent ordinary volume, volume id is not persistent_root_disk dict,
-        vim-volume-id is given as instantiation parameter but disk id is not matching."""
+        vim-volume-id is given as instantiation parameter but disk id is not matching.
+        """
         mock_volume_keeping_required.return_value = True
         vdu_instantiation_volumes_list = [
             {
@@ -4976,6 +4758,76 @@ class TestProcessVduParams(unittest.TestCase):
             persistent_root_disk, target_vdu, vdu_instantiation_vol_list, []
         )
 
+    @patch("osm_ng_ro.ns.Ns._sort_vdu_interfaces")
+    @patch("osm_ng_ro.ns.Ns._partially_locate_vdu_interfaces")
+    @patch("osm_ng_ro.ns.Ns._prepare_vdu_interfaces")
+    @patch("osm_ng_ro.ns.Ns._prepare_vdu_cloud_init")
+    @patch("osm_ng_ro.ns.Ns._prepare_vdu_ssh_keys")
+    @patch("osm_ng_ro.ns.Ns.find_persistent_root_volumes")
+    @patch("osm_ng_ro.ns.Ns.find_persistent_volumes")
+    @patch("osm_ng_ro.ns.Ns._add_persistent_root_disk_to_disk_list")
+    @patch("osm_ng_ro.ns.Ns._add_persistent_ordinary_disks_to_disk_list")
+    @patch("osm_ng_ro.ns.Ns._prepare_vdu_affinity_group_list")
+    def test_process_vdu_params_with_inst_flavor_id(
+        self,
+        mock_prepare_vdu_affinity_group_list,
+        mock_add_persistent_ordinary_disks_to_disk_list,
+        mock_add_persistent_root_disk_to_disk_list,
+        mock_find_persistent_volumes,
+        mock_find_persistent_root_volumes,
+        mock_prepare_vdu_ssh_keys,
+        mock_prepare_vdu_cloud_init,
+        mock_prepare_vdu_interfaces,
+        mock_locate_vdu_interfaces,
+        mock_sort_vdu_interfaces,
+    ):
+        """Instantiation volume list is empty."""
+        target_vdu = deepcopy(target_vdu_wthout_persistent_storage)
+
+        target_vdu["interfaces"] = interfaces_wth_all_positions
+
+        vdu_instantiation_flavor_id = "flavor_test"
+
+        target_vdu["additionalParams"] = {
+            "OSM": {"vim_flavor_id": vdu_instantiation_flavor_id}
+        }
+        mock_prepare_vdu_cloud_init.return_value = {}
+        mock_prepare_vdu_affinity_group_list.return_value = []
+
+        new_kwargs = deepcopy(kwargs)
+        new_kwargs.update(
+            {
+                "vnfr_id": vnfr_id,
+                "nsr_id": nsr_id,
+                "tasks_by_target_record_id": {},
+                "logger": "logger",
+            }
+        )
+        expected_extra_dict_copy = deepcopy(expected_extra_dict3)
+        vnfd = deepcopy(vnfd_wth_persistent_storage)
+        db.get_one.return_value = vnfd
+        result = Ns._process_vdu_params(
+            target_vdu, indata, vim_info=None, target_record_id=None, **new_kwargs
+        )
+        mock_sort_vdu_interfaces.assert_called_once_with(target_vdu)
+        mock_locate_vdu_interfaces.assert_not_called()
+        mock_prepare_vdu_cloud_init.assert_called_once()
+        mock_add_persistent_root_disk_to_disk_list.assert_called_once()
+        mock_add_persistent_ordinary_disks_to_disk_list.assert_called_once()
+        mock_prepare_vdu_interfaces.assert_called_once_with(
+            target_vdu,
+            expected_extra_dict_copy,
+            ns_preffix,
+            vnf_preffix,
+            "logger",
+            {},
+            [],
+        )
+        self.assertDictEqual(result, expected_extra_dict_copy)
+        mock_prepare_vdu_ssh_keys.assert_called_once_with(target_vdu, None, {})
+        mock_prepare_vdu_affinity_group_list.assert_called_once()
+        mock_find_persistent_volumes.assert_not_called()
+
     @patch("osm_ng_ro.ns.Ns._sort_vdu_interfaces")
     @patch("osm_ng_ro.ns.Ns._partially_locate_vdu_interfaces")
     @patch("osm_ng_ro.ns.Ns._prepare_vdu_interfaces")