diff options
Diffstat (limited to 'functest/tests/unit')
38 files changed, 1242 insertions, 614 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 48c94a57..feaf33a8 100644 --- a/functest/tests/unit/ci/test_tier_builder.py +++ b/functest/tests/unit/ci/test_tier_builder.py @@ -22,7 +22,8 @@ class TierBuilderTesting(unittest.TestCase): 'scenario': 'test_scenario'} self.testcase = {'dependencies': self.dependency, - 'name': 'test_name', + 'enabled': 'true', + 'case_name': 'test_name', 'criteria': 'test_criteria', 'blocking': 'test_blocking', 'clean_flag': 'test_clean_flag', @@ -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 new file mode 100644 index 00000000..8de42ec5 --- /dev/null +++ b/functest/tests/unit/core/test_feature.py @@ -0,0 +1,98 @@ +#!/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 + +# pylint: disable=missing-docstring + +import logging +import unittest + +import mock + +from functest.core import feature +from functest.core import testcase + +# logging must be disabled else it calls time.time() +# what will break these unit tests. +logging.disable(logging.CRITICAL) + + +class FeatureTestingBase(unittest.TestCase): + + _case_name = "foo" + _project_name = "bar" + _repo = "dir_repo_copper" + _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -" + _output_file = '/home/opnfv/functest/results/foo.log' + feature = None + + @mock.patch('time.time', side_effect=[1, 2]) + def _test_run(self, status, mock_method=None): + self.assertEqual(self.feature.run(cmd=self._cmd), status) + if status == testcase.TestCase.EX_OK: + self.assertEqual(self.feature.result, 100) + else: + self.assertEqual(self.feature.result, 0) + mock_method.assert_has_calls([mock.call(), mock.call()]) + self.assertEqual(self.feature.start_time, 1) + self.assertEqual(self.feature.stop_time, 2) + + +class FeatureTesting(FeatureTestingBase): + + def setUp(self): + self.feature = feature.Feature( + project_name=self._project_name, case_name=self._case_name) + + def test_run_exc(self): + # pylint: disable=bad-continuation + with mock.patch.object( + self.feature, 'execute', + side_effect=Exception) as mock_method: + self._test_run(testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_called_once_with(cmd=self._cmd) + + def test_run(self): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + + +class BashFeatureTesting(FeatureTestingBase): + + def setUp(self): + self.feature = feature.BashFeature( + project_name=self._project_name, case_name=self._case_name) + + @mock.patch("functest.utils.functest_utils.execute_command") + def test_run_no_cmd(self, mock_method=None): + self.assertEqual(self.feature.run(), testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_not_called() + + @mock.patch("functest.utils.functest_utils.execute_command", + return_value=1) + def test_run_ko(self, mock_method=None): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_called_once_with( + self._cmd, output_file=self._output_file) + + @mock.patch("functest.utils.functest_utils.execute_command", + side_effect=Exception) + def test_run_exc(self, mock_method=None): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_called_once_with( + self._cmd, output_file=self._output_file) + + @mock.patch("functest.utils.functest_utils.execute_command", + return_value=0) + def test_run(self, mock_method): + self._test_run(testcase.TestCase.EX_OK) + mock_method.assert_called_once_with( + self._cmd, output_file=self._output_file) + + +if __name__ == "__main__": + unittest.main(verbosity=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 32104194..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,19 +20,21 @@ __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) + _case_name = "base" + _project_name = "functest" + _published_result = "PASS" + def setUp(self): - self.test = testcase.TestCase() - self.test.project = "functest" - self.test.case_name = "base" + self.test = testcase.TestCase(case_name=self._case_name, + project_name=self._project_name) self.test.start_time = "1" self.test.stop_time = "2" - self.test.criteria = "PASS" + self.test.result = 100 self.test.details = {"Hello": "World"} def test_run_unimplemented(self): @@ -46,12 +48,12 @@ class TestCaseTesting(unittest.TestCase): testcase.TestCase.EX_PUSH_TO_DB_ERROR) mock_function.assert_not_called() - def test_missing_case_name(self): - self.test.case_name = None + def test_missing_project_name(self): + self.test.project_name = None self._test_missing_attribute() - def test_missing_criteria(self): - self.test.criteria = None + def test_missing_case_name(self): + self.test.case_name = None self._test_missing_attribute() def test_missing_start_time(self): @@ -69,8 +71,8 @@ class TestCaseTesting(unittest.TestCase): self.assertEqual(self.test.push_to_db(), testcase.TestCase.EX_OK) mock_function.assert_called_once_with( - self.test.project, self.test.case_name, self.test.start_time, - self.test.stop_time, self.test.criteria, self.test.details) + self._project_name, self._case_name, self.test.start_time, + self.test.stop_time, self._published_result, self.test.details) @mock.patch('functest.utils.functest_utils.push_results_to_db', return_value=False) @@ -78,8 +80,8 @@ class TestCaseTesting(unittest.TestCase): self.assertEqual(self.test.push_to_db(), testcase.TestCase.EX_PUSH_TO_DB_ERROR) mock_function.assert_called_once_with( - self.test.project, self.test.case_name, self.test.start_time, - self.test.stop_time, self.test.criteria, self.test.details) + self._project_name, self._case_name, self.test.start_time, + self.test.stop_time, self._published_result, self.test.details) @mock.patch('functest.utils.functest_utils.push_results_to_db', return_value=True) @@ -87,24 +89,140 @@ class TestCaseTesting(unittest.TestCase): self.assertEqual(self.test.push_to_db(), testcase.TestCase.EX_OK) mock_function.assert_called_once_with( - self.test.project, self.test.case_name, self.test.start_time, - self.test.stop_time, self.test.criteria, self.test.details) + self._project_name, self._case_name, self.test.start_time, + self.test.stop_time, self._published_result, self.test.details) + + @mock.patch('functest.utils.functest_utils.push_results_to_db', + return_value=True) + def test_push_to_db_res_ko(self, mock_function=None): + self.test.result = 0 + self.assertEqual(self.test.push_to_db(), + testcase.TestCase.EX_OK) + mock_function.assert_called_once_with( + self._project_name, self._case_name, self.test.start_time, + self.test.stop_time, 'FAIL', self.test.details) + + @mock.patch('functest.utils.functest_utils.push_results_to_db', + return_value=True) + def test_push_to_db_both_ko(self, mock_function=None): + self.test.result = 0 + self.test.criteria = 0 + self.assertEqual(self.test.push_to_db(), + testcase.TestCase.EX_OK) + mock_function.assert_called_once_with( + self._project_name, self._case_name, self.test.start_time, + self.test.stop_time, 'FAIL', self.test.details) def test_check_criteria_missing(self): self.test.criteria = None - self.assertEqual(self.test.check_criteria(), + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) - def test_check_criteria_failed(self): - self.test.criteria = 'FAILED' - self.assertEqual(self.test.check_criteria(), + def test_check_result_missing(self): + self.test.result = None + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) - def test_check_criteria_pass(self): - self.test.criteria = 'PASS' - self.assertEqual(self.test.check_criteria(), + def test_check_result_failed(self): + # Backward compatibility + # It must be removed as soon as TestCase subclasses + # stop setting result = 'PASS' or 'FAIL'. + self.test.result = 'FAIL' + self.assertEqual(self.test.is_successful(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_check_result_pass(self): + # Backward compatibility + # It must be removed as soon as TestCase subclasses + # stop setting result = 'PASS' or 'FAIL'. + self.test.result = 'PASS' + self.assertEqual(self.test.is_successful(), testcase.TestCase.EX_OK) + def test_check_result_lt(self): + self.test.result = 50 + 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.is_successful(), + testcase.TestCase.EX_OK) + + def test_check_result_gt(self): + self.test.criteria = 50 + self.test.result = 100 + 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.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 1680f03f..00000000 --- a/functest/tests/unit/core/test_vnf_base.py +++ /dev/null @@ -1,53 +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='aaa') - self.test.project = "functest" - self.test.case_name = "aaa" - self.test.start_time = "1" - self.test.stop_time = "5" - self.test.criteria = "" - 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/opnfv_tests/__init__.py b/functest/tests/unit/energy/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/__init__.py +++ 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/opnfv_tests/openstack/__init__.py b/functest/tests/unit/features/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/openstack/__init__.py +++ b/functest/tests/unit/features/__init__.py diff --git a/functest/tests/unit/features/test_barometer.py b/functest/tests/unit/features/test_barometer.py new file mode 100644 index 00000000..8ca463b2 --- /dev/null +++ b/functest/tests/unit/features/test_barometer.py @@ -0,0 +1,50 @@ +#!/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 + +# pylint: disable=missing-docstring + +import logging +import sys +import unittest + +import mock + +from functest.core import testcase +sys.modules['baro_tests'] = mock.Mock() # noqa +# pylint: disable=wrong-import-position +from functest.opnfv_tests.features import barometer + + +class BarometerTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + _case_name = "barometercollectd" + _project_name = "barometer" + + def setUp(self): + self.barometer = barometer.BarometerCollectd( + case_name=self._case_name, project_name=self._project_name) + + def test_init(self): + self.assertEqual(self.barometer.project_name, self._project_name) + self.assertEqual(self.barometer.case_name, self._case_name) + + def test_run_ko(self): + sys.modules['baro_tests'].collectd.main = mock.Mock(return_value=1) + self.assertEqual(self.barometer.run(), + testcase.TestCase.EX_RUN_ERROR) + + def test_run(self): + sys.modules['baro_tests'].collectd.main = mock.Mock(return_value=0) + self.assertEqual(self.barometer.run(), testcase.TestCase.EX_OK) + + +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 e08deb27..6e2e9b1d 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[ @@ -90,7 +88,7 @@ class ODLTesting(unittest.TestCase): os.environ["OS_USERNAME"] = self._os_username os.environ["OS_PASSWORD"] = self._os_password os.environ["OS_TENANT_NAME"] = self._os_tenantname - self.test = odl.ODLTests() + self.test = odl.ODLTests(case_name='odl', project_name='functest') self.defaultargs = {'odlusername': self._odl_username, 'odlpassword': self._odl_password, 'neutronip': self._keystone_ip, @@ -109,21 +107,23 @@ 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): self.test.parse_results() - mock_method.assert_called_once_with() + 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.criteria, 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, @@ -131,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): @@ -151,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] @@ -169,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 = []"] @@ -292,8 +312,6 @@ class ODLMainTesting(ODLTesting): def test_run_ko(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ self.assertRaises(RobotError): self._test_main(testcase.TestCase.EX_RUN_ERROR, *args) @@ -302,63 +320,31 @@ class ODLMainTesting(ODLTesting): def test_parse_results_ko(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ mock.patch.object(self.test, 'parse_results', side_effect=RobotError): self._test_main(testcase.TestCase.EX_RUN_ERROR, *args) - @mock.patch('os.remove', side_effect=Exception) - @mock.patch('robot.run') - @mock.patch('os.makedirs') - def test_remove_exc(self, *args): - with mock.patch.object(self.test, 'set_robotframework_vars', - return_value=True), \ - mock.patch.object(self.test, 'parse_results'), \ - self.assertRaises(Exception): - self._test_main(testcase.TestCase.EX_OK, *args) - - @mock.patch('os.remove') @mock.patch('robot.run') @mock.patch('os.makedirs') def test_ok(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) - @mock.patch('os.remove') @mock.patch('robot.run') @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, '')) def test_makedirs_oserror17(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) - @mock.patch('os.remove') @mock.patch('robot.run', return_value=1) @mock.patch('os.makedirs') def test_testcases_in_failure(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ - mock.patch.object(self.test, 'parse_results'): - self._test_main(testcase.TestCase.EX_OK, *args) - - @mock.patch('os.remove', side_effect=OSError) - @mock.patch('robot.run') - @mock.patch('os.makedirs') - def test_remove_oserror(self, *args): - with mock.patch.object(self.test, 'set_robotframework_vars', - return_value=True), \ - mock.patch.object(odl, 'open', mock.mock_open(), - create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase.TestCase.EX_OK, *args) @@ -368,19 +354,10 @@ class ODLRunTesting(ODLTesting): """The class testing ODLTests.run().""" # pylint: disable=missing-docstring - @classmethod - def _fake_url_for(cls, service_type='identity'): - if service_type == 'identity': - return "http://{}:5000/v2.0".format( - ODLTesting._keystone_ip) - elif service_type == 'network': - return "http://{}:9696".format(ODLTesting._neutron_ip) - else: - return None - def _test_no_env_var(self, var): with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): del os.environ[var] self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR) @@ -393,7 +370,8 @@ class ODLRunTesting(ODLTesting): if 'odlrestconfport' in kwargs else '8181') with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): if exception: self.test.main = mock.Mock(side_effect=exception) else: @@ -410,18 +388,15 @@ class ODLRunTesting(ODLTesting): osusername=self._os_username) def _test_multiple_suites(self, suites, - status=testcase.TestCase.EX_OK, - exception=None, **kwargs): + status=testcase.TestCase.EX_OK, **kwargs): odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3' odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080' odlrestconfport = (kwargs['odlrestconfport'] if 'odlrestconfport' in kwargs else '8181') with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): - if exception: - self.test.main = mock.Mock(side_effect=exception) - else: - self.test.main = mock.Mock(return_value=status) + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): + self.test.main = mock.Mock(return_value=status) self.assertEqual(self.test.run(suites=suites), status) self.test.main.assert_called_once_with( suites, @@ -467,7 +442,8 @@ class ODLRunTesting(ODLTesting): def test_no_sdn_controller_ip(self): with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR) @@ -492,7 +468,8 @@ class ODLRunTesting(ODLTesting): def test_apex_no_controller_ip(self): with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): os.environ["INSTALLER_TYPE"] = "apex" self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR) @@ -506,7 +483,8 @@ class ODLRunTesting(ODLTesting): def test_netvirt_no_controller_ip(self): with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): os.environ["INSTALLER_TYPE"] = "netvirt" self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR) @@ -520,7 +498,8 @@ class ODLRunTesting(ODLTesting): def test_joid_no_controller_ip(self): with mock.patch('functest.utils.openstack_utils.get_endpoint', - side_effect=self._fake_url_for): + return_value="http://{}:9696".format( + ODLTesting._neutron_ip)): os.environ["INSTALLER_TYPE"] = "joid" self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR) @@ -558,12 +537,12 @@ 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): self.parser.parse_args(["--foo=bar"]) - mock_method.assert_called_once_with() + self.assertTrue(mock_method.getvalue().startswith("usage:")) def _test_arg(self, arg, value): self.defaultargs[arg] = value diff --git a/functest/tests/unit/opnfv_tests/openstack/rally/__init__.py b/functest/tests/unit/openstack/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/openstack/rally/__init__.py +++ b/functest/tests/unit/openstack/__init__.py diff --git a/functest/tests/unit/opnfv_tests/openstack/refstack_client/__init__.py b/functest/tests/unit/openstack/rally/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/openstack/refstack_client/__init__.py +++ b/functest/tests/unit/openstack/rally/__init__.py diff --git a/functest/tests/unit/opnfv_tests/openstack/rally/test_rally.py b/functest/tests/unit/openstack/rally/test_rally.py index fe25dfcf..c7828618 100644 --- a/functest/tests/unit/opnfv_tests/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/opnfv_tests/openstack/tempest/__init__.py b/functest/tests/unit/openstack/refstack_client/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/openstack/tempest/__init__.py +++ b/functest/tests/unit/openstack/refstack_client/__init__.py diff --git a/functest/tests/unit/opnfv_tests/openstack/refstack_client/test_refstack_client.py b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py index 60e180c9..60e180c9 100644 --- a/functest/tests/unit/opnfv_tests/openstack/refstack_client/test_refstack_client.py +++ b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py diff --git a/functest/tests/unit/opnfv_tests/vnf/__init__.py b/functest/tests/unit/openstack/tempest/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/vnf/__init__.py +++ b/functest/tests/unit/openstack/tempest/__init__.py diff --git a/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py b/functest/tests/unit/openstack/tempest/test_conf_utils.py index 8ca5cc5b..8ca5cc5b 100644 --- a/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py +++ b/functest/tests/unit/openstack/tempest/test_conf_utils.py diff --git a/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py b/functest/tests/unit/openstack/tempest/test_tempest.py index 34031b40..bb75c9ed 100644 --- a/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py +++ b/functest/tests/unit/openstack/tempest/test_tempest.py @@ -105,7 +105,7 @@ class OSTempestTesting(unittest.TestCase): self._test_generate_test_list_mode_default('full') def test_parse_verifier_result_missing_verification_uuid(self): - self.tempestcommon.VERIFICATION_ID = '' + self.tempestcommon.VERIFICATION_ID = None with self.assertRaises(Exception): self.tempestcommon.parse_verifier_result() @@ -151,118 +151,82 @@ 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) - - def test_run_missing_create_tempest_dir(self): - ret = testcase.TestCase.EX_RUN_ERROR - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.path.exists', return_value=False), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.makedirs') as mock_os_makedirs, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.create_tempest_resources', - return_value="image_and_flavor"): - self.assertEqual(self.tempestcommon.run(), - ret) - self.assertTrue(mock_os_makedirs.called) - - def test_run_missing_configure_tempest(self): - ret = testcase.TestCase.EX_RUN_ERROR - ret_ok = testcase.TestCase.EX_OK - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.path.exists', return_value=False), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.makedirs') as mock_os_makedirs, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.create_tempest_resources', - return_value=ret_ok), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.configure_tempest', - return_value=ret): - self.assertEqual(self.tempestcommon.run(), - ret) - self.assertTrue(mock_os_makedirs.called) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'os.path.exists', return_value=False) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs', + side_effect=Exception) + def test_run_makedirs_ko(self, *args): + self.assertEqual(self.tempestcommon.run(), + testcase.TestCase.EX_RUN_ERROR) + + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'os.path.exists', return_value=False) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs') + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', side_effect=Exception) + def test_run_create_tempest_resources_ko(self, *args): + self.assertEqual(self.tempestcommon.run(), + testcase.TestCase.EX_RUN_ERROR) + + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'os.path.exists', return_value=False) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs') + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', return_value={}) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.configure_tempest', side_effect=Exception) + def test_run_configure_tempest_ko(self, *args): + self.assertEqual(self.tempestcommon.run(), + testcase.TestCase.EX_RUN_ERROR) + + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'os.path.exists', return_value=False) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs') + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', return_value={}) + @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.configure_tempest') + def _test_run(self, status, *args): + self.assertEqual(self.tempestcommon.run(), status) def test_run_missing_generate_test_list(self): - ret = testcase.TestCase.EX_RUN_ERROR - ret_ok = testcase.TestCase.EX_OK - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.path.exists', return_value=False), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.makedirs') as mock_os_makedirs, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.create_tempest_resources', - return_value=ret_ok), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.configure_tempest', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'generate_test_list', - return_value=ret): - self.assertEqual(self.tempestcommon.run(), - ret) - self.assertTrue(mock_os_makedirs.called) - - def test_run_missing_apply_tempest_blacklist(self): - ret = testcase.TestCase.EX_RUN_ERROR - ret_ok = testcase.TestCase.EX_OK - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.path.exists', return_value=False), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.makedirs') as mock_os_makedirs, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.create_tempest_resources', - return_value=ret_ok), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.configure_tempest', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'generate_test_list', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist', - return_value=ret): - self.assertEqual(self.tempestcommon.run(), - ret) - self.assertTrue(mock_os_makedirs.called) - - def test_run_missing_parse_verifier_result(self): - ret = testcase.TestCase.EX_RUN_ERROR - ret_ok = testcase.TestCase.EX_OK - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.path.exists', return_value=False), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os.makedirs') as mock_os_makedirs, \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.create_tempest_resources', - return_value=ret_ok), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'conf_utils.configure_tempest', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'generate_test_list', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'run_verifier_tests', - return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'parse_verifier_result', - return_value=ret): - self.assertEqual(self.tempestcommon.run(), - ret) - self.assertTrue(mock_os_makedirs.called) + with mock.patch.object(self.tempestcommon, 'generate_test_list', + side_effect=Exception): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + + def test_run_apply_tempest_blacklist_ko(self): + with mock.patch.object(self.tempestcommon, 'generate_test_list'), \ + mock.patch.object(self.tempestcommon, + 'apply_tempest_blacklist', + side_effect=Exception()): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + + def test_run_verifier_tests_ko(self, *args): + with mock.patch.object(self.tempestcommon, 'generate_test_list'), \ + mock.patch.object(self.tempestcommon, + 'apply_tempest_blacklist'), \ + mock.patch.object(self.tempestcommon, 'run_verifier_tests', + side_effect=Exception()), \ + mock.patch.object(self.tempestcommon, 'parse_verifier_result', + side_effect=Exception): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + + def test_run_parse_verifier_result_ko(self, *args): + with mock.patch.object(self.tempestcommon, 'generate_test_list'), \ + mock.patch.object(self.tempestcommon, + 'apply_tempest_blacklist'), \ + mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \ + mock.patch.object(self.tempestcommon, 'parse_verifier_result', + side_effect=Exception): + self._test_run(testcase.TestCase.EX_RUN_ERROR) + + def test_run(self, *args): + with mock.patch.object(self.tempestcommon, 'generate_test_list'), \ + mock.patch.object(self.tempestcommon, + 'apply_tempest_blacklist'), \ + mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \ + mock.patch.object(self.tempestcommon, 'parse_verifier_result'): + self._test_run(testcase.TestCase.EX_OK) if __name__ == "__main__": 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 eb241e5d..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 @@ -41,8 +41,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,38 +54,39 @@ 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.db_url_env = 'http://foo/testdb' 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] @@ -278,7 +279,7 @@ class FunctestUtilsTesting(unittest.TestCase): 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 + "\'")) @@ -310,7 +311,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" @@ -333,7 +334,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)) self.assertTrue(mock_logger_error.called) def test_push_results_to_db_default(self): @@ -348,7 +349,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)) readline = 0 test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] @@ -370,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:]) @@ -398,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 @@ -427,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 @@ -502,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_' @@ -530,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: @@ -542,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 @@ -560,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.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): @@ -585,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) diff --git a/functest/tests/unit/opnfv_tests/vnf/ims/__init__.py b/functest/tests/unit/vnf/__init__.py index e69de29b..e69de29b 100644 --- a/functest/tests/unit/opnfv_tests/vnf/ims/__init__.py +++ b/functest/tests/unit/vnf/__init__.py diff --git a/functest/tests/unit/vnf/ims/__init__.py b/functest/tests/unit/vnf/ims/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/vnf/ims/__init__.py diff --git a/functest/tests/unit/opnfv_tests/vnf/ims/test_clearwater.py b/functest/tests/unit/vnf/ims/test_clearwater.py index 18bebfdf..18bebfdf 100644 --- a/functest/tests/unit/opnfv_tests/vnf/ims/test_clearwater.py +++ b/functest/tests/unit/vnf/ims/test_clearwater.py diff --git a/functest/tests/unit/opnfv_tests/vnf/ims/test_cloudify_ims.py b/functest/tests/unit/vnf/ims/test_cloudify_ims.py index f47ea865..f47ea865 100644 --- a/functest/tests/unit/opnfv_tests/vnf/ims/test_cloudify_ims.py +++ b/functest/tests/unit/vnf/ims/test_cloudify_ims.py diff --git a/functest/tests/unit/opnfv_tests/vnf/ims/test_ims_base.by b/functest/tests/unit/vnf/ims/test_ims_base.py index 9440bcdf..e283199c 100644 --- a/functest/tests/unit/opnfv_tests/vnf/ims/test_ims_base.by +++ b/functest/tests/unit/vnf/ims/test_ims_base.py @@ -10,7 +10,7 @@ import unittest import mock -from functest.opnfv_tests.vnf.ims import ims_base +from functest.opnfv_tests.vnf.ims import clearwater_ims_base as ims_base class ClearwaterOnBoardingBaseTesting(unittest.TestCase): @@ -38,8 +38,8 @@ class ClearwaterOnBoardingBaseTesting(unittest.TestCase): self.mock_post_200.configure_mock(**attrs) def test_create_ellis_number_failure(self): - with mock.patch('functest.opnfv_tests.vnf.ims.ims_base.' - 'requests.post', + with mock.patch('functest.opnfv_tests.vnf.ims.' + 'clearwater_ims_base.requests.post', return_value=self.mock_post_500), \ self.assertRaises(Exception) as context: self.ims_vnf.create_ellis_number() diff --git a/functest/tests/unit/opnfv_tests/vnf/ims/test_orchestrator_cloudify.py b/functest/tests/unit/vnf/ims/test_orchestrator_cloudify.py index bf6d483f..bf6d483f 100644 --- a/functest/tests/unit/opnfv_tests/vnf/ims/test_orchestrator_cloudify.py +++ b/functest/tests/unit/vnf/ims/test_orchestrator_cloudify.py diff --git a/functest/tests/unit/vnf/rnc/__init__.py b/functest/tests/unit/vnf/rnc/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/vnf/rnc/__init__.py |