diff options
Diffstat (limited to 'functest/tests')
23 files changed, 927 insertions, 440 deletions
diff --git a/functest/tests/unit/ci/test_generate_report.py b/functest/tests/unit/ci/test_generate_report.py deleted file mode 100644 index 2225586f..00000000 --- a/functest/tests/unit/ci/test_generate_report.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python - -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 - -import logging -import unittest -import urllib2 - -import mock - -from functest.ci import generate_report as gen_report -from functest.tests.unit import test_utils -from functest.utils import functest_utils as ft_utils -from functest.utils.constants import CONST - - -class GenerateReportTesting(unittest.TestCase): - - logging.disable(logging.CRITICAL) - - def test_init(self): - test_array = gen_report.init() - self.assertEqual(test_array, []) - - @mock.patch('functest.ci.generate_report.urllib2.urlopen', - side_effect=urllib2.URLError('no host given')) - def test_get_results_from_db_fail(self, mock_method): - url = "%s/results?build_tag=%s" % (ft_utils.get_db_url(), - CONST.BUILD_TAG) - self.assertIsNone(gen_report.get_results_from_db()) - mock_method.assert_called_once_with(url) - - @mock.patch('functest.ci.generate_report.urllib2.urlopen', - return_value={'results': []}) - def test_get_results_from_db_success(self, mock_method): - url = "%s/results?build_tag=%s" % (ft_utils.get_db_url(), - CONST.BUILD_TAG) - self.assertEqual(gen_report.get_results_from_db(), None) - mock_method.assert_called_once_with(url) - - def test_get_data(self): - self.assertIsInstance(gen_report.get_data({'result': ''}, ''), dict) - - def test_print_line_with_ci_run(self): - CONST.IS_CI_RUN = True - w1 = 'test_print_line' - test_str = ("| %s| %s| %s| %s| %s|\n" - % (w1.ljust(gen_report.COL_1_LEN - 1), - ''.ljust(gen_report.COL_2_LEN - 1), - ''.ljust(gen_report.COL_3_LEN - 1), - ''.ljust(gen_report.COL_4_LEN - 1), - ''.ljust(gen_report.COL_5_LEN - 1))) - self.assertEqual(gen_report.print_line(w1), test_str) - - def test_print_line_without_ci_run(self): - CONST.IS_CI_RUN = False - w1 = 'test_print_line' - test_str = ("| %s| %s| %s| %s|\n" - % (w1.ljust(gen_report.COL_1_LEN - 1), - ''.ljust(gen_report.COL_2_LEN - 1), - ''.ljust(gen_report.COL_3_LEN - 1), - ''.ljust(gen_report.COL_4_LEN - 1))) - self.assertEqual(gen_report.print_line(w1), test_str) - - def test_print_line_no_column_with_ci_run(self): - CONST.IS_CI_RUN = True - TOTAL_LEN = gen_report.COL_1_LEN + gen_report.COL_2_LEN - TOTAL_LEN += gen_report.COL_3_LEN + gen_report.COL_4_LEN + 2 - TOTAL_LEN += gen_report.COL_5_LEN + 1 - test_str = ("| %s|\n" % 'test'.ljust(TOTAL_LEN)) - self.assertEqual(gen_report.print_line_no_columns('test'), test_str) - - def test_print_line_no_column_without_ci_run(self): - CONST.IS_CI_RUN = False - TOTAL_LEN = gen_report.COL_1_LEN + gen_report.COL_2_LEN - TOTAL_LEN += gen_report.COL_3_LEN + gen_report.COL_4_LEN + 2 - test_str = ("| %s|\n" % 'test'.ljust(TOTAL_LEN)) - self.assertEqual(gen_report.print_line_no_columns('test'), test_str) - - def test_print_separator_with_ci_run(self): - CONST.IS_CI_RUN = True - test_str = ("+" + "=" * gen_report.COL_1_LEN + - "+" + "=" * gen_report.COL_2_LEN + - "+" + "=" * gen_report.COL_3_LEN + - "+" + "=" * gen_report.COL_4_LEN + - "+" + "=" * gen_report.COL_5_LEN) - test_str += '+\n' - self.assertEqual(gen_report.print_separator(), test_str) - - def test_print_separator_without_ci_run(self): - CONST.IS_CI_RUN = False - test_str = ("+" + "=" * gen_report.COL_1_LEN + - "+" + "=" * gen_report.COL_2_LEN + - "+" + "=" * gen_report.COL_3_LEN + - "+" + "=" * gen_report.COL_4_LEN) - test_str += "+\n" - self.assertEqual(gen_report.print_separator(), test_str) - - @mock.patch('functest.ci.generate_report.logger.info') - def test_main_with_ci_run(self, mock_method): - CONST.IS_CI_RUN = True - gen_report.main() - mock_method.assert_called_once_with(test_utils.SubstrMatch('URL')) - - @mock.patch('functest.ci.generate_report.logger.info') - def test_main_with_ci_loop(self, mock_method): - CONST.CI_LOOP = 'daily' - gen_report.main() - mock_method.assert_called_once_with(test_utils.SubstrMatch('CI LOOP')) - - @mock.patch('functest.ci.generate_report.logger.info') - def test_main_with_scenario(self, mock_method): - CONST.DEPLOY_SCENARIO = 'test_scenario' - gen_report.main() - mock_method.assert_called_once_with(test_utils.SubstrMatch('SCENARIO')) - - @mock.patch('functest.ci.generate_report.logger.info') - def test_main_with_build_tag(self, mock_method): - CONST.BUILD_TAG = 'test_build_tag' - gen_report.main() - mock_method.assert_called_once_with(test_utils. - SubstrMatch('BUILD TAG')) - - -if __name__ == "__main__": - unittest.main(verbosity=2) diff --git a/functest/tests/unit/ci/test_prepare_env.py b/functest/tests/unit/ci/test_prepare_env.py index 714dd13c..fbb59651 100644 --- a/functest/tests/unit/ci/test_prepare_env.py +++ b/functest/tests/unit/ci/test_prepare_env.py @@ -33,7 +33,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_missing_inst_type(self, mock_logger_warn, mock_logger_info): - CONST.INSTALLER_TYPE = None + CONST.__setattr__('INSTALLER_TYPE', None) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -44,7 +44,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_missing_inst_ip(self, mock_logger_warn, mock_logger_info): - CONST.INSTALLER_IP = None + CONST.__setattr__('INSTALLER_IP', None) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -61,7 +61,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_inst_ip(self, mock_logger_warn, mock_logger_info): - CONST.INSTALLER_IP = mock.Mock() + CONST.__setattr__('INSTALLER_IP', mock.Mock()) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -72,7 +72,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_missing_scenario(self, mock_logger_warn, mock_logger_info): - CONST.DEPLOY_SCENARIO = None + CONST.__setattr__('DEPLOY_SCENARIO', None) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -84,7 +84,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_scenario(self, mock_logger_warn, mock_logger_info): - CONST.DEPLOY_SCENARIO = 'test_scenario' + CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario') prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -95,7 +95,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_ci_debug(self, mock_logger_warn, mock_logger_info): - CONST.CI_DEBUG = mock.Mock() + CONST.__setattr__('CI_DEBUG', mock.Mock()) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -106,7 +106,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_node(self, mock_logger_warn, mock_logger_info): - CONST.NODE_NAME = mock.Mock() + CONST.__setattr__('NODE_NAME', mock.Mock()) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -117,7 +117,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_build_tag(self, mock_logger_warn, mock_logger_info): - CONST.BUILD_TAG = mock.Mock() + CONST.__setattr__('BUILD_TAG', mock.Mock()) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -129,7 +129,7 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.logger.warning') def test_check_env_variables_with_is_ci_run(self, mock_logger_warn, mock_logger_info): - CONST.IS_CI_RUN = mock.Mock() + CONST.__setattr__('IS_CI_RUN', mock.Mock()) prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -140,11 +140,11 @@ class PrepareEnvTesting(unittest.TestCase): def test_get_deployment_handler_missing_const_vars(self): with mock.patch('functest.ci.prepare_env.' 'factory.Factory.get_handler') as m: - CONST.INSTALLER_IP = None + CONST.__setattr__('INSTALLER_IP', None) prepare_env.get_deployment_handler() self.assertFalse(m.called) - CONST.INSTALLER_TYPE = None + CONST.__setattr__('INSTALLER_TYPE', None) prepare_env.get_deployment_handler() self.assertFalse(m.called) @@ -156,8 +156,8 @@ class PrepareEnvTesting(unittest.TestCase): mock.patch('functest.ci.prepare_env.' 'ft_utils.get_parameter_from_yaml', side_effect=ValueError): - CONST.INSTALLER_IP = 'test_ip' - CONST.INSTALLER_TYPE = 'test_inst_type' + CONST.__setattr__('INSTALLER_IP', 'test_ip') + CONST.__setattr__('INSTALLER_TYPE', 'test_inst_type') opnfv_constants.INSTALLERS = ['test_inst_type'] prepare_env.get_deployment_handler() msg = ('Printing deployment info is not supported for ' @@ -172,8 +172,8 @@ class PrepareEnvTesting(unittest.TestCase): side_effect=Exception), \ mock.patch('functest.ci.prepare_env.' 'ft_utils.get_parameter_from_yaml'): - CONST.INSTALLER_IP = 'test_ip' - CONST.INSTALLER_TYPE = 'test_inst_type' + CONST.__setattr__('INSTALLER_IP', 'test_ip') + CONST.__setattr__('INSTALLER_TYPE', 'test_inst_type') opnfv_constants.INSTALLERS = ['test_inst_type'] prepare_env.get_deployment_handler() self.assertTrue(mock_debug.called) @@ -188,12 +188,21 @@ class PrepareEnvTesting(unittest.TestCase): as mock_method: prepare_env.create_directories() mock_logger_info.assert_any_call("Creating needed directories...") - mock_method.assert_any_call(CONST.dir_functest_conf) - mock_method.assert_any_call(CONST.dir_functest_data) + mock_method.assert_any_call( + CONST.__getattribute__('dir_functest_conf')) + mock_method.assert_any_call( + CONST.__getattribute__('dir_functest_data')) + mock_method.assert_any_call( + CONST.__getattribute__('dir_functest_images')) mock_logger_info.assert_any_call(" %s created." % - CONST.dir_functest_conf) + CONST.__getattribute__( + 'dir_functest_conf')) mock_logger_info.assert_any_call(" %s created." % - CONST.dir_functest_data) + CONST.__getattribute__( + 'dir_functest_data')) + mock_logger_info.assert_any_call(" %s created." % + CONST.__getattribute__( + 'dir_functest_images')) @mock.patch('functest.ci.prepare_env.logger.info') @mock.patch('functest.ci.prepare_env.logger.debug') @@ -204,9 +213,14 @@ class PrepareEnvTesting(unittest.TestCase): prepare_env.create_directories() mock_logger_info.assert_any_call("Creating needed directories...") mock_logger_debug.assert_any_call(" %s already exists." % - CONST.dir_functest_conf) + CONST.__getattribute__( + 'dir_functest_conf')) + mock_logger_debug.assert_any_call(" %s already exists." % + CONST.__getattribute__( + 'dir_functest_data')) mock_logger_debug.assert_any_call(" %s already exists." % - CONST.dir_functest_data) + CONST.__getattribute__( + 'dir_functest_images')) def _get_env_cred_dict(self, os_prefix=''): return {'OS_USERNAME': os_prefix + 'username', @@ -230,24 +244,24 @@ class PrepareEnvTesting(unittest.TestCase): mock.patch('functest.ci.prepare_env.os.path.getsize', return_value=0), \ self.assertRaises(Exception): - CONST.openstack_creds = 'test_creds' + CONST.__setattr__('openstack_creds', 'test_creds') prepare_env.source_rc_file() def test_source_rc_missing_installer_ip(self): with mock.patch('functest.ci.prepare_env.os.path.isfile', return_value=False), \ self.assertRaises(Exception): - CONST.INSTALLER_IP = None - CONST.openstack_creds = 'test_creds' + CONST.__setattr__('INSTALLER_IP', None) + CONST.__setattr__('openstack_creds', 'test_creds') prepare_env.source_rc_file() def test_source_rc_missing_installer_type(self): with mock.patch('functest.ci.prepare_env.os.path.isfile', return_value=False), \ self.assertRaises(Exception): - CONST.INSTALLER_IP = 'test_ip' - CONST.openstack_creds = 'test_creds' - CONST.INSTALLER_TYPE = 'test_type' + CONST.__setattr__('INSTALLER_IP', 'test_ip') + CONST.__setattr__('openstack_creds', 'test_creds') + CONST.__setattr__('INSTALLER_TYPE', 'test_type') opnfv_constants.INSTALLERS = [] prepare_env.source_rc_file() @@ -259,9 +273,9 @@ class PrepareEnvTesting(unittest.TestCase): mock.patch('functest.ci.prepare_env.subprocess.Popen') \ as mock_subproc_popen, \ self.assertRaises(Exception): - CONST.openstack_creds = 'test_creds' - CONST.INSTALLER_IP = None - CONST.INSTALLER_TYPE = 'test_type' + CONST.__setattr__('openstack_creds', 'test_creds') + CONST.__setattr__('INSTALLER_IP', None) + CONST.__setattr__('INSTALLER_TYPE', 'test_type') opnfv_constants.INSTALLERS = ['test_type'] process_mock = mock.Mock() @@ -281,7 +295,7 @@ class PrepareEnvTesting(unittest.TestCase): return_value={'tkey1': 'tvalue1'}), \ mock.patch('functest.ci.prepare_env.os.remove') as m, \ mock.patch('functest.ci.prepare_env.yaml.dump'): - CONST.DEPLOY_SCENARIO = 'test_scenario' + CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario') prepare_env.patch_file('test_file') self.assertTrue(m.called) @@ -321,12 +335,12 @@ class PrepareEnvTesting(unittest.TestCase): cmd = "rally deployment destroy opnfv-rally" error_msg = "Deployment %s does not exist." % \ - CONST.rally_deployment_name + CONST.__getattribute__('rally_deployment_name') mock_logger_info.assert_any_call("Creating Rally environment...") mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False) cmd = "rally deployment create --file=rally_conf.json --name=" - cmd += CONST.rally_deployment_name + cmd += CONST.__getattribute__('rally_deployment_name') error_msg = "Problem while creating Rally deployment" mock_exec_raise.assert_any_call(cmd, error_msg=error_msg) @@ -352,7 +366,7 @@ class PrepareEnvTesting(unittest.TestCase): 'stdout.readline.return_value': '0'} mock_popen.configure_mock(**attrs) - CONST.tempest_deployment_name = 'test_dep_name' + CONST.__setattr__('tempest_deployment_name', 'test_dep_name') with mock.patch('functest.ci.prepare_env.' 'ft_utils.execute_command_raise', side_effect=Exception), \ @@ -379,7 +393,7 @@ class PrepareEnvTesting(unittest.TestCase): with mock.patch('functest.ci.prepare_env.os.path.isfile', return_value=False), \ self.assertRaises(Exception): - prepare_env.check_environment() + prepare_env.check_environment() @mock.patch('functest.ci.prepare_env.sys.exit') @mock.patch('functest.ci.prepare_env.logger.error') @@ -431,7 +445,8 @@ class PrepareEnvTesting(unittest.TestCase): self.assertTrue(mock_install_rally.called) self.assertTrue(mock_install_temp.called) self.assertTrue(mock_create_flavor.called) - m.assert_called_once_with(CONST.env_active, "w") + m.assert_called_once_with( + CONST.__getattribute__('env_active'), "w") self.assertTrue(mock_check_env.called) self.assertTrue(mock_print_info.called) diff --git a/functest/tests/unit/ci/test_run_tests.py b/functest/tests/unit/ci/test_run_tests.py index 7d02b1af..d0052392 100644 --- a/functest/tests/unit/ci/test_run_tests.py +++ b/functest/tests/unit/ci/test_run_tests.py @@ -62,24 +62,13 @@ class RunTestsTesting(unittest.TestCase): @mock.patch('functest.ci.run_tests.os_snapshot.main') def test_generate_os_snapshot(self, mock_os_snap): - run_tests.generate_os_snapshot() - self.assertTrue(mock_os_snap.called) + run_tests.generate_os_snapshot() + self.assertTrue(mock_os_snap.called) @mock.patch('functest.ci.run_tests.os_clean.main') def test_cleanup(self, mock_os_clean): - run_tests.cleanup() - self.assertTrue(mock_os_clean.called) - - def test_update_test_info(self): - run_tests.GlobalVariables.EXECUTED_TEST_CASES = [self.test] - run_tests.update_test_info('test_name', - 'test_result', - 'test_duration') - exp = self.test - exp.update({"result": 'test_result', - "duration": 'test_duration'}) - self.assertEqual(run_tests.GlobalVariables.EXECUTED_TEST_CASES, - [exp]) + run_tests.cleanup() + self.assertTrue(mock_os_clean.called) def test_get_run_dict_if_defined_default(self): mock_obj = mock.Mock() @@ -148,10 +137,8 @@ class RunTestsTesting(unittest.TestCase): mock.patch('functest.ci.run_tests.source_rc_file'), \ mock.patch('functest.ci.run_tests.generate_os_snapshot'), \ mock.patch('functest.ci.run_tests.cleanup'), \ - mock.patch('functest.ci.run_tests.update_test_info'), \ mock.patch('functest.ci.run_tests.get_run_dict', return_value=test_run_dict), \ - mock.patch('functest.ci.run_tests.generate_report.main'), \ self.assertRaises(run_tests.BlockingTestFailed) as context: run_tests.GlobalVariables.CLEAN_FLAG = True run_tests.run_test(mock_test, 'tier_name') @@ -176,21 +163,17 @@ class RunTestsTesting(unittest.TestCase): @mock.patch('functest.ci.run_tests.logger.info') def test_run_all_default(self, mock_logger_info): - with mock.patch('functest.ci.run_tests.run_tier') as mock_method, \ - mock.patch('functest.ci.run_tests.generate_report.init'), \ - mock.patch('functest.ci.run_tests.generate_report.main'): - CONST.CI_LOOP = 'test_ci_loop' + with mock.patch('functest.ci.run_tests.run_tier') as mock_method: + CONST.__setattr__('CI_LOOP', 'test_ci_loop') run_tests.run_all(self.tiers) mock_method.assert_any_call(self.tier) self.assertTrue(mock_logger_info.called) @mock.patch('functest.ci.run_tests.logger.info') - def test_run_all__missing_tier(self, mock_logger_info): - with mock.patch('functest.ci.run_tests.generate_report.init'), \ - mock.patch('functest.ci.run_tests.generate_report.main'): - CONST.CI_LOOP = 'loop_re_not_available' - run_tests.run_all(self.tiers) - self.assertTrue(mock_logger_info.called) + def test_run_all_missing_tier(self, mock_logger_info): + CONST.__setattr__('CI_LOOP', 'loop_re_not_available') + run_tests.run_all(self.tiers) + self.assertTrue(mock_logger_info.called) def test_main_failed(self): kwargs = {'test': 'test_name', 'noclean': True, 'report': True} @@ -221,7 +204,6 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.tb.TierBuilder', return_value=mock_obj), \ mock.patch('functest.ci.run_tests.source_rc_file'), \ - mock.patch('functest.ci.run_tests.generate_report.init'), \ mock.patch('functest.ci.run_tests.run_tier') as m: self.assertEqual(run_tests.main(**kwargs), run_tests.Result.EX_OK) @@ -234,7 +216,6 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.tb.TierBuilder', return_value=mock_obj), \ mock.patch('functest.ci.run_tests.source_rc_file'), \ - mock.patch('functest.ci.run_tests.generate_report.init'), \ mock.patch('functest.ci.run_tests.run_test') as m: self.assertEqual(run_tests.main(**kwargs), run_tests.Result.EX_OK) @@ -248,7 +229,6 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.tb.TierBuilder', return_value=mock_obj), \ mock.patch('functest.ci.run_tests.source_rc_file'), \ - mock.patch('functest.ci.run_tests.generate_report.init'), \ mock.patch('functest.ci.run_tests.run_all') as m: self.assertEqual(run_tests.main(**kwargs), run_tests.Result.EX_OK) @@ -262,11 +242,11 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.tb.TierBuilder', return_value=mock_obj), \ mock.patch('functest.ci.run_tests.source_rc_file'), \ - mock.patch('functest.ci.run_tests.generate_report.init'), \ mock.patch('functest.ci.run_tests.logger.debug') as m: self.assertEqual(run_tests.main(**kwargs), run_tests.Result.EX_ERROR) self.assertTrue(m.called) + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/functest/tests/unit/ci/test_tier_builder.py b/functest/tests/unit/ci/test_tier_builder.py index 438fa7c2..feaf33a8 100644 --- a/functest/tests/unit/ci/test_tier_builder.py +++ b/functest/tests/unit/ci/test_tier_builder.py @@ -22,6 +22,7 @@ class TierBuilderTesting(unittest.TestCase): 'scenario': 'test_scenario'} self.testcase = {'dependencies': self.dependency, + 'enabled': 'true', 'case_name': 'test_name', 'criteria': 'test_criteria', 'blocking': 'test_blocking', @@ -78,6 +79,13 @@ class TierBuilderTesting(unittest.TestCase): self.assertEqual(self.tierbuilder.get_tests('test_tier2'), None) + def test_get_tier_name_ok(self): + self.assertEqual(self.tierbuilder.get_tier_name('test_name'), + 'test_tier') + + def test_get_tier_name_ko(self): + self.assertEqual(self.tierbuilder.get_tier_name('test_name2'), None) + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/functest/tests/unit/ci/test_tier_handler.py b/functest/tests/unit/ci/test_tier_handler.py index 21df4098..28006274 100644 --- a/functest/tests/unit/ci/test_tier_handler.py +++ b/functest/tests/unit/ci/test_tier_handler.py @@ -32,6 +32,7 @@ class TierHandlerTesting(unittest.TestCase): 'test_ci_loop', description='test_desc') self.testcase = tier_handler.TestCase('test_name', + 'true', self.mock_depend, 'test_criteria', 'test_blocking', @@ -116,6 +117,10 @@ class TierHandlerTesting(unittest.TestCase): self.assertEqual(self.tier.get_name(), 'test_tier') + def test_testcase_is_enabled(self): + self.assertEqual(self.testcase.is_enabled(), + 'true') + def test_testcase_get_criteria(self): self.assertEqual(self.tier.get_order(), 'test_order') diff --git a/functest/tests/unit/cli/commands/test_cli_env.py b/functest/tests/unit/cli/commands/test_cli_env.py index 4b6ea57a..c3d89ea3 100644 --- a/functest/tests/unit/cli/commands/test_cli_env.py +++ b/functest/tests/unit/cli/commands/test_cli_env.py @@ -28,7 +28,7 @@ class CliEnvTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') def test_prepare_default(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/prepare_env.py start" % - CONST.dir_repo_functest) + CONST.__getattribute__('dir_repo_functest')) self.cli_environ.prepare() mock_ft_utils.assert_called_with(cmd) @@ -40,29 +40,30 @@ class CliEnvTesting(unittest.TestCase): mock.patch('functest.cli.commands.cli_testcase.os.remove') \ as mock_os_remove: cmd = ("python %s/functest/ci/prepare_env.py start" % - CONST.dir_repo_functest) + CONST.__getattribute__('dir_repo_functest')) self.cli_environ.prepare() - mock_os_remove.assert_called_once_with(CONST.env_active) + mock_os_remove.assert_called_once_with( + CONST.__getattribute__('env_active')) mock_ft_utils.assert_called_with(cmd) def _test_show_missing_env_var(self, var, *args): if var == 'INSTALLER_TYPE': - CONST.INSTALLER_TYPE = None + CONST.__setattr__('INSTALLER_TYPE', None) reg_string = "| INSTALLER: Unknown, \S+\s*|" elif var == 'INSTALLER_IP': - CONST.INSTALLER_IP = None + CONST.__setattr__('INSTALLER_IP', None) reg_string = "| INSTALLER: \S+, Unknown\s*|" elif var == 'SCENARIO': - CONST.DEPLOY_SCENARIO = None + CONST.__setattr__('DEPLOY_SCENARIO', None) reg_string = "| SCENARIO: Unknown\s*|" elif var == 'NODE': - CONST.NODE_NAME = None + CONST.__setattr__('NODE_NAME', None) reg_string = "| POD: Unknown\s*|" elif var == 'BUILD_TAG': - CONST.BUILD_TAG = None + CONST.__setattr__('BUILD_TAG', None) reg_string = "| BUILD TAG: None|" elif var == 'DEBUG': - CONST.CI_DEBUG = None + CONST.__setattr__('CI_DEBUG', None) reg_string = "| DEBUG FLAG: false\s*|" elif var == 'STATUS': reg_string = "| STATUS: not ready\s*|" @@ -106,7 +107,7 @@ class CliEnvTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_env.os.path.exists', return_value=False) def test_show_missing_git_repo_dir(self, *args): - CONST.dir_repo_functest = None + CONST.__setattr__('dir_repo_functest', None) self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show()) @mock.patch('functest.cli.commands.cli_env.click.echo') diff --git a/functest/tests/unit/cli/commands/test_cli_os.py b/functest/tests/unit/cli/commands/test_cli_os.py index f0e58c67..54042769 100644 --- a/functest/tests/unit/cli/commands/test_cli_os.py +++ b/functest/tests/unit/cli/commands/test_cli_os.py @@ -69,10 +69,10 @@ class CliOpenStackTesting(unittest.TestCase): def test_fetch_credentials_default(self, mock_click_echo, mock_os_path, mock_ftutils_execute): - CONST.INSTALLER_TYPE = self.installer_type - CONST.INSTALLER_IP = self.installer_ip + CONST.__setattr__('INSTALLER_TYPE', self.installer_type) + CONST.__setattr__('INSTALLER_IP', self.installer_ip) cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" - % (CONST.dir_repos, + % (CONST.__getattribute__('dir_repos'), self.openstack_creds, self.installer_type, self.installer_ip)) @@ -92,15 +92,13 @@ class CliOpenStackTesting(unittest.TestCase): def test_fetch_credentials_missing_installer_type(self, mock_click_echo, mock_os_path, mock_ftutils_execute): - installer_type = None - installer_ip = self.installer_ip - CONST.INSTALLER_TYPE = installer_type - CONST.INSTALLER_IP = installer_ip + CONST.__setattr__('INSTALLER_TYPE', None) + CONST.__setattr__('INSTALLER_IP', self.installer_ip) cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" - % (CONST.dir_repos, + % (CONST.__getattribute__('dir_repos'), self.openstack_creds, - installer_type, - installer_ip)) + None, + self.installer_ip)) self.cli_os.openstack_creds = self.openstack_creds self.cli_os.fetch_credentials() mock_click_echo.assert_any_call("The environment variable " @@ -109,8 +107,8 @@ class CliOpenStackTesting(unittest.TestCase): mock_click_echo.assert_any_call("Fetching credentials from " "installer node '%s' with " "IP=%s.." % - (installer_type, - installer_ip)) + (None, + self.installer_ip)) mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') @@ -122,10 +120,10 @@ class CliOpenStackTesting(unittest.TestCase): mock_ftutils_execute): installer_type = self.installer_type installer_ip = None - CONST.INSTALLER_TYPE = installer_type - CONST.INSTALLER_IP = installer_ip + CONST.__setattr__('INSTALLER_TYPE', installer_type) + CONST.__setattr__('INSTALLER_IP', installer_ip) cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" - % (CONST.dir_repos, + % (CONST.__getattribute__('dir_repos'), self.openstack_creds, installer_type, installer_ip)) @@ -144,8 +142,9 @@ class CliOpenStackTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') def test_check(self, mock_ftutils_execute): with mock.patch.object(self.cli_os, 'ping_endpoint'): - CONST.dir_repo_functest = self.dir_repo_functest - cmd = CONST.dir_repo_functest + "/functest/ci/check_os.sh" + CONST.__setattr__('dir_repo_functest', self.dir_repo_functest) + cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'), + "functest/ci/check_os.sh") self.cli_os.check() mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) diff --git a/functest/tests/unit/cli/commands/test_cli_testcase.py b/functest/tests/unit/cli/commands/test_cli_testcase.py index 39c8139d..2d09514e 100644 --- a/functest/tests/unit/cli/commands/test_cli_testcase.py +++ b/functest/tests/unit/cli/commands/test_cli_testcase.py @@ -42,7 +42,9 @@ class CliTestCasesTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') def test_run_default(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-n -r ", self.testname)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-n -r ", self.testname)) self.cli_tests.run(self.testname, noclean=True, report=True) mock_ft_utils.assert_called_with(cmd) @@ -51,7 +53,9 @@ class CliTestCasesTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') def test_run_noclean_missing_report(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-n ", self.testname)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-n ", self.testname)) self.cli_tests.run(self.testname, noclean=True, report=False) mock_ft_utils.assert_called_with(cmd) @@ -60,7 +64,9 @@ class CliTestCasesTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') def test_run_report_missing_noclean(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-r ", self.testname)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-r ", self.testname)) self.cli_tests.run(self.testname, noclean=False, report=True) mock_ft_utils.assert_called_with(cmd) @@ -69,7 +75,9 @@ class CliTestCasesTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') def test_run_missing_noclean_report(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "", self.testname)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "", self.testname)) self.cli_tests.run(self.testname, noclean=False, report=False) mock_ft_utils.assert_called_with(cmd) diff --git a/functest/tests/unit/cli/commands/test_cli_tier.py b/functest/tests/unit/cli/commands/test_cli_tier.py index 802359f1..fbc75253 100644 --- a/functest/tests/unit/cli/commands/test_cli_tier.py +++ b/functest/tests/unit/cli/commands/test_cli_tier.py @@ -90,8 +90,9 @@ class CliTierTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') def test_run_default(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-n -r ", - self.tiername)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-n -r ", self.tiername)) self.cli_tier.run(self.tiername, noclean=True, report=True) mock_ft_utils.assert_called_with(cmd) @@ -100,8 +101,9 @@ class CliTierTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') def test_run_report_missing_noclean(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-r ", - self.tiername)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-r ", self.tiername)) self.cli_tier.run(self.tiername, noclean=False, report=True) mock_ft_utils.assert_called_with(cmd) @@ -110,8 +112,9 @@ class CliTierTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') def test_run_noclean_missing_report(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "-n ", - self.tiername)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "-n ", self.tiername)) self.cli_tier.run(self.tiername, noclean=True, report=False) mock_ft_utils.assert_called_with(cmd) @@ -120,8 +123,9 @@ class CliTierTesting(unittest.TestCase): @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') def test_run_missing_noclean_report(self, mock_ft_utils, mock_os): cmd = ("python %s/functest/ci/run_tests.py " - "%s -t %s" % (CONST.dir_repo_functest, "", - self.tiername)) + "%s -t %s" % + (CONST.__getattribute__('dir_repo_functest'), + "", self.tiername)) self.cli_tier.run(self.tiername, noclean=False, report=False) mock_ft_utils.assert_called_with(cmd) diff --git a/functest/tests/unit/core/test_feature.py b/functest/tests/unit/core/test_feature.py index 993da5a0..8de42ec5 100644 --- a/functest/tests/unit/core/test_feature.py +++ b/functest/tests/unit/core/test_feature.py @@ -28,7 +28,7 @@ class FeatureTestingBase(unittest.TestCase): _project_name = "bar" _repo = "dir_repo_copper" _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -" - _output_file = '/home/opnfv/functest/results/bar.log' + _output_file = '/home/opnfv/functest/results/foo.log' feature = None @mock.patch('time.time', side_effect=[1, 2]) diff --git a/functest/tests/unit/core/test_pytest_suite_runner.py b/functest/tests/unit/core/test_pytest_suite_runner.py new file mode 100644 index 00000000..15e5bd73 --- /dev/null +++ b/functest/tests/unit/core/test_pytest_suite_runner.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +# pylint: disable=missing-docstring + +import logging +import unittest + +import mock + +from functest.core import pytest_suite_runner +from functest.core import testcase + + +class PyTestSuiteRunnerTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.psrunner = pytest_suite_runner.PyTestSuiteRunner() + self.result = mock.Mock() + attrs = {'errors': [('test1', 'error_msg1')], + 'failures': [('test2', 'failure_msg1')]} + self.result.configure_mock(**attrs) + + self.pass_results = mock.Mock() + attrs = {'errors': None, + 'failures': None} + self.pass_results.configure_mock(**attrs) + + def test_run(self): + self.psrunner.case_name = 'test_case_name' + with mock.patch('functest.core.pytest_suite_runner.' + 'unittest.TextTestRunner.run', + return_value=self.result): + self.assertEqual(self.psrunner.run(), + testcase.TestCase.EX_OK) + + with mock.patch('functest.core.pytest_suite_runner.' + 'unittest.TextTestRunner.run', + return_value=self.pass_results): + self.assertEqual(self.psrunner.run(), + testcase.TestCase.EX_OK) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_testcase.py b/functest/tests/unit/core/test_testcase.py index cc8446d8..2adf4a6d 100644 --- a/functest/tests/unit/core/test_testcase.py +++ b/functest/tests/unit/core/test_testcase.py @@ -7,7 +7,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 -"""Define the classe required to fully cover testcase.""" +"""Define the class required to fully cover testcase.""" import logging import unittest @@ -20,9 +20,8 @@ __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" class TestCaseTesting(unittest.TestCase): - """The class testing TestCase.""" - # pylint: disable=missing-docstring + # pylint: disable=missing-docstring,too-many-public-methods logging.disable(logging.CRITICAL) @@ -116,12 +115,12 @@ class TestCaseTesting(unittest.TestCase): def test_check_criteria_missing(self): self.test.criteria = None - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) def test_check_result_missing(self): self.test.result = None - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) def test_check_result_failed(self): @@ -129,7 +128,7 @@ class TestCaseTesting(unittest.TestCase): # It must be removed as soon as TestCase subclasses # stop setting result = 'PASS' or 'FAIL'. self.test.result = 'FAIL' - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) def test_check_result_pass(self): @@ -137,31 +136,93 @@ class TestCaseTesting(unittest.TestCase): # It must be removed as soon as TestCase subclasses # stop setting result = 'PASS' or 'FAIL'. self.test.result = 'PASS' - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_OK) def test_check_result_lt(self): self.test.result = 50 - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) def test_check_result_eq(self): self.test.result = 100 - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_OK) def test_check_result_gt(self): self.test.criteria = 50 self.test.result = 100 - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_OK) def test_check_result_zero(self): self.test.criteria = 0 self.test.result = 0 - self.assertEqual(self.test.check_result(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) + def test_get_duration_start_ko(self): + self.test.start_time = None + self.assertEqual(self.test.get_duration(), "XX:XX") + self.test.start_time = 0 + self.assertEqual(self.test.get_duration(), "XX:XX") + + def test_get_duration_end_ko(self): + self.test.stop_time = None + self.assertEqual(self.test.get_duration(), "XX:XX") + self.test.stop_time = 0 + self.assertEqual(self.test.get_duration(), "XX:XX") + + def test_get_invalid_duration(self): + self.test.start_time = 2 + self.test.stop_time = 1 + self.assertEqual(self.test.get_duration(), "XX:XX") + + def test_get_zero_duration(self): + self.test.start_time = 2 + self.test.stop_time = 2 + self.assertEqual(self.test.get_duration(), "00:00") + + def test_get_duration(self): + self.test.start_time = 1 + self.test.stop_time = 180 + self.assertEqual(self.test.get_duration(), "02:59") + + def test_str_project_name_ko(self): + self.test.project_name = None + self.assertIn("<functest.core.testcase.TestCase object at", + str(self.test)) + + def test_str_case_name_ko(self): + self.test.case_name = None + self.assertIn("<functest.core.testcase.TestCase object at", + str(self.test)) + + def test_str_pass(self): + duration = '01:01' + with mock.patch.object(self.test, 'get_duration', + return_value=duration), \ + mock.patch.object(self.test, 'is_successful', + return_value=testcase.TestCase.EX_OK): + message = str(self.test) + self.assertIn(self._project_name, message) + self.assertIn(self._case_name, message) + self.assertIn(duration, message) + self.assertIn('PASS', message) + + def test_str_fail(self): + duration = '00:59' + with mock.patch.object(self.test, 'get_duration', + return_value=duration), \ + mock.patch.object( + self.test, 'is_successful', + return_value=testcase.TestCase.EX_TESTCASE_FAILED): + message = str(self.test) + self.assertIn(self._project_name, message) + self.assertIn(self._case_name, message) + self.assertIn(duration, message) + self.assertIn('FAIL', message) + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_vnf.py b/functest/tests/unit/core/test_vnf.py new file mode 100644 index 00000000..793e9576 --- /dev/null +++ b/functest/tests/unit/core/test_vnf.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python + +# Copyright (c) 2016 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +# pylint: disable=missing-docstring + +import logging +import os +import unittest + +import mock + +from functest.core import vnf +from functest.core import testcase + + +class VnfBaseTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.test = vnf.VnfOnBoarding( + project='functest', case_name='aaa') + self.test.project = "functest" + self.test.start_time = "1" + self.test.stop_time = "5" + self.test.result = "" + self.test.details = { + "orchestrator": {"status": "PASS", "result": "", "duration": 20}, + "vnf": {"status": "PASS", "result": "", "duration": 15}, + "test_vnf": {"status": "FAIL", "result": "", "duration": 5}} + self.test.keystone_client = 'test_client' + self.test.tenant_name = 'test_tenant_name' + + def test_execute_deploy_vnf_fail(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf', + side_effect=Exception): + self.assertEqual(self.test.execute(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_execute_test_vnf_fail(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf'), \ + mock.patch.object(self.test, 'test_vnf', + side_effect=Exception): + self.assertEqual(self.test.execute(), + testcase.TestCase.EX_TESTCASE_FAILED) + + @mock.patch('functest.core.vnf.os_utils.get_tenant_id', + return_value='test_tenant_id') + @mock.patch('functest.core.vnf.os_utils.delete_tenant', + return_value=True) + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_user_id') + @mock.patch('functest.core.vnf.os_utils.delete_user', + return_value=True) + def test_execute_default(self, *args): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=None), \ + mock.patch.object(self.test, 'deploy_vnf'), \ + mock.patch.object(self.test, 'test_vnf'), \ + mock.patch.object(self.test, 'parse_results', + return_value='ret_exit_code'), \ + mock.patch.object(self.test, 'log_results'): + self.assertEqual(self.test.execute(), + 'ret_exit_code') + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', return_value='') + def test_prepare_missing_userid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='') + def test_prepare_missing_tenantid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='') + def test_prepare_missing_roleid(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.add_role_user', + return_value='') + def test_prepare_role_add_failure(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_user_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.create_tenant', + return_value='test_tenantid') + @mock.patch('functest.core.vnf.os_utils.get_role_id', + return_value='test_roleid') + @mock.patch('functest.core.vnf.os_utils.add_role_user') + @mock.patch('functest.core.vnf.os_utils.create_user', + return_value='') + def test_create_user_failure(self, *args): + with self.assertRaises(Exception): + self.test.prepare() + + def test_log_results_default(self): + with mock.patch('functest.core.vnf.' + 'ft_utils.logger_test_results') \ + as mock_method: + self.test.log_results() + self.assertTrue(mock_method.called) + + def test_step_failures_default(self): + with self.assertRaises(Exception): + self.test.step_failure("error_msg") + + def test_deploy_vnf_unimplemented(self): + with self.assertRaises(Exception) as context: + self.test.deploy_vnf() + self.assertTrue('VNF not deployed' in context.exception) + + def test_test_vnf_unimplemented(self): + with self.assertRaises(Exception) as context: + self.test.test_vnf()() + self.assertTrue('VNF not tested' in context.exception) + + def test_parse_results_ex_ok(self): + self.test.details['test_vnf']['status'] = 'PASS' + self.assertEqual(self.test.parse_results(), os.EX_OK) + + def test_parse_results_ex_run_error(self): + self.test.details['vnf']['status'] = 'FAIL' + self.assertEqual(self.test.parse_results(), os.EX_SOFTWARE) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_vnf_base.py b/functest/tests/unit/core/test_vnf_base.py deleted file mode 100644 index 540cf610..00000000 --- a/functest/tests/unit/core/test_vnf_base.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2016 Orange and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 - -import logging -import unittest - -from functest.core import vnf_base - - -class VnfBaseTesting(unittest.TestCase): - - logging.disable(logging.CRITICAL) - - def setUp(self): - self.test = vnf_base.VnfOnBoardingBase(project='functest', - case_name='aaa') - self.test.project = "functest" - self.test.start_time = "1" - self.test.stop_time = "5" - self.test.result = "" - self.test.details = {"orchestrator": {"status": "PASS", - "result": "", - "duration": 20}, - "vnf": {"status": "PASS", - "result": "", - "duration": 15}, - "test_vnf": {"status": "FAIL", - "result": "", - "duration": 5}} - - def test_deploy_vnf_unimplemented(self): - with self.assertRaises(Exception) as context: - self.test.deploy_vnf() - self.assertTrue('VNF not deployed' in context.exception) - - def test_test_vnf_unimplemented(self): - with self.assertRaises(Exception) as context: - self.test.test_vnf()() - self.assertTrue('VNF not tested' in context.exception) - - def test_parse_results(self): - self.assertNotEqual(self.test.parse_results(), 0) - - -if __name__ == "__main__": - unittest.main(verbosity=2) diff --git a/functest/tests/unit/energy/__init__.py b/functest/tests/unit/energy/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/energy/__init__.py diff --git a/functest/tests/unit/energy/test_functest_energy.py b/functest/tests/unit/energy/test_functest_energy.py new file mode 100644 index 00000000..ffe044bc --- /dev/null +++ b/functest/tests/unit/energy/test_functest_energy.py @@ -0,0 +1,277 @@ +#!/usr/bin/env python + +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +"""Unitary test for energy module.""" +# pylint: disable=unused-argument +import logging +import unittest + +import mock + +from functest.energy.energy import EnergyRecorder +import functest.energy.energy as energy + + +CASE_NAME = "UNIT_test_CASE" +STEP_NAME = "UNIT_test_STEP" + +logging.disable(logging.CRITICAL) + + +class MockHttpResponse(object): # pylint: disable=too-few-public-methods + """Mock response for Energy recorder API.""" + + def __init__(self, text, status_code): + """Create an instance of MockHttpResponse.""" + self.text = text + self.status_code = status_code + + +RECORDER_OK = MockHttpResponse( + '{"environment": "UNIT_TEST",' + ' "step": "string",' + ' "scenario": "' + CASE_NAME + '"}', + 200 +) +RECORDER_KO = MockHttpResponse( + '{"message": "An unhandled API exception occurred (MOCK)"}', + 500 +) + + +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" + else: + raise Exception("Config not mocked") + + +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 "" + else: + raise Exception("Config not mocked:" + config_key) + + +class EnergyRecorderTest(unittest.TestCase): + """Energy module unitary test suite.""" + + case_name = CASE_NAME + request_headers = {'content-type': 'application/json'} + returned_value_to_preserve = "value" + exception_message_to_preserve = "exception_message" + + @mock.patch('functest.energy.energy.requests.post', + return_value=RECORDER_OK) + def test_start(self, post_mock=None): + """EnergyRecorder.start method (regular case).""" + self.test_load_config() + self.assertTrue(EnergyRecorder.start(self.case_name)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.post', + side_effect=Exception("Internal execution error (MOCK)")) + def test_start_error(self, post_mock=None): + """EnergyRecorder.start method (error in method).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.start(self.case_name)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.post', + return_value=RECORDER_KO) + def test_start_api_error(self, post_mock=None): + """EnergyRecorder.start method (API error).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.start(self.case_name)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.post', + return_value=RECORDER_OK) + def test_set_step(self, post_mock=None): + """EnergyRecorder.set_step method (regular case).""" + self.test_load_config() + self.assertTrue(EnergyRecorder.set_step(STEP_NAME)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"] + "/step", + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.post', + return_value=RECORDER_KO) + def test_set_step_api_error(self, post_mock=None): + """EnergyRecorder.set_step method (API error).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.set_step(STEP_NAME)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"] + "/step", + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.post', + side_effect=Exception("Internal execution error (MOCK)")) + def test_set_step_error(self, post_mock=None): + """EnergyRecorder.set_step method (method error).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.set_step(STEP_NAME)) + post_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"] + "/step", + auth=EnergyRecorder.energy_recorder_api["auth"], + data=mock.ANY, + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.delete', + return_value=RECORDER_OK) + def test_stop(self, delete_mock=None): + """EnergyRecorder.stop method (regular case).""" + self.test_load_config() + self.assertTrue(EnergyRecorder.stop()) + delete_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.delete', + return_value=RECORDER_KO) + def test_stop_api_error(self, delete_mock=None): + """EnergyRecorder.stop method (API Error).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.stop()) + delete_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + headers=self.request_headers + ) + + @mock.patch('functest.energy.energy.requests.delete', + side_effect=Exception("Internal execution error (MOCK)")) + def test_stop_error(self, delete_mock=None): + """EnergyRecorder.stop method (method error).""" + self.test_load_config() + self.assertFalse(EnergyRecorder.stop()) + delete_mock.assert_called_once_with( + EnergyRecorder.energy_recorder_api["uri"], + auth=EnergyRecorder.energy_recorder_api["auth"], + headers=self.request_headers + ) + + @energy.enable_recording + def __decorated_method(self): + """Call with to energy recorder decorators.""" + return self.returned_value_to_preserve + + @energy.enable_recording + def __decorated_method_with_ex(self): + """Call with to energy recorder decorators.""" + raise Exception(self.exception_message_to_preserve) + + @mock.patch("functest.energy.energy.EnergyRecorder") + @mock.patch("functest.utils.functest_utils.get_pod_name", + return_value="MOCK_POD") + @mock.patch("functest.utils.functest_utils.get_functest_config", + side_effect=config_loader_mock) + def test_decorators(self, + loader_mock=None, + pod_mock=None, + recorder_mock=None): + """Test energy module decorators.""" + self.__decorated_method() + calls = [mock.call.start(self.case_name), + mock.call.stop()] + recorder_mock.assert_has_calls(calls) + + def test_decorator_preserve_return(self): + """Test that decorator preserve method returned value.""" + self.test_load_config() + self.assertTrue( + self.__decorated_method() == self.returned_value_to_preserve + ) + + def test_decorator_preserve_ex(self): + """Test that decorator preserve method exceptions.""" + self.test_load_config() + with self.assertRaises(Exception) as context: + self.__decorated_method_with_ex() + self.assertTrue( + self.exception_message_to_preserve in context.exception + ) + + @mock.patch("functest.utils.functest_utils.get_functest_config", + side_effect=config_loader_mock) + @mock.patch("functest.utils.functest_utils.get_pod_name", + return_value="MOCK_POD") + def test_load_config(self, loader_mock=None, pod_mock=None): + """Test load config.""" + EnergyRecorder.energy_recorder_api = None + EnergyRecorder.load_config() + self.assertEquals( + EnergyRecorder.energy_recorder_api["auth"], + ("user", "password") + ) + self.assertEquals( + EnergyRecorder.energy_recorder_api["uri"], + "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.utils.functest_utils.get_pod_name", + return_value="MOCK_POD") + def test_load_config_no_creds(self, loader_mock=None, pod_mock=None): + """Test load config without creds.""" + EnergyRecorder.energy_recorder_api = None + EnergyRecorder.load_config() + self.assertEquals(EnergyRecorder.energy_recorder_api["auth"], None) + self.assertEquals( + EnergyRecorder.energy_recorder_api["uri"], + "http://pod-uri:8888/recorders/environment/MOCK_POD" + ) + + @mock.patch("functest.utils.functest_utils.get_functest_config", + return_value=None) + @mock.patch("functest.utils.functest_utils.get_pod_name", + return_value="MOCK_POD") + def test_load_config_ex(self, loader_mock=None, pod_mock=None): + """Test load config with exception.""" + with self.assertRaises(AssertionError): + EnergyRecorder.energy_recorder_api = None + EnergyRecorder.load_config() + self.assertEquals(EnergyRecorder.energy_recorder_api, None) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/odl/test_odl.py b/functest/tests/unit/odl/test_odl.py index 54d6da72..e2778e24 100644 --- a/functest/tests/unit/odl/test_odl.py +++ b/functest/tests/unit/odl/test_odl.py @@ -12,14 +12,14 @@ import errno import logging import os -import StringIO import unittest from keystoneauth1.exceptions import auth_plugins import mock from robot.errors import DataError, RobotError -from robot.result import testcase as result_testcase +from robot.result import model from robot.utils.robottime import timestamp_to_secs +import six from functest.core import testcase from functest.opnfv_tests.sdn.odl import odl @@ -49,11 +49,9 @@ class ODLVisitorTesting(unittest.TestCase): 'elapsedtime': 1000, 'text': 'Hello, World!', 'critical': True} - test = result_testcase.TestCase(name=data['name'], - status=data['status'], - message=data['text'], - starttime=data['starttime'], - endtime=data['endtime']) + test = model.TestCase( + name=data['name'], status=data['status'], message=data['text'], + starttime=data['starttime'], endtime=data['endtime']) test.parent = mock.Mock() config = {'name': data['parent'], 'criticality.test_is_critical.return_value': data[ @@ -109,6 +107,9 @@ class ODLParseResultTesting(ODLTesting): """The class testing ODLTests.parse_results().""" # pylint: disable=missing-docstring + _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000', + 'endtime': '20161216 16:00:01.000'} + @mock.patch('robot.api.ExecutionResult', side_effect=DataError) def test_raises_exc(self, mock_method): with self.assertRaises(DataError): @@ -116,15 +117,13 @@ class ODLParseResultTesting(ODLTesting): mock_method.assert_called_once_with( os.path.join(odl.ODLTests.res_dir, 'output.xml')) - def test_ok(self): - config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000', - 'endtime': '20161216 16:00:01.000', 'status': 'PASS'} + def _test_result(self, config, result): suite = mock.Mock() suite.configure_mock(**config) with mock.patch('robot.api.ExecutionResult', return_value=mock.Mock(suite=suite)): self.test.parse_results() - self.assertEqual(self.test.result, config['status']) + self.assertEqual(self.test.result, result) self.assertEqual(self.test.start_time, timestamp_to_secs(config['starttime'])) self.assertEqual(self.test.stop_time, @@ -132,6 +131,26 @@ class ODLParseResultTesting(ODLTesting): self.assertEqual(self.test.details, {'description': config['name'], 'tests': []}) + def test_null_passed(self): + self._config.update({'statistics.critical.passed': 0, + 'statistics.critical.total': 20}) + self._test_result(self._config, 0) + + def test_no_test(self): + self._config.update({'statistics.critical.passed': 20, + 'statistics.critical.total': 0}) + self._test_result(self._config, 0) + + def test_half_success(self): + self._config.update({'statistics.critical.passed': 10, + 'statistics.critical.total': 20}) + self._test_result(self._config, 50) + + def test_success(self): + self._config.update({'statistics.critical.passed': 20, + 'statistics.critical.total': 20}) + self._test_result(self._config, 100) + class ODLRobotTesting(ODLTesting): @@ -152,7 +171,7 @@ class ODLRobotTesting(ODLTesting): os.path.join(odl.ODLTests.odl_test_repo, 'csit/variables/Variables.py'), inplace=True) - @mock.patch('sys.stdout', new_callable=StringIO.StringIO) + @mock.patch('sys.stdout', new_callable=six.StringIO) def _test_set_vars(self, msg1, msg2, *args): line = mock.MagicMock() line.__iter__.return_value = [msg1] @@ -170,7 +189,7 @@ class ODLRobotTesting(ODLTesting): def test_set_vars_auth1(self): self._test_set_vars("AUTH1 = []", "AUTH1 = []") - @mock.patch('sys.stdout', new_callable=StringIO.StringIO) + @mock.patch('sys.stdout', new_callable=six.StringIO) def test_set_vars_auth_foo(self, *args): line = mock.MagicMock() line.__iter__.return_value = ["AUTH = []"] @@ -337,9 +356,11 @@ class ODLMainTesting(ODLTesting): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ + create=True) as mock_open, \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) + mock_open.assert_called_once_with( + os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+') @mock.patch('os.remove') @mock.patch('robot.run', return_value=1) @@ -348,9 +369,11 @@ class ODLMainTesting(ODLTesting): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ + create=True) as mock_open, \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) + mock_open.assert_called_once_with( + os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+') @mock.patch('os.remove', side_effect=OSError) @mock.patch('robot.run') @@ -359,9 +382,11 @@ class ODLMainTesting(ODLTesting): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ + create=True) as mock_open, \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) + mock_open.assert_called_once_with( + os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+') class ODLRunTesting(ODLTesting): @@ -552,7 +577,7 @@ class ODLArgParserTesting(ODLTesting): "--odlip={}".format(self._sdn_controller_ip)]), self.defaultargs) - @mock.patch('sys.stderr', new_callable=StringIO.StringIO) + @mock.patch('sys.stderr', new_callable=six.StringIO) def test_fail(self, mock_method): self.defaultargs['foo'] = 'bar' with self.assertRaises(SystemExit): diff --git a/functest/tests/unit/openstack/rally/test_rally.py b/functest/tests/unit/openstack/rally/test_rally.py index fe25dfcf..c7828618 100644 --- a/functest/tests/unit/openstack/rally/test_rally.py +++ b/functest/tests/unit/openstack/rally/test_rally.py @@ -343,19 +343,6 @@ class OSRallyTesting(unittest.TestCase): self.rally_base._run_tests() self.rally_base._run_task.assert_any_call('test1') - @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info') - def test_generate_report(self, mock_logger_info): - summary = [{'test_name': 'test_name', - 'overall_duration': 5, - 'nb_tests': 3, - 'success': 5}] - self.rally_base.summary = summary - with mock.patch('functest.opnfv_tests.openstack.rally.rally.' - 'ft_utils.check_success_rate', - return_value='criteria'): - self.rally_base._generate_report() - self.assertTrue(mock_logger_info.called) - def test_clean_up_default(self): self.rally_base.volume_type = mock.Mock() self.rally_base.cinder_client = mock.Mock() diff --git a/functest/tests/unit/openstack/tempest/test_tempest.py b/functest/tests/unit/openstack/tempest/test_tempest.py index e05e5dfa..bb75c9ed 100644 --- a/functest/tests/unit/openstack/tempest/test_tempest.py +++ b/functest/tests/unit/openstack/tempest/test_tempest.py @@ -151,24 +151,6 @@ class OSTempestTesting(unittest.TestCase): assert_any_call("Starting Tempest test suite: '%s'." % cmd_line) - @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info') - def test_parse_verifier_result_default(self, mock_logger_info): - self.tempestcommon.VERIFICATION_ID = 'test_uuid' - self.tempestcommon.case_name = 'test_case_name' - stdout = ['Testscount||2', 'Success||2', 'Skipped||0', 'Failures||0'] - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'subprocess.Popen') as mock_popen, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'ft_utils.check_success_rate') as mock_method, \ - mock.patch('__builtin__.open', mock.mock_open()): - mock_stdout = mock.Mock() - attrs = {'stdout': stdout} - mock_stdout.configure_mock(**attrs) - mock_popen.return_value = mock_stdout - - self.tempestcommon.parse_verifier_result() - mock_method.assert_any_call('test_case_name', 100) - @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.path.exists', return_value=False) @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs', diff --git a/functest/tests/unit/utils/test_decorators.py b/functest/tests/unit/utils/test_decorators.py new file mode 100644 index 00000000..f8bd9a54 --- /dev/null +++ b/functest/tests/unit/utils/test_decorators.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +"""Define the class required to fully cover decorators.""" + +from datetime import datetime +import errno +import json +import logging +import os +import unittest + +import mock + +from functest.utils import decorators +from functest.utils import functest_utils + +__author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" + +VERSION = 'master' +DIR = '/dev' +FILE = '{}/null'.format(DIR) +URL = 'file://{}'.format(FILE) + + +class DecoratorsTesting(unittest.TestCase): + # pylint: disable=missing-docstring + + logging.disable(logging.CRITICAL) + + _case_name = 'base' + _project_name = 'functest' + _start_time = 1.0 + _stop_time = 2.0 + _result = 'PASS' + _build_tag = VERSION + _node_name = 'bar' + _deploy_scenario = 'foo' + _installer_type = 'debian' + + def setUp(self): + os.environ['INSTALLER_TYPE'] = self._installer_type + os.environ['DEPLOY_SCENARIO'] = self._deploy_scenario + os.environ['NODE_NAME'] = self._node_name + os.environ['BUILD_TAG'] = self._build_tag + + def test_wraps(self): + self.assertEqual(functest_utils.push_results_to_db.__name__, + "push_results_to_db") + + def _get_json(self): + stop_time = datetime.fromtimestamp(self._stop_time).strftime( + '%Y-%m-%d %H:%M:%S') + start_time = datetime.fromtimestamp(self._start_time).strftime( + '%Y-%m-%d %H:%M:%S') + data = {'project_name': self._project_name, + 'stop_date': stop_time, 'start_date': start_time, + 'case_name': self._case_name, 'build_tag': self._build_tag, + 'pod_name': self._node_name, 'installer': self._installer_type, + 'scenario': self._deploy_scenario, 'version': VERSION, + 'details': {}, 'criteria': self._result} + return json.dumps(data) + + @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): + 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): + 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): + with mock.patch.object(decorators, 'open', mock.mock_open(), + create=True) as mock_open: + self.assertTrue(functest_utils.push_results_to_db( + self._project_name, self._case_name, self._start_time, + self._stop_time, self._result, {})) + mock_open.assert_called_once_with(FILE, 'a') + handle = mock_open() + call_args, _ = handle.write.call_args + 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): + self._test_dump() + mock_method.assert_called_once_with(DIR) + + @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, '')) + def test_makedirs_dir_exists(self, mock_method=None): + 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): + 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__": + logging.basicConfig() + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_functest_logger.py b/functest/tests/unit/utils/test_functest_logger.py deleted file mode 100644 index 42e41a14..00000000 --- a/functest/tests/unit/utils/test_functest_logger.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python - -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 - -import logging -import unittest - -import mock - -from functest.utils import functest_logger -from functest.utils.constants import CONST - - -class OSUtilsLogger(unittest.TestCase): - - logging.disable(logging.CRITICAL) - - def setUp(self): - with mock.patch('__builtin__.open', mock.mock_open()): - with mock.patch('functest.utils.functest_logger.os.path.exists', - return_value=True), \ - mock.patch('functest.utils.functest_logger.' - 'json.load'), \ - mock.patch('functest.utils.functest_logger.' - 'logging.config.dictConfig') as m: - self.logger = functest_logger.Logger('os_utils') - self.assertTrue(m.called) - with mock.patch('functest.utils.functest_logger.os.path.exists', - return_value=False), \ - mock.patch('functest.utils.functest_logger.' - 'logging.basicConfig') as m: - self.logger = functest_logger.Logger('os_utils') - self.assertTrue(m.called) - - def test_is_debug_false(self): - CONST.CI_DEBUG = False - self.assertFalse(self.logger.is_debug()) - - def test_is_debug_true(self): - CONST.CI_DEBUG = "True" - self.assertTrue(self.logger.is_debug()) - - -if __name__ == "__main__": - unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 573fcb70..57e0c465 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -11,11 +11,11 @@ import logging 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 from functest.utils import functest_utils @@ -62,31 +62,31 @@ class FunctestUtilsTesting(unittest.TestCase): 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] @@ -371,7 +371,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:]) @@ -399,7 +399,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 @@ -428,7 +429,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 @@ -503,7 +505,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_' @@ -531,7 +533,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: @@ -543,7 +545,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 @@ -561,22 +563,6 @@ 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.result) - 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, - 0) - self.assertEqual(resp, 'FAIL') - # TODO: merge_dicts def test_get_testcases_file_dir(self): @@ -586,7 +572,7 @@ class FunctestUtilsTesting(unittest.TestCase): "functest/ci/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 diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index 7f3995d0..a7df264c 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -418,21 +418,45 @@ class OSUtilsTesting(unittest.TestCase): mock_logger_info.assert_called_once_with("OS_IDENTITY_API_VERSION is " "set in env as '%s'", '3') - def test_get_keystone_client(self): + @mock.patch('functest.utils.openstack_utils.get_session') + @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') + @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', + return_value='3') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='public') + def test_get_keystone_client_with_interface(self, mock_os_getenv, + mock_keystoneclient_version, + mock_key_client, + mock_get_session): mock_keystone_obj = mock.Mock() mock_session_obj = mock.Mock() - with mock.patch('functest.utils.openstack_utils' - '.get_keystone_client_version', return_value='3'), \ - mock.patch('functest.utils.openstack_utils' - '.keystoneclient.Client', - return_value=mock_keystone_obj) \ - as mock_key_client, \ - mock.patch('functest.utils.openstack_utils.get_session', - return_value=mock_session_obj): - self.assertEqual(openstack_utils.get_keystone_client(), - mock_keystone_obj) - mock_key_client.assert_called_once_with('3', - session=mock_session_obj) + mock_key_client.return_value = mock_keystone_obj + mock_get_session.return_value = mock_session_obj + self.assertEqual(openstack_utils.get_keystone_client(), + mock_keystone_obj) + mock_key_client.assert_called_once_with('3', + session=mock_session_obj, + interface='public') + + @mock.patch('functest.utils.openstack_utils.get_session') + @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') + @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', + return_value='3') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='admin') + def test_get_keystone_client_no_interface(self, mock_os_getenv, + mock_keystoneclient_version, + mock_key_client, + mock_get_session): + mock_keystone_obj = mock.Mock() + mock_session_obj = mock.Mock() + mock_key_client.return_value = mock_keystone_obj + mock_get_session.return_value = mock_session_obj + self.assertEqual(openstack_utils.get_keystone_client(), + mock_keystone_obj) + mock_key_client.assert_called_once_with('3', + session=mock_session_obj, + interface='admin') @mock.patch('functest.utils.openstack_utils.os.getenv', return_value=None) |