From 3f7c5e340178ce9e4183dd023ef5d1b04f72baad Mon Sep 17 00:00:00 2001 From: ashishk1994 Date: Sat, 17 Dec 2016 23:45:31 +0530 Subject: Added Unit Tests for cli. JIRA: FUNCTEST-590 Change-Id: I5e28145cae742598927e8edbe65091fedb533a4d Signed-off-by: ashishk1994 --- functest/tests/unit/cli/__init__.py | 0 functest/tests/unit/cli/commands/__init__.py | 0 functest/tests/unit/cli/commands/test_cli_env.py | 130 +++++++++++ functest/tests/unit/cli/commands/test_cli_os.py | 238 +++++++++++++++++++++ .../tests/unit/cli/commands/test_cli_testcase.py | 103 +++++++++ functest/tests/unit/cli/commands/test_cli_tier.py | 130 +++++++++++ functest/tests/unit/cli/test_cli_base.py | 138 ++++++++++++ 7 files changed, 739 insertions(+) create mode 100644 functest/tests/unit/cli/__init__.py create mode 100644 functest/tests/unit/cli/commands/__init__.py create mode 100644 functest/tests/unit/cli/commands/test_cli_env.py create mode 100644 functest/tests/unit/cli/commands/test_cli_os.py create mode 100644 functest/tests/unit/cli/commands/test_cli_testcase.py create mode 100644 functest/tests/unit/cli/commands/test_cli_tier.py create mode 100644 functest/tests/unit/cli/test_cli_base.py (limited to 'functest/tests/unit') diff --git a/functest/tests/unit/cli/__init__.py b/functest/tests/unit/cli/__init__.py new file mode 100644 index 00000000..e69de29b 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 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) -- cgit 1.2.3-korg