blob: 1f813762129c8aa3b9c91951c332d8d5a2736a5d [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alex9a4ad212020-10-01 18:04:25 -05003import os
4
Alex3bc95f62020-03-05 17:00:04 -06005from unittest import mock
6
Alexccb72e02021-01-20 16:38:03 -06007from cfg_checker.common.settings import pkg_dir
Alex9a4ad212020-10-01 18:04:25 -05008from tests.mocks import _fake_kube_config_path
Alex3bc95f62020-03-05 17:00:04 -06009from tests.test_base import CfgCheckerTestBase
10
11
Alex9a4ad212020-10-01 18:04:25 -050012os.environ['MCP_TYPE_FORCE'] = 'SALT'
Alexccb72e02021-01-20 16:38:03 -060013_env_name = 'local'
14_env_file = os.path.join(pkg_dir, 'etc', _env_name + '.env')
Alex9a4ad212020-10-01 18:04:25 -050015
16
Alex3bc95f62020-03-05 17:00:04 -060017class TestCliCommands(CfgCheckerTestBase):
Alex9a4ad212020-10-01 18:04:25 -050018 def setUp(self):
19 # force env type to salt
20 os.environ['MCP_TYPE_FORCE'] = 'SALT'
21
22 def tearDown(self):
23 del os.environ['MCP_TYPE_FORCE']
24
Alex3bc95f62020-03-05 17:00:04 -060025 def test_do_cli_main_command(self):
26 _module_name = 'cfg_checker.cfg_check'
27 _m = self._try_import(_module_name)
28 with self.save_arguments():
29 with self.redirect_output():
30 with self.assertRaises(SystemExit) as ep:
31 import sys
32 sys.argv = ["fake.py", "reclass", "list", "-p", "/tmp"]
33 _m.cfg_check.config_check_entrypoint()
34
35 self.assertEqual(
36 ep.exception.code,
37 0,
38 "'mcp-checker reclass list -p /tmp' command failed"
39 )
40
41 def test_do_cli_main_command_debug(self):
42 _module_name = 'cfg_checker.cfg_check'
43 _m = self._try_import(_module_name)
44 with self.save_arguments():
45 with self.redirect_output():
46 with self.assertRaises(SystemExit) as ep:
47 import sys
48 sys.argv = [
49 "fake.py",
50 "-d",
51 "reclass",
52 "list",
53 "-p",
54 "/tmp"
55 ]
56 _m.cfg_check.config_check_entrypoint()
57
58 self.assertEqual(
59 ep.exception.code,
60 0,
61 "mcp-checker command failes"
62 )
63
64 def test_cli_main_unknown_argument(self):
65 _module_name = 'cfg_checker.cfg_check'
66 _m = self._try_import(_module_name)
67 with self.redirect_output():
68 with self.assertRaises(SystemExit) as ep:
69 import sys
70 sys.argv.append("reclass")
71 sys.argv.append("list")
72 _m.cfg_check.config_check_entrypoint()
73
74 self.assertEqual(
75 ep.exception.code,
76 1,
77 "Unknown argument not handled"
78 )
79
80 def test_do_cli_module_command(self):
81 _module_name = 'cfg_checker.cli.command'
82 _m = self._try_import(_module_name)
83 _command = "reclass"
84 with self.save_arguments():
85 with self.redirect_output():
86 with self.assertRaises(SystemExit) as ep:
87 import sys
88 sys.argv = ["fake.py", "list", "-p", "/tmp"]
89 _m.cli.command.cli_command(
90 "Fake Reclass Comparer",
91 _command
92 )
93
94 self.assertEqual(
95 ep.exception.code,
96 0,
97 "Cli command execution failed"
98 )
99
100 def test_do_cli_module_command_with_error(self):
101 _module_name = 'cfg_checker.cli.command'
102 _m = self._try_import(_module_name)
103 _command = "reclass"
104 with self.save_arguments():
105 with self.redirect_output():
106 with self.assertRaises(SystemExit) as ep:
107 import sys
108 sys.argv = ["fake.py", "list", "-p", "/notexistingfolder"]
109 _m.cli.command.cli_command(
110 "Fake Reclass Comparer",
111 _command
112 )
113
114 self.assertEqual(
115 ep.exception.code,
116 1,
117 "Cli command execution failed"
118 )
119
120 def test_cli_module_unknown_command(self):
121 _module_name = 'cfg_checker.cli.command'
122 _m = self._try_import(_module_name)
123 _fake_args = mock.MagicMock(name="FakeArgsClass")
124 _command = "unknowncommand"
Alex9a4ad212020-10-01 18:04:25 -0500125 _config = None
Alex3bc95f62020-03-05 17:00:04 -0600126 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500127 _r_value = _m.cli.command.execute_command(
128 _fake_args,
129 _command,
130 _config
131 )
Alex3bc95f62020-03-05 17:00:04 -0600132
133 self.assertEqual(
134 _r_value,
135 1,
136 "Unknown command 'type' not handled"
137 )
138
139 def test_cli_module_no_type(self):
140 _module_name = 'cfg_checker.cli.command'
141 _m = self._try_import(_module_name)
142 _type = {}
143 _command = "unknowncommand"
Alex9a4ad212020-10-01 18:04:25 -0500144 _config = None
Alex3bc95f62020-03-05 17:00:04 -0600145 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500146 _r_value = _m.cli.command.execute_command(_type, _command, _config)
Alex3bc95f62020-03-05 17:00:04 -0600147
148 self.assertEqual(
149 _r_value,
150 1,
151 "Unknown command not handled"
152 )
153
154 def test_cli_module_unknown_type(self):
155 _module_name = 'cfg_checker.cli.command'
156 _m = self._try_import(_module_name)
157 _fake_args = mock.MagicMock(name="FakeArgsClass")
Alexccb72e02021-01-20 16:38:03 -0600158 _fake_args.kube_config = _fake_kube_config_path
159 _fake_args.env_name = _env_name
160 _fake_args.env_config = _env_file
Alex3bc95f62020-03-05 17:00:04 -0600161 _command = "reclass"
Alex9a4ad212020-10-01 18:04:25 -0500162
163 from cfg_checker.common.settings import CheckerConfiguration
164 _config = CheckerConfiguration(_fake_args)
165
Alex3bc95f62020-03-05 17:00:04 -0600166 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500167 _r_value = _m.cli.command.execute_command(
168 _fake_args,
169 _command,
170 _config
171 )
Alex3bc95f62020-03-05 17:00:04 -0600172
173 self.assertEqual(
174 _r_value,
175 1,
176 "Unknown command not handled"
177 )
178
179 def test_cli_module_unknown_argument(self):
180 _module_name = 'cfg_checker.cli.command'
181 _m = self._try_import(_module_name)
182 _command = "reclass"
183 with self.redirect_output():
184 with self.assertRaises(SystemExit) as ep:
185 _m.cli.command.cli_command(
186 "Fake Reclass Comparer",
187 _command
188 )
189
190 self.assertEqual(
191 ep.exception.code,
192 1,
193 "Unknown argument not handled"
194 )