diff options
author | Jose Lausuch <jose.lausuch@ericsson.com> | 2017-06-02 08:19:12 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-06-02 08:19:12 +0000 |
commit | 8fde61d643c17b012659d8343ab3c06526958a05 (patch) | |
tree | 58dd3f40df636e8726b07aa6598ff7c03544a54d | |
parent | cce3644b0b0186676de974d32e02219e2ae5dc50 (diff) | |
parent | 8e6b5934deef23386b8047b2f30a481ce42d32b8 (diff) |
Merge "Use CONST to retrieve db_url rather than getter function"
-rwxr-xr-x | functest/ci/prepare_env.py | 17 | ||||
-rw-r--r-- | functest/opnfv_tests/sdn/onos/teston/adapters/foundation.py | 3 | ||||
-rw-r--r-- | functest/tests/unit/ci/test_prepare_env.py | 23 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_decorators.py | 19 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 70 | ||||
-rw-r--r-- | functest/utils/functest_utils.py | 20 |
6 files changed, 74 insertions, 78 deletions
diff --git a/functest/ci/prepare_env.py b/functest/ci/prepare_env.py index 8e17a4fcc..e03bc6547 100755 --- a/functest/ci/prepare_env.py +++ b/functest/ci/prepare_env.py @@ -234,12 +234,15 @@ def source_rc_file(): CONST.__setattr__('OS_PASSWORD', value) -def patch_config_file(): +def update_config_file(): patch_file(CONFIG_PATCH_PATH) if pod_arch and pod_arch in arch_filter: patch_file(CONFIG_AARCH64_PATCH_PATH) + if "TEST_DB_URL" in os.environ: + update_db_url() + def patch_file(patch_file_path): logger.debug('Updating file: %s', patch_file_path) @@ -257,7 +260,15 @@ def patch_file(patch_file_path): os.remove(CONFIG_FUNCTEST_PATH) with open(CONFIG_FUNCTEST_PATH, "w") as f: f.write(yaml.dump(new_functest_yaml, default_style='"')) - f.close() + + +def update_db_url(): + with open(CONFIG_FUNCTEST_PATH) as f: + functest_yaml = yaml.safe_load(f) + + with open(CONFIG_FUNCTEST_PATH, "w") as f: + functest_yaml["results"]["test_db_url"] = os.environ.get('TEST_DB_URL') + f.write(yaml.dump(functest_yaml, default_style='"')) def verify_deployment(): @@ -381,7 +392,7 @@ def main(**kwargs): get_deployment_handler() create_directories() source_rc_file() - patch_config_file() + update_config_file() verify_deployment() install_rally() install_tempest() diff --git a/functest/opnfv_tests/sdn/onos/teston/adapters/foundation.py b/functest/opnfv_tests/sdn/onos/teston/adapters/foundation.py index f9eee7af6..aed98ee48 100644 --- a/functest/opnfv_tests/sdn/onos/teston/adapters/foundation.py +++ b/functest/opnfv_tests/sdn/onos/teston/adapters/foundation.py @@ -18,7 +18,6 @@ import re import time from functest.utils.constants import CONST -import functest.utils.functest_utils as ft_utils class Foundation(object): @@ -55,7 +54,7 @@ class Foundation(object): """ Get Default Parameters value """ - self.Result_DB = ft_utils.get_db_url() + self.Result_DB = CONST.__getattribute__("results_test_db_url") self.masterusername = CONST.__getattribute__('ONOS_onosbench_username') self.masterpassword = CONST.__getattribute__('ONOS_onosbench_password') self.agentusername = CONST.__getattribute__('ONOS_onoscli_username') diff --git a/functest/tests/unit/ci/test_prepare_env.py b/functest/tests/unit/ci/test_prepare_env.py index 513e72308..85d1918d8 100644 --- a/functest/tests/unit/ci/test_prepare_env.py +++ b/functest/tests/unit/ci/test_prepare_env.py @@ -20,6 +20,7 @@ class PrepareEnvTesting(unittest.TestCase): def setUp(self): self.prepare_envparser = prepare_env.PrepareEnvParser() + self.db_url_env = 'http://foo/testdb' @mock.patch('functest.ci.prepare_env.logger.info') def test_print_separator(self, mock_logger_info): @@ -297,6 +298,22 @@ class PrepareEnvTesting(unittest.TestCase): prepare_env.patch_file('test_file') self.assertTrue(m.called) + @mock.patch('functest.ci.prepare_env.ft_utils.get_functest_yaml', + return_value={'tkey1': 'tvalue1'}) + @mock.patch('functest.ci.prepare_env.yaml.safe_load', + return_value={'test_scenario': {'tkey': 'tvalue'}}) + @mock.patch('functest.ci.prepare_env.update_db_url') + def test_update_db_url(self, mock_db_url, mock_safe_load, + mock_get_functest_yaml): + CONST.__setattr__('DEPLOY_SCENARIO', 'default_scenario') + with mock.patch("__builtin__.open", mock.mock_open()), \ + mock.patch('functest.ci.prepare_env.yaml.dump'), \ + mock.patch.dict('functest.ci.prepare_env.os.environ', + {'TEST_DB_URL': self.db_url_env}, + clear=True): + prepare_env.update_config_file() + self.assertTrue(mock_db_url.called) + @mock.patch('functest.ci.prepare_env.logger.info') def test_verify_deployment_error(self, mock_logger_error): mock_popen = mock.Mock() @@ -418,14 +435,14 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.install_tempest') @mock.patch('functest.ci.prepare_env.install_rally') @mock.patch('functest.ci.prepare_env.verify_deployment') - @mock.patch('functest.ci.prepare_env.patch_config_file') + @mock.patch('functest.ci.prepare_env.update_config_file') @mock.patch('functest.ci.prepare_env.source_rc_file') @mock.patch('functest.ci.prepare_env.create_directories') @mock.patch('functest.ci.prepare_env.get_deployment_handler') @mock.patch('functest.ci.prepare_env.check_env_variables') @mock.patch('functest.ci.prepare_env.logger.info') def test_main_start(self, mock_logger_info, mock_env_var, mock_dep_handler, - mock_create_dir, mock_source_rc, mock_patch_config, + mock_create_dir, mock_source_rc, mock_update_config, mock_verify_depl, mock_install_rally, mock_install_temp, mock_create_flavor, mock_check_env, mock_print_info): @@ -438,7 +455,7 @@ class PrepareEnvTesting(unittest.TestCase): self.assertTrue(mock_dep_handler.called) self.assertTrue(mock_create_dir.called) self.assertTrue(mock_source_rc.called) - self.assertTrue(mock_patch_config.called) + self.assertTrue(mock_update_config.called) self.assertTrue(mock_verify_depl.called) self.assertTrue(mock_install_rally.called) self.assertTrue(mock_install_temp.called) diff --git a/functest/tests/unit/utils/test_decorators.py b/functest/tests/unit/utils/test_decorators.py index 44448f238..82291fa2d 100644 --- a/functest/tests/unit/utils/test_decorators.py +++ b/functest/tests/unit/utils/test_decorators.py @@ -20,6 +20,7 @@ import mock from functest.utils import decorators from functest.utils import functest_utils +from functest.utils.constants import CONST __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" @@ -65,34 +66,29 @@ class DecoratorsTesting(unittest.TestCase): 'details': {}, 'criteria': self._result} return json.dumps(data, sort_keys=True) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value='http://127.0.0.1') @mock.patch('{}.get_version'.format(functest_utils.__name__), return_value=VERSION) @mock.patch('requests.post') def test_http_shema(self, *args): + CONST.__setattr__('results_test_db_url', 'http://127.0.0.1') self.assertTrue(functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) args[1].assert_called_once_with() - args[2].assert_called_once_with() args[0].assert_called_once_with( 'http://127.0.0.1', data=self._get_json(), headers={'Content-Type': 'application/json'}) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value="/dev/null") - def test_wrong_shema(self, mock_method=None): + def test_wrong_shema(self): + CONST.__setattr__('results_test_db_url', '/dev/null') self.assertFalse(functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) - mock_method.assert_called_once_with() @mock.patch('{}.get_version'.format(functest_utils.__name__), return_value=VERSION) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value=URL) def _test_dump(self, *args): + CONST.__setattr__('results_test_db_url', URL) with mock.patch.object(decorators, 'open', mock.mock_open(), create=True) as mock_open: self.assertTrue(functest_utils.push_results_to_db( @@ -104,7 +100,6 @@ class DecoratorsTesting(unittest.TestCase): self.assertIn('POST', call_args[0]) self.assertIn(self._get_json(), call_args[0]) args[0].assert_called_once_with() - args[1].assert_called_once_with() @mock.patch('os.makedirs') def test_default_dump(self, mock_method=None): @@ -116,16 +111,14 @@ class DecoratorsTesting(unittest.TestCase): self._test_dump() mock_method.assert_called_once_with(DIR) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value=URL) @mock.patch('os.makedirs', side_effect=OSError) def test_makedirs_exc(self, *args): + CONST.__setattr__('results_test_db_url', URL) self.assertFalse( functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) args[0].assert_called_once_with(DIR) - args[1].assert_called_once_with() if __name__ == "__main__": diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 12604c1a0..d84a32013 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -19,6 +19,7 @@ from six.moves import urllib from functest.tests.unit import test_utils from functest.utils import functest_utils +from functest.utils.constants import CONST class FunctestUtilsTesting(unittest.TestCase): @@ -58,6 +59,7 @@ class FunctestUtilsTesting(unittest.TestCase): self.config_yaml = os.path.normpath(os.path.join(os.path.dirname( os.path.abspath(__file__)), '../../../ci/config_functest.yaml')) self.db_url_env = 'http://foo/testdb' + self.testcases_yaml = "test_testcases_yaml" self.file_yaml = {'general': {'openstack': {'image_name': 'test_image_name'}}} @@ -208,23 +210,9 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_build_tag(), self.build_tag) - def test_get_db_url_env_var(self): - with mock.patch.dict(os.environ, - {'TEST_DB_URL': self.db_url_env, - 'CONFIG_FUNCTEST_YAML': - "./functest/ci/config_functest.yaml"}, - clear=True): - self.assertEqual(functest_utils.get_db_url(), - self.db_url_env) - - @mock.patch('functest.utils.functest_utils.get_functest_config') - def test_get_db_url_default(self, mock_get_functest_config): - mock_get_functest_config.return_value = self.db_url - self.assertEqual(functest_utils.get_db_url(), self.db_url) - mock_get_functest_config.assert_called_once_with('results.test_db_url') - @mock.patch('functest.utils.functest_utils.logger.info') def test_logger_test_results(self, mock_logger_info): + CONST.__setattr__('results_test_db_url', self.db_url) with mock.patch('functest.utils.functest_utils.get_pod_name', return_value=self.node_name), \ mock.patch('functest.utils.functest_utils.get_scenario', @@ -232,9 +220,7 @@ class FunctestUtilsTesting(unittest.TestCase): mock.patch('functest.utils.functest_utils.get_version', return_value=self.version), \ mock.patch('functest.utils.functest_utils.get_build_tag', - return_value=self.build_tag), \ - mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url): + return_value=self.build_tag): functest_utils.logger_test_results(self.project, self.case_name, self.status, self.details) mock_logger_info.assert_called_once_with( @@ -251,7 +237,7 @@ class FunctestUtilsTesting(unittest.TestCase): "details:\t%(d)s\n" % {'p': self.project, 'n': self.case_name, - 'db': self.db_url, + 'db': CONST.__getattribute__('results_test_db_url'), 'pod': self.node_name, 'v': self.version, 's': self.scenario, @@ -269,11 +255,10 @@ class FunctestUtilsTesting(unittest.TestCase): def _test_push_results_to_db_missing_env(self, env_var): dic = self._get_env_dict(env_var) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error: functest_utils.push_results_to_db(self.project, self.case_name, @@ -297,11 +282,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_request_post_failed(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error, \ mock.patch('functest.utils.functest_utils.requests.post', @@ -320,11 +304,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_request_post_exception(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error, \ mock.patch('functest.utils.functest_utils.requests.post', @@ -338,11 +321,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_default(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.requests.post'): self.assertTrue(functest_utils. push_results_to_db(self.project, self.case_name, @@ -562,11 +544,13 @@ class FunctestUtilsTesting(unittest.TestCase): # TODO: merge_dicts - def test_get_testcases_file_dir(self): + @mock.patch('functest.utils.functest_utils.get_functest_config') + def test_get_testcases_file_dir(self, mock_get_functest_config): + mock_get_functest_config.return_value = self.testcases_yaml resp = functest_utils.get_testcases_file_dir() - self.assertEqual(resp, - "/home/opnfv/repos/functest/" - "functest/ci/testcases.yaml") + self.assertEqual(resp, self.testcases_yaml) + mock_get_functest_config.assert_called_once_with( + 'general.functest.testcases_yaml') def test_get_functest_yaml(self): with mock.patch('six.moves.builtins.open', mock.mock_open()), \ diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py index dc20eea10..995d94ff0 100644 --- a/functest/utils/functest_utils.py +++ b/functest/utils/functest_utils.py @@ -26,6 +26,7 @@ from git import Repo from functest.utils import constants from functest.utils import decorators +from functest.utils.constants import CONST logger = logging.getLogger(__name__) @@ -149,24 +150,15 @@ def get_build_tag(): return build_tag -def get_db_url(): +def logger_test_results(project, case_name, status, details): """ - Returns DB URL + Format test case results for the logger """ - # TODO use CONST mechanism - try: - # if TEST_DB_URL declared in env variable, use it! - db_url = os.environ['TEST_DB_URL'] - except KeyError: - db_url = get_functest_config('results.test_db_url') - return db_url - - -def logger_test_results(project, case_name, status, details): pod_name = get_pod_name() scenario = get_scenario() version = get_version() build_tag = get_build_tag() + db_url = CONST.__getattribute__("results_test_db_url") logger.info( "\n" @@ -182,7 +174,7 @@ def logger_test_results(project, case_name, status, details): "details:\t%(d)s\n" % {'p': project, 'n': case_name, - 'db': get_db_url(), + 'db': db_url, 'pod': pod_name, 'v': version, 's': scenario, @@ -198,7 +190,7 @@ def push_results_to_db(project, case_name, POST results to the Result target DB """ # Retrieve params from CI and conf - url = get_db_url() + url = CONST.__getattribute__("results_test_db_url") try: installer = os.environ['INSTALLER_TYPE'] |