Extracting Ns._process_image_params() and creating unit test 79/11379/4
authorsousaedu <eduardo.sousa@canonical.com>
Sun, 21 Nov 2021 16:21:13 +0000 (16:21 +0000)
committersousaedu <eduardo.sousa@canonical.com>
Sun, 21 Nov 2021 16:38:43 +0000 (16:38 +0000)
Extracted the _process_image_params() to ease the testability of that code.
Introduced a unit test to cover that function.

Change-Id: I84ab38b5921deeea25376e433af7e374f436238e
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_process_image_params-925ba22e69861137.yaml [new file with mode: 0644]
tox.ini

index 5ab1c45..25204c0 100644 (file)
@@ -468,6 +468,37 @@ class Ns(object):
 
         return db_ro_task
 
+    @staticmethod
+    def _process_image_params(
+        target_image: Dict[str, Any],
+        vim_info: Dict[str, Any],
+        target_record_id: str,
+    ) -> Dict[str, Any]:
+        """Function to process VDU image parameters.
+
+        Args:
+            target_image (Dict[str, Any]): [description]
+            vim_info (Dict[str, Any]): [description]
+            target_record_id (str): [description]
+
+        Returns:
+            Dict[str, Any]: [description]
+        """
+        find_params = {}
+
+        if target_image.get("image"):
+            find_params["filter_dict"] = {"name": target_image.get("image")}
+
+        if target_image.get("vim_image_id"):
+            find_params["filter_dict"] = {"id": target_image.get("vim_image_id")}
+
+        if target_image.get("image_checksum"):
+            find_params["filter_dict"] = {
+                "checksum": target_image.get("image_checksum")
+            }
+
+        return {"find_params": find_params}
+
     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)
@@ -524,24 +555,6 @@ class Ns(object):
 
                     index += 1
 
-            def _process_image_params(target_image, vim_info, target_record_id):
-                find_params = {}
-
-                if target_image.get("image"):
-                    find_params["filter_dict"] = {"name": target_image.get("image")}
-
-                if target_image.get("vim_image_id"):
-                    find_params["filter_dict"] = {
-                        "id": target_image.get("vim_image_id")
-                    }
-
-                if target_image.get("image_checksum"):
-                    find_params["filter_dict"] = {
-                        "checksum": target_image.get("image_checksum")
-                    }
-
-                return {"find_params": find_params}
-
             def _process_flavor_params(target_flavor, vim_info, target_record_id):
                 def _get_resource_allocation_params(quota_descriptor):
                     """
@@ -1176,7 +1189,7 @@ class Ns(object):
                         db_update=db_nsr_update,
                         db_path="image",
                         item="image",
-                        process_params=_process_image_params,
+                        process_params=Ns._process_image_params,
                     )
 
                     step = "process NS flavors"
index 3fa617b..7e6757a 100644 (file)
@@ -29,6 +29,36 @@ class TestNs(unittest.TestCase):
     def setUp(self):
         pass
 
+    def test__create_task_without_extra_dict(self):
+        expected_result = {
+            "target_id": "vim_openstack_1",
+            "action_id": "123456",
+            "nsr_id": "654321",
+            "task_id": "123456:1",
+            "status": "SCHEDULED",
+            "action": "CREATE",
+            "item": "test_item",
+            "target_record": "test_target_record",
+            "target_record_id": "test_target_record_id",
+        }
+        deployment_info = {
+            "action_id": "123456",
+            "nsr_id": "654321",
+            "task_index": 1,
+        }
+
+        task = Ns._create_task(
+            deployment_info=deployment_info,
+            target_id="vim_openstack_1",
+            item="test_item",
+            action="CREATE",
+            target_record="test_target_record",
+            target_record_id="test_target_record_id",
+        )
+
+        self.assertEqual(deployment_info.get("task_index"), 2)
+        self.assertDictEqual(task, expected_result)
+
     def test__create_task(self):
         expected_result = {
             "target_id": "vim_openstack_1",
@@ -113,3 +143,85 @@ class TestNs(unittest.TestCase):
         )
 
         self.assertDictEqual(ro_task, expected_result)
+
+    def test__process_image_params_with_empty_target_image(self):
+        expected_result = {
+            "find_params": {},
+        }
+        target_image = {}
+
+        result = Ns._process_image_params(
+            target_image=target_image,
+            vim_info=None,
+            target_record_id=None,
+        )
+
+        self.assertDictEqual(expected_result, result)
+
+    def test__process_image_params_with_wrong_target_image(self):
+        expected_result = {
+            "find_params": {},
+        }
+        target_image = {"no_image": "to_see_here"}
+
+        result = Ns._process_image_params(
+            target_image=target_image,
+            vim_info=None,
+            target_record_id=None,
+        )
+
+        self.assertDictEqual(expected_result, result)
+
+    def test__process_image_params_with_image(self):
+        expected_result = {
+            "find_params": {
+                "filter_dict": {
+                    "name": "cirros",
+                },
+            },
+        }
+        target_image = {"image": "cirros"}
+
+        result = Ns._process_image_params(
+            target_image=target_image,
+            vim_info=None,
+            target_record_id=None,
+        )
+
+        self.assertDictEqual(expected_result, result)
+
+    def test__process_image_params_with_vim_image_id(self):
+        expected_result = {
+            "find_params": {
+                "filter_dict": {
+                    "id": "123456",
+                },
+            },
+        }
+        target_image = {"vim_image_id": "123456"}
+
+        result = Ns._process_image_params(
+            target_image=target_image,
+            vim_info=None,
+            target_record_id=None,
+        )
+
+        self.assertDictEqual(expected_result, result)
+
+    def test__process_image_params_with_image_checksum(self):
+        expected_result = {
+            "find_params": {
+                "filter_dict": {
+                    "checksum": "e3fc50a88d0a364313df4b21ef20c29e",
+                },
+            },
+        }
+        target_image = {"image_checksum": "e3fc50a88d0a364313df4b21ef20c29e"}
+
+        result = Ns._process_image_params(
+            target_image=target_image,
+            vim_info=None,
+            target_record_id=None,
+        )
+
+        self.assertDictEqual(expected_result, result)
diff --git a/releasenotes/notes/extracting_process_image_params-925ba22e69861137.yaml b/releasenotes/notes/extracting_process_image_params-925ba22e69861137.yaml
new file mode 100644 (file)
index 0000000..3bea793
--- /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 _process_image_params() from being a nested function inside Ns.deploy().
+    The _process_image_params() function is now a static method inside the Ns class. This
+    eases the testability of _process_image_params().
+    With this extraction a unit test was introduced to cover the extracted function.
diff --git a/tox.ini b/tox.ini
index 34e22ae..35c212b 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -16,7 +16,7 @@
 #######################################################################################
 
 [tox]
-envlist = black, cover, flake8, pylint, safety
+envlist = black, flake8, pylint, safety
 
 [tox:jenkins]
 toxworkdir = /tmp/.tox