X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=functest%2Ftests%2Funit%2Futils%2Ftest_functest_utils.py;h=fbfa420aa797cdcc1ad3bb847e5bf13e713aaee3;hb=0914c7e7011b7ba3ed9eeae7f6c3605afb711640;hp=c4b566608c089896dc17add6cb4c14538b8f98f1;hpb=b58dd104d03db315df1d8c6607e6e8a2cf19aef5;p=functest.git diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index c4b566608..fbfa420aa 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -8,24 +8,22 @@ # http://www.apache.org/licenses/LICENSE-2.0 import logging +import pkg_resources import os import time import unittest -import urllib2 -from git.exc import NoSuchPathError import mock import requests +from six.moves import urllib from functest.tests.unit import test_utils -mock.patch('logging.FileHandler').start() # noqa from functest.utils import functest_utils +from functest.utils.constants import CONST class FunctestUtilsTesting(unittest.TestCase): - logging.disable(logging.CRITICAL) - def setUp(self): self.url = 'http://www.opnfv.org/' self.timeout = 5 @@ -34,6 +32,7 @@ class FunctestUtilsTesting(unittest.TestCase): self.installer = 'test_installer' self.scenario = 'test_scenario' self.build_tag = 'jenkins-functest-fuel-opnfv-jump-2-daily-master-190' + self.build_tag_week = 'jenkins-functest-fuel-baremetal-weekly-master-8' self.version = 'master' self.node_name = 'test_node_name' self.project = 'test_project' @@ -41,8 +40,8 @@ class FunctestUtilsTesting(unittest.TestCase): self.status = 'test_status' self.details = 'test_details' self.db_url = 'test_db_url' - self.success_rate = 2.0 - self.criteria = 'test_criteria==2.0' + self.criteria = 50 + self.result = 75 self.start_date = 1482624000 self.stop_date = 1482624000 self.start_time = time.time() @@ -54,37 +53,41 @@ class FunctestUtilsTesting(unittest.TestCase): self.cmd = 'test_cmd' self.output_file = 'test_output_file' self.testname = 'testname' - self.testcase_dict = {'name': 'testname', 'criteria': self.criteria} + self.testcase_dict = {'case_name': 'testname', + 'criteria': self.criteria} self.parameter = 'general.openstack.image_name' - self.config_yaml = 'test_config_yaml-' + self.config_yaml = pkg_resources.resource_filename( + 'functest', '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'}}} - @mock.patch('urllib2.urlopen', - side_effect=urllib2.URLError('no host given')) + @mock.patch('six.moves.urllib.request.urlopen', + side_effect=urllib.error.URLError('no host given')) def test_check_internet_connectivity_failed(self, mock_method): self.assertFalse(functest_utils.check_internet_connectivity()) mock_method.assert_called_once_with(self.url, timeout=self.timeout) - @mock.patch('urllib2.urlopen') + @mock.patch('six.moves.urllib.request.urlopen') def test_check_internet_connectivity_default(self, mock_method): self.assertTrue(functest_utils.check_internet_connectivity()) mock_method.assert_called_once_with(self.url, timeout=self.timeout) - @mock.patch('urllib2.urlopen') + @mock.patch('six.moves.urllib.request.urlopen') def test_check_internet_connectivity_debian(self, mock_method): self.url = "https://www.debian.org/" self.assertTrue(functest_utils.check_internet_connectivity(self.url)) mock_method.assert_called_once_with(self.url, timeout=self.timeout) - @mock.patch('urllib2.urlopen', - side_effect=urllib2.URLError('no host given')) + @mock.patch('six.moves.urllib.request.urlopen', + side_effect=urllib.error.URLError('no host given')) def test_download_url_failed(self, mock_url): self.assertFalse(functest_utils.download_url(self.url, self.dest_path)) - @mock.patch('urllib2.urlopen') + @mock.patch('six.moves.urllib.request.urlopen') def test_download_url_default(self, mock_url): - with mock.patch("__builtin__.open", mock.mock_open()) as m, \ + with mock.patch("six.moves.builtins.open", mock.mock_open()) as m, \ mock.patch('functest.utils.functest_utils.shutil.copyfileobj')\ as mock_sh: name = self.url.rsplit('/')[-1] @@ -94,27 +97,6 @@ class FunctestUtilsTesting(unittest.TestCase): m.assert_called_once_with(dest, 'wb') self.assertTrue(mock_sh.called) - def test_get_git_branch(self): - with mock.patch('functest.utils.functest_utils.Repo') as mock_repo: - mock_obj2 = mock.Mock() - attrs = {'name': 'test_branch'} - mock_obj2.configure_mock(**attrs) - - mock_obj = mock.Mock() - attrs = {'active_branch': mock_obj2} - mock_obj.configure_mock(**attrs) - - mock_repo.return_value = mock_obj - self.assertEqual(functest_utils.get_git_branch(self.repo_path), - 'test_branch') - - @mock.patch('functest.utils.functest_utils.Repo', - side_effect=NoSuchPathError) - def test_get_git_branch_failed(self, mock_repo): - self.assertRaises(NoSuchPathError, - lambda: functest_utils.get_git_branch(self.repo_path - )) - @mock.patch('functest.utils.functest_utils.logger.error') def test_get_installer_type_failed(self, mock_logger_error): with mock.patch.dict(os.environ, @@ -132,15 +114,17 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_installer_type(), self.installer) - @mock.patch('functest.utils.functest_utils.logger.error') - def test_get_scenario_failed(self, mock_logger_error): + @mock.patch('functest.utils.functest_utils.logger.info') + def test_get_scenario_failed(self, mock_logger_info): with mock.patch.dict(os.environ, {}, clear=True): self.assertEqual(functest_utils.get_scenario(), - "Unknown_scenario") - mock_logger_error.assert_called_once_with("Impossible to retrieve" - " the scenario") + "os-nosdn-nofeature-noha") + mock_logger_info.assert_called_once_with("Impossible to retrieve " + "the scenario.Use " + "default " + "os-nosdn-nofeature-noha") def test_get_scenario_default(self): with mock.patch.dict(os.environ, @@ -150,26 +134,36 @@ class FunctestUtilsTesting(unittest.TestCase): self.scenario) @mock.patch('functest.utils.functest_utils.get_build_tag') - def test_get_version_default(self, mock_get_build_tag): + def test_get_version_daily_job(self, mock_get_build_tag): mock_get_build_tag.return_value = self.build_tag self.assertEqual(functest_utils.get_version(), self.version) + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_weekly_job(self, mock_get_build_tag): + mock_get_build_tag.return_value = self.build_tag_week + self.assertEqual(functest_utils.get_version(), self.version) + + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_with_dummy_build_tag(self, mock_get_build_tag): + mock_get_build_tag.return_value = 'whatever' + self.assertEqual(functest_utils.get_version(), 'unknown') + @mock.patch('functest.utils.functest_utils.get_build_tag') def test_get_version_unknown(self, mock_get_build_tag): mock_get_build_tag.return_value = "unknown_build_tag" self.assertEqual(functest_utils.get_version(), "unknown") - @mock.patch('functest.utils.functest_utils.logger.error') - def test_get_pod_name_failed(self, mock_logger_error): + @mock.patch('functest.utils.functest_utils.logger.info') + def test_get_pod_name_failed(self, mock_logger_info): with mock.patch.dict(os.environ, {}, clear=True): self.assertEqual(functest_utils.get_pod_name(), "unknown-pod") - mock_logger_error.assert_called_once_with("Unable to retrieve " - "the POD name from " - "environment. Using " - "pod name 'unknown-pod'") + mock_logger_info.assert_called_once_with("Unable to retrieve " + "the POD name from " + "environment. Using " + "pod name 'unknown-pod'") def test_get_pod_name_default(self): with mock.patch.dict(os.environ, @@ -178,15 +172,15 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_pod_name(), self.node_name) - @mock.patch('functest.utils.functest_utils.logger.error') - def test_get_build_tag_failed(self, mock_logger_error): + @mock.patch('functest.utils.functest_utils.logger.info') + def test_get_build_tag_failed(self, mock_logger_info): with mock.patch.dict(os.environ, {}, clear=True): self.assertEqual(functest_utils.get_build_tag(), - "unknown_build_tag") - mock_logger_error.assert_called_once_with("Impossible to retrieve" - " the build tag") + "none") + mock_logger_info.assert_called_once_with("Impossible to retrieve" + " the build tag") def test_get_build_tag_default(self): with mock.patch.dict(os.environ, @@ -195,14 +189,9 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_build_tag(), self.build_tag) - @mock.patch('functest.utils.functest_utils.get_functest_config') - def test_get_db_url(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', @@ -210,9 +199,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( @@ -229,7 +216,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, @@ -247,16 +234,15 @@ 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, self.start_date, self.stop_date, - self.criteria, self.details) + self.result, self.details) mock_logger_error.assert_called_once_with("Please set env var: " + str("\'" + env_var + "\'")) @@ -273,32 +259,12 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_missing_buildtag(self): self._test_push_results_to_db_missing_env('BUILD_TAG') - def test_push_results_to_db_incorrect_buildtag(self): - dic = self._get_env_dict(None) - dic['BUILD_TAG'] = 'incorrect_build_tag' - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ - mock.patch('functest.utils.functest_utils.logger.error') \ - as mock_logger_error: - self.assertFalse(functest_utils. - push_results_to_db(self.project, self.case_name, - self.start_date, - self.stop_date, - self.criteria, self.details)) - mock_logger_error.assert_called_once_with("Please fix BUILD_TAG" - " env var: incorrect_" - "build_tag") - 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', @@ -307,7 +273,7 @@ class FunctestUtilsTesting(unittest.TestCase): push_results_to_db(self.project, self.case_name, self.start_date, self.stop_date, - self.criteria, self.details)) + self.result, self.details)) mock_logger_error.assert_called_once_with(test_utils. RegexMatch("Pushing " "Result to" @@ -317,11 +283,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', @@ -330,22 +295,21 @@ class FunctestUtilsTesting(unittest.TestCase): push_results_to_db(self.project, self.case_name, self.start_date, self.stop_date, - self.criteria, self.details)) + self.result, self.details)) self.assertTrue(mock_logger_error.called) 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, self.start_date, self.stop_date, - self.criteria, self.details)) + self.result, self.details)) readline = 0 test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] @@ -367,7 +331,7 @@ class FunctestUtilsTesting(unittest.TestCase): attrs = {'readline.side_effect': self.readline_side} m.configure_mock(**attrs) - with mock.patch("__builtin__.open") as mo: + with mock.patch("six.moves.builtins.open") as mo: mo.return_value = m self.assertEqual(functest_utils.get_resolvconf_ns(), self.test_ip[1:]) @@ -395,7 +359,8 @@ class FunctestUtilsTesting(unittest.TestCase): mock_logger_error): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open, \ - mock.patch('__builtin__.open', mock.mock_open()) as mopen: + mock.patch('six.moves.builtins.open', + mock.mock_open()) as mopen: FunctestUtilsTesting.readline = 0 @@ -424,7 +389,8 @@ class FunctestUtilsTesting(unittest.TestCase): ): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open, \ - mock.patch('__builtin__.open', mock.mock_open()) as mopen: + mock.patch('six.moves.builtins.open', + mock.mock_open()) as mopen: FunctestUtilsTesting.readline = 0 @@ -447,9 +413,8 @@ class FunctestUtilsTesting(unittest.TestCase): mock_logger_info.assert_called_once_with(msg_exec) mopen.assert_called_once_with(self.output_file, "w") - @mock.patch('functest.utils.functest_utils.logger.info') - def test_execute_command_args_missing_with_success(self, mock_logger_info, - ): + @mock.patch('sys.stdout') + def test_execute_command_args_missing_with_success(self, stdout=None): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open: @@ -471,9 +436,8 @@ class FunctestUtilsTesting(unittest.TestCase): output_file=None) self.assertEqual(resp, 0) - @mock.patch('functest.utils.functest_utils.logger.error') - def test_execute_command_args_missing_with_error(self, mock_logger_error, - ): + @mock.patch('sys.stdout') + def test_execute_command_args_missing_with_error(self, stdout=None): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open: @@ -499,7 +463,7 @@ class FunctestUtilsTesting(unittest.TestCase): @mock.patch('functest.utils.functest_utils.logger.error') def test_get_dict_by_test(self, mock_logger_error): - with mock.patch('__builtin__.open', mock.mock_open()), \ + with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ as mock_yaml, \ mock.patch('functest.utils.functest_utils.get_testcases_' @@ -527,7 +491,7 @@ class FunctestUtilsTesting(unittest.TestCase): def test_get_parameter_from_yaml_failed(self): self.file_yaml['general'] = None - with mock.patch('__builtin__.open', mock.mock_open()), \ + with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ as mock_yaml, \ self.assertRaises(ValueError) as excep: @@ -539,7 +503,7 @@ class FunctestUtilsTesting(unittest.TestCase): self.parameter) in excep.exception) def test_get_parameter_from_yaml_default(self): - with mock.patch('__builtin__.open', mock.mock_open()), \ + with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ as mock_yaml: mock_yaml.return_value = self.file_yaml @@ -557,32 +521,18 @@ class FunctestUtilsTesting(unittest.TestCase): assert_called_once_with(self.parameter, self.config_yaml) - def test_check_success_rate_default(self): - with mock.patch('functest.utils.functest_utils.get_criteria_by_test') \ - as mock_criteria: - mock_criteria.return_value = self.criteria - resp = functest_utils.check_success_rate(self.case_name, - self.success_rate) - self.assertEqual(resp, 'PASS') - - def test_check_success_rate_failed(self): - with mock.patch('functest.utils.functest_utils.get_criteria_by_test') \ - as mock_criteria: - mock_criteria.return_value = self.criteria - resp = functest_utils.check_success_rate(self.case_name, - 3.0) - self.assertEqual(resp, 'FAIL') - # 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('__builtin__.open', mock.mock_open()), \ + with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ as mock_yaml: mock_yaml.return_value = self.file_yaml @@ -597,4 +547,5 @@ class FunctestUtilsTesting(unittest.TestCase): if __name__ == "__main__": + logging.disable(logging.CRITICAL) unittest.main(verbosity=2)