Stop getting git data from functest dir
[functest.git] / functest / tests / unit / cli / commands / test_cli_env.py
1 #!/usr/bin/env python
2
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
7
8 import logging
9 import unittest
10
11 import mock
12
13 from functest.cli.commands import cli_env
14 from functest.utils.constants import CONST
15 from functest.tests.unit import test_utils
16
17
18 class CliEnvTesting(unittest.TestCase):
19
20     def setUp(self):
21         self.cli_environ = cli_env.CliEnv()
22
23     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
24                 return_value=False)
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)
31
32     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
33                 return_value=True)
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') \
38                 as mock_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)
45
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*|"
56         elif var == 'NODE':
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|"
62         elif var == 'DEBUG':
63             CONST.__setattr__('CI_DEBUG', None)
64             reg_string = "| DEBUG FLAG: false\s*|"
65         elif var == 'STATUS':
66             reg_string = "|     STATUS: not ready\s*|"
67
68         with mock.patch('functest.cli.commands.cli_env.click.echo') \
69                 as mock_click_echo:
70             self.cli_environ.show()
71             mock_click_echo.assert_called_with(test_utils.
72                                                RegexMatch(reg_string))
73
74     def test_show_missing_ci_installer_type(self, *args):
75         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
76
77     def test_show_missing_ci_installer_ip(self, *args):
78         self._test_show_missing_env_var('INSTALLER_IP', *args)
79
80     def test_show_missing_ci_scenario(self, *args):
81         self._test_show_missing_env_var('SCENARIO', *args)
82
83     def test_show_missing_ci_node(self, *args):
84         self._test_show_missing_env_var('NODE', *args)
85
86     def test_show_missing_ci_build_tag(self, *args):
87         self._test_show_missing_env_var('BUILD_TAG', *args)
88
89     def test_show_missing_ci_debug(self, *args):
90         self._test_show_missing_env_var('DEBUG', *args)
91
92     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
93                 return_value=False)
94     def test_show_missing_environment(self, *args):
95         self._test_show_missing_env_var('STATUS', *args)
96
97     @mock.patch('functest.cli.commands.cli_env.click.echo')
98     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
99                 return_value=True)
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")
104
105     @mock.patch('functest.cli.commands.cli_env.click.echo')
106     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
107                 return_value=False)
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")
112
113
114 if __name__ == "__main__":
115     logging.disable(logging.CRITICAL)
116     unittest.main(verbosity=2)