Merge RegexMatch into test_cli_env.py
[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):
21     def __init__(self, msg):
22         self.msg = msg
23
24     def __eq__(self, other):
25         match = re.search(self.msg, other)
26         if match:
27             return True
28         return False
29
30
31 class CliEnvTesting(unittest.TestCase):
32
33     def setUp(self):
34         self.cli_environ = cli_env.CliEnv()
35
36     def _test_show_missing_env_var(self, var, *args):
37         if var == 'INSTALLER_TYPE':
38             CONST.__setattr__('INSTALLER_TYPE', None)
39             reg_string = "|  INSTALLER: Unknown, \S+\s*|"
40         elif var == 'INSTALLER_IP':
41             CONST.__setattr__('INSTALLER_IP', None)
42             reg_string = "|  INSTALLER: \S+, Unknown\s*|"
43         elif var == 'SCENARIO':
44             CONST.__setattr__('DEPLOY_SCENARIO', None)
45             reg_string = "|   SCENARIO: Unknown\s*|"
46         elif var == 'NODE':
47             CONST.__setattr__('NODE_NAME', None)
48             reg_string = "|        POD: Unknown\s*|"
49         elif var == 'BUILD_TAG':
50             CONST.__setattr__('BUILD_TAG', None)
51             reg_string = "|  BUILD TAG: None|"
52         elif var == 'DEBUG':
53             CONST.__setattr__('CI_DEBUG', None)
54             reg_string = "| DEBUG FLAG: false\s*|"
55
56         with mock.patch('functest.cli.commands.cli_env.click.echo') \
57                 as mock_click_echo:
58             self.cli_environ.show()
59             mock_click_echo.assert_called_with(RegexMatch(reg_string))
60
61     def test_show_missing_ci_installer_type(self, *args):
62         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
63
64     def test_show_missing_ci_installer_ip(self, *args):
65         self._test_show_missing_env_var('INSTALLER_IP', *args)
66
67     def test_show_missing_ci_scenario(self, *args):
68         self._test_show_missing_env_var('SCENARIO', *args)
69
70     def test_show_missing_ci_node(self, *args):
71         self._test_show_missing_env_var('NODE', *args)
72
73     def test_show_missing_ci_build_tag(self, *args):
74         self._test_show_missing_env_var('BUILD_TAG', *args)
75
76     def test_show_missing_ci_debug(self, *args):
77         self._test_show_missing_env_var('DEBUG', *args)
78
79
80 if __name__ == "__main__":
81     logging.disable(logging.CRITICAL)
82     unittest.main(verbosity=2)