From 8e6b5934deef23386b8047b2f30a481ce42d32b8 Mon Sep 17 00:00:00 2001 From: Linda Wang Date: Fri, 31 Mar 2017 02:46:48 +0000 Subject: Use CONST to retrieve db_url rather than getter function 1. Remove get_db_url() 2. If TEST_DB_URL declared in env, update test_db_url in config_functest.yaml file during env preparation 3. No matter TEST_DB_URL declared in env or not, Result_DB = CONST.__getattribute__("results_test_db_url") JIRA: FUNCTEST-763 Change-Id: I20da7ef98bbcc56731e0ff7061b05c1dde6cbfc3 Signed-off-by: Linda Wang --- functest/tests/unit/ci/test_prepare_env.py | 23 +++++++- functest/tests/unit/utils/test_decorators.py | 19 ++----- functest/tests/unit/utils/test_functest_utils.py | 70 +++++++++--------------- 3 files changed, 53 insertions(+), 59 deletions(-) (limited to 'functest/tests/unit') diff --git a/functest/tests/unit/ci/test_prepare_env.py b/functest/tests/unit/ci/test_prepare_env.py index 513e7230..85d1918d 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 44448f23..82291fa2 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 " @@ -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 12604c1a..d84a3201 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()), \ -- cgit 1.2.3-korg