Merge "Leverage logging config and refactor the logger"
[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 from git.exc import NoSuchPathError
12 import mock
13
14 from functest.cli.commands import cli_env
15 from functest.utils.constants import CONST
16 from functest.tests.unit import test_utils
17
18
19 class CliEnvTesting(unittest.TestCase):
20
21     logging.disable(logging.CRITICAL)
22
23     def setUp(self):
24         self.cli_environ = cli_env.CliEnv()
25
26     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
27                 return_value=False)
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)
34
35     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
36                 return_value=True)
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') \
41                 as mock_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)
47
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*|"
58         elif var == 'NODE':
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|"
64         elif var == 'DEBUG':
65             CONST.CI_DEBUG = None
66             reg_string = "| DEBUG FLAG: false\s*|"
67         elif var == 'STATUS':
68             reg_string = "|     STATUS: not ready\s*|"
69
70         with mock.patch('functest.cli.commands.cli_env.click.echo') \
71                 as mock_click_echo:
72             self.cli_environ.show()
73             mock_click_echo.assert_called_with(test_utils.
74                                                RegexMatch(reg_string))
75
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)
79
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)
83
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)
87
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)
91
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)
95
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)
99
100     @mock.patch('functest.cli.commands.cli_env.git.Repo')
101     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
102                 return_value=False)
103     def test_show_missing_environment(self, *args):
104         self._test_show_missing_env_var('STATUS', *args)
105
106     @mock.patch('functest.cli.commands.cli_env.os.path.exists',
107                 return_value=False)
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())
111
112     @mock.patch('functest.cli.commands.cli_env.click.echo')
113     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
114                 return_value=True)
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")
119
120     @mock.patch('functest.cli.commands.cli_env.click.echo')
121     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
122                 return_value=False)
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")
127
128
129 if __name__ == "__main__":
130     unittest.main(verbosity=2)