e5c409bf48263800ca50958ca8512030ad3add7e
[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 # pylint: disable=missing-docstring
9
10 import logging
11 import re
12 import unittest
13
14 import mock
15
16 from functest.cli.commands import cli_env
17 from functest.utils.constants import CONST
18
19
20 class RegexMatch(object):  # pylint: disable=too-few-public-methods
21     def __init__(self, msg):
22         self.msg = msg
23
24     def __eq__(self, other):
25         match = re.search(self.msg, other)
26         return match is not None
27
28
29 class CliEnvTesting(unittest.TestCase):
30
31     def setUp(self):
32         self.cli_environ = cli_env.CliEnv()
33
34     def _test_show_missing_env_var(self, var, *args):
35         # pylint: disable=unused-argument
36         if var == 'INSTALLER_TYPE':
37             CONST.__setattr__('INSTALLER_TYPE', None)
38             reg_string = r"|  INSTALLER: Unknown, \S+\s*|"
39         elif var == 'INSTALLER_IP':
40             CONST.__setattr__('INSTALLER_IP', None)
41             reg_string = r"|  INSTALLER: \S+, Unknown\s*|"
42         elif var == 'SCENARIO':
43             CONST.__setattr__('DEPLOY_SCENARIO', None)
44             reg_string = r"|   SCENARIO: Unknown\s*|"
45         elif var == 'NODE':
46             CONST.__setattr__('NODE_NAME', None)
47             reg_string = r"|        POD: Unknown\s*|"
48         elif var == 'BUILD_TAG':
49             CONST.__setattr__('BUILD_TAG', None)
50             reg_string = r"|  BUILD TAG: None|"
51         elif var == 'DEBUG':
52             CONST.__setattr__('CI_DEBUG', None)
53             reg_string = r"| DEBUG FLAG: false\s*|"
54
55         with mock.patch('functest.cli.commands.cli_env.click.echo') \
56                 as mock_click_echo:
57             self.cli_environ.show()
58             mock_click_echo.assert_called_with(RegexMatch(reg_string))
59
60     def test_show_ci_installer_type_ko(self, *args):
61         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
62
63     def test_show_ci_installer_ip_ko(self, *args):
64         self._test_show_missing_env_var('INSTALLER_IP', *args)
65
66     def test_show_missing_ci_scenario(self, *args):
67         self._test_show_missing_env_var('SCENARIO', *args)
68
69     def test_show_missing_ci_node(self, *args):
70         self._test_show_missing_env_var('NODE', *args)
71
72     def test_show_missing_ci_build_tag(self, *args):
73         self._test_show_missing_env_var('BUILD_TAG', *args)
74
75     def test_show_missing_ci_debug(self, *args):
76         self._test_show_missing_env_var('DEBUG', *args)
77
78
79 if __name__ == "__main__":
80     logging.disable(logging.CRITICAL)
81     unittest.main(verbosity=2)