diff options
Diffstat (limited to 'sfc/unit_tests/unit/lib/test_cleanup.py')
-rw-r--r-- | sfc/unit_tests/unit/lib/test_cleanup.py | 225 |
1 files changed, 164 insertions, 61 deletions
diff --git a/sfc/unit_tests/unit/lib/test_cleanup.py b/sfc/unit_tests/unit/lib/test_cleanup.py index 5ec4261e..e6f59d23 100644 --- a/sfc/unit_tests/unit/lib/test_cleanup.py +++ b/sfc/unit_tests/unit/lib/test_cleanup.py @@ -14,8 +14,9 @@ import sfc.lib.cleanup as cleanup from mock import patch from mock import call -from mock import Mock from mock import DEFAULT +from mock import Mock + __author__ = "Dimitrios Markou <mardim@intracom-telecom.com>" @@ -262,33 +263,6 @@ class SfcCleanupTesting(unittest.TestCase): mock_log.assert_has_calls(log_calls) mock_del_vim.assert_has_calls(del_calls) - @patch('sfc.lib.cleanup.logger.error') - def test_delete_openstack_objects_exception(self, mock_log): - - """ - Check the proper functionality of the delete_openstack_objects - function when exception occurs. - """ - - mock_creator_obj_one = Mock() - mock_creator_obj_two = Mock() - exception_one = Exception('First Boom!') - exception_two = Exception('Second Boom!') - attrs_list = [{'clean.side_effect': exception_one}, - {'clean.side_effect': exception_two}] - - mock_creator_obj_one.configure_mock(**attrs_list[0]) - mock_creator_obj_two.configure_mock(**attrs_list[1]) - - mock_creator_objs_list = [mock_creator_obj_one, mock_creator_obj_two] - - log_calls = [call('Unexpected error cleaning - %s', exception_two), - call('Unexpected error cleaning - %s', exception_one)] - - cleanup.delete_openstack_objects(mock_creator_objs_list) - - mock_log.assert_has_calls(log_calls) - @patch('sfc.lib.openstack_utils.OpenStackSFC', autospec=True) def test_delete_untracked_security_groups(self, mock_obj): @@ -301,10 +275,7 @@ class SfcCleanupTesting(unittest.TestCase): def test_cleanup_odl(self, mock_del_odl_ietf, mock_del_odl_res): - resources = ['service-function-forwarder', - 'service-function-chain', - 'service-function-path', - 'service-function'] + resources = ['service-function-forwarder'] odl_res_calls = [call(self.odl_ip, self.odl_port, item) for item in resources] @@ -314,53 +285,185 @@ class SfcCleanupTesting(unittest.TestCase): mock_del_odl_res.assert_has_calls(odl_res_calls) mock_del_odl_ietf.assert_called_once_with(self.odl_ip, self.odl_port) + @patch('sfc.lib.openstack_utils.OpenStackSFC', autospec=True) + def test_cleanup_nsfc_objects(self, mock_os_sfc): + mock_os_sfc_ins = mock_os_sfc.return_value + cleanup.cleanup_nsfc_objects() + mock_os_sfc_ins.delete_chain.assert_called_once() + mock_os_sfc_ins.delete_port_groups.assert_called_once() + @patch('time.sleep') - @patch('sfc.lib.cleanup.delete_openstack_objects') - @patch('sfc.lib.cleanup.cleanup_odl') - def test_cleanup(self, - mock_cleanup_odl, - mock_del_os_obj, - mock_time): + def test_cleanup_tacker_objects(self, mock_time): mock_dict = {'delete_vnffgs': DEFAULT, 'delete_vnffgds': DEFAULT, 'delete_vnfs': DEFAULT, 'delete_vnfds': DEFAULT, - 'delete_vims': DEFAULT, - 'delete_untracked_security_groups': DEFAULT} + 'delete_vims': DEFAULT} with patch.multiple('sfc.lib.cleanup', **mock_dict) as mock_values: - - cleanup.cleanup(['creator_one', 'creator_two'], - self.odl_ip, - self.odl_port) + cleanup.cleanup_tacker_objects() for key in mock_values: mock_values[key].assert_called_once() + + mock_time.assert_called_once_with(20) + + @patch('sfc.lib.cleanup.cleanup_tacker_objects') + def test_cleanup_mano_objects_tacker(self, mock_cleanup_tacker): + cleanup.cleanup_mano_objects('tacker') + mock_cleanup_tacker.assert_called_once() + + @patch('sfc.lib.cleanup.cleanup_nsfc_objects') + def test_cleanup_mano_objects_nsfc(self, mock_cleanup_nsfc): + cleanup.cleanup_mano_objects('no-mano') + mock_cleanup_nsfc.assert_called_once() + + @patch('sfc.lib.cleanup.connection') + @patch('sfc.lib.cleanup.logger.info') + def test_delete_openstack_objects(self, mock_log, mock_conn): + """ + Checks the delete_chain method + """ + testcase_config = Mock() + conn = Mock() + mock_creator_obj_one = Mock() + mock_creator_obj_one.name = 'subnet_name' + mock_creator_obj_two = Mock() + mock_creator_obj_two.name = 'creator_name' + mock_creator_objs_list = [mock_creator_obj_one, mock_creator_obj_two] + + mock_conn.from_config.return_value = conn + testcase_config.subnet_name = mock_creator_obj_one.name + log_calls = [call('Deleting ' + mock_creator_obj_two.name), + call('Deleting ' + mock_creator_obj_one.name)] + + cleanup.delete_openstack_objects(testcase_config, + mock_creator_objs_list) + mock_creator_obj_one.delete.\ + assert_called_once_with(conn.session) + mock_creator_obj_two.delete.\ + assert_called_once_with(conn.session) + mock_log.assert_has_calls(log_calls) + + @patch('sfc.lib.cleanup.connection') + @patch('sfc.lib.cleanup.logger.info') + def test_delete_openstack_objects_router(self, mock_log, mock_conn): + """ + Checks the delete_chain method + """ + testcase_config = Mock() + conn = Mock() + mock_creator_obj = Mock() + mock_creator_obj.name = 'creator_name' + mock_creator_router = Mock() + mock_creator_router.name = 'router_name' + mock_creator_router.id = '1' + mock_creator_subnet = Mock() + mock_creator_subnet.name = 'subnet_name' + mock_creator_subnet.id = '2' + mock_creator_objs_list = [mock_creator_subnet, + mock_creator_router, + mock_creator_obj] + + mock_conn.from_config.return_value = conn + testcase_config.router_name = mock_creator_router.name + testcase_config.subnet_name = mock_creator_subnet.name + + conn.network.get_subnet.return_value = mock_creator_subnet + log_calls = [call('Deleting ' + mock_creator_obj.name), + call('Deleting ' + mock_creator_router.name), + call('Removing subnet from router'), + call('Deleting router'), + call('Deleting ' + mock_creator_subnet.name)] + + cleanup.delete_openstack_objects(testcase_config, + mock_creator_objs_list) + conn.network.remove_interface_from_router.\ + assert_called_once_with(mock_creator_router.id, + mock_creator_subnet.id) + conn.network.delete_router.\ + assert_called_once_with(mock_creator_router) + mock_creator_obj.delete.\ + assert_called_once_with(conn.session) + mock_creator_subnet.delete.\ + assert_called_once_with(conn.session) + mock_log.assert_has_calls(log_calls) + + @patch('sfc.lib.cleanup.connection') + @patch('sfc.lib.cleanup.logger.info') + @patch('sfc.lib.cleanup.logger.error') + def test_delete_openstack_objects_exception(self, mock_log_err, + mock_log_info, mock_conn): + """ + Check the proper functionality of the delete_openstack_objects + function when exception occurs. + """ + testcase_config = Mock() + conn = Mock() + mock_creator_obj_one = Mock() + mock_creator_obj_one.name = 'subnet_name' + mock_creator_obj_two = Mock() + mock_creator_obj_two.name = 'creator_name' + exception_one = Exception('First Boom!') + exception_two = Exception('Second Boom!') + attrs_list = [{'delete.side_effect': exception_one}, + {'delete.side_effect': exception_two}] + + mock_creator_obj_one.configure_mock(**attrs_list[0]) + mock_creator_obj_two.configure_mock(**attrs_list[1]) + + mock_creator_objs_list = [mock_creator_obj_one, mock_creator_obj_two] + mock_conn.from_config.return_value = conn + testcase_config.subnet_name = mock_creator_obj_one.name + + log_calls = [call('Deleting ' + mock_creator_obj_two.name), + call('Deleting ' + mock_creator_obj_one.name), + call('Unexpected error cleaning - %s', exception_two), + call('Unexpected error cleaning - %s', exception_one)] + + cleanup.delete_openstack_objects(testcase_config, + mock_creator_objs_list) + mock_creator_obj_one.delete.\ + assert_called_once_with(conn.session) + mock_creator_obj_two.delete.\ + assert_called_once_with(conn.session) + + mock_log_info.assert_has_calls(log_calls[:2]) + mock_log_err.assert_has_calls(log_calls[2:]) + + @patch('sfc.lib.cleanup.delete_untracked_security_groups') + @patch('sfc.lib.cleanup.cleanup_mano_objects') + @patch('sfc.lib.cleanup.delete_openstack_objects') + @patch('sfc.lib.cleanup.cleanup_odl') + def test_cleanup(self, + mock_cleanup_odl, + mock_del_os_obj, + mock_cleanup_mano, + mock_untr_sec_grps): + + cleanup.cleanup('testcase_config', ['creator_one', 'creator_two'], + 'mano', + self.odl_ip, + self.odl_port) + mock_cleanup_odl.assert_called_once_with(self.odl_ip, self.odl_port) - mock_del_os_obj.assert_called_once_with(['creator_one', 'creator_two']) - mock_time.assert_called_once_with(20) + mock_del_os_obj.assert_called_once_with('testcase_config', + ['creator_one', 'creator_two']) + mock_cleanup_mano.assert_called_once_with('mano') + mock_untr_sec_grps.assert_called_once() - @patch('time.sleep') + @patch('sfc.lib.cleanup.cleanup_mano_objects') @patch('sfc.lib.cleanup.cleanup_odl') def test_cleanup_from_bash(self, mock_cleanup_odl, - mock_time): + mock_cleanup_mano): - mock_dict = {'delete_vnffgs': DEFAULT, - 'delete_vnffgds': DEFAULT, - 'delete_vnfs': DEFAULT, - 'delete_vnfds': DEFAULT, - 'delete_vims': DEFAULT} - with patch.multiple('sfc.lib.cleanup', - **mock_dict) as mock_values: - - cleanup.cleanup_from_bash(self.odl_ip, - self.odl_port) + cleanup.cleanup_from_bash(self.odl_ip, + self.odl_port, + 'mano') - for key in mock_values: - mock_values[key].assert_called_once() mock_cleanup_odl.assert_called_once_with(self.odl_ip, self.odl_port) - mock_time.assert_called_once_with(20) + mock_cleanup_mano.assert_called_once_with(mano='mano') |