diff options
author | Cédric Ollivier <cedric.ollivier@orange.com> | 2017-12-12 06:05:54 +0100 |
---|---|---|
committer | Benoit HERARD <benoit.herard@orange.com> | 2017-12-12 16:52:22 +0100 |
commit | ca73d9e37e7969e71de0c7e80269a042c984ab3a (patch) | |
tree | 35eb64fb5ca0e2588e9f4c38ffe930e50ae95335 /functest | |
parent | def49808fcca40d837f0a37cc23783af80198c01 (diff) |
Ensure that all energy modules are fully covered
Change-Id: I501a404d1202cc92da5e62dbf9db2aa453ffd324
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
Diffstat (limited to 'functest')
-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) |