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
13 from functest.cli.commands import cli_env
14 from functest.utils.constants import CONST
15 from functest.tests.unit import test_utils
18 class CliEnvTesting(unittest.TestCase):
21 self.cli_environ = cli_env.CliEnv()
23 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
25 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
26 def test_prepare_default(self, mock_ft_utils, mock_os):
27 cmd = ("python %s/functest/ci/prepare_env.py start" %
28 CONST.__getattribute__('dir_repo_functest'))
29 self.cli_environ.prepare()
30 mock_ft_utils.assert_called_with(cmd)
32 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
34 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
35 def test_prepare_missing_status(self, mock_ft_utils, mock_os):
36 with mock.patch('__builtin__.raw_input', return_value="y"), \
37 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
39 cmd = ("python %s/functest/ci/prepare_env.py start" %
40 CONST.__getattribute__('dir_repo_functest'))
41 self.cli_environ.prepare()
42 mock_os_remove.assert_called_once_with(
43 CONST.__getattribute__('env_active'))
44 mock_ft_utils.assert_called_with(cmd)
46 def _test_show_missing_env_var(self, var, *args):
47 if var == 'INSTALLER_TYPE':
48 CONST.__setattr__('INSTALLER_TYPE', None)
49 reg_string = "| INSTALLER: Unknown, \S+\s*|"
50 elif var == 'INSTALLER_IP':
51 CONST.__setattr__('INSTALLER_IP', None)
52 reg_string = "| INSTALLER: \S+, Unknown\s*|"
53 elif var == 'SCENARIO':
54 CONST.__setattr__('DEPLOY_SCENARIO', None)
55 reg_string = "| SCENARIO: Unknown\s*|"
57 CONST.__setattr__('NODE_NAME', None)
58 reg_string = "| POD: Unknown\s*|"
59 elif var == 'BUILD_TAG':
60 CONST.__setattr__('BUILD_TAG', None)
61 reg_string = "| BUILD TAG: None|"
63 CONST.__setattr__('CI_DEBUG', None)
64 reg_string = "| DEBUG FLAG: false\s*|"
66 reg_string = "| STATUS: not ready\s*|"
68 with mock.patch('functest.cli.commands.cli_env.click.echo') \
70 self.cli_environ.show()
71 mock_click_echo.assert_called_with(test_utils.
72 RegexMatch(reg_string))
74 def test_show_missing_ci_installer_type(self, *args):
75 self._test_show_missing_env_var('INSTALLER_TYPE', *args)
77 def test_show_missing_ci_installer_ip(self, *args):
78 self._test_show_missing_env_var('INSTALLER_IP', *args)
80 def test_show_missing_ci_scenario(self, *args):
81 self._test_show_missing_env_var('SCENARIO', *args)
83 def test_show_missing_ci_node(self, *args):
84 self._test_show_missing_env_var('NODE', *args)
86 def test_show_missing_ci_build_tag(self, *args):
87 self._test_show_missing_env_var('BUILD_TAG', *args)
89 def test_show_missing_ci_debug(self, *args):
90 self._test_show_missing_env_var('DEBUG', *args)
92 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
94 def test_show_missing_environment(self, *args):
95 self._test_show_missing_env_var('STATUS', *args)
97 @mock.patch('functest.cli.commands.cli_env.click.echo')
98 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
100 def test_status_environment_present(self, mock_path, mock_click_echo):
101 self.assertEqual(self.cli_environ.status(), 0)
102 mock_click_echo.assert_called_with("Functest environment"
103 " ready to run tests.\n")
105 @mock.patch('functest.cli.commands.cli_env.click.echo')
106 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
108 def test_status_environment_absent(self, mock_path, mock_click_echo):
109 self.assertEqual(self.cli_environ.status(), 1)
110 mock_click_echo.assert_called_with("Functest environment"
111 " is not installed.\n")
114 if __name__ == "__main__":
115 logging.disable(logging.CRITICAL)
116 unittest.main(verbosity=2)