diff options
author | Cedric Ollivier <cedric.ollivier@orange.com> | 2017-12-12 16:40:18 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-12-12 16:40:18 +0000 |
commit | 5ed771a9a0dbc09610ecc900c0c3e86edc08eee6 (patch) | |
tree | cd4b471dcadae0c6c700a50726e8d3e9c8f1a727 /functest/tests | |
parent | 6e08695a88cd128bcd880d6b5d6763819cff8476 (diff) | |
parent | ca73d9e37e7969e71de0c7e80269a042c984ab3a (diff) |
Merge "Ensure that all energy modules are fully covered"
Diffstat (limited to 'functest/tests')
-rw-r--r-- | functest/tests/unit/energy/test_functest_energy.py | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/functest/tests/unit/energy/test_functest_energy.py b/functest/tests/unit/energy/test_functest_energy.py index 9e19a33d..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): @@ -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) |