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 | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 3 | import inspect |
| 4 | import os |
| 5 | import sys |
| 6 | from unittest import mock |
| 7 | |
| 8 | |
| 9 | from tests.test_base import CfgCheckerTestBase |
| 10 | from tests.test_base import tests_dir |
| 11 | |
| 12 | gzip_filename = "textfile.txt.gz" |
| 13 | fake_gzip_file_path = os.path.join(tests_dir, 'res', gzip_filename) |
| 14 | _patch_buf = [] |
| 15 | with open(fake_gzip_file_path, 'rb') as _f: |
| 16 | _patch_buf = _f.read() |
| 17 | |
| 18 | |
| 19 | def mocked_requests_get(*args, **kwargs): |
| 20 | class MockResponse: |
| 21 | def __init__(self, content, status_code): |
| 22 | self.content = content |
| 23 | self.status_code = status_code |
| 24 | |
| 25 | def content(self): |
| 26 | return self.content |
| 27 | |
| 28 | if args[0] == fake_gzip_file_path: |
| 29 | return MockResponse(_patch_buf, 200) |
| 30 | |
| 31 | return MockResponse(None, 404) |
| 32 | |
| 33 | |
| 34 | class TestCommonModules(CfgCheckerTestBase): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 35 | def setUp(self): |
| 36 | # force env type to salt |
| 37 | os.environ['MCP_TYPE_FORCE'] = 'SALT' |
| 38 | |
| 39 | def tearDown(self): |
| 40 | del os.environ['MCP_TYPE_FORCE'] |
| 41 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 42 | def test_exceptions(self): |
| 43 | _m = self._try_import("cfg_checker.common.exception") |
| 44 | # Get all classes from the exceptions module |
| 45 | _classes = inspect.getmembers( |
| 46 | sys.modules[_m.common.exception.__name__], |
| 47 | inspect.isclass |
| 48 | ) |
| 49 | # Create instance for all detected classes except for the Base one |
| 50 | _errors = [] |
| 51 | for _name, _class in _classes: |
| 52 | if _name.startswith("CheckerBase"): |
| 53 | continue |
| 54 | _, _msg = self._safe_run(_class, "Fake exception message") |
| 55 | if _msg: |
| 56 | _errors.append(_msg) |
| 57 | |
| 58 | self.assertEqual( |
| 59 | len(_errors), |
| 60 | 0, |
| 61 | "Invalid Exception classes detected: \n{}".format( |
| 62 | "\n".join(_errors) |
| 63 | ) |
| 64 | ) |
| 65 | |
| 66 | def test_file_utils(self): |
| 67 | # File operations itself is not to be tested |
| 68 | # Only classes that provide api methods |
| 69 | # I.e. no exceptions - no errors, |
| 70 | # file contents is not to be checked, only return types |
| 71 | _m = self._try_import("cfg_checker.common.file_utils") |
| 72 | _futils = _m.common.file_utils |
| 73 | _filename = "/tmp/fakefile.txt" |
| 74 | _fakestr = "Fake String in the file" |
| 75 | _errors = [] |
| 76 | |
| 77 | # write_str_to_file |
| 78 | _, _msg = self._safe_run( |
| 79 | _futils.write_str_to_file, |
| 80 | _filename, |
| 81 | _fakestr |
| 82 | ) |
| 83 | if _msg: |
| 84 | _errors.append(_msg) |
| 85 | |
| 86 | # append_str_to_file |
| 87 | _, _msg = self._safe_run( |
| 88 | _futils.append_str_to_file, |
| 89 | _filename, |
| 90 | _fakestr |
| 91 | ) |
| 92 | if _msg: |
| 93 | _errors.append(_msg) |
| 94 | |
| 95 | # remove_file |
| 96 | _, _msg = self._safe_run(_futils.remove_file, _filename) |
| 97 | if _msg: |
| 98 | _errors.append(_msg) |
| 99 | |
| 100 | # write_lines_to_file |
| 101 | _, _msg = self._safe_run( |
| 102 | _futils.write_lines_to_file, |
| 103 | _filename, |
| 104 | [_fakestr] |
| 105 | ) |
| 106 | if _msg: |
| 107 | _errors.append(_msg) |
| 108 | |
| 109 | # append_lines_to_file |
| 110 | _, _msg = self._safe_run( |
| 111 | _futils.append_lines_to_file, |
| 112 | _filename, |
| 113 | [_fakestr] |
| 114 | ) |
| 115 | if _msg: |
| 116 | _errors.append(_msg) |
| 117 | |
| 118 | # append_line_to_file |
| 119 | _, _msg = self._safe_run( |
| 120 | _futils.append_line_to_file, |
| 121 | _filename, |
| 122 | _fakestr |
| 123 | ) |
| 124 | if _msg: |
| 125 | _errors.append(_msg) |
| 126 | |
| 127 | # read_file |
| 128 | _r, _msg = self._safe_run(_futils.read_file, _filename) |
| 129 | if _msg: |
| 130 | _errors.append(_msg) |
| 131 | self.assertNotEqual( |
| 132 | len(_r), |
| 133 | 0, |
| 134 | "Empty buffer returned by 'read_file'" |
| 135 | ) |
| 136 | |
| 137 | # read_file_as_lines |
| 138 | _r, _msg = self._safe_run(_futils.read_file_as_lines, _filename) |
| 139 | if _msg: |
| 140 | _errors.append(_msg) |
| 141 | self.assertNotEqual( |
| 142 | len(_r), |
| 143 | 0, |
| 144 | "Empty buffer returned by 'read_file_as_lines'" |
| 145 | ) |
| 146 | self.assertIsInstance( |
| 147 | _r, |
| 148 | list, |
| 149 | "Non-list type returned by 'read_file_as_lines'" |
| 150 | ) |
| 151 | # get_file_info_fd |
| 152 | with open(_filename) as _fd: |
| 153 | _r, _msg = self._safe_run(_futils.get_file_info_fd, _fd) |
| 154 | if _msg: |
| 155 | _errors.append(_msg) |
| 156 | self.assertIsInstance( |
| 157 | _r, |
| 158 | dict, |
| 159 | "Non-dict type returned by get_file_info_fd" |
| 160 | ) |
| 161 | _, _msg = self._safe_run(_futils.remove_file, _filename) |
| 162 | |
| 163 | # get_gzipped_file |
| 164 | |
| 165 | _folder = "/tmp/cfgcheckertmpfolder" |
| 166 | # ensure_folder_exists |
| 167 | _, _msg = self._safe_run(_futils.ensure_folder_exists, _folder) |
| 168 | if _msg: |
| 169 | _errors.append(_msg) |
| 170 | _, _msg = self._safe_run(_futils.ensure_folder_exists, _folder) |
| 171 | if _msg: |
| 172 | _errors.append(_msg) |
| 173 | |
| 174 | # ensure_folder_removed |
| 175 | _, _msg = self._safe_run(_futils.ensure_folder_removed, _folder) |
| 176 | if _msg: |
| 177 | _errors.append(_msg) |
| 178 | _, _msg = self._safe_run(_futils.ensure_folder_removed, _folder) |
| 179 | if _msg: |
| 180 | _errors.append(_msg) |
| 181 | |
| 182 | self.assertEqual( |
| 183 | len(_errors), |
| 184 | 0, |
| 185 | "Invalid file operations: \n{}".format( |
| 186 | "\n".join(_errors) |
| 187 | ) |
| 188 | ) |
| 189 | |
| 190 | @mock.patch( |
| 191 | 'requests.get', |
| 192 | side_effect=mocked_requests_get |
| 193 | ) |
| 194 | def test_get_gzip_file(self, mock_get): |
| 195 | _m = self._try_import("cfg_checker.common.file_utils") |
| 196 | _futils = _m.common.file_utils |
| 197 | _fakecontent = b"fakecontent\n" |
| 198 | _errors = [] |
| 199 | |
| 200 | # Call the method with patched data |
| 201 | _buf, _msg = self._safe_run( |
| 202 | _futils.get_gzipped_file, |
| 203 | fake_gzip_file_path |
| 204 | ) |
| 205 | if _msg: |
| 206 | _errors.append(_msg) |
| 207 | |
| 208 | self.assertNotEqual( |
| 209 | len(_buf), |
| 210 | 0, |
| 211 | "Empty buffer returned by 'get_gzipped_file'" |
| 212 | ) |
| 213 | self.assertEqual( |
| 214 | _buf, |
| 215 | _fakecontent, |
| 216 | "Incorrect content returned by 'get_gzipped_file'" |
| 217 | ) |