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):
22 self.cli_environ = cli_env.CliEnv()
24 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
26 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
27 def test_prepare_default(self, mock_ft_utils, mock_os):
28 cmd = ("python %s/functest/ci/prepare_env.py start" %
29 CONST.__getattribute__('dir_repo_functest'))
30 self.cli_environ.prepare()
31 mock_ft_utils.assert_called_with(cmd)
33 @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
35 @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
36 def test_prepare_missing_status(self, mock_ft_utils, mock_os):
37 with mock.patch('__builtin__.raw_input', return_value="y"), \
38 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
40 cmd = ("python %s/functest/ci/prepare_env.py start" %
41 CONST.__getattribute__('dir_repo_functest'))
42 self.cli_environ.prepare()
43 mock_os_remove.assert_called_once_with(
44 CONST.__getattribute__('env_active'))
45 mock_ft_utils.assert_called_with(cmd)
47 def _test_show_missing_env_var(self, var, *args):
48 if var == 'INSTALLER_TYPE':
49 CONST.__setattr__('INSTALLER_TYPE', None)
50 reg_string = "| INSTALLER: Unknown, \S+\s*|"
51 elif var == 'INSTALLER_IP':
52 CONST.__setattr__('INSTALLER_IP', None)
53 reg_string = "| INSTALLER: \S+, Unknown\s*|"
54 elif var == 'SCENARIO':
55 CONST.__setattr__('DEPLOY_SCENARIO', None)
56 reg_string = "| SCENARIO: Unknown\s*|"
58 CONST.__setattr__('NODE_NAME', None)
59 reg_string = "| POD: Unknown\s*|"
60 elif var == 'BUILD_TAG':
61 CONST.__setattr__('BUILD_TAG', None)
62 reg_string = "| BUILD TAG: None|"
64 CONST.__setattr__('CI_DEBUG', None)
65 reg_string = "| DEBUG FLAG: false\s*|"
67 reg_string = "| STATUS: not ready\s*|"
69 with mock.patch('functest.cli.commands.cli_env.click.echo') \
71 self.cli_environ.show()
72 mock_click_echo.assert_called_with(test_utils.
73 RegexMatch(reg_string))
75 @mock.patch('functest.cli.commands.cli_env.git.Repo')
76 def test_show_missing_ci_installer_type(self, *args):
77 self._test_show_missing_env_var('INSTALLER_TYPE', *args)
79 @mock.patch('functest.cli.commands.cli_env.git.Repo')
80 def test_show_missing_ci_installer_ip(self, *args):
81 self._test_show_missing_env_var('INSTALLER_IP', *args)
83 @mock.patch('functest.cli.commands.cli_env.git.Repo')
84 def test_show_missing_ci_scenario(self, *args):
85 self._test_show_missing_env_var('SCENARIO', *args)
87 @mock.patch('functest.cli.commands.cli_env.git.Repo')
88 def test_show_missing_ci_node(self, *args):
89 self._test_show_missing_env_var('NODE', *args)
91 @mock.patch('functest.cli.commands.cli_env.git.Repo')
92 def test_show_missing_ci_build_tag(self, *args):
93 self._test_show_missing_env_var('BUILD_TAG', *args)
95 @mock.patch('functest.cli.commands.cli_env.git.Repo')
96 def test_show_missing_ci_debug(self, *args):
97 self._test_show_missing_env_var('DEBUG', *args)
99 @mock.patch('functest.cli.commands.cli_env.git.Repo')
100 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
102 def test_show_missing_environment(self, *args):
103 self._test_show_missing_env_var('STATUS', *args)
105 @mock.patch('functest.cli.commands.cli_env.os.path.exists',
107 def test_show_missing_git_repo_dir(self, *args):
108 CONST.__setattr__('dir_repo_functest', None)
109 self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show())
111 @mock.patch('functest.cli.commands.cli_env.click.echo')
112 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
114 def test_status_environment_present(self, mock_path, mock_click_echo):
115 self.assertEqual(self.cli_environ.status(), 0)
116 mock_click_echo.assert_called_with("Functest environment"
117 " ready to run tests.\n")
119 @mock.patch('functest.cli.commands.cli_env.click.echo')
120 @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
122 def test_status_environment_absent(self, mock_path, mock_click_echo):
123 self.assertEqual(self.cli_environ.status(), 1)
124 mock_click_echo.assert_called_with("Functest environment"
125 " is not installed.\n")
128 if __name__ == "__main__":
129 logging.disable(logging.CRITICAL)
130 unittest.main(verbosity=2)