-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_errors.py
More file actions
336 lines (236 loc) · 9.57 KB
/
test_errors.py
File metadata and controls
336 lines (236 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""Tests for core.errors failure classification."""
import builtins
import errno
import importlib
import types
import pytest
from django.core.exceptions import ValidationError
from django.core.management.base import CommandError
from core.errors import (
CollectorFailureCategory,
_classify_os_error,
classify_failure,
)
_swapped: dict[tuple[str, str], type[BaseException]] = {}
@pytest.fixture(autouse=True)
def _restore_swapped_exception_classes():
yield
for (module_name, attr_name), original in list(_swapped.items()):
mod = importlib.import_module(module_name)
setattr(mod, attr_name, original)
del _swapped[(module_name, attr_name)]
def _swap_exc(module_name: str, attr_name: str, bases=(Exception,)):
mod = importlib.import_module(module_name)
key = (module_name, attr_name)
if key not in _swapped:
_swapped[key] = getattr(mod, attr_name)
cls = types.new_class(attr_name, bases, exec_body=lambda ns: None)
cls.__module__ = module_name
setattr(mod, attr_name, cls)
return cls
def _make_requests_exc(name: str, bases=(Exception,)):
return _swap_exc("requests.exceptions", name, bases)
def _make_urllib3_exc(name: str):
return _swap_exc("urllib3.exceptions", name)
def _make_httpx_exc(name: str):
httpx = pytest.importorskip("httpx")
exc_cls = getattr(httpx, name)
mod_name = getattr(exc_cls, "__module__", "httpx")
return _swap_exc(mod_name, name)
def test_classify_command_error():
assert classify_failure(CommandError("bad")) is CollectorFailureCategory.COMMAND
def test_classify_django_db_error_unknown():
from django.db import OperationalError
assert classify_failure(OperationalError("db")) is CollectorFailureCategory.UNKNOWN
def test_classify_discord_errors_module_429():
class HTTPException(Exception):
pass
HTTPException.__module__ = "discord.errors"
exc = HTTPException("rate limited")
exc.status = 429
assert classify_failure(exc) is CollectorFailureCategory.RATE_LIMIT
def test_classify_discord_errors_module_unknown_without_status():
class DiscordExc(Exception):
pass
DiscordExc.__module__ = "discord.errors"
assert classify_failure(DiscordExc("x")) is CollectorFailureCategory.UNKNOWN
def test_classify_slack_sdk_errors_module_401():
class SlackApiError(Exception):
pass
SlackApiError.__module__ = "slack_sdk.errors"
class Resp:
status_code = 401
exc = SlackApiError("auth")
exc.response = Resp()
assert classify_failure(exc) is CollectorFailureCategory.AUTH
def test_classify_validation_error():
assert classify_failure(ValidationError("x")) is CollectorFailureCategory.VALIDATION
def test_classify_value_error():
assert classify_failure(ValueError("x")) is CollectorFailureCategory.VALIDATION
def test_classify_permission_error():
assert (
classify_failure(PermissionError("no")) is CollectorFailureCategory.PERMISSION
)
def test_classify_timeout_error():
assert classify_failure(TimeoutError()) is CollectorFailureCategory.TIMEOUT
def test_classify_os_error_network_errno():
assert (
classify_failure(OSError(errno.EPIPE, "Broken pipe"))
is CollectorFailureCategory.NETWORK
)
def test_classify_os_error_direct_errno_network():
assert (
_classify_os_error(OSError(errno.ENOTCONN, "not connected"))
is CollectorFailureCategory.NETWORK
)
def test_classify_os_error_direct_winerror_network():
exc = OSError(0, "wsa", None, 10061)
assert _classify_os_error(exc) is CollectorFailureCategory.NETWORK
def test_classify_os_error_winerror_network_when_errno_not_in_sets():
exc = OSError(999999, "generic message")
exc.winerror = 10061
assert classify_failure(exc) is CollectorFailureCategory.NETWORK
def test_classify_os_error_network_errno_connrefused():
assert (
classify_failure(OSError(errno.ECONNREFUSED, "refused"))
is CollectorFailureCategory.NETWORK
)
def test_classify_os_error_local_io_errno_unknown():
assert (
classify_failure(OSError(errno.ENOSPC, "no space"))
is CollectorFailureCategory.UNKNOWN
)
def test_classify_os_error_winerror_network():
exc = OSError(0, "wsa", None, 10054)
assert classify_failure(exc) is CollectorFailureCategory.NETWORK
def test_classify_os_error_connection_error_subclass():
assert classify_failure(ConnectionResetError()) is CollectorFailureCategory.NETWORK
def test_classify_os_error_io_ambiguous_is_unknown():
assert (
classify_failure(OSError(errno.EIO, "Input/output error"))
is CollectorFailureCategory.UNKNOWN
)
def test_classify_os_error_filesystem_errno_unknown():
assert (
classify_failure(OSError(errno.ENOENT, "No such file"))
is CollectorFailureCategory.UNKNOWN
)
def test_classify_file_not_found_unknown():
assert (
classify_failure(FileNotFoundError("/no/such/path"))
is CollectorFailureCategory.UNKNOWN
)
def test_classify_unknown():
assert classify_failure(RuntimeError("x")) is CollectorFailureCategory.UNKNOWN
@pytest.mark.parametrize(
("name", "category"),
[
("HTTPError", CollectorFailureCategory.NETWORK),
("SSLError", CollectorFailureCategory.NETWORK),
("Timeout", CollectorFailureCategory.TIMEOUT),
("ReadTimeout", CollectorFailureCategory.TIMEOUT),
("ConnectTimeout", CollectorFailureCategory.TIMEOUT),
("ConnectionError", CollectorFailureCategory.NETWORK),
("ChunkedEncodingError", CollectorFailureCategory.NETWORK),
],
)
def test_classify_requests_exceptions(name, category):
cls = _make_requests_exc(name)
assert classify_failure(cls("x")) is category
def _http_error_with_status(status_code: int | None):
pytest.importorskip("requests")
import requests
exc = requests.HTTPError()
if status_code is None:
exc.response = None
else:
resp = requests.Response()
resp.status_code = status_code
exc.response = resp
return exc
def test_classify_requests_http_error_429_rate_limit():
assert (
classify_failure(_http_error_with_status(429))
is CollectorFailureCategory.RATE_LIMIT
)
def test_classify_requests_http_error_401_auth():
assert (
classify_failure(_http_error_with_status(401)) is CollectorFailureCategory.AUTH
)
def test_classify_requests_http_error_403_auth():
assert (
classify_failure(_http_error_with_status(403)) is CollectorFailureCategory.AUTH
)
def test_classify_requests_http_error_other_status_network():
assert (
classify_failure(_http_error_with_status(500))
is CollectorFailureCategory.NETWORK
)
def test_classify_requests_http_error_no_response_network():
assert (
classify_failure(_http_error_with_status(None))
is CollectorFailureCategory.NETWORK
)
@pytest.mark.parametrize(
("name", "category"),
[
("ReadTimeoutError", CollectorFailureCategory.TIMEOUT),
("ConnectTimeoutError", CollectorFailureCategory.TIMEOUT),
("TimeoutError", CollectorFailureCategory.TIMEOUT),
("ProtocolError", CollectorFailureCategory.NETWORK),
],
)
def test_classify_urllib3_exceptions(name, category):
cls = _make_urllib3_exc(name)
assert classify_failure(cls("x")) is category
@pytest.mark.parametrize(
("name", "category"),
[
("ReadTimeout", CollectorFailureCategory.TIMEOUT),
("HTTPStatusError", CollectorFailureCategory.NETWORK),
("TransportError", CollectorFailureCategory.NETWORK),
("ConnectError", CollectorFailureCategory.NETWORK),
],
)
def test_classify_httpx(name, category):
cls = _make_httpx_exc(name)
assert classify_failure(cls("x")) is category
def test_classify_validation_error_when_django_exceptions_import_fails(monkeypatch):
real_import = builtins.__import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == "django.core.exceptions":
raise ImportError("blocked")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", fake_import)
assert classify_failure(ValidationError("x")) is CollectorFailureCategory.UNKNOWN
def test_classify_command_error_when_management_import_fails(monkeypatch):
real_import = builtins.__import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == "django.core.management.base":
raise ImportError("blocked")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", fake_import)
assert classify_failure(CommandError("x")) is CollectorFailureCategory.UNKNOWN
def test_classify_pydantic_validation_error():
pydantic = pytest.importorskip("pydantic")
from pydantic import BaseModel
class M(BaseModel):
x: int
try:
M.model_validate({"x": "not-int"})
except pydantic.ValidationError as exc:
assert classify_failure(exc) is CollectorFailureCategory.VALIDATION
else:
pytest.fail("expected ValidationError")
def test_classify_github_api_validation_error():
from github_activity_tracker.api_schemas import GitHubApiValidationError
assert (
classify_failure(GitHubApiValidationError("bad issue"))
is CollectorFailureCategory.VALIDATION
)
def test_classify_slack_api_validation_error():
from cppa_slack_tracker.api_schemas import SlackApiValidationError
assert (
classify_failure(SlackApiValidationError("bad slack"))
is CollectorFailureCategory.VALIDATION
)