Extracting Ns._ip_profile_to_ro() and creating unit test 78/11478/1
authorsousaedu <eduardo.sousa@canonical.com>
Sun, 5 Dec 2021 19:31:03 +0000 (19:31 +0000)
committersousaedu <eduardo.sousa@canonical.com>
Sun, 5 Dec 2021 23:41:26 +0000 (23:41 +0000)
Change-Id: I0a04a7b2cbe24660529389926f7b4d51f010ca7d
Signed-off-by: sousaedu <eduardo.sousa@canonical.com>
NG-RO/osm_ng_ro/ns.py
NG-RO/osm_ng_ro/tests/test_ns.py
releasenotes/notes/extracting_ip_profile_to_ro-974d5bbf91cb2c76.yaml [new file with mode: 0644]

index a7d8aa6..43f0146 100644 (file)
@@ -755,6 +755,44 @@ class Ns(object):
 
         return extra_dict
 
+    @staticmethod
+    def _ip_profile_to_ro(
+        ip_profile: Dict[str, Any],
+    ) -> Dict[str, Any]:
+        """[summary]
+
+        Args:
+            ip_profile (Dict[str, Any]): [description]
+
+        Returns:
+            Dict[str, Any]: [description]
+        """
+        if not ip_profile:
+            return None
+
+        ro_ip_profile = {
+            "ip_version": "IPv4"
+            if "v4" in ip_profile.get("ip-version", "ipv4")
+            else "IPv6",
+            "subnet_address": ip_profile.get("subnet-address"),
+            "gateway_address": ip_profile.get("gateway-address"),
+            "dhcp_enabled": ip_profile.get("dhcp-params", {}).get("enabled", False),
+            "dhcp_start_address": ip_profile.get("dhcp-params", {}).get(
+                "start-address", None
+            ),
+            "dhcp_count": ip_profile.get("dhcp-params", {}).get("count", None),
+        }
+
+        if ip_profile.get("dns-server"):
+            ro_ip_profile["dns_address"] = ";".join(
+                [v["address"] for v in ip_profile["dns-server"] if v.get("address")]
+            )
+
+        if ip_profile.get("security-group"):
+            ro_ip_profile["security_group"] = ip_profile["security-group"]
+
+        return ro_ip_profile
+
     def deploy(self, session, indata, version, nsr_id, *args, **kwargs):
         self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata))
         validate_input(indata, deploy_schema)
@@ -811,35 +849,6 @@ class Ns(object):
 
                     index += 1
 
-            def _ip_profile_2_ro(ip_profile):
-                if not ip_profile:
-                    return None
-
-                ro_ip_profile = {
-                    "ip_version": "IPv4"
-                    if "v4" in ip_profile.get("ip-version", "ipv4")
-                    else "IPv6",
-                    "subnet_address": ip_profile.get("subnet-address"),
-                    "gateway_address": ip_profile.get("gateway-address"),
-                    "dhcp_enabled": ip_profile.get("dhcp-params", {}).get(
-                        "enabled", False
-                    ),
-                    "dhcp_start_address": ip_profile.get("dhcp-params", {}).get(
-                        "start-address", None
-                    ),
-                    "dhcp_count": ip_profile.get("dhcp-params", {}).get("count", None),
-                }
-
-                if ip_profile.get("dns-server"):
-                    ro_ip_profile["dns_address"] = ";".join(
-                        [v["address"] for v in ip_profile["dns-server"]]
-                    )
-
-                if ip_profile.get("security-group"):
-                    ro_ip_profile["security_group"] = ip_profile["security-group"]
-
-                return ro_ip_profile
-
             def _process_net_params(target_vld, indata, vim_info, target_record_id):
                 extra_dict = {}
 
@@ -879,7 +888,7 @@ class Ns(object):
                             indata["name"][:16],
                             target_vld.get("name", target_vld["id"])[:16],
                         ),
-                        "ip_profile": _ip_profile_2_ro(vim_info.get("ip_profile")),
+                        "ip_profile": Ns._ip_profile_to_ro(vim_info.get("ip_profile")),
                         "provider_network_profile": vim_info.get("provider_network"),
                     }
 
index 6980b88..eb43d17 100644 (file)
@@ -1788,3 +1788,206 @@ 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)
diff --git a/releasenotes/notes/extracting_ip_profile_to_ro-974d5bbf91cb2c76.yaml b/releasenotes/notes/extracting_ip_profile_to_ro-974d5bbf91cb2c76.yaml
new file mode 100644 (file)
index 0000000..003155a
--- /dev/null
@@ -0,0 +1,23 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+---
+other:
+  - |
+    Extraction of _ip_profile_to_ro() from being a nested function inside Ns.deploy().
+    The _ip_profile_to_ro() function is now a static method inside the Ns class. This
+    eases the testability of _ip_profile_to_ro().
+    With this extraction a unit test was introduced to cover the extracted function.