blob: bad05c902ff70d593a0eaee7ac923c0a7829e82b [file] [log] [blame]
Alex9a4ad212020-10-01 18:04:25 -05001import os
2
Alex3bc95f62020-03-05 17:00:04 -06003from unittest import mock
4
Alexccb72e02021-01-20 16:38:03 -06005from cfg_checker.common.settings import pkg_dir
Alex9a4ad212020-10-01 18:04:25 -05006from tests.mocks import _fake_kube_config_path
Alex3bc95f62020-03-05 17:00:04 -06007from tests.test_base import CfgCheckerTestBase
8
9
Alex9a4ad212020-10-01 18:04:25 -050010os.environ['MCP_TYPE_FORCE'] = 'SALT'
Alexccb72e02021-01-20 16:38:03 -060011_env_name = 'local'
12_env_file = os.path.join(pkg_dir, 'etc', _env_name + '.env')
Alex9a4ad212020-10-01 18:04:25 -050013
14
Alex3bc95f62020-03-05 17:00:04 -060015class TestCliCommands(CfgCheckerTestBase):
Alex9a4ad212020-10-01 18:04:25 -050016 def setUp(self):
17 # force env type to salt
18 os.environ['MCP_TYPE_FORCE'] = 'SALT'
19
20 def tearDown(self):
21 del os.environ['MCP_TYPE_FORCE']
22
Alex3bc95f62020-03-05 17:00:04 -060023 def test_do_cli_main_command(self):
24 _module_name = 'cfg_checker.cfg_check'
25 _m = self._try_import(_module_name)
26 with self.save_arguments():
27 with self.redirect_output():
28 with self.assertRaises(SystemExit) as ep:
29 import sys
30 sys.argv = ["fake.py", "reclass", "list", "-p", "/tmp"]
31 _m.cfg_check.config_check_entrypoint()
32
33 self.assertEqual(
34 ep.exception.code,
35 0,
36 "'mcp-checker reclass list -p /tmp' command failed"
37 )
38
39 def test_do_cli_main_command_debug(self):
40 _module_name = 'cfg_checker.cfg_check'
41 _m = self._try_import(_module_name)
42 with self.save_arguments():
43 with self.redirect_output():
44 with self.assertRaises(SystemExit) as ep:
45 import sys
46 sys.argv = [
47 "fake.py",
48 "-d",
49 "reclass",
50 "list",
51 "-p",
52 "/tmp"
53 ]
54 _m.cfg_check.config_check_entrypoint()
55
56 self.assertEqual(
57 ep.exception.code,
58 0,
59 "mcp-checker command failes"
60 )
61
62 def test_cli_main_unknown_argument(self):
63 _module_name = 'cfg_checker.cfg_check'
64 _m = self._try_import(_module_name)
65 with self.redirect_output():
66 with self.assertRaises(SystemExit) as ep:
67 import sys
68 sys.argv.append("reclass")
69 sys.argv.append("list")
70 _m.cfg_check.config_check_entrypoint()
71
72 self.assertEqual(
73 ep.exception.code,
74 1,
75 "Unknown argument not handled"
76 )
77
78 def test_do_cli_module_command(self):
79 _module_name = 'cfg_checker.cli.command'
80 _m = self._try_import(_module_name)
81 _command = "reclass"
82 with self.save_arguments():
83 with self.redirect_output():
84 with self.assertRaises(SystemExit) as ep:
85 import sys
86 sys.argv = ["fake.py", "list", "-p", "/tmp"]
87 _m.cli.command.cli_command(
88 "Fake Reclass Comparer",
89 _command
90 )
91
92 self.assertEqual(
93 ep.exception.code,
94 0,
95 "Cli command execution failed"
96 )
97
98 def test_do_cli_module_command_with_error(self):
99 _module_name = 'cfg_checker.cli.command'
100 _m = self._try_import(_module_name)
101 _command = "reclass"
102 with self.save_arguments():
103 with self.redirect_output():
104 with self.assertRaises(SystemExit) as ep:
105 import sys
106 sys.argv = ["fake.py", "list", "-p", "/notexistingfolder"]
107 _m.cli.command.cli_command(
108 "Fake Reclass Comparer",
109 _command
110 )
111
112 self.assertEqual(
113 ep.exception.code,
114 1,
115 "Cli command execution failed"
116 )
117
118 def test_cli_module_unknown_command(self):
119 _module_name = 'cfg_checker.cli.command'
120 _m = self._try_import(_module_name)
121 _fake_args = mock.MagicMock(name="FakeArgsClass")
122 _command = "unknowncommand"
Alex9a4ad212020-10-01 18:04:25 -0500123 _config = None
Alex3bc95f62020-03-05 17:00:04 -0600124 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500125 _r_value = _m.cli.command.execute_command(
126 _fake_args,
127 _command,
128 _config
129 )
Alex3bc95f62020-03-05 17:00:04 -0600130
131 self.assertEqual(
132 _r_value,
133 1,
134 "Unknown command 'type' not handled"
135 )
136
137 def test_cli_module_no_type(self):
138 _module_name = 'cfg_checker.cli.command'
139 _m = self._try_import(_module_name)
140 _type = {}
141 _command = "unknowncommand"
Alex9a4ad212020-10-01 18:04:25 -0500142 _config = None
Alex3bc95f62020-03-05 17:00:04 -0600143 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500144 _r_value = _m.cli.command.execute_command(_type, _command, _config)
Alex3bc95f62020-03-05 17:00:04 -0600145
146 self.assertEqual(
147 _r_value,
148 1,
149 "Unknown command not handled"
150 )
151
152 def test_cli_module_unknown_type(self):
153 _module_name = 'cfg_checker.cli.command'
154 _m = self._try_import(_module_name)
155 _fake_args = mock.MagicMock(name="FakeArgsClass")
Alexccb72e02021-01-20 16:38:03 -0600156 _fake_args.kube_config = _fake_kube_config_path
157 _fake_args.env_name = _env_name
158 _fake_args.env_config = _env_file
Alex3bc95f62020-03-05 17:00:04 -0600159 _command = "reclass"
Alex9a4ad212020-10-01 18:04:25 -0500160
161 from cfg_checker.common.settings import CheckerConfiguration
162 _config = CheckerConfiguration(_fake_args)
163
Alex3bc95f62020-03-05 17:00:04 -0600164 with self.redirect_output():
Alex9a4ad212020-10-01 18:04:25 -0500165 _r_value = _m.cli.command.execute_command(
166 _fake_args,
167 _command,
168 _config
169 )
Alex3bc95f62020-03-05 17:00:04 -0600170
171 self.assertEqual(
172 _r_value,
173 1,
174 "Unknown command not handled"
175 )
176
177 def test_cli_module_unknown_argument(self):
178 _module_name = 'cfg_checker.cli.command'
179 _m = self._try_import(_module_name)
180 _command = "reclass"
181 with self.redirect_output():
182 with self.assertRaises(SystemExit) as ep:
183 _m.cli.command.cli_command(
184 "Fake Reclass Comparer",
185 _command
186 )
187
188 self.assertEqual(
189 ep.exception.code,
190 1,
191 "Unknown argument not handled"
192 )