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>
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)
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):
"""
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"
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",
)
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)
--- /dev/null
+#######################################################################################
+# 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.
#######################################################################################
[tox]
-envlist = black, cover, flake8, pylint, safety
+envlist = black, flake8, pylint, safety
[tox:jenkins]
toxworkdir = /tmp/.tox