Merge "Enable cloudify_ims"
[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 = "prepare_env start"
28         self.cli_environ.prepare()
29         mock_ft_utils.assert_called_with(cmd)
30
31     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
32                 return_value=True)
33     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
34     def test_prepare_missing_status(self, mock_ft_utils, mock_os):
35         with mock.patch('__builtin__.raw_input', return_value="y"), \
36                 mock.patch('functest.cli.commands.cli_testcase.os.remove') \
37                 as mock_os_remove:
38             cmd = "prepare_env start"
39             self.cli_environ.prepare()
40             mock_os_remove.assert_called_once_with(
41                 CONST.__getattribute__('env_active'))
42             mock_ft_utils.assert_called_with(cmd)
43
44     def _test_show_missing_env_var(self, var, *args):
45         if var == 'INSTALLER_TYPE':
46             CONST.__setattr__('INSTALLER_TYPE', None)
47             reg_string = "|  INSTALLER: Unknown, \S+\s*|"
48         elif var == 'INSTALLER_IP':
49             CONST.__setattr__('INSTALLER_IP', None)
50             reg_string = "|  INSTALLER: \S+, Unknown\s*|"
51         elif var == 'SCENARIO':
52             CONST.__setattr__('DEPLOY_SCENARIO', None)
53             reg_string = "|   SCENARIO: Unknown\s*|"
54         elif var == 'NODE':
55             CONST.__setattr__('NODE_NAME', None)
56             reg_string = "|        POD: Unknown\s*|"
57         elif var == 'BUILD_TAG':
58             CONST.__setattr__('BUILD_TAG', None)
59             reg_string = "|  BUILD TAG: None|"
60         elif var == 'DEBUG':
61             CONST.__setattr__('CI_DEBUG', None)
62             reg_string = "| DEBUG FLAG: false\s*|"
63         elif var == 'STATUS':
64             reg_string = "|     STATUS: not ready\s*|"
65
66         with mock.patch('functest.cli.commands.cli_env.click.echo') \
67                 as mock_click_echo:
68             self.cli_environ.show()
69             mock_click_echo.assert_called_with(test_utils.
70                                                RegexMatch(reg_string))
71
72     def test_show_missing_ci_installer_type(self, *args):
73         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
74
75     def test_show_missing_ci_installer_ip(self, *args):
76         self._test_show_missing_env_var('INSTALLER_IP', *args)
77
78     def test_show_missing_ci_scenario(self, *args):
79         self._test_show_missing_env_var('SCENARIO', *args)
80
81     def test_show_missing_ci_node(self, *args):
82         self._test_show_missing_env_var('NODE', *args)
83
84     def test_show_missing_ci_build_tag(self, *args):
85         self._test_show_missing_env_var('BUILD_TAG', *args)
86
87     def test_show_missing_ci_debug(self, *args):
88         self._test_show_missing_env_var('DEBUG', *args)
89
90     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
91                 return_value=False)
92     def test_show_missing_environment(self, *args):
93         self._test_show_missing_env_var('STATUS', *args)
94
95     @mock.patch('functest.cli.commands.cli_env.click.echo')
96     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
97                 return_value=True)
98     def test_status_environment_present(self, mock_path, mock_click_echo):
99         self.assertEqual(self.cli_environ.status(), 0)
100         mock_click_echo.assert_called_with("Functest environment"
101                                            " ready to run tests.\n")
102
103     @mock.patch('functest.cli.commands.cli_env.click.echo')
104     @mock.patch('functest.cli.commands.cli_env.os.path.isfile',
105                 return_value=False)
106     def test_status_environment_absent(self, mock_path, mock_click_echo):
107         self.assertEqual(self.cli_environ.status(), 1)
108         mock_click_echo.assert_called_with("Functest environment"
109                                            " is not installed.\n")
110
111
112 if __name__ == "__main__":
113     logging.disable(logging.CRITICAL)
114     unittest.main(verbosity=2)