diff options
author | Cedric Ollivier <cedric.ollivier@orange.com> | 2018-02-09 15:34:42 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-02-09 15:34:42 +0000 |
commit | 17049b67f1085492d83837da99ca245174d40db5 (patch) | |
tree | a99e523df04d36daae4ab0aea6c7b79ee8b994fd /functest/tests/unit | |
parent | 5aa9bd71ba9f2e135fcb7ad05952487dc6a74eb2 (diff) | |
parent | ab6ee580b80b6aacd39323c4668b14c81b54cbcf (diff) |
Merge "Unlink Energy from functest utils and constants"
Diffstat (limited to 'functest/tests/unit')
-rw-r--r-- | functest/tests/unit/energy/test_functest_energy.py | 82 |
1 files changed, 34 insertions, 48 deletions
diff --git a/functest/tests/unit/energy/test_functest_energy.py b/functest/tests/unit/energy/test_functest_energy.py index f0711ca0..fd110432 100644 --- a/functest/tests/unit/energy/test_functest_energy.py +++ b/functest/tests/unit/energy/test_functest_energy.py @@ -11,14 +11,14 @@ """Unitary test for energy module.""" # pylint: disable=unused-argument import logging -import requests +import os import unittest import mock +import requests from functest.energy.energy import EnergyRecorder import functest.energy.energy as energy -from functest.utils.constants import CONST CASE_NAME = "UNIT_TEST_CASE" STEP_NAME = "UNIT_TEST_STEP" @@ -61,26 +61,6 @@ RECORDER_NOT_FOUND = MockHttpResponse( ) -def config_loader_mock(config_key): - """Return mocked config values.""" - if config_key == "energy_recorder.api_url": - return "http://pod-uri:8888" - elif config_key == "energy_recorder.api_user": - return "user" - elif config_key == "energy_recorder.api_password": - return "password" - - -def config_loader_mock_no_creds(config_key): - """Return mocked config values.""" - if config_key == "energy_recorder.api_url": - return "http://pod-uri:8888" - elif config_key == "energy_recorder.api_user": - return "" - elif config_key == "energy_recorder.api_password": - return "" - - # pylint: disable=too-many-public-methods class EnergyRecorderTest(unittest.TestCase): """Energy module unitary test suite.""" @@ -90,6 +70,20 @@ class EnergyRecorderTest(unittest.TestCase): returned_value_to_preserve = "value" exception_message_to_preserve = "exception_message" + @staticmethod + def _set_env_creds(): + """Set config values.""" + os.environ["ENERGY_RECORDER_API_URL"] = "http://pod-uri:8888" + os.environ["ENERGY_RECORDER_API_USER"] = "user" + os.environ["ENERGY_RECORDER_API_PASSWORD"] = "password" + + @staticmethod + def _set_env_nocreds(): + """Set config values.""" + os.environ["ENERGY_RECORDER_API_URL"] = "http://pod-uri:8888" + del os.environ["ENERGY_RECORDER_API_USER"] + del os.environ["ENERGY_RECORDER_API_PASSWORD"] + @mock.patch('functest.energy.energy.requests.post', return_value=RECORDER_OK) def test_start(self, post_mock=None, get_mock=None): @@ -253,14 +247,12 @@ class EnergyRecorderTest(unittest.TestCase): return_value={"scenario": PREVIOUS_SCENARIO, "step": PREVIOUS_STEP}) @mock.patch("functest.energy.energy.EnergyRecorder") - @mock.patch("functest.utils.functest_utils.get_functest_config", - side_effect=config_loader_mock) def test_decorators_with_previous(self, - loader_mock=None, recorder_mock=None, cur_scenario_mock=None): """Test energy module decorators.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') + os.environ['NODE_NAME'] = 'MOCK_POD' + self._set_env_creds() self.__decorated_method() calls = [mock.call.start(self.case_name), mock.call.submit_scenario(PREVIOUS_SCENARIO, @@ -286,13 +278,12 @@ class EnergyRecorderTest(unittest.TestCase): ) self.assertTrue(finish_mock.called) - @mock.patch("functest.utils.functest_utils.get_functest_config", - side_effect=config_loader_mock) @mock.patch("functest.energy.energy.requests.get", return_value=API_OK) def test_load_config(self, loader_mock=None, get_mock=None): """Test load config.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') + os.environ['NODE_NAME'] = 'MOCK_POD' + self._set_env_creds() EnergyRecorder.energy_recorder_api = None EnergyRecorder.load_config() @@ -305,13 +296,12 @@ class EnergyRecorderTest(unittest.TestCase): "http://pod-uri:8888/recorders/environment/MOCK_POD" ) - @mock.patch("functest.utils.functest_utils.get_functest_config", - side_effect=config_loader_mock_no_creds) @mock.patch("functest.energy.energy.requests.get", return_value=API_OK) def test_load_config_no_creds(self, loader_mock=None, get_mock=None): """Test load config without creds.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') + os.environ['NODE_NAME'] = 'MOCK_POD' + self._set_env_nocreds() EnergyRecorder.energy_recorder_api = None EnergyRecorder.load_config() self.assertEquals(EnergyRecorder.energy_recorder_api["auth"], None) @@ -320,37 +310,33 @@ class EnergyRecorderTest(unittest.TestCase): "http://pod-uri:8888/recorders/environment/MOCK_POD" ) - @mock.patch("functest.utils.functest_utils.get_functest_config", - return_value=None) @mock.patch("functest.energy.energy.requests.get", return_value=API_OK) def test_load_config_ex(self, loader_mock=None, get_mock=None): """Test load config with exception.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') - with self.assertRaises(AssertionError): - EnergyRecorder.energy_recorder_api = None - EnergyRecorder.load_config() - self.assertEquals(EnergyRecorder.energy_recorder_api, None) - - @mock.patch("functest.utils.functest_utils.get_functest_config", - side_effect=config_loader_mock) + for key in ['NODE_NAME', 'ENERGY_RECORDER_API_URL']: + os.environ[key] = '' + with self.assertRaises(AssertionError): + EnergyRecorder.energy_recorder_api = None + EnergyRecorder.load_config() + self.assertEquals(EnergyRecorder.energy_recorder_api, None) + @mock.patch("functest.energy.energy.requests.get", return_value=API_KO) def test_load_config_api_ko(self, loader_mock=None, get_mock=None): """Test load config with API unavailable.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') + os.environ['NODE_NAME'] = 'MOCK_POD' + self._set_env_creds() EnergyRecorder.energy_recorder_api = None EnergyRecorder.load_config() self.assertEquals(EnergyRecorder.energy_recorder_api["available"], False) - @mock.patch("functest.utils.functest_utils.get_functest_config", - return_value=None) @mock.patch('functest.energy.energy.requests.get', return_value=RECORDER_OK) def test_get_current_scenario(self, loader_mock=None, get_mock=None): """Test get_current_scenario.""" - CONST.__setattr__('NODE_NAME', 'MOCK_POD') + os.environ['NODE_NAME'] = 'MOCK_POD' self.test_load_config() scenario = EnergyRecorder.get_current_scenario() self.assertTrue(scenario is not None) @@ -359,7 +345,7 @@ class EnergyRecorderTest(unittest.TestCase): 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') + os.environ['NODE_NAME'] = 'MOCK_POD' self.test_load_config() scenario = EnergyRecorder.get_current_scenario() self.assertTrue(scenario is None) @@ -368,7 +354,7 @@ class EnergyRecorderTest(unittest.TestCase): 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') + os.environ['NODE_NAME'] = 'MOCK_POD' self.test_load_config() scenario = EnergyRecorder.get_current_scenario() self.assertTrue(scenario is None) |