| 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 ( |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 22 | ChangeVnfInstantiationStateInput, |
| 23 | ChangeVnfStateInput, |
| 24 | GetTaskQueueInput, |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 25 | GetVimCloudInput, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 26 | GetVnfDetailsInput, |
| 27 | VnfInstantiateInput, |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 28 | VnfInstantiationState, |
| 29 | VnfState, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 30 | ) |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 31 | from osm_common.dbbase import DbException |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 32 | from osm_common.temporal_constants import ( |
| 33 | LCM_TASK_QUEUE, |
| 34 | ) |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 35 | from osm_lcm.temporal.vnf_activities import VnfDbActivity, VnfOperations |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 36 | from temporalio.testing import ActivityEnvironment |
| 37 | from unittest.mock import Mock |
| 38 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 39 | vnfr_uuid = "d08d2da5-2120-476c-8538-deaeb4e88b3e" |
| 40 | model_name = "a-model-name" |
| 41 | vnf_instantiate_input = VnfInstantiateInput(vnfr_uuid=vnfr_uuid, model_name=model_name) |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 42 | cloud = "microk8s" |
| 43 | sample_vnfr = { |
| 44 | "_id": "9f472177-95c0-4335-b357-5cdc17a79965", |
| 45 | "id": "9f472177-95c0-4335-b357-5cdc17a79965", |
| 46 | "nsr-id-ref": "dcf4c922-5a73-41bf-a6ca-870c22d6336c", |
| 47 | "vnfd-ref": "jar_vnfd_scalable", |
| 48 | "vnfd-id": "f1b38eac-190c-485d-9a74-c6e169c929d8", |
| 49 | "vim-account-id": "9b0bedc3-ea8e-42fd-acc9-dd03f4dee73c", |
| 50 | } |
| 51 | sample_vnfd = { |
| 52 | "_id": "97784f19-d254-4252-946c-cf92d85443ca", |
| 53 | "id": "sol006-vnf", |
| 54 | "provider": "Canonical", |
| 55 | "product-name": "test-vnf", |
| 56 | "software-version": "1.0", |
| 57 | } |
| 58 | sample_vim_record = { |
| 59 | "_id": "a64f7c6c-bc27-4ec8-b664-5500a3324eca", |
| 60 | "name": "juju", |
| 61 | "vim_type": "paas", |
| 62 | "vim_url": "192.168.1.100:17070", |
| 63 | "vim_user": "admin", |
| 64 | "vim_password": "c16gylWEepEREN6vWw==", |
| 65 | "config": { |
| 66 | "paas_provider": "juju", |
| 67 | "cloud": cloud, |
| 68 | "cloud_credentials": "microk8s", |
| 69 | "authorized_keys": "$HOME/.local/share/juju/ssh/juju_id_rsa.pub", |
| 70 | "ca_cert_content": "-----BEGIN-----", |
| 71 | }, |
| 72 | } |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class TestVnfDbActivity(asynctest.TestCase): |
| 76 | def setUp(self): |
| 77 | self.db = Mock() |
| 78 | self.env = ActivityEnvironment() |
| 79 | self.vnf_db_activity = VnfDbActivity(self.db) |
| 80 | |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 81 | async def test_change_vnf_state_nominal_case(self): |
| 82 | vnf_state = VnfState.STOPPED |
| 83 | change_vnf_state_input = ChangeVnfStateInput(vnfr_uuid, vnf_state) |
| 84 | await self.env.run( |
| 85 | self.vnf_db_activity.change_vnf_state, change_vnf_state_input |
| 86 | ) |
| 87 | self.db.set_one.assert_called_with( |
| 88 | "vnfrs", {"_id": vnfr_uuid}, {"vnfState": vnf_state.name} |
| 89 | ) |
| 90 | |
| 91 | async def test_change_vnf_state_db_raises_exception(self): |
| 92 | change_vnf_state_input = ChangeVnfStateInput(vnfr_uuid, VnfState.STARTED) |
| 93 | self.db.set_one.side_effect = DbException("not found") |
| 94 | with self.assertRaises(DbException): |
| 95 | await self.env.run( |
| 96 | self.vnf_db_activity.change_vnf_state, change_vnf_state_input |
| 97 | ) |
| 98 | |
| 99 | async def test_change_vnf_instantiation_state_nominal_case(self): |
| 100 | instantiation_state = VnfInstantiationState.NOT_INSTANTIATED |
| 101 | change_instantiation_input = ChangeVnfInstantiationStateInput( |
| 102 | vnfr_uuid, instantiation_state |
| 103 | ) |
| 104 | await self.env.run( |
| 105 | self.vnf_db_activity.change_vnf_instantiation_state, |
| 106 | change_instantiation_input, |
| 107 | ) |
| 108 | self.db.set_one.assert_called_with( |
| 109 | "vnfrs", |
| 110 | {"_id": vnfr_uuid}, |
| 111 | {"instantiationState": instantiation_state.name}, |
| 112 | ) |
| 113 | |
| 114 | async def test_change_vnf_instantiation_state_db_raises_exception(self): |
| 115 | change_instantiation_input = ChangeVnfInstantiationStateInput( |
| 116 | vnfr_uuid, VnfInstantiationState.INSTANTIATED |
| 117 | ) |
| 118 | self.db.set_one.side_effect = DbException("not found") |
| 119 | with self.assertRaises(DbException): |
| 120 | await self.env.run( |
| 121 | self.vnf_db_activity.change_vnf_state, change_instantiation_input |
| 122 | ) |
| 123 | |
| 124 | async def test_set_vnf_model_nominal_case(self): |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 125 | await self.env.run(self.vnf_db_activity.set_vnf_model, vnf_instantiate_input) |
| 126 | self.db.set_one.assert_called_with( |
| 127 | "vnfrs", {"_id": vnfr_uuid}, {"namespace": model_name} |
| 128 | ) |
| 129 | |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 130 | async def test_set_vnf_model_db_raises_exception(self): |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 131 | self.db.set_one.side_effect = DbException("not found") |
| 132 | with self.assertRaises(DbException): |
| 133 | await self.env.run( |
| 134 | self.vnf_db_activity.set_vnf_model, vnf_instantiate_input |
| 135 | ) |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 136 | |
| 137 | |
| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 138 | class TestGetTaskQueue(asynctest.TestCase): |
| 139 | async def setUp(self): |
| 140 | self.db = Mock() |
| 141 | self.vnf_operations_instance = VnfOperations(self.db) |
| 142 | self.env = ActivityEnvironment() |
| 143 | self.get_task_queue_input = GetTaskQueueInput(vnfr_uuid=sample_vnfr["id"]) |
| 144 | |
| 145 | async def test_activity_succeeded(self): |
| 146 | self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] |
| 147 | activity_result = await self.env.run( |
| 148 | self.vnf_operations_instance.get_task_queue, |
| 149 | self.get_task_queue_input, |
| 150 | ) |
| 151 | self.assertEqual(activity_result.task_queue, LCM_TASK_QUEUE) |
| 152 | |
| 153 | async def test_db_raises_exception(self): |
| 154 | self.db.get_one.side_effect = DbException("not found") |
| 155 | with self.assertRaises(DbException): |
| 156 | await self.env.run( |
| 157 | self.vnf_operations_instance.get_task_queue, |
| 158 | self.get_task_queue_input, |
| 159 | ) |
| 160 | |
| 161 | async def test_invalid_task_queue(self): |
| 162 | vim_record = deepcopy(sample_vim_record) |
| 163 | vim_record["vim_type"] = "some-vim-type" |
| 164 | self.db.get_one.side_effect = [sample_vnfr, vim_record] |
| 165 | with self.assertRaises(KeyError): |
| 166 | await self.env.run( |
| 167 | self.vnf_operations_instance.get_task_queue, |
| 168 | self.get_task_queue_input, |
| 169 | ) |
| 170 | |
| 171 | async def test_activity_cancelled(self): |
| 172 | self.env._cancelled = True |
| 173 | self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] |
| 174 | with self.assertRaises(CancelledError): |
| 175 | activity_result = await self.env.run( |
| 176 | self.vnf_operations_instance.get_task_queue, |
| 177 | self.get_task_queue_input, |
| 178 | ) |
| 179 | self.assertEqual(activity_result, None) |
| 180 | |
| 181 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 182 | class TestVnfDetails(asynctest.TestCase): |
| 183 | async def setUp(self): |
| 184 | self.db = Mock() |
| 185 | self.vnf_operations_instance = VnfOperations(self.db) |
| 186 | self.env = ActivityEnvironment() |
| 187 | |
| 188 | async def test_activity_succeeded(self): |
| 189 | self.db.get_one.side_effect = [sample_vnfr, sample_vnfd] |
| 190 | activity_result = await self.env.run( |
| 191 | self.vnf_operations_instance.get_vnf_details, |
| 192 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 193 | ) |
| 194 | self.assertEqual(activity_result.vnfd, sample_vnfd) |
| 195 | self.assertEqual(activity_result.vnfr, sample_vnfr) |
| 196 | |
| 197 | async def test_activity_failed_db_exception(self): |
| 198 | self.db.get_one.side_effect = DbException("Can not connect to Database.") |
| 199 | with self.assertRaises(DbException) as err: |
| 200 | activity_result = await self.env.run( |
| 201 | self.vnf_operations_instance.get_vnf_details, |
| 202 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 203 | ) |
| 204 | self.assertEqual(activity_result, None) |
| 205 | self.assertEqual( |
| 206 | str(err.exception), "database exception Can not connect to Database." |
| 207 | ) |
| 208 | |
| 209 | async def test_activity_failed_key_error(self): |
| 210 | vnfr = deepcopy(sample_vnfr) |
| 211 | vnfr.pop("vnfd-id") |
| 212 | self.db.get_one.side_effect = [vnfr, sample_vnfd] |
| 213 | with self.assertRaises(KeyError) as err: |
| 214 | activity_result = await self.env.run( |
| 215 | self.vnf_operations_instance.get_vnf_details, |
| 216 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 217 | ) |
| 218 | self.assertEqual(activity_result, None) |
| 219 | self.assertEqual(str(err.exception.args[0]), "vnfd-id") |
| 220 | |
| 221 | async def test_activity_cancelled(self): |
| 222 | self.env._cancelled = True |
| 223 | self.db.get_one.side_effect = [sample_vnfr, sample_vnfd] |
| 224 | with self.assertRaises(CancelledError): |
| 225 | activity_result = await self.env.run( |
| 226 | self.vnf_operations_instance.get_vnf_details, |
| 227 | GetVnfDetailsInput(vnfr_uuid=sample_vnfr["id"]), |
| 228 | ) |
| 229 | self.assertEqual(activity_result, None) |
| 230 | |
| 231 | |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 232 | class TestGetVimCloud(asynctest.TestCase): |
| 233 | async def setUp(self): |
| 234 | self.db = Mock() |
| 235 | self.vnf_operations_instance = VnfOperations(self.db) |
| 236 | self.env = ActivityEnvironment() |
| 237 | |
| 238 | async def test_activity_succeeded(self): |
| 239 | self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] |
| 240 | activity_result = await self.env.run( |
| 241 | self.vnf_operations_instance.get_vim_cloud, |
| 242 | GetVimCloudInput(vnfr_uuid=sample_vnfr["id"]), |
| 243 | ) |
| 244 | self.assertEqual(activity_result.cloud, cloud) |
| 245 | |
| 246 | async def test_activity_vim_record_without_cloud(self): |
| 247 | vim_record = deepcopy(sample_vim_record) |
| 248 | vim_record["config"].pop("cloud") |
| 249 | self.db.get_one.side_effect = [sample_vnfr, vim_record] |
| 250 | activity_result = await self.env.run( |
| 251 | self.vnf_operations_instance.get_vim_cloud, |
| 252 | GetVimCloudInput(vnfr_uuid=sample_vnfr["id"]), |
| 253 | ) |
| 254 | self.assertEqual(activity_result.cloud, "") |
| 255 | |
| 256 | async def test_activity_failed_db_exception(self): |
| 257 | self.db.get_one.side_effect = DbException("Can not connect to Database.") |
| 258 | with self.assertRaises(DbException) as err: |
| 259 | activity_result = await self.env.run( |
| 260 | self.vnf_operations_instance.get_vim_cloud, |
| 261 | GetVimCloudInput(vnfr_uuid=sample_vnfr["id"]), |
| 262 | ) |
| 263 | self.assertEqual(activity_result, None) |
| 264 | self.assertEqual( |
| 265 | str(err.exception), "database exception Can not connect to Database." |
| 266 | ) |
| 267 | |
| 268 | async def test_activity_failed_key_error(self): |
| 269 | vim_record = deepcopy(sample_vim_record) |
| 270 | vim_record.pop("config") |
| 271 | self.db.get_one.side_effect = [sample_vnfr, vim_record] |
| 272 | with self.assertRaises(KeyError) as err: |
| 273 | activity_result = await self.env.run( |
| 274 | self.vnf_operations_instance.get_vim_cloud, |
| 275 | GetVimCloudInput(vnfr_uuid=sample_vnfr["id"]), |
| 276 | ) |
| 277 | self.assertEqual(activity_result, None) |
| 278 | self.assertEqual(str(err.exception.args[0]), "config") |
| 279 | |
| 280 | async def test_activity_cancelled(self): |
| 281 | self.env._cancelled = True |
| 282 | self.db.get_one.side_effect = [sample_vnfr, sample_vim_record] |
| 283 | with self.assertRaises(CancelledError): |
| 284 | activity_result = await self.env.run( |
| 285 | self.vnf_operations_instance.get_vim_cloud, |
| 286 | GetVimCloudInput(vnfr_uuid=sample_vnfr["id"]), |
| 287 | ) |
| 288 | self.assertEqual(activity_result, None) |
| 289 | |
| 290 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 291 | if __name__ == "__main__": |
| 292 | asynctest.main() |