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 mock.patch('logging.FileHandler').start() # noqa
15 from functest.cli.commands import cli_env
16 from functest.utils.constants import CONST
17 from functest.tests.unit import test_utils
20 class CliEnvTesting(unittest.TestCase):
22 logging.disable(logging.CRITICAL)
25 self.cli_environ = cli_env.CliEnv()
27 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
29 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
30 def test_prepare_default(self, mock_ft_utils, mock_os):
31 cmd = ("python %s/functest/ci/prepare_env.py start" %
32 CONST.dir_repo_functest)
33 self.cli_environ.prepare()
34 mock_ft_utils.assert_called_with(cmd)
36 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
38 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
39 def test_prepare_missing_status(self, mock_ft_utils, mock_os):
40 with mock.patch('__builtin__.raw_input', return_value="y"), \
41 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
43 cmd = ("python %s/functest/ci/prepare_env.py start" %
44 CONST.dir_repo_functest)
45 self.cli_environ.prepare()
46 mock_os_remove.assert_called_once_with(CONST.env_active)
47 mock_ft_utils.assert_called_with(cmd)
49 def _test_show_missing_env_var(self, var, *args):
50 if var == 'INSTALLER_TYPE':
51 CONST.INSTALLER_TYPE = None
52 reg_string = "| INSTALLER: Unknown, \S+\s*|"
53 elif var == 'INSTALLER_IP':
54 CONST.INSTALLER_IP = None
55 reg_string = "| INSTALLER: \S+, Unknown\s*|"
56 elif var == 'SCENARIO':
57 CONST.DEPLOY_SCENARIO = None
58 reg_string = "| SCENARIO: Unknown\s*|"
60 CONST.NODE_NAME = None
61 reg_string = "| POD: Unknown\s*|"
62 elif var == 'BUILD_TAG':
63 CONST.BUILD_TAG = None
64 reg_string = "| BUILD TAG: None|"
67 reg_string = "| DEBUG FLAG: false\s*|"
69 reg_string = "| STATUS: not ready\s*|"
71 with mock.patch('functest.cli.commands.cli_env.click.echo') \
73 self.cli_environ.show()
74 mock_click_echo.assert_called_with(test_utils.
75 RegexMatch(reg_string))
77 @mock.patch('functest.cli.commands.cli_env.git.Repo')
78 def test_show_missing_ci_installer_type(self, *args):
79 self._test_show_missing_env_var('INSTALLER_TYPE', *args)
81 @mock.patch('functest.cli.commands.cli_env.git.Repo')
82 def test_show_missing_ci_installer_ip(self, *args):
83 self._test_show_missing_env_var('INSTALLER_IP', *args)
85 @mock.patch('functest.cli.commands.cli_env.git.Repo')
86 def test_show_missing_ci_scenario(self, *args):
87 self._test_show_missing_env_var('SCENARIO', *args)
89 @mock.patch('functest.cli.commands.cli_env.git.Repo')
90 def test_show_missing_ci_node(self, *args):
91 self._test_show_missing_env_var('NODE', *args)
93 @mock.patch('functest.cli.commands.cli_env.git.Repo')
94 def test_show_missing_ci_build_tag(self, *args):
95 self._test_show_missing_env_var('BUILD_TAG', *args)
97 @mock.patch('functest.cli.commands.cli_env.git.Repo')
98 def test_show_missing_ci_debug(self, *args):
99 self._test_show_missing_env_var('DEBUG', *args)
101 @mock.patch('functest.cli.commands.cli_env.git.Repo')
102 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
104 def test_show_missing_environment(self, *args):
105 self._test_show_missing_env_var('STATUS', *args)
107 @mock.patch('functest.cli.commands.cli_env.os.path.exists',
109 def test_show_missing_git_repo_dir(self, *args):
110 CONST.dir_repo_functest = None
111 self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show())
113 @mock.patch('functest.cli.commands.cli_env.click.echo')
114 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
116 def test_status_environment_present(self, mock_path, mock_click_echo):
117 self.assertEqual(self.cli_environ.status(), 0)
118 mock_click_echo.assert_called_with("Functest environment"
119 " ready to run tests.\n")
121 @mock.patch('functest.cli.commands.cli_env.click.echo')
122 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
124 def test_status_environment_absent(self, mock_path, mock_click_echo):
125 self.assertEqual(self.cli_environ.status(), 1)
126 mock_click_echo.assert_called_with("Functest environment"
127 " is not installed.\n")
130 if __name__ == "__main__":
131 unittest.main(verbosity=2)