X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=NG-RO%2Fosm_ng_ro%2Ftests%2Ftest_ns_thread.py;h=0914a0664543d25a6d5333694a63120ffabd4a94;hb=24a2b5184371649bff2806b9a72473f315787ef2;hp=86ba996eebedc807fdf5beb478c07d0a12f583f0;hpb=c8b3fb65fe58052717f842f49df1d4c36c982e13;p=osm%2FRO.git diff --git a/NG-RO/osm_ng_ro/tests/test_ns_thread.py b/NG-RO/osm_ng_ro/tests/test_ns_thread.py index 86ba996e..0914a066 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns_thread.py +++ b/NG-RO/osm_ng_ro/tests/test_ns_thread.py @@ -14,12 +14,15 @@ # See the License for the specific language governing permissions and # limitations under the License. ####################################################################################### - import logging import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, Mock, mock_open, patch +from osm_common.dbmemory import DbMemory from osm_ng_ro.ns_thread import ( + ConfigValidate, + NsWorker, + NsWorkerException, VimInteractionAffinityGroup, VimInteractionMigration, VimInteractionNet, @@ -27,6 +30,344 @@ from osm_ng_ro.ns_thread import ( ) from osm_ro_plugin.vimconn import VimConnConnectionException, VimConnException +# Variables used in tests +db_vims_openstack = { + "my_target_vim": {"vim_type": "openstack"}, +} +db_vims_aws = { + "my_target_vim": {"vim_type": "aws"}, +} + + +class TestConfigValidate(unittest.TestCase): + def setUp(self): + self.config_dict = { + "period": { + "refresh_active": 65, + "refresh_build": 20, + "refresh_image": 3600, + "refresh_error": 300, + "queue_size": 50, + } + } + + def test__get_configuration(self): + with self.subTest(i=1, t="Get config attributes with config input"): + configuration = ConfigValidate(self.config_dict) + self.assertEqual(configuration.active, 65) + self.assertEqual(configuration.build, 20) + self.assertEqual(configuration.image, 3600) + self.assertEqual(configuration.error, 300) + self.assertEqual(configuration.queue_size, 50) + + with self.subTest(i=2, t="Unallowed refresh active input"): + # > 60 (except -1) is not allowed to set, so it should return default value 60 + self.config_dict["period"]["refresh_active"] = 20 + configuration = ConfigValidate(self.config_dict) + self.assertEqual(configuration.active, 60) + + with self.subTest(i=3, t="Config to disable VM status periodic checks"): + # -1 is allowed to set to disable VM status updates + self.config_dict["period"]["refresh_active"] = -1 + configuration = ConfigValidate(self.config_dict) + self.assertEqual(configuration.active, -1) + + +class TestNsWorker(unittest.TestCase): + @patch("logging.getLogger", autospec=True) + def setUp(self, mock_logger): + mock_logger = logging.getLogger() + mock_logger.disabled = True + self.task_depends = None + self.plugins = {} + self.db_vims = db_vims_openstack + self.db = Mock(DbMemory()) + self.worker_index = "worker-3" + self.config = { + "period": { + "refresh_active": 60, + "refresh_build": 20, + "refresh_image": 3600, + "refresh_error": 600, + "queue_size": 100, + }, + "process_id": "343435353", + "global": {"task_locked_time": 16373242100.994312}, + } + + self.ro_task = { + "_id": "122436:1", + "locked_by": None, + "locked_at": 0.0, + "target_id": "my_target_vim", + "vim_info": { + "created": False, + "created_items": None, + "vim_id": "test-vim-id", + "vim_name": "test-vim", + "vim_status": "DONE", + "vim_details": "", + "vim_message": None, + "refresh_at": None, + }, + "modified_at": 1637324200.994312, + "created_at": 1637324200.994312, + "to_check_at": 16373242400.994312, + "tasks": [ + { + "target_id": 0, + "action_id": "123456", + "nsr_id": "654321", + "task_id": "123456:1", + "status": "DONE", + "action": "CREATE", + "item": "test_item", + "target_record": "test_target_record", + "target_record_id": "test_target_record_id", + }, + ], + } + self.instance = NsWorker(self.worker_index, self.config, self.plugins, self.db) + self.instance.db_vims = db_vims_openstack + self.instance.refresh_config = Mock() + + def get_disabled_tasks(self, db, status): + db_disabled_tasks = db.get_list( + "ro_tasks", + q_filter={ + "tasks.status": status, + "to_check_at.lt": 0, + }, + ) + return db_disabled_tasks + + def test_update_vm_refresh_disabled_task_with_status_build_vim_openstack_with_refresh( + self, + ): + """1 disabled task with status BUILD in DB, refresh_active parameter is not equal to -1.""" + # Disabled task with status build is not enabled again + db = DbMemory() + self.ro_task["tasks"][0]["status"] = "BUILD" + self.config["period"]["refresh_active"] = 70 + self.ro_task["to_check_at"] = -1 + db.create("ro_tasks", self.ro_task) + disabled_tasks_count = len(self.get_disabled_tasks(db, "BUILD")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + instance.update_vm_refresh(self.ro_task) + self.assertEqual( + len(self.get_disabled_tasks(db, "BUILD")), disabled_tasks_count + ) + + def test_update_vm_refresh_disabled_task_with_status_done_vim_openstack_no_refresh( + self, + ): + """1 disabled task with status DONE in DB, refresh_active parameter is equal to -1.""" + # As refresh_active parameter is equal to -1, task is not be enabled to process again + db = DbMemory() + self.config["period"]["refresh_active"] = -1 + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = -1 + db.create("ro_tasks", self.ro_task) + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + instance.update_vm_refresh(self.ro_task) + self.assertEqual(len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count) + + def test_update_vm_refresh_disabled_task_with_status_done_vim_aws_with_refresh( + self, + ): + """2 disabled task with status DONE in DB, refresh_active parameter is not equal to -1.""" + # Disabled tasks should be enabled to process again as vim type aws + db = DbMemory() + self.config["period"]["refresh_active"] = 66 + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = -1 + db.create("ro_tasks", self.ro_task) + self.ro_task2 = self.ro_task + self.ro_task2["_id"] = "122437:1" + db.create("ro_tasks", self.ro_task2) + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + with patch.object(instance, "db_vims", db_vims_aws): + instance.update_vm_refresh(self.ro_task) + self.assertEqual( + len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count - 2 + ) + + def test_update_vm_refresh_no_disabled_task_with_status_done_vim_openstack_with_refresh( + self, + ): + """No disabled task with status DONE in DB, refresh_active parameter is not equal to -1.""" + # There is not any disabled task, method does not change anything + db = DbMemory() + self.config["period"]["refresh_active"] = 66 + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = 16373242400.994312 + db.create("ro_tasks", self.ro_task) + self.ro_task2 = self.ro_task + self.ro_task2["_id"] = "122437:1" + db.create("ro_tasks", self.ro_task2) + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + instance.update_vm_refresh(self.ro_task) + self.assertEqual(len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count) + + def test_update_vm_refresh_disabled_task_with_status_done_vim_openstack_with_refresh( + self, + ): + """1 disabled task with status DONE in DB, refresh_active parameter is equal to -1, vim type is Openstack.""" + # Disabled task with status done is not enabled again as vim type is openstack + db = DbMemory() + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = -1 + db.create("ro_tasks", self.ro_task) + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + instance.update_vm_refresh(self.ro_task) + self.assertEqual(len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count) + + def test_process_pending_tasks_status_done_vim_aws_no_refresh(self): + """Refresh_active parameter is equal to -1, task status is DONE.""" + # Task should be disabled to process again + db = DbMemory() + self.config["period"]["refresh_active"] = -1 + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = 16373242400.994312 + db.create("ro_tasks", self.ro_task) + # Number of disabled tasks in DB + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + with patch.object(instance, "db_vims", db_vims_aws): + instance._process_pending_tasks(self.ro_task) + self.assertEqual( + len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count + 1 + ) + + def test_process_pending_tasks_status_failed_vim_aws_no_refresh(self): + """Refresh_active parameter is equal to -1, task status is FAILED.""" + # Task is not disabled to process as task status is not DONE + db = DbMemory() + self.config["period"]["refresh_active"] = -1 + self.ro_task["tasks"][0]["status"] = "FAILED" + self.ro_task["to_check_at"] = 16373242400.994312 + db.create("ro_tasks", self.ro_task) + disabled_tasks_count = len(self.get_disabled_tasks(db, "FAILED")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + with patch.object(instance, "db_vims", db_vims_aws): + instance._process_pending_tasks(self.ro_task) + self.assertEqual( + len(self.get_disabled_tasks(db, "FAILED")), disabled_tasks_count + ) + + def test_process_pending_tasks_status_done_vim_aws_with_refresh(self): + """Refresh_active parameter is not equal to -1, task status is DONE.""" + # Task is not disabled to process as refresh_active parameter is not -1 + db = DbMemory() + self.config["period"]["refresh_active"] = 70 + self.ro_task["tasks"][0]["status"] = "DONE" + self.ro_task["to_check_at"] = 16373242400.994312 + db.create("ro_tasks", self.ro_task) + disabled_tasks_count = len(self.get_disabled_tasks(db, "DONE")) + instance = NsWorker(self.worker_index, self.config, self.plugins, db) + with patch.object(instance, "db_vims", db_vims_aws): + instance._process_pending_tasks(self.ro_task) + self.assertEqual( + len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count + ) + + @patch("osm_ng_ro.ns_thread.makedirs", return_value="") + def test_create_file_cert(self, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert ( + vim_config["config"]["ca_cert"] + == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert" + ) + + @patch("osm_ng_ro.ns_thread.makedirs") + @patch("osm_ng_ro.ns_thread.path") + def test_create_file_cert_exists(self, mock_path, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + mock_path.isdir.return_value = True + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + nsw._process_vim_config(target_id, vim_config) + mock_makedirs.assert_not_called() + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert ( + vim_config["config"]["ca_cert"] + == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert" + ) + + @patch("osm_ng_ro.ns_thread.path") + @patch("osm_ng_ro.ns_thread.makedirs", side_effect=Exception) + def test_create_file_cert_makedirs_except(self, mock_makedirs, mock_path): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + mock_path.isdir.return_value = False + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + with self.assertRaises(NsWorkerException): + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_not_called() + assert vim_config["config"]["ca_cert_content"] == "test" + + @patch("osm_ng_ro.ns_thread.makedirs", return_value="") + def test_create_file_cert_open_excepts(self, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + + with patch("builtins.open", mock_open()) as mocked_file: + mocked_file.side_effect = Exception + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + with self.assertRaises(NsWorkerException): + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert vim_config["config"]["ca_cert_content"] == "test" + + def test_get_next_refresh_vim_type_openstack(self): + next_refresh = 163535353434.3434 + result = self.instance._get_next_refresh(self.ro_task, next_refresh) + self.assertEqual(result, -1) + + def test_get_next_refresh_vim_type_openstack_refresh_disabled(self): + next_refresh = 163535353434.3434 + self.instance.refresh_config.active = -1 + result = self.instance._get_next_refresh(self.ro_task, next_refresh) + self.assertEqual(result, -1) + + def test_get_next_refresh_vim_type_aws_refresh_disabled(self): + self.db_vims = db_vims_aws + next_refresh = 163535353434.3434 + self.instance.refresh_config.active = -1 + result = self.instance._get_next_refresh(self.ro_task, next_refresh) + self.assertEqual(result, -1) + + def test_get_next_refresh_vim_type_aws(self): + self.instance.db_vims = db_vims_aws + next_refresh = 163535353434.3434 + self.instance.refresh_config.active = 140 + result = self.instance._get_next_refresh(self.ro_task, next_refresh) + self.assertEqual(result, next_refresh + 140) + class TestVimInteractionNet(unittest.TestCase): def setUp(self): @@ -802,6 +1143,7 @@ class TestVimInteractionNet(unittest.TestCase): db_vims = { "vim_openstack_1": { "config": {}, + "vim_type": "openstack", }, } instance = VimInteractionNet(db, logger, my_vims, db_vims) @@ -857,6 +1199,7 @@ class TestVimInteractionNet(unittest.TestCase): db_vims = { "vim_openstack_1": { "config": {}, + "vim_type": "openstack", }, } instance = VimInteractionNet(db, logger, my_vims, db_vims) @@ -913,6 +1256,7 @@ class TestVimInteractionNet(unittest.TestCase): db_vims = { "vim_openstack_1": { "config": {}, + "vim_type": "openstack", }, } instance = VimInteractionNet(db, logger, my_vims, db_vims) @@ -969,6 +1313,7 @@ class TestVimInteractionNet(unittest.TestCase): db_vims = { "vim_openstack_1": { "config": {}, + "vim_type": "openstack", }, } instance = VimInteractionNet(db, logger, my_vims, db_vims) @@ -1014,6 +1359,7 @@ class TestVimInteractionNet(unittest.TestCase): db_vims = { "vim_openstack_1": { "config": {}, + "vim_type": "openstack", }, } instance = VimInteractionNet(db, logger, my_vims, db_vims)