Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame^] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 3 | import os |
| 4 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 5 | from unittest import mock |
| 6 | |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 7 | from cfg_checker.common.settings import pkg_dir |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 8 | from tests.mocks import _fake_kube_config_path |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 9 | from tests.test_base import CfgCheckerTestBase |
| 10 | |
| 11 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 12 | os.environ['MCP_TYPE_FORCE'] = 'SALT' |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 13 | _env_name = 'local' |
| 14 | _env_file = os.path.join(pkg_dir, 'etc', _env_name + '.env') |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 15 | |
| 16 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 17 | class TestCliCommands(CfgCheckerTestBase): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 18 | 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 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 25 | 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" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 125 | _config = None |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 126 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 127 | _r_value = _m.cli.command.execute_command( |
| 128 | _fake_args, |
| 129 | _command, |
| 130 | _config |
| 131 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 132 | |
| 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" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 144 | _config = None |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 145 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 146 | _r_value = _m.cli.command.execute_command(_type, _command, _config) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 147 | |
| 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") |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 158 | _fake_args.kube_config = _fake_kube_config_path |
| 159 | _fake_args.env_name = _env_name |
| 160 | _fake_args.env_config = _env_file |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 161 | _command = "reclass" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 162 | |
| 163 | from cfg_checker.common.settings import CheckerConfiguration |
| 164 | _config = CheckerConfiguration(_fake_args) |
| 165 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 166 | with self.redirect_output(): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 167 | _r_value = _m.cli.command.execute_command( |
| 168 | _fake_args, |
| 169 | _command, |
| 170 | _config |
| 171 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 172 | |
| 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 | ) |