1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
19 from unittest
.mock
import MagicMock
, Mock
, patch
21 from osm_common
.dbmemory
import DbMemory
22 from osm_ng_ro
.ns_thread
import (
25 VimInteractionAffinityGroup
,
28 # Variables used in tests
30 "my_target_vim": {"vim_type": "openstack"},
33 "my_target_vim": {"vim_type": "aws"},
37 class TestConfigValidate(unittest
.TestCase
):
43 "refresh_image": 3600,
49 def test_get_configuration(self
):
50 with self
.subTest(i
=1, t
="Get config attributes with config input"):
51 configuration
= ConfigValidate(self
.config_dict
)
52 self
.assertEqual(configuration
.active
, 65)
53 self
.assertEqual(configuration
.build
, 20)
54 self
.assertEqual(configuration
.image
, 3600)
55 self
.assertEqual(configuration
.error
, 300)
56 self
.assertEqual(configuration
.queue_size
, 50)
58 with self
.subTest(i
=2, t
="Unallowed refresh active input"):
59 # > 60 (except -1) is not allowed to set, so it should return default value 60
60 self
.config_dict
["period"]["refresh_active"] = 20
61 configuration
= ConfigValidate(self
.config_dict
)
62 self
.assertEqual(configuration
.active
, 60)
64 with self
.subTest(i
=3, t
="Config to disable VM status periodic checks"):
65 # -1 is allowed to set to disable VM status updates
66 self
.config_dict
["period"]["refresh_active"] = -1
67 configuration
= ConfigValidate(self
.config_dict
)
68 self
.assertEqual(configuration
.active
, -1)
71 class TestNsWorker(unittest
.TestCase
):
72 @patch("logging.getLogger", autospec
=True)
73 def setUp(self
, mock_logger
):
74 mock_logger
= logging
.getLogger()
75 mock_logger
.disabled
= True
76 self
.task_depends
= None
78 self
.db_vims
= db_vims_openstack
79 self
.db
= Mock(DbMemory())
80 self
.worker_index
= "worker-3"
85 "refresh_image": 3600,
89 "process_id": "343435353",
90 "global": {"task_locked_time": 16373242100.994312},
97 "target_id": "my_target_vim",
100 "created_items": None,
101 "vim_id": "test-vim-id",
102 "vim_name": "test-vim",
103 "vim_status": "DONE",
108 "modified_at": 1637324200.994312,
109 "created_at": 1637324200.994312,
110 "to_check_at": 16373242400.994312,
114 "action_id": "123456",
116 "task_id": "123456:1",
120 "target_record": "test_target_record",
121 "target_record_id": "test_target_record_id",
125 self
.instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, self
.db
)
126 self
.instance
.db_vims
= db_vims_openstack
127 self
.instance
.refresh_config
= Mock()
129 def get_disabled_tasks(self
, db
, status
):
130 db_disabled_tasks
= db
.get_list(
133 "tasks.status": status
,
137 return db_disabled_tasks
139 def test_update_vm_refresh_disabled_task_with_status_build_vim_openstack_with_refresh(
142 """1 disabled task with status BUILD in DB, refresh_active parameter is not equal to -1."""
143 # Disabled task with status build is not enabled again
145 self
.ro_task
["tasks"][0]["status"] = "BUILD"
146 self
.config
["period"]["refresh_active"] = 70
147 self
.ro_task
["to_check_at"] = -1
148 db
.create("ro_tasks", self
.ro_task
)
149 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "BUILD"))
150 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
151 instance
.update_vm_refresh(self
.ro_task
)
153 len(self
.get_disabled_tasks(db
, "BUILD")), disabled_tasks_count
156 def test_update_vm_refresh_disabled_task_with_status_done_vim_openstack_no_refresh(
159 """1 disabled task with status DONE in DB, refresh_active parameter is equal to -1."""
160 # As refresh_active parameter is equal to -1, task is not be enabled to process again
162 self
.config
["period"]["refresh_active"] = -1
163 self
.ro_task
["tasks"][0]["status"] = "DONE"
164 self
.ro_task
["to_check_at"] = -1
165 db
.create("ro_tasks", self
.ro_task
)
166 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
167 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
168 instance
.update_vm_refresh(self
.ro_task
)
169 self
.assertEqual(len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
)
171 def test_update_vm_refresh_disabled_task_with_status_done_vim_aws_with_refresh(
174 """2 disabled task with status DONE in DB, refresh_active parameter is not equal to -1."""
175 # Disabled tasks should be enabled to process again as vim type aws
177 self
.config
["period"]["refresh_active"] = 66
178 self
.ro_task
["tasks"][0]["status"] = "DONE"
179 self
.ro_task
["to_check_at"] = -1
180 db
.create("ro_tasks", self
.ro_task
)
181 self
.ro_task2
= self
.ro_task
182 self
.ro_task2
["_id"] = "122437:1"
183 db
.create("ro_tasks", self
.ro_task2
)
184 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
185 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
186 with patch
.object(instance
, "db_vims", db_vims_aws
):
187 instance
.update_vm_refresh(self
.ro_task
)
189 len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
- 2
192 def test_update_vm_refresh_no_disabled_task_with_status_done_vim_openstack_with_refresh(
195 """No disabled task with status DONE in DB, refresh_active parameter is not equal to -1."""
196 # There is not any disabled task, method does not change anything
198 self
.config
["period"]["refresh_active"] = 66
199 self
.ro_task
["tasks"][0]["status"] = "DONE"
200 self
.ro_task
["to_check_at"] = 16373242400.994312
201 db
.create("ro_tasks", self
.ro_task
)
202 self
.ro_task2
= self
.ro_task
203 self
.ro_task2
["_id"] = "122437:1"
204 db
.create("ro_tasks", self
.ro_task2
)
205 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
206 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
207 instance
.update_vm_refresh(self
.ro_task
)
208 self
.assertEqual(len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
)
210 def test_update_vm_refresh_disabled_task_with_status_done_vim_openstack_with_refresh(
213 """1 disabled task with status DONE in DB, refresh_active parameter is equal to -1, vim type is Openstack."""
214 # Disabled task with status done is not enabled again as vim type is openstack
216 self
.ro_task
["tasks"][0]["status"] = "DONE"
217 self
.ro_task
["to_check_at"] = -1
218 db
.create("ro_tasks", self
.ro_task
)
219 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
220 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
221 instance
.update_vm_refresh(self
.ro_task
)
222 self
.assertEqual(len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
)
224 def test_process_pending_tasks_status_done_vim_aws_no_refresh(self
):
225 """Refresh_active parameter is equal to -1, task status is DONE."""
226 # Task should be disabled to process again
228 self
.config
["period"]["refresh_active"] = -1
229 self
.ro_task
["tasks"][0]["status"] = "DONE"
230 self
.ro_task
["to_check_at"] = 16373242400.994312
231 db
.create("ro_tasks", self
.ro_task
)
232 # Number of disabled tasks in DB
233 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
234 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
235 with patch
.object(instance
, "db_vims", db_vims_aws
):
236 instance
._process
_pending
_tasks
(self
.ro_task
)
238 len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
+ 1
241 def test_process_pending_tasks_status_failed_vim_aws_no_refresh(self
):
242 """Refresh_active parameter is equal to -1, task status is FAILED."""
243 # Task is not disabled to process as task status is not DONE
245 self
.config
["period"]["refresh_active"] = -1
246 self
.ro_task
["tasks"][0]["status"] = "FAILED"
247 self
.ro_task
["to_check_at"] = 16373242400.994312
248 db
.create("ro_tasks", self
.ro_task
)
249 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "FAILED"))
250 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
251 with patch
.object(instance
, "db_vims", db_vims_aws
):
252 instance
._process
_pending
_tasks
(self
.ro_task
)
254 len(self
.get_disabled_tasks(db
, "FAILED")), disabled_tasks_count
257 def test_process_pending_tasks_status_done_vim_aws_with_refresh(self
):
258 """Refresh_active parameter is not equal to -1, task status is DONE."""
259 # Task is not disabled to process as refresh_active parameter is not -1
261 self
.config
["period"]["refresh_active"] = 70
262 self
.ro_task
["tasks"][0]["status"] = "DONE"
263 self
.ro_task
["to_check_at"] = 16373242400.994312
264 db
.create("ro_tasks", self
.ro_task
)
265 disabled_tasks_count
= len(self
.get_disabled_tasks(db
, "DONE"))
266 instance
= NsWorker(self
.worker_index
, self
.config
, self
.plugins
, db
)
267 with patch
.object(instance
, "db_vims", db_vims_aws
):
268 instance
._process
_pending
_tasks
(self
.ro_task
)
270 len(self
.get_disabled_tasks(db
, "DONE")), disabled_tasks_count
274 class TestVimInteractionAffinityGroup(unittest
.TestCase
):
276 module_name
= "osm_ro_plugin"
277 self
.target_vim
= MagicMock(name
=f
"{module_name}.vimconn.VimConnector")
278 self
.task_depends
= None
280 patches
= [patch(f
"{module_name}.vimconn.VimConnector", self
.target_vim
)]
282 # Enabling mocks and add cleanups
285 self
.addCleanup(mock
.stop
)
287 def test__new_affinity_group_ok(self
):
289 create affinity group with attributes set in params
292 logger
= "test_logger"
300 instance
= VimInteractionAffinityGroup(db
, logger
, my_vims
, db_vims
)
301 with patch
.object(instance
, "my_vims", [self
.target_vim
]), patch
.object(
302 instance
, "logger", logging
303 ), patch
.object(instance
, "db_vims", db_vims
):
309 "action_id": "123456",
311 "task_id": "123456:1",
312 "status": "SCHEDULED",
315 "target_record": "test_target_record",
316 "target_record_id": "test_target_record_id",
317 # values coming from extra_dict
319 "affinity_group_data": {
320 "name": "affinity_group_1",
322 "scope": "nfvi-node",
326 "depends_on": "test_depends_on",
331 task_index
= "task_index_1"
332 self
.target_vim
.new_affinity_group
.return_value
= (
333 "sample_affinity_group_id_1"
335 result
= instance
.new(ro_task
, task_index
, self
.task_depends
)
336 self
.assertEqual(result
[0], "DONE")
337 self
.assertEqual(result
[1].get("vim_id"), "sample_affinity_group_id_1")
338 self
.assertEqual(result
[1].get("created"), True)
339 self
.assertEqual(result
[1].get("vim_status"), "DONE")
341 def test__new_affinity_group_failed(self
):
343 create affinity group with no attributes set in params
346 logger
= "test_logger"
354 instance
= VimInteractionAffinityGroup(db
, logger
, my_vims
, db_vims
)
355 with patch
.object(instance
, "my_vims", [self
.target_vim
]), patch
.object(
356 instance
, "logger", logging
357 ), patch
.object(instance
, "db_vims", db_vims
):
363 "action_id": "123456",
365 "task_id": "123456:1",
366 "status": "SCHEDULED",
369 "target_record": "test_target_record",
370 "target_record_id": "test_target_record_id",
371 # values coming from extra_dict
374 "depends_on": "test_depends_on",
379 task_index
= "task_index_2"
380 self
.target_vim
.new_affinity_group
.return_value
= (
381 "sample_affinity_group_id_1"
383 result
= instance
.new(ro_task
, task_index
, self
.task_depends
)
384 self
.assertEqual(result
[0], "DONE")
385 self
.assertEqual(result
[1].get("vim_id"), None)
386 self
.assertEqual(result
[1].get("created"), False)
387 self
.assertEqual(result
[1].get("vim_status"), "DONE")
389 def test__delete_affinity_group_ok(self
):
391 delete affinity group with a proper vim_id
394 logger
= "test_logger"
402 instance
= VimInteractionAffinityGroup(db
, logger
, my_vims
, db_vims
)
403 with patch
.object(instance
, "my_vims", [self
.target_vim
]), patch
.object(
404 instance
, "logger", logging
405 ), patch
.object(instance
, "db_vims", db_vims
):
411 "task_id": "123456:1",
416 "created_items": None,
417 "vim_id": "sample_affinity_group_id_3",
418 "vim_name": "sample_affinity_group_id_3",
420 "vim_details": "some-details",
425 task_index
= "task_index_3"
426 self
.target_vim
.delete_affinity_group
.return_value
= (
427 "sample_affinity_group_id_3"
429 result
= instance
.delete(ro_task
, task_index
)
430 self
.assertEqual(result
[0], "DONE")
431 self
.assertEqual(result
[1].get("vim_details"), "DELETED")
432 self
.assertEqual(result
[1].get("created"), False)
433 self
.assertEqual(result
[1].get("vim_status"), "DELETED")
435 def test__delete_affinity_group_failed(self
):
437 delete affinity group with missing vim_id
440 logger
= "test_logger"
448 instance
= VimInteractionAffinityGroup(db
, logger
, my_vims
, db_vims
)
449 with patch
.object(instance
, "my_vims", [self
.target_vim
]), patch
.object(
450 instance
, "logger", logging
451 ), patch
.object(instance
, "db_vims", db_vims
):
457 "task_id": "123456:1",
462 "created_items": None,
466 "vim_details": "some-details",
471 task_index
= "task_index_4"
472 self
.target_vim
.delete_affinity_group
.return_value
= ""
473 result
= instance
.delete(ro_task
, task_index
)
474 self
.assertEqual(result
[0], "DONE")
475 self
.assertEqual(result
[1].get("vim_details"), "DELETED")
476 self
.assertEqual(result
[1].get("created"), False)
477 self
.assertEqual(result
[1].get("vim_status"), "DELETED")