diff options
Diffstat (limited to 'functest/tests')
-rw-r--r-- | functest/tests/unit/cli/__init__.py | 0 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/__init__.py | 0 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_env.py | 130 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_os.py | 238 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_testcase.py | 103 | ||||
-rw-r--r-- | functest/tests/unit/cli/commands/test_cli_tier.py | 130 | ||||
-rw-r--r-- | functest/tests/unit/cli/test_cli_base.py | 138 | ||||
-rw-r--r-- | functest/tests/unit/core/test_testcase_base.py | 19 | ||||
-rw-r--r-- | functest/tests/unit/odl/test_odl.py | 140 | ||||
-rw-r--r-- | functest/tests/unit/test_utils.py | 23 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 599 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_utils.py | 45 |
12 files changed, 1454 insertions, 111 deletions
diff --git a/functest/tests/unit/cli/__init__.py b/functest/tests/unit/cli/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/cli/__init__.py diff --git a/functest/tests/unit/cli/commands/__init__.py b/functest/tests/unit/cli/commands/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/cli/commands/__init__.py diff --git a/functest/tests/unit/cli/commands/test_cli_env.py b/functest/tests/unit/cli/commands/test_cli_env.py new file mode 100644 index 00000000..4b6ea57a --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_env.py @@ -0,0 +1,130 @@ +#!/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 + +from git.exc import NoSuchPathError +import mock + +from functest.cli.commands import cli_env +from functest.utils.constants import CONST +from functest.tests.unit import test_utils + + +class CliEnvTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.cli_environ = cli_env.CliEnv() + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + 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.dir_repo_functest) + self.cli_environ.prepare() + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_prepare_missing_status(self, mock_ft_utils, mock_os): + 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.dir_repo_functest) + self.cli_environ.prepare() + mock_os_remove.assert_called_once_with(CONST.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 + reg_string = "| INSTALLER: Unknown, \S+\s*|" + elif var == 'INSTALLER_IP': + CONST.INSTALLER_IP = None + reg_string = "| INSTALLER: \S+, Unknown\s*|" + elif var == 'SCENARIO': + CONST.DEPLOY_SCENARIO = None + reg_string = "| SCENARIO: Unknown\s*|" + elif var == 'NODE': + CONST.NODE_NAME = None + reg_string = "| POD: Unknown\s*|" + elif var == 'BUILD_TAG': + CONST.BUILD_TAG = None + reg_string = "| BUILD TAG: None|" + elif var == 'DEBUG': + CONST.CI_DEBUG = None + reg_string = "| DEBUG FLAG: false\s*|" + elif var == 'STATUS': + reg_string = "| STATUS: not ready\s*|" + + with mock.patch('functest.cli.commands.cli_env.click.echo') \ + as mock_click_echo: + self.cli_environ.show() + 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.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) + def test_status_environment_present(self, mock_path, mock_click_echo): + self.assertEqual(self.cli_environ.status(), 0) + mock_click_echo.assert_called_with("Functest environment" + " ready to run tests.\n") + + @mock.patch('functest.cli.commands.cli_env.click.echo') + @mock.patch('functest.cli.commands.cli_env.os.path.isfile', + return_value=False) + def test_status_environment_absent(self, mock_path, mock_click_echo): + self.assertEqual(self.cli_environ.status(), 1) + mock_click_echo.assert_called_with("Functest environment" + " is not installed.\n") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_os.py b/functest/tests/unit/cli/commands/test_cli_os.py new file mode 100644 index 00000000..9e704806 --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_os.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# 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 os + +import mock + +from functest.cli.commands import cli_os +from functest.utils.constants import CONST + + +class CliOpenStackTesting(unittest.TestCase): + logging.disable(logging.CRITICAL) + + def setUp(self): + self.endpoint_ip = 'test_ip' + self.os_auth_url = 'http://test_ip:test_port/v2.0' + 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() + + def test_ping_endpoint_default(self): + self.cli_os.os_auth_url = self.os_auth_url + self.cli_os.endpoint_ip = self.endpoint_ip + with mock.patch('functest.cli.commands.cli_os.os.system', + return_value=0): + self.assertEqual(self.cli_os.ping_endpoint(), 0) + + @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_ping_endpoint_missing_auth_url(self, mock_click_echo, + mock_exit): + with self.assertRaises(Exception): + self.cli_os.os_auth_url = None + self.cli_os.ping_endpoint() + mock_click_echo.assert_called_once_with("Source the OpenStack " + "credentials first '. " + "$creds'") + + @mock.patch('functest.cli.commands.cli_os.exit') + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_ping_endpoint_os_system_fails(self, mock_click_echo, + mock_exit): + self.cli_os.os_auth_url = self.os_auth_url + self.cli_os.endpoint_ip = self.endpoint_ip + with mock.patch('functest.cli.commands.cli_os.os.system', + return_value=1): + self.cli_os.ping_endpoint() + mock_click_echo.assert_called_once_with("Cannot talk to the " + "endpoint %s\n" % + self.endpoint_ip) + mock_exit.assert_called_once_with(0) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + 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 + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + self.installer_type, + self.installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_called_once_with("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (self.installer_type, + self.installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + 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 + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + installer_type, + installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_any_call("The environment variable " + "'INSTALLER_TYPE' is not" + "defined. Please export it") + mock_click_echo.assert_any_call("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (installer_type, + installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_fetch_credentials_missing_installer_ip(self, mock_click_echo, + mock_os_path, + mock_ftutils_execute): + installer_type = self.installer_type + installer_ip = None + CONST.INSTALLER_TYPE = installer_type + CONST.INSTALLER_IP = installer_ip + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + installer_type, + installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_any_call("The environment variable " + "'INSTALLER_IP' is not" + "defined. Please export it") + mock_click_echo.assert_any_call("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (installer_type, + installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @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" + self.cli_os.check() + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_create(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \ + as mock_os_snapshot: + self.cli_os.snapshot_create() + mock_click_echo.assert_called_once_with("Generating Openstack " + "snapshot...") + self.assertTrue(mock_os_snapshot.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path): + with mock.patch('__builtin__.raw_input', return_value="y") \ + as mock_raw_input, \ + mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \ + as mock_os_snapshot: + self.cli_os.snapshot_create() + mock_click_echo.assert_called_once_with("Generating Openstack " + "snapshot...") + mock_raw_input.assert_any_call("It seems there is already an " + "OpenStack snapshot. Do you want " + "to overwrite it with the current " + "OpenStack status? [y|n]\n") + self.assertTrue(mock_os_snapshot.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path): + self.cli_os.snapshot_show() + mock_click_echo.assert_called_once_with("There is no OpenStack " + "snapshot created. To create " + "one run the command " + "'functest openstack " + "snapshot-create'") + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_show_default(self, mock_click_echo, mock_os_path): + with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \ + as m: + self.cli_os.snapshot_file = self.snapshot_file + self.cli_os.snapshot_show() + m.assert_called_once_with(self.snapshot_file, 'r') + mock_click_echo.assert_called_once_with("\n0") + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_clean(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_clean.main') \ + as mock_os_clean: + self.cli_os.clean() + self.assertTrue(mock_os_clean.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_clean_missing_file(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'): + self.cli_os.clean() + mock_click_echo.assert_called_once_with("Not possible to clean " + "OpenStack without a " + "snapshot. This could " + "cause problems. " + "Run first the command " + "'functest openstack " + "snapshot-create'") + + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_show_credentials(self, mock_click_echo): + key = 'OS_KEY' + value = 'OS_VALUE' + with mock.patch.dict(os.environ, {key: value}): + self.cli_os.show_credentials() + mock_click_echo.assert_called_once_with("{}={}".format(key, value)) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_testcase.py b/functest/tests/unit/cli/commands/test_cli_testcase.py new file mode 100644 index 00000000..39c8139d --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_testcase.py @@ -0,0 +1,103 @@ +#!/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.cli.commands import cli_testcase +from functest.utils.constants import CONST + + +class CliTestCasesTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.testname = 'testname' + with mock.patch('functest.cli.commands.cli_testcase.tb'): + self.cli_tests = cli_testcase.CliTestcase() + + @mock.patch('functest.cli.commands.cli_testcase.vacation.main') + def test_run_vacation(self, mock_method): + self.cli_tests.run('vacation') + self.assertTrue(mock_method.called) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_run_missing_env_file(self, mock_click_echo, mock_os): + self.cli_tests.run(self.testname) + mock_click_echo.assert_called_with("Functest environment is not ready." + " Run first 'functest env prepare'") + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + 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.dir_repo_functest, "-n -r ", self.testname)) + self.cli_tests.run(self.testname, noclean=True, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + 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.dir_repo_functest, "-n ", self.testname)) + self.cli_tests.run(self.testname, noclean=True, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + 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.dir_repo_functest, "-r ", self.testname)) + self.cli_tests.run(self.testname, noclean=False, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + 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.dir_repo_functest, "", self.testname)) + self.cli_tests.run(self.testname, noclean=False, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_list(self, mock_click_echo): + with mock.patch.object(self.cli_tests.tiers, 'get_tiers', + return_value=[]): + self.cli_tests.list() + mock_click_echo.assert_called_with("") + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_show_default_desc_none(self, mock_click_echo): + with mock.patch.object(self.cli_tests.tiers, 'get_test', + return_value=None): + self.cli_tests.show(self.testname) + mock_click_echo.assert_any_call("The test case '%s' " + "does not exist or is" + " not supported." + % self.testname) + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_show_default(self, mock_click_echo): + mock_obj = mock.Mock() + with mock.patch.object(self.cli_tests.tiers, 'get_test', + return_value=mock_obj): + self.cli_tests.show(self.testname) + mock_click_echo.assert_called_with(mock_obj) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_tier.py b/functest/tests/unit/cli/commands/test_cli_tier.py new file mode 100644 index 00000000..802359f1 --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_tier.py @@ -0,0 +1,130 @@ +#!/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.cli.commands import cli_tier +from functest.utils.constants import CONST + + +class CliTierTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.tiername = 'tiername' + self.testnames = 'testnames' + with mock.patch('functest.cli.commands.cli_tier.tb'): + self.cli_tier = cli_tier.CliTier() + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_list(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tiers', + return_value=[]): + self.cli_tier.list() + mock_click_echo.assert_called_with("") + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_show_default(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=self.tiername): + self.cli_tier.show(self.tiername) + mock_click_echo.assert_called_with(self.tiername) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_show_missing_tier(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=None), \ + mock.patch.object(self.cli_tier.tiers, 'get_tier_names', + return_value='tiernames'): + self.cli_tier.show(self.tiername) + mock_click_echo.assert_called_with("The tier with name '%s' does " + "not exist. Available tiers are" + ":\n %s\n" % (self.tiername, + 'tiernames')) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_gettests_default(self, mock_click_echo): + mock_obj = mock.Mock() + attrs = {'get_test_names.return_value': self.testnames} + mock_obj.configure_mock(**attrs) + + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=mock_obj): + self.cli_tier.gettests(self.tiername) + mock_click_echo.assert_called_with("Test cases in tier " + "'%s':\n %s\n" % (self.tiername, + self.testnames + )) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_gettests_missing_tier(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=None), \ + mock.patch.object(self.cli_tier.tiers, 'get_tier_names', + return_value='tiernames'): + self.cli_tier.gettests(self.tiername) + mock_click_echo.assert_called_with("The tier with name '%s' does " + "not exist. Available tiers are" + ":\n %s\n" % (self.tiername, + 'tiernames')) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_run_missing_env_file(self, mock_click_echo, mock_os): + self.cli_tier.run(self.tiername) + mock_click_echo.assert_called_with("Functest environment is not ready." + " Run first 'functest env prepare'") + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + 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.dir_repo_functest, "-n -r ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=True, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + 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.dir_repo_functest, "-r ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=False, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + 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.dir_repo_functest, "-n ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=True, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + 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.dir_repo_functest, "", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=False, report=False) + mock_ft_utils.assert_called_with(cmd) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/test_cli_base.py b/functest/tests/unit/cli/test_cli_base.py new file mode 100644 index 00000000..fe065c2a --- /dev/null +++ b/functest/tests/unit/cli/test_cli_base.py @@ -0,0 +1,138 @@ +#!/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 + +import mock +from click.testing import CliRunner + +with mock.patch('functest.cli.commands.cli_testcase.CliTestcase.__init__', + mock.Mock(return_value=None)), \ + mock.patch('functest.cli.commands.cli_tier.CliTier.__init__', + mock.Mock(return_value=None)): + from functest.cli import cli_base + + +class CliBaseTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.runner = CliRunner() + self._openstack = cli_base._openstack + self._env = cli_base._env + self._testcase = cli_base._testcase + self._tier = cli_base._tier + + def test_os_check(self): + with mock.patch.object(self._openstack, 'check') as mock_method: + result = self.runner.invoke(cli_base.os_check) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_snapshot_create(self): + with mock.patch.object(self._openstack, 'snapshot_create') \ + as mock_method: + result = self.runner.invoke(cli_base.os_snapshot_create) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_snapshot_show(self): + with mock.patch.object(self._openstack, 'snapshot_show') \ + as mock_method: + result = self.runner.invoke(cli_base.os_snapshot_show) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_clean(self): + with mock.patch.object(self._openstack, 'clean') as mock_method: + result = self.runner.invoke(cli_base.os_clean) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_show_credentials(self): + with mock.patch.object(self._openstack, 'show_credentials') \ + as mock_method: + result = self.runner.invoke(cli_base.os_show_credentials) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_fetch_rc(self): + with mock.patch.object(self._openstack, 'fetch_credentials') \ + as mock_method: + result = self.runner.invoke(cli_base.os_fetch_rc) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_prepare(self): + with mock.patch.object(self._env, 'prepare') as mock_method: + result = self.runner.invoke(cli_base.env_prepare) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_show(self): + with mock.patch.object(self._env, 'show') as mock_method: + result = self.runner.invoke(cli_base.env_show) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_status(self): + with mock.patch.object(self._env, 'status') as mock_method: + result = self.runner.invoke(cli_base.env_status) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_list(self): + with mock.patch.object(self._testcase, 'list') as mock_method: + result = self.runner.invoke(cli_base.testcase_list) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_show(self): + with mock.patch.object(self._testcase, 'show') as mock_method: + result = self.runner.invoke(cli_base.testcase_show, ['testname']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_run(self): + with mock.patch.object(self._testcase, 'run') as mock_method: + result = self.runner.invoke(cli_base.testcase_run, + ['testname', '--noclean']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_list(self): + with mock.patch.object(self._tier, 'list') as mock_method: + result = self.runner.invoke(cli_base.tier_list) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_show(self): + with mock.patch.object(self._tier, 'show') as mock_method: + result = self.runner.invoke(cli_base.tier_show, ['tiername']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_gettests(self): + with mock.patch.object(self._tier, 'gettests') as mock_method: + result = self.runner.invoke(cli_base.tier_gettests, ['tiername']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_run(self): + with mock.patch.object(self._tier, 'run') as mock_method: + result = self.runner.invoke(cli_base.tier_run, + ['tiername', '--noclean']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_testcase_base.py b/functest/tests/unit/core/test_testcase_base.py index fe7b0d05..b7c81d87 100644 --- a/functest/tests/unit/core/test_testcase_base.py +++ b/functest/tests/unit/core/test_testcase_base.py @@ -11,7 +11,7 @@ import logging import mock import unittest -import functest.core.testcase_base as testcase_base +from functest.core import testcase_base class TestcaseBaseTesting(unittest.TestCase): @@ -24,7 +24,7 @@ class TestcaseBaseTesting(unittest.TestCase): self.test.case_name = "base" self.test.start_time = "1" self.test.stop_time = "2" - self.test.criteria = "100" + self.test.criteria = "PASS" self.test.details = {"Hello": "World"} def test_run_unimplemented(self): @@ -82,6 +82,21 @@ class TestcaseBaseTesting(unittest.TestCase): self.test.project, self.test.case_name, self.test.start_time, self.test.stop_time, self.test.criteria, self.test.details) + def test_check_criteria_missing(self): + self.test.criteria = None + self.assertEqual(self.test.check_criteria(), + testcase_base.TestcaseBase.EX_TESTCASE_FAILED) + + def test_check_criteria_failed(self): + self.test.criteria = 'FAILED' + self.assertEqual(self.test.check_criteria(), + testcase_base.TestcaseBase.EX_TESTCASE_FAILED) + + def test_check_criteria_pass(self): + self.test.criteria = 'PASS' + self.assertEqual(self.test.check_criteria(), + testcase_base.TestcaseBase.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 ef18016b..d8c7f84e 100644 --- a/functest/tests/unit/odl/test_odl.py +++ b/functest/tests/unit/odl/test_odl.py @@ -13,11 +13,12 @@ import mock import os import unittest +from keystoneauth1.exceptions import auth_plugins from robot.errors import RobotError +from robot.result import testcase -import functest.core.testcase_base as testcase_base +from functest.core import testcase_base from functest.opnfv_tests.sdn.odl import odl -from functest.utils import functest_constants as ft_constants class ODLTesting(unittest.TestCase): @@ -36,11 +37,41 @@ class ODLTesting(unittest.TestCase): _odl_password = "admin" def setUp(self): - ft_constants.OS_USERNAME = self._os_username - ft_constants.OS_PASSWORD = self._os_password - ft_constants.OS_TENANT_NAME = self._os_tenantname + for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"): + if var in os.environ: + del os.environ[var] + 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() + def test_empty_visitor(self): + visitor = odl.ODLResultVisitor() + self.assertFalse(visitor.get_data()) + + def test_visitor(self): + visitor = odl.ODLResultVisitor() + data = {'name': 'foo', + 'parent': 'bar', + 'status': 'PASS', + 'starttime': "20161216 16:00:00.000", + 'endtime': "20161216 16:00:01.000", + 'elapsedtime': 1000, + 'text': 'Hello, World!', + 'critical': True} + test = testcase.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[ + 'critical']} + test.parent.configure_mock(**config) + visitor.visit_test(test) + self.assertEqual(visitor.get_data(), [data]) + @mock.patch('fileinput.input', side_effect=Exception()) def test_set_robotframework_vars_failed(self, *args): self.assertFalse(self.test.set_robotframework_vars()) @@ -59,14 +90,6 @@ class ODLTesting(unittest.TestCase): else: return None - @classmethod - def _get_fake_keystone_client(cls): - kclient = mock.Mock() - kclient.service_catalog = mock.Mock() - kclient.service_catalog.url_for = mock.Mock( - side_effect=cls._fake_url_for) - return kclient - def _get_main_kwargs(self, key=None): kwargs = {'odlusername': self._odl_username, 'odlpassword': self._odl_password, @@ -85,9 +108,9 @@ class ODLTesting(unittest.TestCase): def _test_main(self, status, *args): kwargs = self._get_main_kwargs() self.assertEqual(self.test.main(**kwargs), status) - odl_res_dir = odl.ODLTests.res_dir if len(args) > 0: - args[0].assert_called_once_with(odl_res_dir) + args[0].assert_called_once_with( + odl.ODLTests.res_dir) if len(args) > 1: variable = ['KEYSTONE:{}'.format(self._keystone_ip), 'NEUTRON:{}'.format(self._neutron_ip), @@ -97,18 +120,17 @@ class ODLTesting(unittest.TestCase): 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip), 'PORT:{}'.format(self._odl_webport), 'RESTCONFPORT:{}'.format(self._odl_restconfport)] - output_file = os.path.join(odl_res_dir, 'output.xml') args[1].assert_called_once_with( odl.ODLTests.basic_suite_dir, odl.ODLTests.neutron_suite_dir, log='NONE', - output=output_file, + output=os.path.join(odl.ODLTests.res_dir, 'output.xml'), report='NONE', stdout=mock.ANY, variable=variable) if len(args) > 2: - stdout_file = os.path.join(odl_res_dir, 'stdout.txt') - args[2].assert_called_with(stdout_file) + args[2].assert_called_with( + os.path.join(odl.ODLTests.res_dir, 'stdout.txt')) def _test_main_missing_keyword(self, key): kwargs = self._get_main_kwargs(key) @@ -200,8 +222,7 @@ class ODLTesting(unittest.TestCase): def test_main(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(self.test, 'parse_results', - return_value="PASS"): + mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @mock.patch('os.remove') @@ -210,8 +231,7 @@ class ODLTesting(unittest.TestCase): def test_main_makedirs_oserror17(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(self.test, 'parse_results', - return_value="PASS"): + mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @mock.patch('os.remove') @@ -220,8 +240,7 @@ class ODLTesting(unittest.TestCase): def test_main_testcases_in_failure(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(self.test, 'parse_results', - return_value="PASS"): + mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @mock.patch('os.remove', side_effect=OSError) @@ -230,25 +249,20 @@ class ODLTesting(unittest.TestCase): def test_main_remove_oserror(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ - mock.patch.object(self.test, 'parse_results', - return_value="PASS"): + mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) def _test_run_missing_env_var(self, var): - if var == 'OS_USERNAME': - ft_constants.OS_USERNAME = None - elif var == 'OS_PASSWORD': - ft_constants.OS_PASSWORD = None - elif var == 'OS_TENANT_NAME': - ft_constants.OS_TENANT_NAME = None - - self.assertEqual(self.test.run(), - testcase_base.TestcaseBase.EX_RUN_ERROR) + with mock.patch('functest.utils.openstack_utils.get_endpoint', + side_effect=self._fake_url_for): + del os.environ[var] + self.assertEqual(self.test.run(), + testcase_base.TestcaseBase.EX_RUN_ERROR) def _test_run(self, status=testcase_base.TestcaseBase.EX_OK, exception=None, odlip="127.0.0.3", odlwebport="8080"): - with mock.patch('functest.utils.openstack_utils.get_keystone_client', - return_value=self._get_fake_keystone_client()): + 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: @@ -262,6 +276,12 @@ class ODLTesting(unittest.TestCase): ospassword=self._os_password, ostenantname=self._os_tenantname, osusername=self._os_username) + def test_run_exception(self): + with mock.patch('functest.utils.openstack_utils.get_endpoint', + side_effect=auth_plugins.MissingAuthPlugin()): + self.assertEqual(self.test.run(), + testcase_base.TestcaseBase.EX_RUN_ERROR) + def test_run_missing_os_username(self): self._test_run_missing_env_var("OS_USERNAME") @@ -272,72 +292,64 @@ class ODLTesting(unittest.TestCase): self._test_run_missing_env_var("OS_TENANT_NAME") def test_run_main_false(self): - ft_constants.CI_INSTALLER_TYPE = None - ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip + os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip self._test_run(testcase_base.TestcaseBase.EX_RUN_ERROR, odlip=self._sdn_controller_ip, odlwebport=self._odl_webport) def test_run_main_exception(self): - ft_constants.CI_INSTALLER_TYPE = None - ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip with self.assertRaises(Exception): + os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip self._test_run(status=testcase_base.TestcaseBase.EX_RUN_ERROR, exception=Exception(), odlip=self._sdn_controller_ip, odlwebport=self._odl_webport) def test_run_missing_sdn_controller_ip(self): - with mock.patch('functest.utils.openstack_utils.get_keystone_client', - return_value=self._get_fake_keystone_client()): - ft_constants.CI_INSTALLER_TYPE = None - ft_constants.SDN_CONTROLLER_IP = None + with mock.patch('functest.utils.openstack_utils.get_endpoint', + side_effect=self._fake_url_for): self.assertEqual(self.test.run(), testcase_base.TestcaseBase.EX_RUN_ERROR) def test_run_without_installer_type(self): - ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip - ft_constants.CI_INSTALLER_TYPE = None + os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip self._test_run(testcase_base.TestcaseBase.EX_OK, odlip=self._sdn_controller_ip, odlwebport=self._odl_webport) def test_run_fuel(self): - ft_constants.CI_INSTALLER_TYPE = "fuel" + os.environ["INSTALLER_TYPE"] = "fuel" self._test_run(testcase_base.TestcaseBase.EX_OK, odlip=self._neutron_ip, odlwebport='8282') def test_run_apex_missing_sdn_controller_ip(self): - with mock.patch('functest.utils.openstack_utils.get_keystone_client', - return_value=self._get_fake_keystone_client()): - ft_constants.CI_INSTALLER_TYPE = "apex" - ft_constants.SDN_CONTROLLER_IP = None + with mock.patch('functest.utils.openstack_utils.get_endpoint', + side_effect=self._fake_url_for): + os.environ["INSTALLER_TYPE"] = "apex" self.assertEqual(self.test.run(), testcase_base.TestcaseBase.EX_RUN_ERROR) def test_run_apex(self): - ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip - ft_constants.CI_INSTALLER_TYPE = "apex" + os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip + os.environ["INSTALLER_TYPE"] = "apex" self._test_run(testcase_base.TestcaseBase.EX_OK, odlip=self._sdn_controller_ip, odlwebport='8181') def test_run_joid_missing_sdn_controller(self): - with mock.patch('functest.utils.openstack_utils.get_keystone_client', - return_value=self._get_fake_keystone_client()): - ft_constants.CI_INSTALLER_TYPE = "joid" - ft_constants.SDN_CONTROLLER = None + with mock.patch('functest.utils.openstack_utils.get_endpoint', + side_effect=self._fake_url_for): + os.environ["INSTALLER_TYPE"] = "joid" self.assertEqual(self.test.run(), testcase_base.TestcaseBase.EX_RUN_ERROR) def test_run_joid(self): - ft_constants.SDN_CONTROLLER = self._sdn_controller_ip - ft_constants.CI_INSTALLER_TYPE = "joid" + os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip + os.environ["INSTALLER_TYPE"] = "joid" self._test_run(testcase_base.TestcaseBase.EX_OK, - odlip=self._sdn_controller_ip, - odlwebport=self._odl_webport) + odlip=self._sdn_controller_ip, odlwebport='8080') def test_run_compass(self, *args): - ft_constants.CI_INSTALLER_TYPE = "compass" + os.environ["INSTALLER_TYPE"] = "compass" self._test_run(testcase_base.TestcaseBase.EX_OK, odlip=self._neutron_ip, odlwebport='8181') diff --git a/functest/tests/unit/test_utils.py b/functest/tests/unit/test_utils.py new file mode 100644 index 00000000..e171db02 --- /dev/null +++ b/functest/tests/unit/test_utils.py @@ -0,0 +1,23 @@ +#!/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 re + + +class RegexMatch(str): + def __eq__(self, other): + match = re.search(self, other) + if match: + return True + return False + + +class SubstrMatch(str): + def __eq__(self, other): + if self in other: + return True + return False diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py new file mode 100644 index 00000000..ce9086a7 --- /dev/null +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -0,0 +1,599 @@ +#!/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 os +import time +import unittest +import urllib2 + +from git.exc import NoSuchPathError +import mock +import requests + +from functest.tests.unit import test_utils +from functest.utils import functest_utils + + +class FunctestUtilsTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.url = 'http://www.opnfv.org/' + self.timeout = 5 + self.dest_path = 'test_path' + self.repo_path = 'test_repo_path' + self.installer = 'test_installer' + self.scenario = 'test_scenario' + self.build_tag = 'jenkins-functest-fuel-opnfv-jump-2-daily-master-190' + self.version = 'master' + self.node_name = 'test_node_name' + self.project = 'test_project' + self.case_name = 'test_case_name' + 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.start_date = 1482624000 + self.stop_date = 1482624000 + self.start_time = time.time() + self.stop_time = time.time() + self.readline = -1 + self.test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] + self.test_file = 'test_file' + self.error_msg = 'test_error_msg' + self.cmd = 'test_cmd' + self.output_file = 'test_output_file' + self.testname = 'testname' + self.testcase_dict = {'name': 'testname', 'criteria': self.criteria} + self.parameter = 'general.openstack.image_name' + self.config_yaml = 'test_config_yaml-' + self.file_yaml = {'general': {'openstack': {'image_name': + 'test_image_name'}}} + + @mock.patch('urllib2.urlopen', + side_effect=urllib2.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') + 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') + 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')) + def test_download_url_failed(self, mock_url): + self.assertFalse(functest_utils.download_url(self.url, self.dest_path)) + + @mock.patch('urllib2.urlopen') + def test_download_url_default(self, mock_url): + with mock.patch("__builtin__.open", mock.mock_open()) as m, \ + mock.patch('functest.utils.functest_utils.shutil.copyfileobj')\ + as mock_sh: + name = self.url.rsplit('/')[-1] + dest = self.dest_path + "/" + name + self.assertTrue(functest_utils.download_url(self.url, + self.dest_path)) + 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, + {}, + clear=True): + self.assertEqual(functest_utils.get_installer_type(), + "Unknown_installer") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the installer type") + + def test_get_installer_type_default(self): + with mock.patch.dict(os.environ, + {'INSTALLER_TYPE': 'test_installer'}, + clear=True): + self.assertEqual(functest_utils.get_installer_type(), + self.installer) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_scenario_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_scenario(), + "Unknown_scenario") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the scenario") + + def test_get_scenario_default(self): + with mock.patch.dict(os.environ, + {'DEPLOY_SCENARIO': 'test_scenario'}, + clear=True): + self.assertEqual(functest_utils.get_scenario(), + self.scenario) + + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_default(self, mock_get_build_tag): + mock_get_build_tag.return_value = self.build_tag + self.assertEqual(functest_utils.get_version(), self.version) + + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_unknown(self, mock_get_build_tag): + mock_get_build_tag.return_value = "unknown_build_tag" + self.assertEqual(functest_utils.get_version(), "unknown") + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_pod_name_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_pod_name(), + "unknown-pod") + mock_logger_error.assert_called_once_with("Unable to retrieve " + "the POD name from " + "environment. Using " + "pod name 'unknown-pod'") + + def test_get_pod_name_default(self): + with mock.patch.dict(os.environ, + {'NODE_NAME': 'test_node_name'}, + clear=True): + self.assertEqual(functest_utils.get_pod_name(), + self.node_name) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_build_tag_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_build_tag(), + "unknown_build_tag") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the build tag") + + def test_get_build_tag_default(self): + with mock.patch.dict(os.environ, + {'BUILD_TAG': self.build_tag}, + clear=True): + self.assertEqual(functest_utils.get_build_tag(), + self.build_tag) + + @mock.patch('functest.utils.functest_utils.get_functest_config') + def test_get_db_url(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): + with mock.patch('functest.utils.functest_utils.get_pod_name', + return_value=self.node_name), \ + mock.patch('functest.utils.functest_utils.get_scenario', + return_value=self.scenario), \ + 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): + functest_utils.logger_test_results(self.project, self.case_name, + self.status, self.details) + mock_logger_info.assert_called_once_with( + "\n" + "****************************************\n" + "\t %(p)s/%(n)s results \n\n" + "****************************************\n" + "DB:\t%(db)s\n" + "pod:\t%(pod)s\n" + "version:\t%(v)s\n" + "scenario:\t%(s)s\n" + "status:\t%(c)s\n" + "build tag:\t%(b)s\n" + "details:\t%(d)s\n" + % {'p': self.project, + 'n': self.case_name, + 'db': self.db_url, + 'pod': self.node_name, + 'v': self.version, + 's': self.scenario, + 'c': self.status, + 'b': self.build_tag, + 'd': self.details}) + + def _get_env_dict(self, var): + dic = {'INSTALLER_TYPE': self.installer, + 'DEPLOY_SCENARIO': self.scenario, + 'NODE_NAME': self.node_name, + 'BUILD_TAG': self.build_tag} + dic.pop(var, None) + return dic + + 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), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + 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) + mock_logger_error.assert_called_once_with("Please set env var: " + + str("\'" + env_var + + "\'")) + + def test_push_results_to_db_missing_installer(self): + self._test_push_results_to_db_missing_env('INSTALLER_TYPE') + + def test_push_results_to_db_missing_scenario(self): + self._test_push_results_to_db_missing_env('DEPLOY_SCENARIO') + + def test_push_results_to_db_missing_nodename(self): + self._test_push_results_to_db_missing_env('NODE_NAME') + + def test_push_results_to_db_missing_buildtag(self): + self._test_push_results_to_db_missing_env('BUILD_TAG') + + def test_push_results_to_db_incorrect_buildtag(self): + dic = self._get_env_dict(None) + dic['BUILD_TAG'] = 'incorrect_build_tag' + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error: + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + mock_logger_error.assert_called_once_with("Please fix BUILD_TAG" + " env var: incorrect_" + "build_tag") + + 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), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error, \ + mock.patch('functest.utils.functest_utils.requests.post', + side_effect=requests.RequestException): + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + mock_logger_error.assert_called_once_with(test_utils. + RegexMatch("Pushing " + "Result to" + " DB" + "(\S+\s*) " + "failed:")) + + 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), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error, \ + mock.patch('functest.utils.functest_utils.requests.post', + side_effect=Exception): + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + self.assertTrue(mock_logger_error.called) + + 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), \ + mock.patch('functest.utils.functest_utils.requests.post'): + self.assertTrue(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + readline = 0 + test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] + + @staticmethod + def readline_side(): + if FunctestUtilsTesting.readline == \ + len(FunctestUtilsTesting.test_ip) - 1: + return False + FunctestUtilsTesting.readline += 1 + return FunctestUtilsTesting.test_ip[FunctestUtilsTesting.readline] + + # TODO: get_resolvconf_ns + @mock.patch('functest.utils.functest_utils.dns.resolver.Resolver') + def test_get_resolvconf_ns_default(self, mock_dns_resolve): + attrs = {'query.return_value': ["test"]} + mock_dns_resolve.configure_mock(**attrs) + + m = mock.Mock() + attrs = {'readline.side_effect': self.readline_side} + m.configure_mock(**attrs) + + with mock.patch("__builtin__.open") as mo: + mo.return_value = m + self.assertEqual(functest_utils.get_resolvconf_ns(), + self.test_ip[1:]) + + def _get_environ(self, var): + if var == 'INSTALLER_TYPE': + return self.installer + elif var == 'DEPLOY_SCENARIO': + return self.scenario + return var + + def test_get_ci_envvars_default(self): + with mock.patch('os.environ.get', + side_effect=self._get_environ): + dic = {"installer": self.installer, + "scenario": self.scenario} + self.assertDictEqual(functest_utils.get_ci_envvars(), dic) + + def cmd_readline(self): + return 'test_value\n' + + @mock.patch('functest.utils.functest_utils.logger.error') + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_present_with_error(self, mock_logger_info, + 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: + + FunctestUtilsTesting.readline = 0 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 1} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=True, + error_msg=self.error_msg, + verbose=True, + output_file=self.output_file) + self.assertEqual(resp, 1) + msg_exec = ("Executing command: '%s'" % self.cmd) + mock_logger_info.assert_called_once_with(msg_exec) + mopen.assert_called_once_with(self.output_file, "w") + mock_logger_error.assert_called_once_with(self.error_msg) + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_present_with_success(self, mock_logger_info, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open, \ + mock.patch('__builtin__.open', mock.mock_open()) as mopen: + + FunctestUtilsTesting.readline = 0 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 0} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=True, + error_msg=self.error_msg, + verbose=True, + output_file=self.output_file) + self.assertEqual(resp, 0) + msg_exec = ("Executing command: '%s'" % self.cmd) + mock_logger_info.assert_called_once_with(msg_exec) + mopen.assert_called_once_with(self.output_file, "w") + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_missing_with_success(self, mock_logger_info, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open: + + FunctestUtilsTesting.readline = 2 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 0} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=False, + error_msg="", + verbose=False, + output_file=None) + self.assertEqual(resp, 0) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_execute_command_args_missing_with_error(self, mock_logger_error, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open: + + FunctestUtilsTesting.readline = 2 + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 1} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=False, + error_msg="", + verbose=False, + output_file=None) + self.assertEqual(resp, 1) + + def _get_functest_config(self, var): + return var + + @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()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml, \ + mock.patch('functest.utils.functest_utils.get_testcases_' + 'file_dir'): + mock_obj = mock.Mock() + attrs = {'get.return_value': [{'testcases': [self.testcase_dict]}]} + mock_obj.configure_mock(**attrs) + + mock_yaml.return_value = mock_obj + + self.assertDictEqual(functest_utils. + get_dict_by_test(self.testname), + self.testcase_dict) + + @mock.patch('functest.utils.functest_utils.get_dict_by_test') + def test_get_criteria_by_test_default(self, mock_get_dict_by_test): + mock_get_dict_by_test.return_value = self.testcase_dict + self.assertEqual(functest_utils.get_criteria_by_test(self.testname), + self.criteria) + + @mock.patch('functest.utils.functest_utils.get_dict_by_test') + def test_get_criteria_by_test_failed(self, mock_get_dict_by_test): + mock_get_dict_by_test.return_value = None + self.assertIsNone(functest_utils.get_criteria_by_test(self.testname)) + + def test_get_parameter_from_yaml_failed(self): + self.file_yaml['general'] = None + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml, \ + self.assertRaises(ValueError) as excep: + mock_yaml.return_value = self.file_yaml + functest_utils.get_parameter_from_yaml(self.parameter, + self.test_file) + self.assertTrue(("The parameter %s is not" + " defined in config_functest.yaml" % + self.parameter) in excep.exception) + + def test_get_parameter_from_yaml_default(self): + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml: + mock_yaml.return_value = self.file_yaml + self.assertEqual(functest_utils. + get_parameter_from_yaml(self.parameter, + self.test_file), + 'test_image_name') + + @mock.patch('functest.utils.functest_utils.get_parameter_from_yaml') + def test_get_functest_config_default(self, mock_get_parameter_from_yaml): + with mock.patch.dict(os.environ, + {'CONFIG_FUNCTEST_YAML': self.config_yaml}): + functest_utils.get_functest_config(self.parameter) + mock_get_parameter_from_yaml. \ + 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): + resp = functest_utils.get_testcases_file_dir() + self.assertEqual(resp, + "/home/opnfv/repos/functest/" + "functest/ci/testcases.yaml") + + def test_get_functest_yaml(self): + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml: + mock_yaml.return_value = self.file_yaml + resp = functest_utils.get_functest_yaml() + self.assertEqual(resp, self.file_yaml) + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_print_separator(self, mock_logger_info): + functest_utils.print_separator() + mock_logger_info.assert_called_once_with("=======================" + "=======================") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_utils.py b/functest/tests/unit/utils/test_utils.py deleted file mode 100644 index 8b6c5e1b..00000000 --- a/functest/tests/unit/utils/test_utils.py +++ /dev/null @@ -1,45 +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 mock -import unittest -import urllib2 - -from functest.utils import functest_utils - - -class FunctestUtilsTesting(unittest.TestCase): - - logging.disable(logging.CRITICAL) - - def setUp(self): - self.url = 'http://www.opnfv.org/' - self.timeout = 5 - - @mock.patch('urllib2.urlopen', - side_effect=urllib2.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') - 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') - 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) - - -if __name__ == "__main__": - unittest.main(verbosity=2) |