| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | import asynctest |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 19 | from asyncio.exceptions import CancelledError |
| 20 | from copy import deepcopy |
| 21 | from osm_common.dataclasses.temporal_dataclasses import ( |
| 22 | GetVnfDetailsInput, |
| 23 | VnfInstantiateInput, |
| 24 | ) |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 25 | from osm_common.dbbase import DbException |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 26 | from osm_lcm.temporal.vnf_activities import VnfDbActivity, VnfOperations |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 27 | from temporalio.testing import ActivityEnvironment |
| 28 | from unittest.mock import Mock |
| 29 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 30 | vnfr_uuid = "d08d2da5-2120-476c-8538-deaeb4e88b3e" |
| 31 | model_name = "a-model-name" |
| 32 | vnf_instantiate_input = VnfInstantiateInput(vnfr_uuid=vnfr_uuid, model_name=model_name) |
| 33 | |
| 34 | |
| 35 | class TestVnfDbActivity(asynctest.TestCase): |
| 36 | def setUp(self): |
| 37 | self.db = Mock() |
| 38 | self.env = ActivityEnvironment() |
| 39 | self.vnf_db_activity = VnfDbActivity(self.db) |
| 40 | |
| 41 | async def test_set_vnf_model(self): |
| 42 | await self.env.run(self.vnf_db_activity.set_vnf_model, vnf_instantiate_input) |
| 43 | self.db.set_one.assert_called_with( |
| 44 | "vnfrs", {"_id": vnfr_uuid}, {"namespace": model_name} |
| 45 | ) |
| 46 | |
| 47 | async def test_db_raises_exception(self): |
| 48 | self.db.set_one.side_effect = DbException("not found") |
| 49 | with self.assertRaises(DbException): |
| 50 | await self.env.run( |
| 51 | self.vnf_db_activity.set_vnf_model, vnf_instantiate_input |
| 52 | ) |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 53 | |
| 54 | |
| 55 | sample_vnfr = { |
| 56 | "_id": "9f472177-95c0-4335-b357-5cdc17a79965", |
| 57 | "id": "9f472177-95c0-4335-b357-5cdc17a79965", |
| 58 | "nsr-id-ref": "dcf4c922-5a73-41bf-a6ca-870c22d6336c", |
| 59 | "vnfd-ref": "jar_vnfd_scalable", |
| 60 | "vnfd-id": "f1b38eac-190c-485d-9a74-c6e169c929d8", |
| 61 | "vim-account-id": "9b0bedc3-ea8e-42fd-acc9-dd03f4dee73c", |
| 62 | } |
| 63 | |
| 64 | sample_vnfd = { |
| 65 | "_id": "97784f19-d254-4252-946c-cf92d85443ca", |
| 66 | "id": "sol006-vnf", |
| 67 | "provider": "Canonical", |
| 68 | "product-name": "test-vnf", |
| 69 | "software-version": "1.0", |
| 70 | } |
| 71 | |
| 72 | |
| 73 | class TestVnfDetails(asynctest.TestCase): |
| 74 | async def setUp(self): |
| 75 | self.db = Mock() |
| 76 | self.vnf_operations_instance = VnfOperations(self.db) |
| 77 | self.env = ActivityEnvironment() |
| 78 | |
| 79 | async def test_activity_succeeded(self): |
| 80 | self.db.get_one.side_effect = [sample_vnfr, sample_vnfd] |
| 81 | activity_result = await self.env.run( |
| 82 | self.vnf_operations_instance.get_vnf_details, |
| 83 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 84 | ) |
| 85 | self.assertEqual(activity_result.vnfd, sample_vnfd) |
| 86 | self.assertEqual(activity_result.vnfr, sample_vnfr) |
| 87 | |
| 88 | async def test_activity_failed_db_exception(self): |
| 89 | self.db.get_one.side_effect = DbException("Can not connect to Database.") |
| 90 | with self.assertRaises(DbException) as err: |
| 91 | activity_result = await self.env.run( |
| 92 | self.vnf_operations_instance.get_vnf_details, |
| 93 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 94 | ) |
| 95 | self.assertEqual(activity_result, None) |
| 96 | self.assertEqual( |
| 97 | str(err.exception), "database exception Can not connect to Database." |
| 98 | ) |
| 99 | |
| 100 | async def test_activity_failed_key_error(self): |
| 101 | vnfr = deepcopy(sample_vnfr) |
| 102 | vnfr.pop("vnfd-id") |
| 103 | self.db.get_one.side_effect = [vnfr, sample_vnfd] |
| 104 | with self.assertRaises(KeyError) as err: |
| 105 | activity_result = await self.env.run( |
| 106 | self.vnf_operations_instance.get_vnf_details, |
| 107 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 108 | ) |
| 109 | self.assertEqual(activity_result, None) |
| 110 | self.assertEqual(str(err.exception.args[0]), "vnfd-id") |
| 111 | |
| 112 | async def test_activity_cancelled(self): |
| 113 | self.env._cancelled = True |
| 114 | self.db.get_one.side_effect = [sample_vnfr, sample_vnfd] |
| 115 | with self.assertRaises(CancelledError): |
| 116 | activity_result = await self.env.run( |
| 117 | self.vnf_operations_instance.get_vnf_details, |
| 118 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 119 | ) |
| 120 | self.assertEqual(activity_result, None) |
| 121 | |
| 122 | |
| 123 | if __name__ == "__main__": |
| 124 | asynctest.main() |