From 839e5ca652d48ee67929d3022e4cddbf19e7ddde Mon Sep 17 00:00:00 2001 From: sousaedu Date: Fri, 19 Nov 2021 16:41:38 +0000 Subject: [PATCH] Extracting Ns._create_ro_task() and creating unit test Extracted the _create_ro_task() to ease the testability of that code. Introduced a unit test to cover that function. Change-Id: I684ba7fc63e1d77545c84517024f541a9e630573 Signed-off-by: sousaedu --- NG-RO/osm_ng_ro/ns.py | 69 +++++++++++-------- NG-RO/osm_ng_ro/tests/test_ns.py | 47 +++++++++++++ ...cting_create_ro_task-7c2c75c065890104.yaml | 23 +++++++ 3 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 releasenotes/notes/extracting_create_ro_task-7c2c75c065890104.yaml diff --git a/NG-RO/osm_ng_ro/ns.py b/NG-RO/osm_ng_ro/ns.py index 35502c28..5ab1c45e 100644 --- a/NG-RO/osm_ng_ro/ns.py +++ b/NG-RO/osm_ng_ro/ns.py @@ -429,6 +429,45 @@ class Ns(object): return task + @staticmethod + def _create_ro_task( + target_id: str, + task: Dict[str, Any], + ) -> Dict[str, Any]: + """Function to create an RO task from task information. + + Args: + target_id (str): [description] + task (Dict[str, Any]): [description] + + Returns: + Dict[str, Any]: [description] + """ + now = time() + + _id = task.get("task_id") + db_ro_task = { + "_id": _id, + "locked_by": None, + "locked_at": 0.0, + "target_id": target_id, + "vim_info": { + "created": False, + "created_items": None, + "vim_id": None, + "vim_name": None, + "vim_status": None, + "vim_details": None, + "refresh_at": None, + }, + "modified_at": now, + "created_at": now, + "to_check_at": now, + "tasks": [task], + } + + return db_ro_task + 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) @@ -485,34 +524,6 @@ class Ns(object): index += 1 - def _create_ro_task(target_id, task): - nonlocal action_id - nonlocal task_index - nonlocal now - - _id = task["task_id"] - db_ro_task = { - "_id": _id, - "locked_by": None, - "locked_at": 0.0, - "target_id": target_id, - "vim_info": { - "created": False, - "created_items": None, - "vim_id": None, - "vim_name": None, - "vim_status": None, - "vim_details": None, - "refresh_at": None, - }, - "modified_at": now, - "created_at": now, - "to_check_at": now, - "tasks": [task], - } - - return db_ro_task - def _process_image_params(target_image, vim_info, target_record_id): find_params = {} @@ -1244,7 +1255,7 @@ class Ns(object): ): # Create a ro_task step = "Updating database, Creating ro_tasks" - db_ro_task = _create_ro_task(target_id, db_task) + db_ro_task = Ns._create_ro_task(target_id, db_task) nb_ro_tasks += 1 self.db.create("ro_tasks", db_ro_task) diff --git a/NG-RO/osm_ng_ro/tests/test_ns.py b/NG-RO/osm_ng_ro/tests/test_ns.py index fc563299..3fa617b9 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns.py +++ b/NG-RO/osm_ng_ro/tests/test_ns.py @@ -16,6 +16,7 @@ ####################################################################################### import unittest +from unittest.mock import patch, Mock from osm_ng_ro.ns import Ns @@ -66,3 +67,49 @@ class TestNs(unittest.TestCase): self.assertEqual(deployment_info.get("task_index"), 2) self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.time") + def test__create_ro_task(self, mock_time: Mock): + now = 1637324838.994551 + mock_time.return_value = now + task = { + "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", + # values coming from extra_dict + "params": "test_params", + "find_params": "test_find_params", + "depends_on": "test_depends_on", + } + expected_result = { + "_id": "123456:1", + "locked_by": None, + "locked_at": 0.0, + "target_id": "vim_openstack_1", + "vim_info": { + "created": False, + "created_items": None, + "vim_id": None, + "vim_name": None, + "vim_status": None, + "vim_details": None, + "refresh_at": None, + }, + "modified_at": now, + "created_at": now, + "to_check_at": now, + "tasks": [task], + } + + ro_task = Ns._create_ro_task( + target_id="vim_openstack_1", + task=task, + ) + + self.assertDictEqual(ro_task, expected_result) diff --git a/releasenotes/notes/extracting_create_ro_task-7c2c75c065890104.yaml b/releasenotes/notes/extracting_create_ro_task-7c2c75c065890104.yaml new file mode 100644 index 00000000..bf44e967 --- /dev/null +++ b/releasenotes/notes/extracting_create_ro_task-7c2c75c065890104.yaml @@ -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 _create_ro_task() from being a nested function inside Ns.deploy(). + The _create_ro_task() function is now a static method inside the Ns class. This + eases the testability of _create_ro_task(). + With this extraction a unit test was introduced to cover the extracted function. -- 2.25.1