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