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): |
| 33 | def test_exceptions(self): |
| 34 | _m = self._try_import("cfg_checker.common.exception") |
| 35 | # Get all classes from the exceptions module |
| 36 | _classes = inspect.getmembers( |
| 37 | sys.modules[_m.common.exception.__name__], |
| 38 | inspect.isclass |
| 39 | ) |
| 40 | # Create instance for all detected classes except for the Base one |
| 41 | _errors = [] |
| 42 | for _name, _class in _classes: |
| 43 | if _name.startswith("CheckerBase"): |
| 44 | continue |
| 45 | _, _msg = self._safe_run(_class, "Fake exception message") |
| 46 | if _msg: |
| 47 | _errors.append(_msg) |
| 48 | |
| 49 | self.assertEqual( |
| 50 | len(_errors), |
| 51 | 0, |
| 52 | "Invalid Exception classes detected: \n{}".format( |
| 53 | "\n".join(_errors) |
| 54 | ) |
| 55 | ) |
| 56 | |
| 57 | def test_file_utils(self): |
| 58 | # File operations itself is not to be tested |
| 59 | # Only classes that provide api methods |
| 60 | # I.e. no exceptions - no errors, |
| 61 | # file contents is not to be checked, only return types |
| 62 | _m = self._try_import("cfg_checker.common.file_utils") |
| 63 | _futils = _m.common.file_utils |
| 64 | _filename = "/tmp/fakefile.txt" |
| 65 | _fakestr = "Fake String in the file" |
| 66 | _errors = [] |
| 67 | |
| 68 | # write_str_to_file |
| 69 | _, _msg = self._safe_run( |
| 70 | _futils.write_str_to_file, |
| 71 | _filename, |
| 72 | _fakestr |
| 73 | ) |
| 74 | if _msg: |
| 75 | _errors.append(_msg) |
| 76 | |
| 77 | # append_str_to_file |
| 78 | _, _msg = self._safe_run( |
| 79 | _futils.append_str_to_file, |
| 80 | _filename, |
| 81 | _fakestr |
| 82 | ) |
| 83 | if _msg: |
| 84 | _errors.append(_msg) |
| 85 | |
| 86 | # remove_file |
| 87 | _, _msg = self._safe_run(_futils.remove_file, _filename) |
| 88 | if _msg: |
| 89 | _errors.append(_msg) |
| 90 | |
| 91 | # write_lines_to_file |
| 92 | _, _msg = self._safe_run( |
| 93 | _futils.write_lines_to_file, |
| 94 | _filename, |
| 95 | [_fakestr] |
| 96 | ) |
| 97 | if _msg: |
| 98 | _errors.append(_msg) |
| 99 | |
| 100 | # append_lines_to_file |
| 101 | _, _msg = self._safe_run( |
| 102 | _futils.append_lines_to_file, |
| 103 | _filename, |
| 104 | [_fakestr] |
| 105 | ) |
| 106 | if _msg: |
| 107 | _errors.append(_msg) |
| 108 | |
| 109 | # append_line_to_file |
| 110 | _, _msg = self._safe_run( |
| 111 | _futils.append_line_to_file, |
| 112 | _filename, |
| 113 | _fakestr |
| 114 | ) |
| 115 | if _msg: |
| 116 | _errors.append(_msg) |
| 117 | |
| 118 | # read_file |
| 119 | _r, _msg = self._safe_run(_futils.read_file, _filename) |
| 120 | if _msg: |
| 121 | _errors.append(_msg) |
| 122 | self.assertNotEqual( |
| 123 | len(_r), |
| 124 | 0, |
| 125 | "Empty buffer returned by 'read_file'" |
| 126 | ) |
| 127 | |
| 128 | # read_file_as_lines |
| 129 | _r, _msg = self._safe_run(_futils.read_file_as_lines, _filename) |
| 130 | if _msg: |
| 131 | _errors.append(_msg) |
| 132 | self.assertNotEqual( |
| 133 | len(_r), |
| 134 | 0, |
| 135 | "Empty buffer returned by 'read_file_as_lines'" |
| 136 | ) |
| 137 | self.assertIsInstance( |
| 138 | _r, |
| 139 | list, |
| 140 | "Non-list type returned by 'read_file_as_lines'" |
| 141 | ) |
| 142 | # get_file_info_fd |
| 143 | with open(_filename) as _fd: |
| 144 | _r, _msg = self._safe_run(_futils.get_file_info_fd, _fd) |
| 145 | if _msg: |
| 146 | _errors.append(_msg) |
| 147 | self.assertIsInstance( |
| 148 | _r, |
| 149 | dict, |
| 150 | "Non-dict type returned by get_file_info_fd" |
| 151 | ) |
| 152 | _, _msg = self._safe_run(_futils.remove_file, _filename) |
| 153 | |
| 154 | # get_gzipped_file |
| 155 | |
| 156 | _folder = "/tmp/cfgcheckertmpfolder" |
| 157 | # ensure_folder_exists |
| 158 | _, _msg = self._safe_run(_futils.ensure_folder_exists, _folder) |
| 159 | if _msg: |
| 160 | _errors.append(_msg) |
| 161 | _, _msg = self._safe_run(_futils.ensure_folder_exists, _folder) |
| 162 | if _msg: |
| 163 | _errors.append(_msg) |
| 164 | |
| 165 | # ensure_folder_removed |
| 166 | _, _msg = self._safe_run(_futils.ensure_folder_removed, _folder) |
| 167 | if _msg: |
| 168 | _errors.append(_msg) |
| 169 | _, _msg = self._safe_run(_futils.ensure_folder_removed, _folder) |
| 170 | if _msg: |
| 171 | _errors.append(_msg) |
| 172 | |
| 173 | self.assertEqual( |
| 174 | len(_errors), |
| 175 | 0, |
| 176 | "Invalid file operations: \n{}".format( |
| 177 | "\n".join(_errors) |
| 178 | ) |
| 179 | ) |
| 180 | |
| 181 | @mock.patch( |
| 182 | 'requests.get', |
| 183 | side_effect=mocked_requests_get |
| 184 | ) |
| 185 | def test_get_gzip_file(self, mock_get): |
| 186 | _m = self._try_import("cfg_checker.common.file_utils") |
| 187 | _futils = _m.common.file_utils |
| 188 | _fakecontent = b"fakecontent\n" |
| 189 | _errors = [] |
| 190 | |
| 191 | # Call the method with patched data |
| 192 | _buf, _msg = self._safe_run( |
| 193 | _futils.get_gzipped_file, |
| 194 | fake_gzip_file_path |
| 195 | ) |
| 196 | if _msg: |
| 197 | _errors.append(_msg) |
| 198 | |
| 199 | self.assertNotEqual( |
| 200 | len(_buf), |
| 201 | 0, |
| 202 | "Empty buffer returned by 'get_gzipped_file'" |
| 203 | ) |
| 204 | self.assertEqual( |
| 205 | _buf, |
| 206 | _fakecontent, |
| 207 | "Incorrect content returned by 'get_gzipped_file'" |
| 208 | ) |