From debf35a0dceaf0f9b2113ca5845c75214528bb8c Mon Sep 17 00:00:00 2001 From: ashishk1994 Date: Mon, 27 Mar 2017 02:04:13 +0530 Subject: More Unit Tests for ci directory This patch adds more unit tests for ci/prepare_env, ci/run_tests and ci/tier_handler to reach the coverage more than 80% Change-Id: Ia3b7105542105bf8c5e3db58c898ba0569190a80 Signed-off-by: ashishk1994 --- functest/tests/unit/ci/test_prepare_env.py | 111 +++++++++++++++++++++++++++- functest/tests/unit/ci/test_run_tests.py | 84 ++++++++++++++++++++- functest/tests/unit/ci/test_tier_handler.py | 9 +++ 3 files changed, 199 insertions(+), 5 deletions(-) (limited to 'functest/tests') diff --git a/functest/tests/unit/ci/test_prepare_env.py b/functest/tests/unit/ci/test_prepare_env.py index 540501ff..714dd13c 100644 --- a/functest/tests/unit/ci/test_prepare_env.py +++ b/functest/tests/unit/ci/test_prepare_env.py @@ -20,6 +20,9 @@ class PrepareEnvTesting(unittest.TestCase): logging.disable(logging.CRITICAL) + def setUp(self): + self.prepare_envparser = prepare_env.PrepareEnvParser() + @mock.patch('functest.ci.prepare_env.logger.info') def test_print_separator(self, mock_logger_info): str = "==============================================" @@ -81,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 = mock.Mock() + CONST.DEPLOY_SCENARIO = 'test_scenario' prepare_env.check_env_variables() mock_logger_info.assert_any_call("Checking environment variables" "...") @@ -134,6 +137,47 @@ class PrepareEnvTesting(unittest.TestCase): mock_logger_info.assert_any_call(test_utils. SubstrMatch(" IS_CI_RUN=")) + 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 + prepare_env.get_deployment_handler() + self.assertFalse(m.called) + + CONST.INSTALLER_TYPE = None + prepare_env.get_deployment_handler() + self.assertFalse(m.called) + + @mock.patch('functest.ci.prepare_env.logger.debug') + def test_get_deployment_handler_missing_print_deploy_info(self, + mock_debug): + with mock.patch('functest.ci.prepare_env.' + 'factory.Factory.get_handler') as m, \ + 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' + opnfv_constants.INSTALLERS = ['test_inst_type'] + prepare_env.get_deployment_handler() + msg = ('Printing deployment info is not supported for ' + 'test_inst_type') + mock_debug.assert_any_call(msg) + self.assertFalse(m.called) + + @mock.patch('functest.ci.prepare_env.logger.debug') + def test_get_deployment_handler_exception(self, mock_debug): + with mock.patch('functest.ci.prepare_env.' + 'factory.Factory.get_handler', + 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' + opnfv_constants.INSTALLERS = ['test_inst_type'] + prepare_env.get_deployment_handler() + self.assertTrue(mock_debug.called) + @mock.patch('functest.ci.prepare_env.logger.info') @mock.patch('functest.ci.prepare_env.logger.debug') def test_create_directories_missing_dir(self, mock_logger_debug, @@ -228,6 +272,36 @@ class PrepareEnvTesting(unittest.TestCase): prepare_env.source_rc_file() + @mock.patch('functest.ci.prepare_env.logger.debug') + def test_patch_file(self, mock_logger_debug): + with mock.patch("__builtin__.open", mock.mock_open()), \ + mock.patch('functest.ci.prepare_env.yaml.safe_load', + return_value={'test_scenario': {'tkey': 'tvalue'}}), \ + mock.patch('functest.ci.prepare_env.ft_utils.get_functest_yaml', + 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' + prepare_env.patch_file('test_file') + self.assertTrue(m.called) + + @mock.patch('functest.ci.prepare_env.logger.info') + def test_verify_deployment_error(self, mock_logger_error): + mock_popen = mock.Mock() + attrs = {'poll.return_value': None, + 'stdout.readline.return_value': 'ERROR'} + mock_popen.configure_mock(**attrs) + + with mock.patch('functest.ci.prepare_env.print_separator') as m, \ + mock.patch('functest.ci.prepare_env.subprocess.Popen', + return_value=mock_popen), \ + self.assertRaises(Exception) as context: + prepare_env.verify_deployment() + self.assertTrue(m.called) + msg = "Problem while running 'check_os.sh'." + mock_logger_error.assert_called_once_with('ERROR') + self.assertTrue(msg in context) + def _get_rally_creds(self): return {"type": "ExistingCloud", "admin": {"username": 'test_user_name', @@ -271,6 +345,33 @@ class PrepareEnvTesting(unittest.TestCase): "Rally plugins.") mock_exec.assert_any_call(cmd, error_msg=error_msg) + @mock.patch('functest.ci.prepare_env.logger.debug') + def test_install_tempest(self, mock_logger_debug): + mock_popen = mock.Mock() + attrs = {'poll.return_value': None, + 'stdout.readline.return_value': '0'} + mock_popen.configure_mock(**attrs) + + CONST.tempest_deployment_name = 'test_dep_name' + with mock.patch('functest.ci.prepare_env.' + 'ft_utils.execute_command_raise', + side_effect=Exception), \ + mock.patch('functest.ci.prepare_env.subprocess.Popen', + return_value=mock_popen), \ + self.assertRaises(Exception): + prepare_env.install_tempest() + mock_logger_debug.assert_any_call("Tempest test_dep_name" + " does not exist") + + def test_create_flavor(self): + with mock.patch('functest.ci.prepare_env.' + 'os_utils.get_or_create_flavor', + return_value=('test_', None)), \ + self.assertRaises(Exception) as context: + prepare_env.create_flavor() + msg = 'Failed to create flavor' + self.assertTrue(msg in context) + @mock.patch('functest.ci.prepare_env.sys.exit') @mock.patch('functest.ci.prepare_env.logger.error') def test_check_environment_missing_file(self, mock_logger_error, @@ -299,6 +400,7 @@ class PrepareEnvTesting(unittest.TestCase): mock_logger_info.assert_any_call("Functest environment" " is installed.") + @mock.patch('functest.ci.prepare_env.print_deployment_info') @mock.patch('functest.ci.prepare_env.check_environment') @mock.patch('functest.ci.prepare_env.create_flavor') @mock.patch('functest.ci.prepare_env.install_tempest') @@ -307,19 +409,21 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.patch_config_file') @mock.patch('functest.ci.prepare_env.source_rc_file') @mock.patch('functest.ci.prepare_env.create_directories') + @mock.patch('functest.ci.prepare_env.get_deployment_handler') @mock.patch('functest.ci.prepare_env.check_env_variables') @mock.patch('functest.ci.prepare_env.logger.info') - def test_main_start(self, mock_logger_info, mock_env_var, + def test_main_start(self, mock_logger_info, mock_env_var, mock_dep_handler, mock_create_dir, mock_source_rc, mock_patch_config, mock_verify_depl, mock_install_rally, mock_install_temp, mock_create_flavor, - mock_check_env): + mock_check_env, mock_print_info): with mock.patch("__builtin__.open", mock.mock_open()) as m: args = {'action': 'start'} self.assertEqual(prepare_env.main(**args), 0) mock_logger_info.assert_any_call("######### Preparing Functest " "environment #########\n") self.assertTrue(mock_env_var.called) + self.assertTrue(mock_dep_handler.called) self.assertTrue(mock_create_dir.called) self.assertTrue(mock_source_rc.called) self.assertTrue(mock_patch_config.called) @@ -329,6 +433,7 @@ class PrepareEnvTesting(unittest.TestCase): self.assertTrue(mock_create_flavor.called) m.assert_called_once_with(CONST.env_active, "w") self.assertTrue(mock_check_env.called) + self.assertTrue(mock_print_info.called) @mock.patch('functest.ci.prepare_env.check_environment') def test_main_check(self, mock_check_env): diff --git a/functest/tests/unit/ci/test_run_tests.py b/functest/tests/unit/ci/test_run_tests.py index 02140610..7d02b1af 100644 --- a/functest/tests/unit/ci/test_run_tests.py +++ b/functest/tests/unit/ci/test_run_tests.py @@ -37,6 +37,9 @@ class RunTestsTesting(unittest.TestCase): attrs = {'get_tiers.return_value': [self.tier]} self.tiers.configure_mock(**attrs) + self.run_tests_parser = run_tests.RunTestsParser() + self.global_variables = run_tests.GlobalVariables() + @mock.patch('functest.ci.run_tests.logger.info') def test_print_separator(self, mock_logger_info): run_tests.print_separator(self.sep) @@ -121,8 +124,8 @@ class RunTestsTesting(unittest.TestCase): def test_run_tests_import_test_class_exception(self): mock_test = mock.Mock() - args = {'get_name': 'test_name', - 'needs_clean': False} + args = {'get_name.return_value': 'test_name', + 'needs_clean.return_value': False} mock_test.configure_mock(**args) with mock.patch('functest.ci.run_tests.print_separator'),\ mock.patch('functest.ci.run_tests.source_rc_file'), \ @@ -133,6 +136,28 @@ class RunTestsTesting(unittest.TestCase): msg = "Cannot import the class for the test case." self.assertTrue(msg in context) + def test_run_tests_default(self): + mock_test = mock.Mock() + args = {'get_name.return_value': 'test_name', + 'needs_clean.return_value': True} + mock_test.configure_mock(**args) + test_run_dict = {'module': 'test_module', + 'class': mock.Mock, + 'args': 'test_args'} + with mock.patch('functest.ci.run_tests.print_separator'),\ + 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') + msg = 'The test case test_name failed and is blocking' + self.assertTrue(msg in context) + @mock.patch('functest.ci.run_tests.logger.info') def test_run_tier_default(self, mock_logger_info): with mock.patch('functest.ci.run_tests.print_separator'), \ @@ -187,6 +212,61 @@ class RunTestsTesting(unittest.TestCase): self.assertEqual(run_tests.main(**kwargs), run_tests.Result.EX_ERROR) + def test_main_default(self): + kwargs = {'test': 'test_name', 'noclean': True, 'report': True} + mock_obj = mock.Mock() + args = {'get_tier.return_value': True, + 'get_test.return_value': False} + mock_obj.configure_mock(**args) + 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) + self.assertTrue(m.called) + + mock_obj = mock.Mock() + args = {'get_tier.return_value': False, + 'get_test.return_value': True} + mock_obj.configure_mock(**args) + 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) + self.assertTrue(m.called) + + kwargs = {'test': 'all', 'noclean': True, 'report': True} + mock_obj = mock.Mock() + args = {'get_tier.return_value': False, + 'get_test.return_value': False} + mock_obj.configure_mock(**args) + 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) + self.assertTrue(m.called) + + kwargs = {'test': 'any', 'noclean': True, 'report': True} + mock_obj = mock.Mock() + args = {'get_tier.return_value': False, + 'get_test.return_value': False} + mock_obj.configure_mock(**args) + 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_handler.py b/functest/tests/unit/ci/test_tier_handler.py index 01d99d7e..21df4098 100644 --- a/functest/tests/unit/ci/test_tier_handler.py +++ b/functest/tests/unit/ci/test_tier_handler.py @@ -41,6 +41,15 @@ class TierHandlerTesting(unittest.TestCase): self.dependency = tier_handler.Dependency('test_installer', 'test_scenario') + self.testcase.str = self.testcase.__str__() + self.dependency.str = self.dependency.__str__() + self.tier.str = self.tier.__str__() + + def test_split_text(self): + test_str = 'this is for testing' + self.assertEqual(tier_handler.split_text(test_str, 10), + ['this is ', 'for ', 'testing ']) + def test_add_test(self): self.tier.add_test(self.test) self.assertEqual(self.tier.tests_array, -- cgit 1.2.3-korg