diff options
Diffstat (limited to 'functest/tests')
-rw-r--r-- | functest/tests/unit/ci/test_run_tests.py | 50 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_os.py | 5 | ||||
-rw-r--r-- | functest/tests/unit/energy/test_functest_energy.py | 50 | ||||
-rw-r--r-- | functest/tests/unit/test_utils.py | 14 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_openstack_clean.py | 37 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_openstack_snapshot.py | 2 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_openstack_utils.py | 5 |
7 files changed, 87 insertions, 76 deletions
diff --git a/functest/tests/unit/ci/test_run_tests.py b/functest/tests/unit/ci/test_run_tests.py index b5af18c9..bc95f8f3 100644 --- a/functest/tests/unit/ci/test_run_tests.py +++ b/functest/tests/unit/ci/test_run_tests.py @@ -58,56 +58,6 @@ class RunTestsTesting(unittest.TestCase): self.run_tests_parser = run_tests.RunTestsParser() - self.config_file_yaml = {'general': { - 'openstack': { - 'image_name': 'test_image_name'}}, - 'results': { - 'test_db_url': 'url1'}} - self.config_file_patch_yaml = {'fdio': {'general': { - 'openstack': { - 'image_name': - 'test_image_name_2'}}}} - self.config_file_aarcg64_patch_yaml = {'os': {'general': { - 'openstack': {'image_name': 'test_image_name_3'}}}} - - @mock.patch('functest.ci.run_tests.Runner.patch_file') - def test_update_config_file_default(self, mock_patch): - self.runner.update_config_file() - mock_patch.assert_called() - - def test_patch_file_missing_file(self): - patch_file_path = "unexisting_file" - with self.assertRaises(IOError): - self.runner.patch_file(patch_file_path) - - @mock.patch('functest.ci.run_tests.ft_utils.merge_dicts') - @mock.patch('functest.ci.run_tests.ft_utils.get_functest_yaml') - def test_patch_file_default(self, *mock_methods): - CONST.__setattr__('DEPLOY_SCENARIO', 'os-nosdn-nofeature-noha') - with mock.patch( - 'six.moves.builtins.open', mock.mock_open()), mock.patch( - 'functest.ci.run_tests.yaml.safe_load') as yaml1, mock.patch( - 'functest.ci.run_tests.ft_utils.get_functest_yaml') as yaml2: - yaml1.return_value = self.config_file_patch_yaml - yaml2.return_value = self.config_file_yaml - self.runner.patch_file(yaml1) - mock_methods[1].assert_not_called() - mock_methods[0].assert_not_called() - - @mock.patch('functest.ci.run_tests.ft_utils.merge_dicts') - @mock.patch('functest.ci.run_tests.os.remove') - def test_patch_file_match_scenario(self, *mock_methods): - CONST.__setattr__('DEPLOY_SCENARIO', 'os-nosdn-fdio-noha') - with mock.patch( - 'six.moves.builtins.open', mock.mock_open()), mock.patch( - 'functest.ci.run_tests.yaml.safe_load') as yaml1, mock.patch( - 'functest.ci.run_tests.ft_utils.get_functest_yaml') as yaml2: - yaml1.return_value = self.config_file_patch_yaml - yaml2.return_value = self.config_file_yaml - self.runner.patch_file(yaml2) - mock_methods[1].assert_called() - mock_methods[0].assert_called() - @mock.patch('functest.ci.run_tests.logger.error') def test_source_rc_file_missing_file(self, mock_logger_error): with mock.patch('functest.ci.run_tests.os.path.isfile', diff --git a/functest/tests/unit/cli/commands/test_cli_os.py b/functest/tests/unit/cli/commands/test_cli_os.py index 806bc931..434370a5 100644 --- a/functest/tests/unit/cli/commands/test_cli_os.py +++ b/functest/tests/unit/cli/commands/test_cli_os.py @@ -82,7 +82,7 @@ class CliOpenStackTesting(unittest.TestCase): return_value=True) @mock.patch('functest.cli.commands.cli_os.click.echo') def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path): - with mock.patch('__builtin__.raw_input', return_value="y") \ + with mock.patch('six.moves.input', return_value="y") \ as mock_raw_input, \ mock.patch.object(self.cli_os, 'ping_endpoint'), \ mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \ @@ -111,7 +111,8 @@ class CliOpenStackTesting(unittest.TestCase): return_value=True) @mock.patch('functest.cli.commands.cli_os.click.echo') def test_snapshot_show_default(self, mock_click_echo, mock_os_path): - with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \ + with mock.patch('six.moves.builtins.open', + mock.mock_open(read_data='0')) \ as m: self.cli_os.snapshot_file = self.snapshot_file self.cli_os.snapshot_show() diff --git a/functest/tests/unit/energy/test_functest_energy.py b/functest/tests/unit/energy/test_functest_energy.py index 99110802..f0711ca0 100644 --- a/functest/tests/unit/energy/test_functest_energy.py +++ b/functest/tests/unit/energy/test_functest_energy.py @@ -11,6 +11,7 @@ """Unitary test for energy module.""" # pylint: disable=unused-argument import logging +import requests import unittest import mock @@ -54,6 +55,10 @@ RECORDER_KO = MockHttpResponse( '{"message": "An unhandled API exception occurred (MOCK)"}', 500 ) +RECORDER_NOT_FOUND = MockHttpResponse( + '{"message": "Recorder not found (MOCK)"}', + 404 +) def config_loader_mock(config_key): @@ -64,8 +69,6 @@ def config_loader_mock(config_key): return "user" elif config_key == "energy_recorder.api_password": return "password" - else: - raise Exception("Config not mocked") def config_loader_mock_no_creds(config_key): @@ -76,10 +79,9 @@ def config_loader_mock_no_creds(config_key): return "" elif config_key == "energy_recorder.api_password": return "" - else: - raise Exception("Config not mocked:" + config_key) +# pylint: disable=too-many-public-methods class EnergyRecorderTest(unittest.TestCase): """Energy module unitary test suite.""" @@ -116,6 +118,13 @@ class EnergyRecorderTest(unittest.TestCase): timeout=EnergyRecorder.CONNECTION_TIMEOUT ) + @mock.patch('functest.energy.energy.EnergyRecorder.load_config', + side_effect=Exception("Internal execution error (MOCK)")) + def test_start_exception(self, conf_loader_mock=None): + """EnergyRecorder.start test with exception during execution.""" + start_status = EnergyRecorder.start(CASE_NAME) + self.assertFalse(start_status) + @mock.patch('functest.energy.energy.requests.post', return_value=RECORDER_KO) def test_start_api_error(self, post_mock=None): @@ -172,6 +181,13 @@ class EnergyRecorderTest(unittest.TestCase): timeout=EnergyRecorder.CONNECTION_TIMEOUT ) + @mock.patch('functest.energy.energy.EnergyRecorder.load_config', + side_effect=requests.exceptions.ConnectionError()) + def test_set_step_connection_error(self, conf_loader_mock=None): + """EnergyRecorder.start test with exception during execution.""" + step_status = EnergyRecorder.set_step(STEP_NAME) + self.assertFalse(step_status) + @mock.patch('functest.energy.energy.requests.delete', return_value=RECORDER_OK) def test_stop(self, delete_mock=None): @@ -266,7 +282,7 @@ class EnergyRecorderTest(unittest.TestCase): with self.assertRaises(Exception) as context: self.__decorated_method_with_ex() self.assertTrue( - self.exception_message_to_preserve in context.exception + self.exception_message_to_preserve in str(context.exception) ) self.assertTrue(finish_mock.called) @@ -339,6 +355,30 @@ class EnergyRecorderTest(unittest.TestCase): scenario = EnergyRecorder.get_current_scenario() self.assertTrue(scenario is not None) + @mock.patch('functest.energy.energy.requests.get', + return_value=RECORDER_NOT_FOUND) + def test_current_scenario_not_found(self, get_mock=None): + """Test get current scenario not existing.""" + CONST.__setattr__('NODE_NAME', 'MOCK_POD') + self.test_load_config() + scenario = EnergyRecorder.get_current_scenario() + self.assertTrue(scenario is None) + + @mock.patch('functest.energy.energy.requests.get', + return_value=RECORDER_KO) + def test_current_scenario_api_error(self, get_mock=None): + """Test get current scenario with API error.""" + CONST.__setattr__('NODE_NAME', 'MOCK_POD') + self.test_load_config() + scenario = EnergyRecorder.get_current_scenario() + self.assertTrue(scenario is None) + + @mock.patch('functest.energy.energy.EnergyRecorder.load_config', + side_effect=Exception("Internal execution error (MOCK)")) + def test_current_scenario_exception(self, get_mock=None): + """Test get current scenario with exception.""" + scenario = EnergyRecorder.get_current_scenario() + self.assertTrue(scenario is None) if __name__ == "__main__": logging.disable(logging.CRITICAL) diff --git a/functest/tests/unit/test_utils.py b/functest/tests/unit/test_utils.py index e171db02..15904764 100644 --- a/functest/tests/unit/test_utils.py +++ b/functest/tests/unit/test_utils.py @@ -8,16 +8,22 @@ import re -class RegexMatch(str): +class RegexMatch(object): + def __init__(self, msg): + self.msg = msg + def __eq__(self, other): - match = re.search(self, other) + match = re.search(self.msg, other) if match: return True return False -class SubstrMatch(str): +class SubstrMatch(object): + def __init__(self, msg): + self.msg = msg + def __eq__(self, other): - if self in other: + if self.msg in other: return True return False diff --git a/functest/tests/unit/utils/test_openstack_clean.py b/functest/tests/unit/utils/test_openstack_clean.py index 6ae7faa4..afd9120a 100644 --- a/functest/tests/unit/utils/test_openstack_clean.py +++ b/functest/tests/unit/utils/test_openstack_clean.py @@ -73,8 +73,9 @@ class OSCleanTesting(unittest.TestCase): "-----------------" "---------") + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.debug') - def test_remove_instances(self, mock_logger_debug): + def test_remove_instances(self, mock_logger_debug, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=self.test_list): openstack_clean.remove_instances(self.client, self.update_list) @@ -83,16 +84,19 @@ class OSCleanTesting(unittest.TestCase): "instance and will " "NOT be deleted.") + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.debug') - def test_remove_instances_missing_instances(self, mock_logger_debug): + def test_remove_instances_missing_instances(self, mock_logger_debug, + *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=[]): openstack_clean.remove_instances(self.client, self.update_list) mock_logger_debug.assert_any_call("Removing Nova instances...") mock_logger_debug.assert_any_call("No instances found.") + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.debug') - def test_remove_instances_delete_success(self, mock_logger_debug): + def test_remove_instances_delete_success(self, mock_logger_debug, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=self.test_list), \ mock.patch('functest.utils.openstack_clean.os_utils' @@ -105,8 +109,10 @@ class OSCleanTesting(unittest.TestCase): " '\s*\S+'" " ...")) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.debug') - def test_remove_instances_pending_delete_success(self, mock_logger_debug): + def test_remove_instances_pending_delete_success(self, mock_logger_debug, + *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=self.deleted_list), \ mock.patch('functest.utils.openstack_clean.os_utils' @@ -118,8 +124,10 @@ class OSCleanTesting(unittest.TestCase): " '\s*\S+'" " ...").assert_not_called() + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.debug') - def test_remove_instances_other_delete_success(self, mock_logger_debug): + def test_remove_instances_other_delete_success(self, mock_logger_debug, + *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=self.other_list), \ mock.patch('functest.utils.openstack_clean.os_utils' @@ -132,10 +140,11 @@ class OSCleanTesting(unittest.TestCase): " '\s*\S+'" " ...")) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.logger.error') @mock.patch('functest.utils.openstack_clean.logger.debug') def test_remove_instances_delete_failed(self, mock_logger_debug, - mock_logger_error): + mock_logger_error, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_instances', return_value=self.test_list), \ mock.patch('functest.utils.openstack_clean.os_utils' @@ -276,7 +285,7 @@ class OSCleanTesting(unittest.TestCase): def test_remove_floatingips_delete_success(self, mock_logger_debug): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_floating_ips', - return_value=self.floatingips_list), \ + side_effect=[self.floatingips_list, None]), \ mock.patch('functest.utils.openstack_clean.os_utils' '.delete_floating_ip', return_value=True): openstack_clean.remove_floatingips(self.client, self.remove_list) @@ -306,12 +315,13 @@ class OSCleanTesting(unittest.TestCase): RegexMatch("Removing floating " "IP \s*\S+ ...")) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.remove_routers') @mock.patch('functest.utils.openstack_clean.remove_ports') @mock.patch('functest.utils.openstack_clean.logger.debug') def test_remove_networks(self, mock_logger_debug, mock_remove_ports, - mock_remove_routers): + mock_remove_routers, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_network_list', return_value=self.test_dict_list), \ @@ -331,12 +341,13 @@ class OSCleanTesting(unittest.TestCase): self.routers, self.update_list) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.remove_routers') @mock.patch('functest.utils.openstack_clean.remove_ports') @mock.patch('functest.utils.openstack_clean.logger.debug') def test_remove_networks_missing_networks(self, mock_logger_debug, mock_remove_ports, - mock_remove_routers): + mock_remove_routers, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_network_list', return_value=None), \ mock.patch('functest.utils.openstack_clean.os_utils' @@ -354,12 +365,13 @@ class OSCleanTesting(unittest.TestCase): self.routers, self.update_list) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.remove_routers') @mock.patch('functest.utils.openstack_clean.remove_ports') @mock.patch('functest.utils.openstack_clean.logger.debug') def test_remove_networks_delete_success(self, mock_logger_debug, mock_remove_ports, - mock_remove_routers): + mock_remove_routers, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_network_list', @@ -385,6 +397,7 @@ class OSCleanTesting(unittest.TestCase): self.routers, self.remove_list) + @mock.patch('time.sleep') @mock.patch('functest.utils.openstack_clean.remove_routers') @mock.patch('functest.utils.openstack_clean.remove_ports') @mock.patch('functest.utils.openstack_clean.logger.error') @@ -392,7 +405,7 @@ class OSCleanTesting(unittest.TestCase): def test_remove_networks_delete_failed(self, mock_logger_debug, mock_logger_error, mock_remove_ports, - mock_remove_routers): + mock_remove_routers, *args): with mock.patch('functest.utils.openstack_clean.os_utils' '.get_network_list', return_value=self.test_dict_list), \ @@ -711,7 +724,7 @@ class OSCleanTesting(unittest.TestCase): as mock_remove_tenants, \ mock.patch('functest.utils.openstack_clean.yaml.safe_load', return_value=mock.Mock()), \ - mock.patch('__builtin__.open', mock.mock_open()) as m: + mock.patch('six.moves.builtins.open', mock.mock_open()) as m: openstack_clean.main() self.assertTrue(mock_remove_instances) self.assertTrue(mock_remove_images) diff --git a/functest/tests/unit/utils/test_openstack_snapshot.py b/functest/tests/unit/utils/test_openstack_snapshot.py index 33e74609..919b28c6 100644 --- a/functest/tests/unit/utils/test_openstack_snapshot.py +++ b/functest/tests/unit/utils/test_openstack_snapshot.py @@ -222,7 +222,7 @@ class OSSnapshotTesting(unittest.TestCase): return_value=self.update_list), \ mock.patch('functest.utils.openstack_snapshot.get_tenants', return_value=self.update_list), \ - mock.patch('__builtin__.open', mock.mock_open()) as m: + mock.patch('six.moves.builtins.open', mock.mock_open()) as m: openstack_snapshot.main() mock_logger_info.assert_called_once_with("Generating OpenStack " "snapshot...") diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index 307cbe37..01085bb7 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -365,7 +365,8 @@ class OSUtilsTesting(unittest.TestCase): except: pass f = 'rc_file' - with mock.patch('__builtin__.open', mock.mock_open(read_data=msg), + with mock.patch('six.moves.builtins.open', + mock.mock_open(read_data=msg), create=True) as m: m.return_value.__iter__ = lambda self: iter(self.readline, '') openstack_utils.source_credentials(f) @@ -1460,7 +1461,7 @@ class OSUtilsTesting(unittest.TestCase): return_value=True), \ mock.patch('functest.utils.openstack_utils.get_image_id', return_value=''), \ - mock.patch('__builtin__.open', + mock.patch('six.moves.builtins.open', mock.mock_open(read_data='1')) as m: self.assertEqual(openstack_utils. create_glance_image(self.glance_client, |