Merge "Read env vars instead of using CONST in API"
[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 os
12 import re
13 import unittest
14
15 import mock
16
17 from functest.cli.commands import cli_env
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             os.environ['INSTALLER_TYPE'] = ''
38             reg_string = r"|  INSTALLER: Unknown, \S+\s*|"
39         elif var == 'INSTALLER_IP':
40             os.environ['INSTALLER_IP'] = ''
41             reg_string = r"|  INSTALLER: \S+, Unknown\s*|"
42         elif var == 'SCENARIO':
43             os.environ['DEPLOY_SCENARIO'] = ''
44             reg_string = r"|   SCENARIO: Unknown\s*|"
45         elif var == 'NODE':
46             os.environ['NODE_NAME'] = ''
47             reg_string = r"|        POD: Unknown\s*|"
48         elif var == 'BUILD_TAG':
49             os.environ['BUILD_TAG'] = ''
50             reg_string = r"|  BUILD TAG: None|"
51
52         with mock.patch('functest.cli.commands.cli_env.click.echo') \
53                 as mock_click_echo:
54             self.cli_environ.show()
55             mock_click_echo.assert_called_with(RegexMatch(reg_string))
56
57     def test_show_ci_installer_type_ko(self, *args):
58         self._test_show_missing_env_var('INSTALLER_TYPE', *args)
59
60     def test_show_ci_installer_ip_ko(self, *args):
61         self._test_show_missing_env_var('INSTALLER_IP', *args)
62
63     def test_show_missing_ci_scenario(self, *args):
64         self._test_show_missing_env_var('SCENARIO', *args)
65
66     def test_show_missing_ci_node(self, *args):
67         self._test_show_missing_env_var('NODE', *args)
68
69     def test_show_missing_ci_build_tag(self, *args):
70         self._test_show_missing_env_var('BUILD_TAG', *args)
71
72
73 if __name__ == "__main__":
74     logging.disable(logging.CRITICAL)
75     unittest.main(verbosity=2)