diff options
Diffstat (limited to 'functest/tests/unit')
-rw-r--r-- | functest/tests/unit/ci/test_prepare_env.py | 23 | ||||
-rw-r--r-- | functest/tests/unit/ci/test_run_tests.py | 27 | ||||
-rw-r--r-- | functest/tests/unit/ci/test_tier_builder.py | 1 | ||||
-rw-r--r-- | functest/tests/unit/ci/test_tier_handler.py | 1 | ||||
-rw-r--r-- | functest/tests/unit/core/test_testcase.py | 49 | ||||
-rw-r--r-- | functest/tests/unit/openstack/snaps/__init__.py | 0 | ||||
-rw-r--r-- | functest/tests/unit/openstack/snaps/test_snaps.py | 73 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_decorators.py | 19 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 70 |
9 files changed, 185 insertions, 78 deletions
diff --git a/functest/tests/unit/ci/test_prepare_env.py b/functest/tests/unit/ci/test_prepare_env.py index 513e7230..85d1918d 100644 --- a/functest/tests/unit/ci/test_prepare_env.py +++ b/functest/tests/unit/ci/test_prepare_env.py @@ -20,6 +20,7 @@ class PrepareEnvTesting(unittest.TestCase): def setUp(self): self.prepare_envparser = prepare_env.PrepareEnvParser() + self.db_url_env = 'http://foo/testdb' @mock.patch('functest.ci.prepare_env.logger.info') def test_print_separator(self, mock_logger_info): @@ -297,6 +298,22 @@ class PrepareEnvTesting(unittest.TestCase): prepare_env.patch_file('test_file') self.assertTrue(m.called) + @mock.patch('functest.ci.prepare_env.ft_utils.get_functest_yaml', + return_value={'tkey1': 'tvalue1'}) + @mock.patch('functest.ci.prepare_env.yaml.safe_load', + return_value={'test_scenario': {'tkey': 'tvalue'}}) + @mock.patch('functest.ci.prepare_env.update_db_url') + def test_update_db_url(self, mock_db_url, mock_safe_load, + mock_get_functest_yaml): + CONST.__setattr__('DEPLOY_SCENARIO', 'default_scenario') + with mock.patch("__builtin__.open", mock.mock_open()), \ + mock.patch('functest.ci.prepare_env.yaml.dump'), \ + mock.patch.dict('functest.ci.prepare_env.os.environ', + {'TEST_DB_URL': self.db_url_env}, + clear=True): + prepare_env.update_config_file() + self.assertTrue(mock_db_url.called) + @mock.patch('functest.ci.prepare_env.logger.info') def test_verify_deployment_error(self, mock_logger_error): mock_popen = mock.Mock() @@ -418,14 +435,14 @@ class PrepareEnvTesting(unittest.TestCase): @mock.patch('functest.ci.prepare_env.install_tempest') @mock.patch('functest.ci.prepare_env.install_rally') @mock.patch('functest.ci.prepare_env.verify_deployment') - @mock.patch('functest.ci.prepare_env.patch_config_file') + @mock.patch('functest.ci.prepare_env.update_config_file') @mock.patch('functest.ci.prepare_env.source_rc_file') @mock.patch('functest.ci.prepare_env.create_directories') @mock.patch('functest.ci.prepare_env.get_deployment_handler') @mock.patch('functest.ci.prepare_env.check_env_variables') @mock.patch('functest.ci.prepare_env.logger.info') def test_main_start(self, mock_logger_info, mock_env_var, mock_dep_handler, - mock_create_dir, mock_source_rc, mock_patch_config, + mock_create_dir, mock_source_rc, mock_update_config, mock_verify_depl, mock_install_rally, mock_install_temp, mock_create_flavor, mock_check_env, mock_print_info): @@ -438,7 +455,7 @@ class PrepareEnvTesting(unittest.TestCase): self.assertTrue(mock_dep_handler.called) self.assertTrue(mock_create_dir.called) self.assertTrue(mock_source_rc.called) - self.assertTrue(mock_patch_config.called) + self.assertTrue(mock_update_config.called) self.assertTrue(mock_verify_depl.called) self.assertTrue(mock_install_rally.called) self.assertTrue(mock_install_temp.called) diff --git a/functest/tests/unit/ci/test_run_tests.py b/functest/tests/unit/ci/test_run_tests.py index 88e5d2b8..fb8cb391 100644 --- a/functest/tests/unit/ci/test_run_tests.py +++ b/functest/tests/unit/ci/test_run_tests.py @@ -38,8 +38,12 @@ class RunTestsTesting(unittest.TestCase): 'OS_PASSWORD': 'test_password'} self.test = {'test_name': 'test_name'} self.tier = mock.Mock() + test1 = mock.Mock() + test1.get_name.return_value = 'test1' + test2 = mock.Mock() + test2.get_name.return_value = 'test2' attrs = {'get_name.return_value': 'test_tier', - 'get_tests.return_value': ['test1', 'test2'], + 'get_tests.return_value': [test1, test2], 'get_ci_loop.return_value': 'test_ci_loop', 'get_test_names.return_value': ['test1', 'test2']} self.tier.configure_mock(**attrs) @@ -70,16 +74,6 @@ class RunTestsTesting(unittest.TestCase): return_value=self.creds): self.runner.source_rc_file() - @mock.patch('functest.ci.run_tests.os_snapshot.main') - def test_generate_os_snapshot(self, mock_os_snap): - self.runner.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): - self.runner.cleanup() - self.assertTrue(mock_os_clean.called) - def test_get_run_dict_if_defined_default(self): mock_obj = mock.Mock() with mock.patch('functest.ci.run_tests.' @@ -137,8 +131,6 @@ class RunTestsTesting(unittest.TestCase): @mock.patch('functest.ci.run_tests.Runner.print_separator') @mock.patch('functest.ci.run_tests.Runner.source_rc_file') - @mock.patch('functest.ci.run_tests.Runner.generate_os_snapshot') - @mock.patch('functest.ci.run_tests.Runner.cleanup') @mock.patch('importlib.import_module', name="module", return_value=mock.Mock(test_class=mock.Mock( side_effect=FakeModule))) @@ -161,10 +153,10 @@ class RunTestsTesting(unittest.TestCase): def test_run_tier_default(self, mock_logger_info): with mock.patch('functest.ci.run_tests.Runner.print_separator'), \ mock.patch( - 'functest.ci.run_tests.Runner.run_test') as mock_method: + 'functest.ci.run_tests.Runner.run_test', + return_value=TestCase.EX_OK) as mock_method: self.runner.run_tier(self.tier) - mock_method.assert_any_call('test1', 'test_tier') - mock_method.assert_any_call('test2', 'test_tier') + mock_method.assert_any_call(mock.ANY, 'test_tier') self.assertTrue(mock_logger_info.called) @mock.patch('functest.ci.run_tests.logger.info') @@ -237,7 +229,8 @@ class RunTestsTesting(unittest.TestCase): with mock.patch('functest.ci.run_tests.tb.TierBuilder', return_value=mock_obj), \ mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \ - mock.patch('functest.ci.run_tests.Runner.run_test') as m: + mock.patch('functest.ci.run_tests.Runner.run_test', + return_value=TestCase.EX_OK) as m: self.assertEqual(self.runner.main(**kwargs), run_tests.Result.EX_OK) self.assertTrue(m.called) diff --git a/functest/tests/unit/ci/test_tier_builder.py b/functest/tests/unit/ci/test_tier_builder.py index 989c0870..ab75e15b 100644 --- a/functest/tests/unit/ci/test_tier_builder.py +++ b/functest/tests/unit/ci/test_tier_builder.py @@ -24,7 +24,6 @@ class TierBuilderTesting(unittest.TestCase): 'case_name': 'test_name', 'criteria': 'test_criteria', 'blocking': 'test_blocking', - 'clean_flag': 'test_clean_flag', 'description': 'test_desc'} self.dic_tier = {'name': 'test_tier', diff --git a/functest/tests/unit/ci/test_tier_handler.py b/functest/tests/unit/ci/test_tier_handler.py index c93fffd3..1909ac22 100644 --- a/functest/tests/unit/ci/test_tier_handler.py +++ b/functest/tests/unit/ci/test_tier_handler.py @@ -34,7 +34,6 @@ class TierHandlerTesting(unittest.TestCase): self.mock_depend, 'test_criteria', 'test_blocking', - 'test_clean_flag', description='test_desc') self.dependency = tier_handler.Dependency('test_installer', diff --git a/functest/tests/unit/core/test_testcase.py b/functest/tests/unit/core/test_testcase.py index ef0983cc..0a6f0c9f 100644 --- a/functest/tests/unit/core/test_testcase.py +++ b/functest/tests/unit/core/test_testcase.py @@ -221,6 +221,55 @@ class TestCaseTesting(unittest.TestCase): self.assertIn(duration, message) self.assertIn('FAIL', message) + def test_create_snapshot(self): + self.assertEqual(self.test.create_snapshot(), + testcase.TestCase.EX_OK) + + def test_clean(self): + self.assertEqual(self.test.clean(), None) + + +class OSGCTestCaseTesting(unittest.TestCase): + """The class testing OSGCTestCase.""" + # pylint: disable=missing-docstring + + def setUp(self): + self.test = testcase.OSGCTestCase() + + @mock.patch('functest.utils.openstack_snapshot.main', + side_effect=Exception) + def test_create_snapshot_exc(self, mock_method=None): + self.assertEqual(self.test.create_snapshot(), + testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_called_once_with() + + @mock.patch('functest.utils.openstack_snapshot.main', return_value=-1) + def test_create_snapshot_ko(self, mock_method=None): + self.assertEqual(self.test.create_snapshot(), + testcase.TestCase.EX_RUN_ERROR) + mock_method.assert_called_once_with() + + @mock.patch('functest.utils.openstack_snapshot.main', return_value=0) + def test_create_snapshot_env(self, mock_method=None): + self.assertEqual(self.test.create_snapshot(), + testcase.TestCase.EX_OK) + mock_method.assert_called_once_with() + + @mock.patch('functest.utils.openstack_clean.main', side_effect=Exception) + def test_clean_exc(self, mock_method=None): + self.assertEqual(self.test.clean(), None) + mock_method.assert_called_once_with() + + @mock.patch('functest.utils.openstack_clean.main', return_value=-1) + def test_clean_ko(self, mock_method=None): + self.assertEqual(self.test.clean(), None) + mock_method.assert_called_once_with() + + @mock.patch('functest.utils.openstack_clean.main', return_value=0) + def test_clean(self, mock_method=None): + self.assertEqual(self.test.clean(), None) + mock_method.assert_called_once_with() + if __name__ == "__main__": logging.disable(logging.CRITICAL) diff --git a/functest/tests/unit/openstack/snaps/__init__.py b/functest/tests/unit/openstack/snaps/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/openstack/snaps/__init__.py diff --git a/functest/tests/unit/openstack/snaps/test_snaps.py b/functest/tests/unit/openstack/snaps/test_snaps.py new file mode 100644 index 00000000..9cfcc0a6 --- /dev/null +++ b/functest/tests/unit/openstack/snaps/test_snaps.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Cable Television Laboratories, Inc. 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 mock +import unittest + +from snaps.openstack.os_credentials import OSCreds + +from functest.core.testcase import TestCase +from functest.opnfv_tests.openstack.snaps import connection_check + + +class ConnectionCheckTesting(unittest.TestCase): + """ + Ensures the VPingUserdata class can run in Functest. This test does not + actually connect with an OpenStack pod. + """ + + def setUp(self): + self.os_creds = OSCreds( + username='user', password='pass', + auth_url='http://foo.com:5000/v3', project_name='bar') + + self.connection_check = connection_check.ConnectionCheck( + os_creds=self.os_creds, ext_net_name='foo') + + @mock.patch('functest.opnfv_tests.openstack.snaps.connection_check.' + 'ConnectionCheck') + @mock.patch('snaps.test_suite_builder.add_openstack_client_tests') + def test_run_success(self, mock_test, add_os_client_tests): + result = mock.MagicMock(name='unittest.TextTestResult') + result.testsRun = 100 + result.failures = [] + result.errors = [] + with mock.patch('unittest.TextTestRunner.run', return_value=result): + self.assertEquals(TestCase.EX_OK, self.connection_check.run()) + self.assertEquals(TestCase.EX_OK, + self.connection_check.is_successful()) + + @mock.patch('functest.opnfv_tests.openstack.snaps.connection_check.' + 'ConnectionCheck') + @mock.patch('snaps.test_suite_builder.add_openstack_client_tests') + def test_run_1_of_100_failures(self, mock_test, add_os_client_tests): + result = mock.MagicMock(name='unittest.TextTestResult') + result.testsRun = 100 + result.failures = ['foo'] + result.errors = [] + with mock.patch('unittest.TextTestRunner.run', return_value=result): + self.assertEquals(TestCase.EX_OK, self.connection_check.run()) + self.assertEquals(TestCase.EX_TESTCASE_FAILED, + self.connection_check.is_successful()) + + @mock.patch('functest.opnfv_tests.openstack.snaps.connection_check.' + 'ConnectionCheck') + @mock.patch('snaps.test_suite_builder.add_openstack_client_tests') + def test_run_1_of_100_failures_within_criteria(self, mock_test, + add_os_client_tests): + self.connection_check.criteria = 90 + result = mock.MagicMock(name='unittest.TextTestResult') + result.testsRun = 100 + result.failures = ['foo'] + result.errors = [] + with mock.patch('unittest.TextTestRunner.run', return_value=result): + self.assertEquals(TestCase.EX_OK, self.connection_check.run()) + self.assertEquals(TestCase.EX_OK, + self.connection_check.is_successful()) diff --git a/functest/tests/unit/utils/test_decorators.py b/functest/tests/unit/utils/test_decorators.py index 44448f23..82291fa2 100644 --- a/functest/tests/unit/utils/test_decorators.py +++ b/functest/tests/unit/utils/test_decorators.py @@ -20,6 +20,7 @@ import mock from functest.utils import decorators from functest.utils import functest_utils +from functest.utils.constants import CONST __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" @@ -65,34 +66,29 @@ class DecoratorsTesting(unittest.TestCase): 'details': {}, 'criteria': self._result} return json.dumps(data, sort_keys=True) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value='http://127.0.0.1') @mock.patch('{}.get_version'.format(functest_utils.__name__), return_value=VERSION) @mock.patch('requests.post') def test_http_shema(self, *args): + CONST.__setattr__('results_test_db_url', 'http://127.0.0.1') self.assertTrue(functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) args[1].assert_called_once_with() - args[2].assert_called_once_with() args[0].assert_called_once_with( 'http://127.0.0.1', data=self._get_json(), headers={'Content-Type': 'application/json'}) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value="/dev/null") - def test_wrong_shema(self, mock_method=None): + def test_wrong_shema(self): + CONST.__setattr__('results_test_db_url', '/dev/null') self.assertFalse(functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) - mock_method.assert_called_once_with() @mock.patch('{}.get_version'.format(functest_utils.__name__), return_value=VERSION) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value=URL) def _test_dump(self, *args): + CONST.__setattr__('results_test_db_url', URL) with mock.patch.object(decorators, 'open', mock.mock_open(), create=True) as mock_open: self.assertTrue(functest_utils.push_results_to_db( @@ -104,7 +100,6 @@ class DecoratorsTesting(unittest.TestCase): self.assertIn('POST', call_args[0]) self.assertIn(self._get_json(), call_args[0]) args[0].assert_called_once_with() - args[1].assert_called_once_with() @mock.patch('os.makedirs') def test_default_dump(self, mock_method=None): @@ -116,16 +111,14 @@ class DecoratorsTesting(unittest.TestCase): self._test_dump() mock_method.assert_called_once_with(DIR) - @mock.patch('{}.get_db_url'.format(functest_utils.__name__), - return_value=URL) @mock.patch('os.makedirs', side_effect=OSError) def test_makedirs_exc(self, *args): + CONST.__setattr__('results_test_db_url', URL) self.assertFalse( functest_utils.push_results_to_db( self._project_name, self._case_name, self._start_time, self._stop_time, self._result, {})) args[0].assert_called_once_with(DIR) - args[1].assert_called_once_with() if __name__ == "__main__": diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 12604c1a..d84a3201 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -19,6 +19,7 @@ from six.moves import urllib from functest.tests.unit import test_utils from functest.utils import functest_utils +from functest.utils.constants import CONST class FunctestUtilsTesting(unittest.TestCase): @@ -58,6 +59,7 @@ class FunctestUtilsTesting(unittest.TestCase): self.config_yaml = os.path.normpath(os.path.join(os.path.dirname( os.path.abspath(__file__)), '../../../ci/config_functest.yaml')) self.db_url_env = 'http://foo/testdb' + self.testcases_yaml = "test_testcases_yaml" self.file_yaml = {'general': {'openstack': {'image_name': 'test_image_name'}}} @@ -208,23 +210,9 @@ class FunctestUtilsTesting(unittest.TestCase): self.assertEqual(functest_utils.get_build_tag(), self.build_tag) - def test_get_db_url_env_var(self): - with mock.patch.dict(os.environ, - {'TEST_DB_URL': self.db_url_env, - 'CONFIG_FUNCTEST_YAML': - "./functest/ci/config_functest.yaml"}, - clear=True): - self.assertEqual(functest_utils.get_db_url(), - self.db_url_env) - - @mock.patch('functest.utils.functest_utils.get_functest_config') - def test_get_db_url_default(self, mock_get_functest_config): - mock_get_functest_config.return_value = self.db_url - self.assertEqual(functest_utils.get_db_url(), self.db_url) - mock_get_functest_config.assert_called_once_with('results.test_db_url') - @mock.patch('functest.utils.functest_utils.logger.info') def test_logger_test_results(self, mock_logger_info): + CONST.__setattr__('results_test_db_url', self.db_url) with mock.patch('functest.utils.functest_utils.get_pod_name', return_value=self.node_name), \ mock.patch('functest.utils.functest_utils.get_scenario', @@ -232,9 +220,7 @@ class FunctestUtilsTesting(unittest.TestCase): mock.patch('functest.utils.functest_utils.get_version', return_value=self.version), \ mock.patch('functest.utils.functest_utils.get_build_tag', - return_value=self.build_tag), \ - mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url): + return_value=self.build_tag): functest_utils.logger_test_results(self.project, self.case_name, self.status, self.details) mock_logger_info.assert_called_once_with( @@ -251,7 +237,7 @@ class FunctestUtilsTesting(unittest.TestCase): "details:\t%(d)s\n" % {'p': self.project, 'n': self.case_name, - 'db': self.db_url, + 'db': CONST.__getattribute__('results_test_db_url'), 'pod': self.node_name, 'v': self.version, 's': self.scenario, @@ -269,11 +255,10 @@ class FunctestUtilsTesting(unittest.TestCase): def _test_push_results_to_db_missing_env(self, env_var): dic = self._get_env_dict(env_var) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error: functest_utils.push_results_to_db(self.project, self.case_name, @@ -297,11 +282,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_request_post_failed(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error, \ mock.patch('functest.utils.functest_utils.requests.post', @@ -320,11 +304,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_request_post_exception(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.logger.error') \ as mock_logger_error, \ mock.patch('functest.utils.functest_utils.requests.post', @@ -338,11 +321,10 @@ class FunctestUtilsTesting(unittest.TestCase): def test_push_results_to_db_default(self): dic = self._get_env_dict(None) - with mock.patch('functest.utils.functest_utils.get_db_url', - return_value=self.db_url), \ - mock.patch.dict(os.environ, - dic, - clear=True), \ + CONST.__setattr__('results_test_db_url', self.db_url) + with mock.patch.dict(os.environ, + dic, + clear=True), \ mock.patch('functest.utils.functest_utils.requests.post'): self.assertTrue(functest_utils. push_results_to_db(self.project, self.case_name, @@ -562,11 +544,13 @@ class FunctestUtilsTesting(unittest.TestCase): # TODO: merge_dicts - def test_get_testcases_file_dir(self): + @mock.patch('functest.utils.functest_utils.get_functest_config') + def test_get_testcases_file_dir(self, mock_get_functest_config): + mock_get_functest_config.return_value = self.testcases_yaml resp = functest_utils.get_testcases_file_dir() - self.assertEqual(resp, - "/home/opnfv/repos/functest/" - "functest/ci/testcases.yaml") + self.assertEqual(resp, self.testcases_yaml) + mock_get_functest_config.assert_called_once_with( + 'general.functest.testcases_yaml') def test_get_functest_yaml(self): with mock.patch('six.moves.builtins.open', mock.mock_open()), \ |