From: Patricia Reinoso Date: Thu, 27 Apr 2023 13:48:50 +0000 (+0000) Subject: Add UTs for VNF activities X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=38fb5daaf1d054650c59ce8ade064a53f67da879;p=osm%2FLCM.git Add UTs for VNF activities Change-Id: I2e055bcecb358f1531052815d6a8ba89f8270b07 Signed-off-by: Patricia Reinoso --- diff --git a/osm_lcm/temporal/vnf_activities.py b/osm_lcm/temporal/vnf_activities.py index 6f64a82..2e8ab1c 100644 --- a/osm_lcm/temporal/vnf_activities.py +++ b/osm_lcm/temporal/vnf_activities.py @@ -1,5 +1,4 @@ -####################################################################################### -# 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. @@ -15,7 +14,6 @@ # limitations under the License. import logging -from temporalio import activity from osm_common.temporal_constants import ( ACTIVITY_CHANGE_VNF_STATE, ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE, @@ -38,6 +36,7 @@ from osm_common.dataclasses.temporal_dataclasses import ( VnfInstantiateInput, ) from osm_lcm.data_utils.database.database import Database +from temporalio import activity class VnfOperations: @@ -52,7 +51,7 @@ class VnfOperations: """Finds the appropriate task queue according to VIM type of VNF. Collaborators: - DB Access Object + DB read: vim_accounts, vnfrs Raises (retryable): DbException: If DB read operations fail, the collection or DB record ID does not exist. @@ -112,7 +111,7 @@ class VnfOperations: """Gets the VNF record and VNF descriptor from Database. Collaborators: - DB Access Object + DB read: vnfrs, vnfds Raises (retryable): DbException: If DB read operations fail, the collection or DB record ID does not exist. @@ -134,8 +133,7 @@ class VnfOperations: class VnfDbActivity: - - """Perform Database operations for NS accounts. + """Perform Database operations for VNF accounts. Args: db (Database): Data Access Object @@ -150,7 +148,7 @@ class VnfDbActivity: """Updates the VNF State in VNFR. Collaborators: - DB Access Object + DB Write: vnfrs Raises (retryable): DbException: If DB access/update fails, the collection or DB record ID does not exist. @@ -180,7 +178,7 @@ class VnfDbActivity: """Updates the VNF Instantiation State in VNFR. Collaborators: - DB Access Object + DB Write: vnfrs Raises (retryable): DbException: If DB access or update fails, the collection or DB record ID does not exist. @@ -214,7 +212,7 @@ class VnfDbActivity: """Updates the model name of VNF in VNFR. Collaborators: - DB Access Object + DB Write: vnfrs Raises (retryable): DbException: If DB access or update fails, the collection or DB record ID does not exist. diff --git a/osm_lcm/temporal/vnf_workflows.py b/osm_lcm/temporal/vnf_workflows.py index 6c7eae0..d3f0a98 100644 --- a/osm_lcm/temporal/vnf_workflows.py +++ b/osm_lcm/temporal/vnf_workflows.py @@ -269,6 +269,7 @@ class VnfInstantiateWorkflow: charm_info=vdu_info, constraints=compute_constraints, cloud=cloud, + config={}, ) vdu_instantiate_workflow_id = ( vdu_instantiate_input.model_name diff --git a/osm_lcm/tests/test_juju_paas_activities.py b/osm_lcm/tests/test_juju_paas_activities.py index cb06f26..db1de7c 100644 --- a/osm_lcm/tests/test_juju_paas_activities.py +++ b/osm_lcm/tests/test_juju_paas_activities.py @@ -259,6 +259,7 @@ class TestDeployCharm(TestJujuPaasActivitiesBase): entity_url = "ch:my-charm" cloud_k8s = "microk8s" cloud_other = "other" + config = {} charm_info = CharmInfo(app_name, channel, entity_url) vdu_instantiate_input_with_constraints_k8s = VduInstantiateInput( vim_content["_id"], @@ -266,6 +267,7 @@ class TestDeployCharm(TestJujuPaasActivitiesBase): charm_info, VduComputeConstraints(mem=1, cores=1), cloud_k8s, + config, ) async def test_deploy_charm_with_constraints_k8s_cloud(self): @@ -288,6 +290,7 @@ class TestDeployCharm(TestJujuPaasActivitiesBase): self.charm_info, VduComputeConstraints(mem=1, cores=1), self.cloud_other, + self.config, ), ) self.model.deploy.assert_called_once_with( @@ -306,6 +309,7 @@ class TestDeployCharm(TestJujuPaasActivitiesBase): self.charm_info, VduComputeConstraints(mem=0, cores=0), self.cloud_k8s, + self.config, ), ) self.model.deploy.assert_called_once_with( @@ -324,6 +328,7 @@ class TestDeployCharm(TestJujuPaasActivitiesBase): self.charm_info, VduComputeConstraints(mem=0, cores=0), self.cloud_other, + self.config, ), ) self.model.deploy.assert_called_once_with( diff --git a/osm_lcm/tests/test_vdu_workflow.py b/osm_lcm/tests/test_vdu_workflow.py index 15b30f6..22def4e 100644 --- a/osm_lcm/tests/test_vdu_workflow.py +++ b/osm_lcm/tests/test_vdu_workflow.py @@ -63,10 +63,11 @@ class TestVduWorkflows(asynctest.TestCase): channel = "latest" entity_url = "ch:my-charm" cloud = "microk8s" + config = {} constraints = VduComputeConstraints(mem=1, cores=1) charm_info = CharmInfo(app_name, channel, entity_url) vdu_instantiate_input = VduInstantiateInput( - vim_id, namespace, charm_info, constraints, cloud + vim_id, namespace, charm_info, constraints, cloud, config ) worflow_id = namespace + "-" + app_name diff --git a/osm_lcm/tests/test_vnf_activities.py b/osm_lcm/tests/test_vnf_activities.py index 592c523..02fad62 100644 --- a/osm_lcm/tests/test_vnf_activities.py +++ b/osm_lcm/tests/test_vnf_activities.py @@ -19,11 +19,19 @@ import asynctest from asyncio.exceptions import CancelledError from copy import deepcopy from osm_common.dataclasses.temporal_dataclasses import ( + ChangeVnfInstantiationStateInput, + ChangeVnfStateInput, + GetTaskQueueInput, GetVimCloudInput, GetVnfDetailsInput, VnfInstantiateInput, + VnfInstantiationState, + VnfState, ) from osm_common.dbbase import DbException +from osm_common.temporal_constants import ( + LCM_TASK_QUEUE, +) from osm_lcm.temporal.vnf_activities import VnfDbActivity, VnfOperations from temporalio.testing import ActivityEnvironment from unittest.mock import Mock @@ -70,13 +78,56 @@ class TestVnfDbActivity(asynctest.TestCase): self.env = ActivityEnvironment() self.vnf_db_activity = VnfDbActivity(self.db) - async def test_set_vnf_model(self): + async def test_change_vnf_state_nominal_case(self): + vnf_state = VnfState.STOPPED + change_vnf_state_input = ChangeVnfStateInput(vnfr_uuid, vnf_state) + await self.env.run( + self.vnf_db_activity.change_vnf_state, change_vnf_state_input + ) + self.db.set_one.assert_called_with( + "vnfrs", {"_id": vnfr_uuid}, {"vnfState": vnf_state.name} + ) + + async def test_change_vnf_state_db_raises_exception(self): + change_vnf_state_input = ChangeVnfStateInput(vnfr_uuid, VnfState.STARTED) + self.db.set_one.side_effect = DbException("not found") + with self.assertRaises(DbException): + await self.env.run( + self.vnf_db_activity.change_vnf_state, change_vnf_state_input + ) + + async def test_change_vnf_instantiation_state_nominal_case(self): + instantiation_state = VnfInstantiationState.NOT_INSTANTIATED + change_instantiation_input = ChangeVnfInstantiationStateInput( + vnfr_uuid, instantiation_state + ) + await self.env.run( + self.vnf_db_activity.change_vnf_instantiation_state, + change_instantiation_input, + ) + self.db.set_one.assert_called_with( + "vnfrs", + {"_id": vnfr_uuid}, + {"instantiationState": instantiation_state.name}, + ) + + async def test_change_vnf_instantiation_state_db_raises_exception(self): + change_instantiation_input = ChangeVnfInstantiationStateInput( + vnfr_uuid, VnfInstantiationState.INSTANTIATED + ) + self.db.set_one.side_effect = DbException("not found") + with self.assertRaises(DbException): + await self.env.run( + self.vnf_db_activity.change_vnf_state, change_instantiation_input + ) + + async def test_set_vnf_model_nominal_case(self): await self.env.run(self.vnf_db_activity.set_vnf_model, vnf_instantiate_input) self.db.set_one.assert_called_with( "vnfrs", {"_id": vnfr_uuid}, {"namespace": model_name} ) - async def test_db_raises_exception(self): + async def test_set_vnf_model_db_raises_exception(self): self.db.set_one.side_effect = DbException("not found") with self.assertRaises(DbException): await self.env.run( @@ -84,6 +135,50 @@ class TestVnfDbActivity(asynctest.TestCase): ) +class TestGetTaskQueue(asynctest.TestCase): + async def setUp(self): + self.db = Mock() + self.vnf_operations_instance = VnfOperations(self.db) + self.env = ActivityEnvironment() + self.get_task_queue_input = GetTaskQueueInput(vnfr_uuid=sample_vnfr["id"]) + + async def test_activity_succeeded(self): + self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] + activity_result = await self.env.run( + self.vnf_operations_instance.get_task_queue, + self.get_task_queue_input, + ) + self.assertEqual(activity_result.task_queue, LCM_TASK_QUEUE) + + async def test_db_raises_exception(self): + self.db.get_one.side_effect = DbException("not found") + with self.assertRaises(DbException): + await self.env.run( + self.vnf_operations_instance.get_task_queue, + self.get_task_queue_input, + ) + + async def test_invalid_task_queue(self): + vim_record = deepcopy(sample_vim_record) + vim_record["vim_type"] = "some-vim-type" + self.db.get_one.side_effect = [sample_vnfr, vim_record] + with self.assertRaises(KeyError): + await self.env.run( + self.vnf_operations_instance.get_task_queue, + self.get_task_queue_input, + ) + + async def test_activity_cancelled(self): + self.env._cancelled = True + self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] + with self.assertRaises(CancelledError): + activity_result = await self.env.run( + self.vnf_operations_instance.get_task_queue, + self.get_task_queue_input, + ) + self.assertEqual(activity_result, None) + + class TestVnfDetails(asynctest.TestCase): async def setUp(self): self.db = Mock() diff --git a/osm_lcm/tests/test_vnf_workflows.py b/osm_lcm/tests/test_vnf_workflows.py index 2f2c148..113f578 100644 --- a/osm_lcm/tests/test_vnf_workflows.py +++ b/osm_lcm/tests/test_vnf_workflows.py @@ -112,6 +112,7 @@ sample_vdu_instantiate_input = VduInstantiateInput( charm_info=sample_charm_info, constraints=sample_constraints, cloud=cloud, + config={}, ) SANDBOXED = False DEBUG_MODE = True @@ -829,6 +830,7 @@ class TestGetVduInstantiateInputMethods(TestCase): charm_info=charm_info, constraints=sample_constraints, cloud=cloud, + config={}, ), ) self.assertEqual(vdu_instantiate_wf_id, "my-model-name-")