3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
11 from git.exc import NoSuchPathError
14 from functest.cli.commands import cli_env
15 from functest.utils.constants import CONST
16 from functest.tests.unit import test_utils
19 class CliEnvTesting(unittest.TestCase):
21 logging.disable(logging.CRITICAL)
24 self.cli_environ = cli_env.CliEnv()
26 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
28 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
29 def test_prepare_default(self, mock_ft_utils, mock_os):
30 cmd = ("python %s/functest/ci/prepare_env.py start" %
31 CONST.dir_repo_functest)
32 self.cli_environ.prepare()
33 mock_ft_utils.assert_called_with(cmd)
35 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
37 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
38 def test_prepare_missing_status(self, mock_ft_utils, mock_os):
39 with mock.patch('__builtin__.raw_input', return_value="y"), \
40 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
42 cmd = ("python %s/functest/ci/prepare_env.py start" %
43 CONST.dir_repo_functest)
44 self.cli_environ.prepare()
45 mock_os_remove.assert_called_once_with(CONST.env_active)
46 mock_ft_utils.assert_called_with(cmd)
48 def _test_show_missing_env_var(self, var, *args):
49 if var == 'INSTALLER_TYPE':
50 CONST.INSTALLER_TYPE = None
51 reg_string = "| INSTALLER: Unknown, \S+\s*|"
52 elif var == 'INSTALLER_IP':
53 CONST.INSTALLER_IP = None
54 reg_string = "| INSTALLER: \S+, Unknown\s*|"
55 elif var == 'SCENARIO':
56 CONST.DEPLOY_SCENARIO = None
57 reg_string = "| SCENARIO: Unknown\s*|"
59 CONST.NODE_NAME = None
60 reg_string = "| POD: Unknown\s*|"
61 elif var == 'BUILD_TAG':
62 CONST.BUILD_TAG = None
63 reg_string = "| BUILD TAG: None|"
66 reg_string = "| DEBUG FLAG: false\s*|"
68 reg_string = "| STATUS: not ready\s*|"
70 with mock.patch('functest.cli.commands.cli_env.click.echo') \
72 self.cli_environ.show()
73 mock_click_echo.assert_called_with(test_utils.
74 RegexMatch(reg_string))
76 @mock.patch('functest.cli.commands.cli_env.git.Repo')
77 def test_show_missing_ci_installer_type(self, *args):
78 self._test_show_missing_env_var('INSTALLER_TYPE', *args)
80 @mock.patch('functest.cli.commands.cli_env.git.Repo')
81 def test_show_missing_ci_installer_ip(self, *args):
82 self._test_show_missing_env_var('INSTALLER_IP', *args)
84 @mock.patch('functest.cli.commands.cli_env.git.Repo')
85 def test_show_missing_ci_scenario(self, *args):
86 self._test_show_missing_env_var('SCENARIO', *args)
88 @mock.patch('functest.cli.commands.cli_env.git.Repo')
89 def test_show_missing_ci_node(self, *args):
90 self._test_show_missing_env_var('NODE', *args)
92 @mock.patch('functest.cli.commands.cli_env.git.Repo')
93 def test_show_missing_ci_build_tag(self, *args):
94 self._test_show_missing_env_var('BUILD_TAG', *args)
96 @mock.patch('functest.cli.commands.cli_env.git.Repo')
97 def test_show_missing_ci_debug(self, *args):
98 self._test_show_missing_env_var('DEBUG', *args)
100 @mock.patch('functest.cli.commands.cli_env.git.Repo')
101 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
103 def test_show_missing_environment(self, *args):
104 self._test_show_missing_env_var('STATUS', *args)
106 @mock.patch('functest.cli.commands.cli_env.os.path.exists',
108 def test_show_missing_git_repo_dir(self, *args):
109 CONST.dir_repo_functest = None
110 self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show())
112 @mock.patch('functest.cli.commands.cli_env.click.echo')
113 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
115 def test_status_environment_present(self, mock_path, mock_click_echo):
116 self.assertEqual(self.cli_environ.status(), 0)
117 mock_click_echo.assert_called_with("Functest environment"
118 " ready to run tests.\n")
120 @mock.patch('functest.cli.commands.cli_env.click.echo')
121 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
123 def test_status_environment_absent(self, mock_path, mock_click_echo):
124 self.assertEqual(self.cli_environ.status(), 1)
125 mock_click_echo.assert_called_with("Functest environment"
126 " is not installed.\n")
129 if __name__ == "__main__":
130 unittest.main(verbosity=2)