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 <eduardo.sousa@canonical.com>
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)
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 = {}
):
# 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)
#######################################################################################
import unittest
+from unittest.mock import patch, Mock
from osm_ng_ro.ns import Ns
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)
--- /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 _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.