Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 1 | import os |
| 2 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 3 | from unittest import mock |
| 4 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 5 | from tests.mocks import _fake_kube_config_path |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 6 | from tests.test_base import CfgCheckerTestBase |
| 7 | |
| 8 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 9 | os.environ['MCP_TYPE_FORCE'] = 'SALT' |
| 10 | |
| 11 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 12 | class TestCliCommands(CfgCheckerTestBase): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 13 | def setUp(self): |
| 14 | # force env type to salt |
| 15 | os.environ['MCP_TYPE_FORCE'] = 'SALT' |
| 16 | |
| 17 | def tearDown(self): |
| 18 | del os.environ['MCP_TYPE_FORCE'] |
| 19 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 20 | def test_do_cli_main_command(self): |
| 21 | _module_name = 'cfg_checker.cfg_check' |
| 22 | _m = self._try_import(_module_name) |
| 23 | with self.save_arguments(): |
| 24 | with self.redirect_output(): |
| 25 | with self.assertRaises(SystemExit) as ep: |
| 26 | import sys |
| 27 | sys.argv = ["fake.py", "reclass", "list", "-p", "/tmp"] |
| 28 | _m.cfg_check.config_check_entrypoint() |
| 29 | |
| 30 | self.assertEqual( |
| 31 | ep.exception.code, |
| 32 | 0, |
| 33 | "'mcp-checker reclass list -p /tmp' command failed" |
| 34 | ) |
| 35 | |
| 36 | def test_do_cli_main_command_debug(self): |
| 37 | _module_name = 'cfg_checker.cfg_check' |
| 38 | _m = self._try_import(_module_name) |
| 39 | with self.save_arguments(): |
| 40 | with self.redirect_output(): |
| 41 | with self.assertRaises(SystemExit) as ep: |
| 42 | import sys |
| 43 | sys.argv = [ |
| 44 | "fake.py", |
| 45 | "-d", |
| 46 | "reclass", |
| 47 | "list", |
| 48 | "-p", |
| 49 | "/tmp" |
| 50 | ] |
| 51 | _m.cfg_check.config_check_entrypoint() |
| 52 | |
| 53 | self.assertEqual( |
| 54 | ep.exception.code, |
| 55 | 0, |
| 56 | "mcp-checker command failes" |
| 57 | ) |
| 58 | |
| 59 | def test_cli_main_unknown_argument(self): |
| 60 | _module_name = 'cfg_checker.cfg_check' |
| 61 | _m = self._try_import(_module_name) |
| 62 | with self.redirect_output(): |
| 63 | with self.assertRaises(SystemExit) as ep: |
| 64 | import sys |
| 65 | sys.argv.append("reclass") |
| 66 | sys.argv.append("list") |
| 67 | _m.cfg_check.config_check_entrypoint() |
| 68 | |
| 69 | self.assertEqual( |
| 70 | ep.exception.code, |
| 71 | 1, |
| 72 | "Unknown argument not handled" |
| 73 | ) |
| 74 | |
| 75 | def test_do_cli_module_command(self): |
| 76 | _module_name = 'cfg_checker.cli.command' |
| 77 | _m = self._try_import(_module_name) |
| 78 | _command = "reclass" |
| 79 | with self.save_arguments(): |
| 80 | with self.redirect_output(): |
| 81 | with self.assertRaises(SystemExit) as ep: |
| 82 | import sys |
| 83 | sys.argv = ["fake.py", "list", "-p", "/tmp"] |
| 84 | _m.cli.command.cli_command( |
| 85 | "Fake Reclass Comparer", |
| 86 | _command |
| 87 | ) |
| 88 | |
| 89 | self.assertEqual( |
| 90 | ep.exception.code, |
| 91 | 0, |
| 92 | "Cli command execution failed" |
| 93 | ) |
| 94 | |
| 95 | def test_do_cli_module_command_with_error(self): |
| 96 | _module_name = 'cfg_checker.cli.command' |
| 97 | _m = self._try_import(_module_name) |
| 98 | _command = "reclass" |
| 99 | with self.save_arguments(): |
| 100 | with self.redirect_output(): |
| 101 | with self.assertRaises(SystemExit) as ep: |
| 102 | import sys |
| 103 | sys.argv = ["fake.py", "list", "-p", "/notexistingfolder"] |
| 104 | _m.cli.command.cli_command( |
| 105 | "Fake Reclass Comparer", |
| 106 | _command |
| 107 | ) |
| 108 | |
| 109 | self.assertEqual( |
| 110 | ep.exception.code, |
| 111 | 1, |
| 112 | "Cli command execution failed" |
| 113 | ) |
| 114 | |
| 115 | def test_cli_module_unknown_command(self): |
| 116 | _module_name = 'cfg_checker.cli.command' |
| 117 | _m = self._try_import(_module_name) |
| 118 | _fake_args = mock.MagicMock(name="FakeArgsClass") |
| 119 | _command = "unknowncommand" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 120 | _config = None |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 121 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 122 | _r_value = _m.cli.command.execute_command( |
| 123 | _fake_args, |
| 124 | _command, |
| 125 | _config |
| 126 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 127 | |
| 128 | self.assertEqual( |
| 129 | _r_value, |
| 130 | 1, |
| 131 | "Unknown command 'type' not handled" |
| 132 | ) |
| 133 | |
| 134 | def test_cli_module_no_type(self): |
| 135 | _module_name = 'cfg_checker.cli.command' |
| 136 | _m = self._try_import(_module_name) |
| 137 | _type = {} |
| 138 | _command = "unknowncommand" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 139 | _config = None |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 140 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 141 | _r_value = _m.cli.command.execute_command(_type, _command, _config) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 142 | |
| 143 | self.assertEqual( |
| 144 | _r_value, |
| 145 | 1, |
| 146 | "Unknown command not handled" |
| 147 | ) |
| 148 | |
| 149 | def test_cli_module_unknown_type(self): |
| 150 | _module_name = 'cfg_checker.cli.command' |
| 151 | _m = self._try_import(_module_name) |
| 152 | _fake_args = mock.MagicMock(name="FakeArgsClass") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 153 | _fake_args.kube_config_path = _fake_kube_config_path |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 154 | _command = "reclass" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 155 | |
| 156 | from cfg_checker.common.settings import CheckerConfiguration |
| 157 | _config = CheckerConfiguration(_fake_args) |
| 158 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 159 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame^] | 160 | _r_value = _m.cli.command.execute_command( |
| 161 | _fake_args, |
| 162 | _command, |
| 163 | _config |
| 164 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 165 | |
| 166 | self.assertEqual( |
| 167 | _r_value, |
| 168 | 1, |
| 169 | "Unknown command not handled" |
| 170 | ) |
| 171 | |
| 172 | def test_cli_module_unknown_argument(self): |
| 173 | _module_name = 'cfg_checker.cli.command' |
| 174 | _m = self._try_import(_module_name) |
| 175 | _command = "reclass" |
| 176 | with self.redirect_output(): |
| 177 | with self.assertRaises(SystemExit) as ep: |
| 178 | _m.cli.command.cli_command( |
| 179 | "Fake Reclass Comparer", |
| 180 | _command |
| 181 | ) |
| 182 | |
| 183 | self.assertEqual( |
| 184 | ep.exception.code, |
| 185 | 1, |
| 186 | "Unknown argument not handled" |
| 187 | ) |