diff options
Diffstat (limited to 'functest/tests')
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_env.py | 23 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_os.py | 10 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_testcase.py | 26 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_tier.py | 26 | ||||
-rw-r--r-- | functest/tests/unit/core/test_unit.py | 12 | ||||
-rw-r--r-- | functest/tests/unit/core/test_vnf.py | 263 | ||||
-rw-r--r-- | functest/tests/unit/features/__init__.py | 0 | ||||
-rw-r--r-- | functest/tests/unit/features/test_barometer.py | 48 | ||||
-rw-r--r-- | functest/tests/unit/openstack/refstack_client/test_refstack_client.py | 14 | ||||
-rw-r--r-- | functest/tests/unit/openstack/tempest/test_conf_utils.py | 3 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 41 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_openstack_utils.py | 71 | ||||
-rw-r--r-- | functest/tests/unit/vnf/ims/test_cloudify_ims.py | 31 | ||||
-rw-r--r-- | functest/tests/unit/vnf/ims/test_ims_base.py | 17 |
14 files changed, 278 insertions, 307 deletions
diff --git a/functest/tests/unit/cli/commands/test_cli_env.py b/functest/tests/unit/cli/commands/test_cli_env.py index 14e926eb..0b18554b 100644 --- a/functest/tests/unit/cli/commands/test_cli_env.py +++ b/functest/tests/unit/cli/commands/test_cli_env.py @@ -6,9 +6,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 import logging +import pkg_resources import unittest -from git.exc import NoSuchPathError import mock from functest.cli.commands import cli_env @@ -25,8 +25,8 @@ class CliEnvTesting(unittest.TestCase): return_value=False) @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.__getattribute__('dir_repo_functest')) + cmd = ("python %s start" % pkg_resources.resource_filename( + 'functest', 'ci/prepare_env.py')) self.cli_environ.prepare() mock_ft_utils.assert_called_with(cmd) @@ -37,8 +37,8 @@ class CliEnvTesting(unittest.TestCase): with mock.patch('__builtin__.raw_input', return_value="y"), \ mock.patch('functest.cli.commands.cli_testcase.os.remove') \ as mock_os_remove: - cmd = ("python %s/functest/ci/prepare_env.py start" % - CONST.__getattribute__('dir_repo_functest')) + cmd = ("python %s start" % pkg_resources.resource_filename( + 'functest', 'ci/prepare_env.py')) self.cli_environ.prepare() mock_os_remove.assert_called_once_with( CONST.__getattribute__('env_active')) @@ -72,42 +72,29 @@ class CliEnvTesting(unittest.TestCase): mock_click_echo.assert_called_with(test_utils. RegexMatch(reg_string)) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_installer_type(self, *args): self._test_show_missing_env_var('INSTALLER_TYPE', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_installer_ip(self, *args): self._test_show_missing_env_var('INSTALLER_IP', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_scenario(self, *args): self._test_show_missing_env_var('SCENARIO', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_node(self, *args): self._test_show_missing_env_var('NODE', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_build_tag(self, *args): self._test_show_missing_env_var('BUILD_TAG', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') def test_show_missing_ci_debug(self, *args): self._test_show_missing_env_var('DEBUG', *args) - @mock.patch('functest.cli.commands.cli_env.git.Repo') @mock.patch('functest.cli.commands.cli_env.os.path.isfile', return_value=False) def test_show_missing_environment(self, *args): self._test_show_missing_env_var('STATUS', *args) - @mock.patch('functest.cli.commands.cli_env.os.path.exists', - return_value=False) - def test_show_missing_git_repo_dir(self, *args): - CONST.__setattr__('dir_repo_functest', None) - self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show()) - @mock.patch('functest.cli.commands.cli_env.click.echo') @mock.patch('functest.cli.commands.cli_env.os.path.isfile', return_value=True) diff --git a/functest/tests/unit/cli/commands/test_cli_os.py b/functest/tests/unit/cli/commands/test_cli_os.py index c3285ecb..50ebe4b5 100644 --- a/functest/tests/unit/cli/commands/test_cli_os.py +++ b/functest/tests/unit/cli/commands/test_cli_os.py @@ -8,13 +8,13 @@ # import logging +import pkg_resources import unittest import os import mock from functest.cli.commands import cli_os -from functest.utils.constants import CONST class CliOpenStackTesting(unittest.TestCase): @@ -25,7 +25,6 @@ class CliOpenStackTesting(unittest.TestCase): self.installer_type = 'test_installer_type' self.installer_ip = 'test_installer_ip' self.openstack_creds = 'test_openstack_creds' - self.dir_repo_functest = 'test_dir_repo_functest' self.snapshot_file = 'test_snapshot_file' self.cli_os = cli_os.CliOpenStack() @@ -64,11 +63,10 @@ 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.__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) + mock_ftutils_execute.assert_called_once_with( + "sh %s" % pkg_resources.resource_filename( + 'functest', 'ci/check_os.sh'), verbose=False) @mock.patch('functest.cli.commands.cli_os.os.path.isfile', return_value=False) diff --git a/functest/tests/unit/cli/commands/test_cli_testcase.py b/functest/tests/unit/cli/commands/test_cli_testcase.py index fddfc317..4d0bdc2f 100644 --- a/functest/tests/unit/cli/commands/test_cli_testcase.py +++ b/functest/tests/unit/cli/commands/test_cli_testcase.py @@ -7,12 +7,12 @@ import logging +import pkg_resources import unittest import mock from functest.cli.commands import cli_testcase -from functest.utils.constants import CONST class CliTestCasesTesting(unittest.TestCase): @@ -39,9 +39,9 @@ class CliTestCasesTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-n -r ", self.testname)) self.cli_tests.run(self.testname, noclean=True, report=True) mock_ft_utils.assert_called_with(cmd) @@ -50,9 +50,9 @@ class CliTestCasesTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-n ", self.testname)) self.cli_tests.run(self.testname, noclean=True, report=False) mock_ft_utils.assert_called_with(cmd) @@ -61,9 +61,9 @@ class CliTestCasesTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-r ", self.testname)) self.cli_tests.run(self.testname, noclean=False, report=True) mock_ft_utils.assert_called_with(cmd) @@ -72,9 +72,9 @@ class CliTestCasesTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "", 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 550eec93..b42e3581 100644 --- a/functest/tests/unit/cli/commands/test_cli_tier.py +++ b/functest/tests/unit/cli/commands/test_cli_tier.py @@ -7,12 +7,12 @@ import logging +import pkg_resources import unittest import mock from functest.cli.commands import cli_tier -from functest.utils.constants import CONST class CliTierTesting(unittest.TestCase): @@ -87,9 +87,9 @@ class CliTierTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-n -r ", self.tiername)) self.cli_tier.run(self.tiername, noclean=True, report=True) mock_ft_utils.assert_called_with(cmd) @@ -98,9 +98,9 @@ class CliTierTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-r ", self.tiername)) self.cli_tier.run(self.tiername, noclean=False, report=True) mock_ft_utils.assert_called_with(cmd) @@ -109,9 +109,9 @@ class CliTierTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "-n ", self.tiername)) self.cli_tier.run(self.tiername, noclean=True, report=False) mock_ft_utils.assert_called_with(cmd) @@ -120,9 +120,9 @@ class CliTierTesting(unittest.TestCase): return_value=True) @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.__getattribute__('dir_repo_functest'), + cmd = ("python %s %s -t %s" % + (pkg_resources.resource_filename( + 'functest', 'ci/run_tests.py'), "", 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_unit.py b/functest/tests/unit/core/test_unit.py index 79c4e7d7..ca73de67 100644 --- a/functest/tests/unit/core/test_unit.py +++ b/functest/tests/unit/core/test_unit.py @@ -41,7 +41,9 @@ class PyTestSuiteRunnerTesting(unittest.TestCase): self._test_run(result=mock_result, status=testcase.TestCase.EX_RUN_ERROR) self.assertEqual(self.psrunner.result, 0) - self.assertEqual(self.psrunner.details, {'errors': [], 'failures': []}) + self.assertEqual(self.psrunner.details, + {'errors': 0, 'failures': 0, 'stream': '', + 'testsRun': 0}) self.assertEqual(self.psrunner.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) @@ -52,8 +54,8 @@ class PyTestSuiteRunnerTesting(unittest.TestCase): self._test_run(result=mock_result) self.assertEqual(self.psrunner.result, 96) self.assertEqual(self.psrunner.details, - {'errors': [('test1', 'error_msg1')], - 'failures': [('test2', 'failure_msg1')]}) + {'errors': 1, 'failures': 1, 'stream': '', + 'testsRun': 50}) self.assertEqual(self.psrunner.is_successful(), testcase.TestCase.EX_TESTCASE_FAILED) @@ -62,7 +64,9 @@ class PyTestSuiteRunnerTesting(unittest.TestCase): failures=[]) self._test_run(result=mock_result) self.assertEqual(self.psrunner.result, 100) - self.assertEqual(self.psrunner.details, {'errors': [], 'failures': []}) + self.assertEqual(self.psrunner.details, + {'errors': 0, 'failures': 0, 'stream': '', + 'testsRun': 50}) self.assertEqual(self.psrunner.is_successful(), testcase.TestCase.EX_OK) diff --git a/functest/tests/unit/core/test_vnf.py b/functest/tests/unit/core/test_vnf.py index ce859040..f061c409 100644 --- a/functest/tests/unit/core/test_vnf.py +++ b/functest/tests/unit/core/test_vnf.py @@ -10,156 +10,199 @@ # pylint: disable=missing-docstring import logging -import os import unittest import mock from functest.core import vnf from functest.core import testcase +from functest.utils import constants class VnfBaseTesting(unittest.TestCase): + """The class testing VNF.""" + # pylint: disable=missing-docstring,too-many-public-methods + + tenant_name = 'test_tenant_name' + tenant_description = 'description' 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): + constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name) + constants.CONST.__setattr__( + "vnf_foo_tenant_description", self.tenant_description) + self.test = vnf.VnfOnBoarding(project='functest', case_name='foo') + + def test_run_deploy_vnf_exc(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(), + self.assertEqual(self.test.run(), testcase.TestCase.EX_TESTCASE_FAILED) - def test_execute_test_vnf_fail(self): + def test_run_test_vnf_exc(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(), + self.assertEqual(self.test.run(), 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): + def test_run_deploy_orch_ko(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'), \ - 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') + return_value=False), \ + mock.patch.object(self.test, 'deploy_vnf', + return_value=True), \ + mock.patch.object(self.test, 'test_vnf', + return_value=True): + self.assertEqual(self.test.run(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_run_vnf_deploy_ko(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=True), \ + mock.patch.object(self.test, 'deploy_vnf', + return_value=False), \ + mock.patch.object(self.test, 'test_vnf', + return_value=True): + self.assertEqual(self.test.run(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_run_vnf_test_ko(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=True), \ + mock.patch.object(self.test, 'deploy_vnf', + return_value=True), \ + mock.patch.object(self.test, 'test_vnf', + return_value=False): + self.assertEqual(self.test.run(), + testcase.TestCase.EX_TESTCASE_FAILED) + + def test_run_default(self): + with mock.patch.object(self.test, 'prepare'),\ + mock.patch.object(self.test, 'deploy_orchestrator', + return_value=True), \ + mock.patch.object(self.test, 'deploy_vnf', + return_value=True), \ + mock.patch.object(self.test, 'test_vnf', + return_value=True): + self.assertEqual(self.test.run(), testcase.TestCase.EX_OK) + + def test_deploy_vnf_unimplemented(self): + with self.assertRaises(vnf.VnfDeploymentException): + self.test.deploy_vnf() + + def test_test_vnf_unimplemented(self): + with self.assertRaises(vnf.VnfTestException): + self.test.test_vnf() + @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.delete_user', + return_value=True) + def test_clean_user_set(self, *args): + self.test.user_created = True + self.test.clean() + args[0].assert_called_once_with(mock.ANY, self.tenant_name) + args[1].assert_called_once_with() - @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.delete_user', + return_value=True) + def test_clean_user_unset(self, *args): + self.test.user_created = False + self.test.clean() + args[0].assert_not_called() + args[1].assert_called_once_with() - @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.delete_tenant', + return_value=True) + def test_clean_tenant_set(self, *args): + self.test.tenant_created = True + self.test.clean() + args[0].assert_called_once_with(mock.ANY, self.tenant_name) + args[1].assert_called_once_with() - @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): + @mock.patch('functest.core.vnf.os_utils.delete_tenant', + return_value=True) + def test_clean_tenant_unset(self, *args): + self.test.tenant_created = False + self.test.clean() + args[0].assert_not_called() + args[1].assert_called_once_with() + + def test_deploy_orch_unimplemented(self): + self.assertTrue(self.test.deploy_orchestrator()) + + @mock.patch('functest.core.vnf.os_utils.get_credentials', + return_value={'creds': 'test'}) + @mock.patch('functest.core.vnf.os_utils.get_keystone_client', + return_value='test') + @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf', + return_value=0) + @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf', + return_value=0) + def test_prepare(self, *args): + self.assertEqual(self.test.prepare(), + testcase.TestCase.EX_OK) + args[0].assert_called_once_with('test', self.tenant_name) + args[1].assert_called_once_with( + 'test', self.tenant_name, self.tenant_description) + args[2].assert_called_once_with() + args[3].assert_called_once_with() + + @mock.patch('functest.core.vnf.os_utils.get_credentials', + side_effect=Exception) + def test_prepare_admin_creds_ko(self, *args): + with self.assertRaises(vnf.VnfPreparationException): self.test.prepare() + args[0].assert_called_once_with() + + @mock.patch('functest.core.vnf.os_utils.get_credentials', + return_value='creds') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client', + side_effect=Exception) + def test_prepare_keystone_client_ko(self, *args): + with self.assertRaises(vnf.VnfPreparationException): + self.test.prepare() + args[0].assert_called_once_with() + args[1].assert_called_once_with() - @mock.patch('functest.core.vnf.os_utils.get_credentials') + @mock.patch('functest.core.vnf.os_utils.get_credentials', + return_value='creds') @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): + @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf', + side_effect=Exception) + def test_prepare_tenant_creation_ko(self, *args): + with self.assertRaises(vnf.VnfPreparationException): self.test.prepare() + args[0].assert_called_once_with( + mock.ANY, self.tenant_name, self.tenant_description) + args[1].assert_called_once_with() + args[2].assert_called_once_with() - 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.assertIn('VNF not deployed', str(context.exception)) - - def test_test_vnf_unimplemented(self): - with self.assertRaises(Exception) as context: - self.test.test_vnf()() - self.assertIn('VNF not tested', str(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) + @mock.patch('functest.core.vnf.os_utils.get_credentials', + return_value='creds') + @mock.patch('functest.core.vnf.os_utils.get_keystone_client') + @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf', + return_value=0) + @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf', + side_effect=Exception) + def test_prepare_user_creation_ko(self, *args): + with self.assertRaises(vnf.VnfPreparationException): + self.test.prepare() + args[0].assert_called_once_with(mock.ANY, self.tenant_name) + args[1].assert_called_once_with( + mock.ANY, self.tenant_name, self.tenant_description) + args[2].assert_called_once_with() + args[3].assert_called_once_with() if __name__ == "__main__": diff --git a/functest/tests/unit/features/__init__.py b/functest/tests/unit/features/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/functest/tests/unit/features/__init__.py +++ /dev/null diff --git a/functest/tests/unit/features/test_barometer.py b/functest/tests/unit/features/test_barometer.py deleted file mode 100644 index 8c2585d9..00000000 --- a/functest/tests/unit/features/test_barometer.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/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 -with mock.patch('functest.utils.functest_utils.get_parameter_from_yaml'): - from functest.opnfv_tests.features import barometer - - -class BarometerTesting(unittest.TestCase): - - _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__": - logging.disable(logging.CRITICAL) - unittest.main(verbosity=2) diff --git a/functest/tests/unit/openstack/refstack_client/test_refstack_client.py b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py index 8c149baa..3a121245 100644 --- a/functest/tests/unit/openstack/refstack_client/test_refstack_client.py +++ b/functest/tests/unit/openstack/refstack_client/test_refstack_client.py @@ -7,7 +7,7 @@ import logging import mock -import os +import pkg_resources import unittest from functest.core import testcase @@ -17,12 +17,11 @@ from functest.utils.constants import CONST class OSRefstackClientTesting(unittest.TestCase): - _config = os.path.join( - CONST.__getattribute__('dir_functest_test'), - CONST.__getattribute__('refstack_tempest_conf_path')) - _testlist = os.path.join( - CONST.__getattribute__('dir_functest_test'), - CONST.__getattribute__('refstack_defcore_list')) + _config = pkg_resources.resource_filename( + 'functest', + 'opnfv_tests/openstack/refstack_client/refstack_tempest.conf') + _testlist = pkg_resources.resource_filename( + 'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt') def setUp(self): self.defaultargs = {'config': self._config, @@ -30,7 +29,6 @@ class OSRefstackClientTesting(unittest.TestCase): self.refstackclient = refstack_client.RefstackClient() def test_source_venv(self): - CONST.__setattr__('dir_refstack_client', 'test_repo_dir') with mock.patch('functest.opnfv_tests.openstack.refstack_client.' 'refstack_client.ft_utils.execute_command') as m: cmd = ("cd {0};" diff --git a/functest/tests/unit/openstack/tempest/test_conf_utils.py b/functest/tests/unit/openstack/tempest/test_conf_utils.py index 23f6e45c..37904965 100644 --- a/functest/tests/unit/openstack/tempest/test_conf_utils.py +++ b/functest/tests/unit/openstack/tempest/test_conf_utils.py @@ -238,8 +238,6 @@ class OSTempestConfUtilsTesting(unittest.TestCase): mock.patch('__builtin__.open', mock.mock_open()), \ mock.patch('functest.opnfv_tests.openstack.tempest.' 'conf_utils.shutil.copyfile'): - CONST.__setattr__('dir_functest_test', 'test_dir') - CONST.__setattr__('refstack_tempest_conf_path', 'test_path') conf_utils.configure_tempest_defcore('test_dep_dir', img_flavor_dict) mset.assert_any_call('compute', 'image_ref', 'test_image_id') @@ -264,7 +262,6 @@ class OSTempestConfUtilsTesting(unittest.TestCase): mock.patch('__builtin__.open', mock.mock_open()), \ mock.patch('functest.opnfv_tests.openstack.tempest.' 'conf_utils.backup_tempest_config'): - CONST.__setattr__('dir_functest_test', 'test_dir') CONST.__setattr__('OS_ENDPOINT_TYPE', None) conf_utils.\ configure_tempest_update_params('test_conf_file', diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index d84a3201..98c7d6e9 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -8,11 +8,11 @@ # http://www.apache.org/licenses/LICENSE-2.0 import logging +import pkg_resources import os import time import unittest -from git.exc import NoSuchPathError import mock import requests from six.moves import urllib @@ -56,8 +56,8 @@ class FunctestUtilsTesting(unittest.TestCase): self.testcase_dict = {'case_name': 'testname', 'criteria': self.criteria} self.parameter = 'general.openstack.image_name' - self.config_yaml = os.path.normpath(os.path.join(os.path.dirname( - os.path.abspath(__file__)), '../../../ci/config_functest.yaml')) + self.config_yaml = pkg_resources.resource_filename( + 'functest', 'ci/config_functest.yaml') self.db_url_env = 'http://foo/testdb' self.testcases_yaml = "test_testcases_yaml" self.file_yaml = {'general': {'openstack': {'image_name': @@ -97,27 +97,6 @@ class FunctestUtilsTesting(unittest.TestCase): m.assert_called_once_with(dest, 'wb') self.assertTrue(mock_sh.called) - def test_get_git_branch(self): - with mock.patch('functest.utils.functest_utils.Repo') as mock_repo: - mock_obj2 = mock.Mock() - attrs = {'name': 'test_branch'} - mock_obj2.configure_mock(**attrs) - - mock_obj = mock.Mock() - attrs = {'active_branch': mock_obj2} - mock_obj.configure_mock(**attrs) - - mock_repo.return_value = mock_obj - self.assertEqual(functest_utils.get_git_branch(self.repo_path), - 'test_branch') - - @mock.patch('functest.utils.functest_utils.Repo', - side_effect=NoSuchPathError) - def test_get_git_branch_failed(self, mock_repo): - self.assertRaises(NoSuchPathError, - lambda: functest_utils.get_git_branch(self.repo_path - )) - @mock.patch('functest.utils.functest_utils.logger.error') def test_get_installer_type_failed(self, mock_logger_error): with mock.patch.dict(os.environ, @@ -486,9 +465,7 @@ class FunctestUtilsTesting(unittest.TestCase): def test_get_dict_by_test(self, mock_logger_error): 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_' - 'file_dir'): + as mock_yaml: mock_obj = mock.Mock() attrs = {'get.return_value': [{'testcases': [self.testcase_dict]}]} mock_obj.configure_mock(**attrs) @@ -542,16 +519,6 @@ class FunctestUtilsTesting(unittest.TestCase): assert_called_once_with(self.parameter, self.config_yaml) - # TODO: merge_dicts - - @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, 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()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index 15b54057..0f06b1e1 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -1835,6 +1835,77 @@ class OSUtilsTesting(unittest.TestCase): None) self.assertTrue(mock_logger_error.called) + def test_get_or_create_user_for_vnf_get(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_user_id', + return_value='user_id'), \ + mock.patch('functest.utils.openstack_utils.get_tenant_id', + return_value='tenant_id'): + self.assertFalse(openstack_utils. + get_or_create_user_for_vnf(self.keystone_client, + 'my_vnf')) + + def test_get_or_create_user_for_vnf_create(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_user_id', + return_value=None), \ + mock.patch('functest.utils.openstack_utils.get_tenant_id', + return_value='tenant_id'): + self.assertTrue(openstack_utils. + get_or_create_user_for_vnf(self.keystone_client, + 'my_vnf')) + + def test_get_or_create_user_for_vnf_error_get_user_id(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_user_id', + side_effect=Exception): + self.assertRaises(Exception) + + def test_get_or_create_user_for_vnf_error_get_tenant_id(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_user_id', + return_value='user_id'), \ + mock.patch('functest.utils.openstack_utils.get_tenant_id', + side_effect='Exception'): + self.assertRaises(Exception) + + def test_get_or_create_tenant_for_vnf_get(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_tenant_id', + return_value='tenant_id'): + self.assertFalse( + openstack_utils.get_or_create_tenant_for_vnf( + self.keystone_client, 'tenant_name', 'tenant_description')) + + def test_get_or_create_tenant_for_vnf_create(self): + with mock.patch('functest.utils.openstack_utils.get_tenant_id', + return_value=None): + self.assertTrue( + openstack_utils.get_or_create_tenant_for_vnf( + self.keystone_client, 'tenant_name', 'tenant_description')) + + def test_get_or_create_tenant_for_vnf_error_get_tenant_id(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_tenant_id', + side_effect=Exception): + self.assertRaises(Exception) + + def test_download_and_add_image_on_glance_image_creation_failure(self): + with mock.patch('functest.utils.openstack_utils.' + 'os.makedirs'), \ + mock.patch('functest.utils.openstack_utils.' + 'ft_utils.download_url', + return_value=True), \ + mock.patch('functest.utils.openstack_utils.' + 'create_glance_image', + return_value=''): + resp = openstack_utils.download_and_add_image_on_glance( + self.glance_client, + 'image_name', + 'http://url', + 'data_dir') + self.assertEqual(resp, False) + if __name__ == "__main__": logging.disable(logging.CRITICAL) diff --git a/functest/tests/unit/vnf/ims/test_cloudify_ims.py b/functest/tests/unit/vnf/ims/test_cloudify_ims.py index c3c04e1d..2156a122 100644 --- a/functest/tests/unit/vnf/ims/test_cloudify_ims.py +++ b/functest/tests/unit/vnf/ims/test_cloudify_ims.py @@ -69,7 +69,7 @@ class CloudifyImsTesting(unittest.TestCase): mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' 'os_utils.get_image_id', return_value=''), \ - mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' + mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.os_utils.' 'download_and_add_image_on_glance') as m, \ self.assertRaises(Exception) as context: self.ims_vnf.deploy_orchestrator() @@ -458,35 +458,6 @@ class CloudifyImsTesting(unittest.TestCase): {'status': 'PASS', 'result': 'vims_test_result'}) - def test_download_and_add_image_on_glance_incorrect_url(self): - with mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' - 'os.makedirs'), \ - mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' - 'ft_utils.download_url', - return_value=False): - resp = cloudify_ims.download_and_add_image_on_glance(self. - glance_client, - 'image_name', - 'http://url', - 'data_dir') - self.assertEqual(resp, False) - - def test_download_and_add_image_on_glance_image_creation_failure(self): - with mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' - 'os.makedirs'), \ - mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' - 'ft_utils.download_url', - return_value=True), \ - mock.patch('functest.opnfv_tests.vnf.ims.cloudify_ims.' - 'os_utils.create_glance_image', - return_value=''): - resp = cloudify_ims.download_and_add_image_on_glance(self. - glance_client, - 'image_name', - 'http://url', - 'data_dir') - self.assertEqual(resp, False) - if __name__ == "__main__": logging.disable(logging.CRITICAL) diff --git a/functest/tests/unit/vnf/ims/test_ims_base.py b/functest/tests/unit/vnf/ims/test_ims_base.py index db5b18d7..66d35e39 100644 --- a/functest/tests/unit/vnf/ims/test_ims_base.py +++ b/functest/tests/unit/vnf/ims/test_ims_base.py @@ -35,23 +35,6 @@ class ClearwaterOnBoardingBaseTesting(unittest.TestCase): 'cookies': ""} self.mock_post_200.configure_mock(**attrs) - def test_create_ellis_number_failure(self): - 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() - - msg = "Unable to create a number:" - self.assertTrue(msg in context.exception) - - def _get_post_status(self, url, cookies='', data=''): - ellis_url = "http://test_ellis_ip/session" - if url == ellis_url: - return self.mock_post_200 - return self.mock_post - - if __name__ == "__main__": logging.disable(logging.CRITICAL) unittest.main(verbosity=2) |