forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_celery_beat_crons.py
304 lines (259 loc) · 8.91 KB
/
test_celery_beat_crons.py
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
import pytest
pytest.importorskip("celery")
from sentry_sdk.integrations.celery import (
_get_headers,
_get_humanized_interval,
_get_monitor_config,
_patch_beat_apply_entry,
crons_task_success,
crons_task_failure,
crons_task_retry,
)
from sentry_sdk.crons import MonitorStatus
from celery.schedules import crontab, schedule
try:
from unittest import mock # python 3.3 and above
from unittest.mock import MagicMock
except ImportError:
import mock # python < 3.3
from mock import MagicMock
def test_get_headers():
fake_task = MagicMock()
fake_task.request = {
"bla": "blub",
"foo": "bar",
}
assert _get_headers(fake_task) == {}
fake_task.request.update(
{
"headers": {
"bla": "blub",
},
}
)
assert _get_headers(fake_task) == {"bla": "blub"}
fake_task.request.update(
{
"headers": {
"headers": {
"tri": "blub",
"bar": "baz",
},
"bla": "blub",
},
}
)
assert _get_headers(fake_task) == {"bla": "blub", "tri": "blub", "bar": "baz"}
@pytest.mark.parametrize(
"seconds, expected_tuple",
[
(0, (1, "minute")),
(0.00001, (1, "minute")),
(1, (1, "minute")),
(100, (1, "minute")),
(1000, (16, "minute")),
(10000, (2, "hour")),
(100000, (1, "day")),
(100000000, (1157, "day")),
],
)
def test_get_humanized_interval(seconds, expected_tuple):
assert _get_humanized_interval(seconds) == expected_tuple
def test_crons_task_success():
fake_task = MagicMock()
fake_task.request = {
"headers": {
"sentry-monitor-slug": "test123",
"sentry-monitor-check-in-id": "1234567890",
"sentry-monitor-start-timestamp-s": 200.1,
"sentry-monitor-config": {
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
"sentry-monitor-some-future-key": "some-future-value",
},
}
with mock.patch(
"sentry_sdk.integrations.celery.capture_checkin"
) as mock_capture_checkin:
with mock.patch(
"sentry_sdk.integrations.celery._now_seconds_since_epoch",
return_value=500.5,
):
crons_task_success(fake_task)
mock_capture_checkin.assert_called_once_with(
monitor_slug="test123",
monitor_config={
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
duration=300.4,
check_in_id="1234567890",
status=MonitorStatus.OK,
)
def test_crons_task_failure():
fake_task = MagicMock()
fake_task.request = {
"headers": {
"sentry-monitor-slug": "test123",
"sentry-monitor-check-in-id": "1234567890",
"sentry-monitor-start-timestamp-s": 200.1,
"sentry-monitor-config": {
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
"sentry-monitor-some-future-key": "some-future-value",
},
}
with mock.patch(
"sentry_sdk.integrations.celery.capture_checkin"
) as mock_capture_checkin:
with mock.patch(
"sentry_sdk.integrations.celery._now_seconds_since_epoch",
return_value=500.5,
):
crons_task_failure(fake_task)
mock_capture_checkin.assert_called_once_with(
monitor_slug="test123",
monitor_config={
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
duration=300.4,
check_in_id="1234567890",
status=MonitorStatus.ERROR,
)
def test_crons_task_retry():
fake_task = MagicMock()
fake_task.request = {
"headers": {
"sentry-monitor-slug": "test123",
"sentry-monitor-check-in-id": "1234567890",
"sentry-monitor-start-timestamp-s": 200.1,
"sentry-monitor-config": {
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
"sentry-monitor-some-future-key": "some-future-value",
},
}
with mock.patch(
"sentry_sdk.integrations.celery.capture_checkin"
) as mock_capture_checkin:
with mock.patch(
"sentry_sdk.integrations.celery._now_seconds_since_epoch",
return_value=500.5,
):
crons_task_retry(fake_task)
mock_capture_checkin.assert_called_once_with(
monitor_slug="test123",
monitor_config={
"schedule": {
"type": "interval",
"value": 3,
"unit": "day",
},
"timezone": "Europe/Vienna",
},
duration=300.4,
check_in_id="1234567890",
status=MonitorStatus.ERROR,
)
def test_get_monitor_config():
app = MagicMock()
app.conf = MagicMock()
app.conf.timezone = "Europe/Vienna"
celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10")
monitor_config = _get_monitor_config(celery_schedule, app)
assert monitor_config == {
"schedule": {
"type": "crontab",
"value": "*/10 12 3 * *",
},
"timezone": "Europe/Vienna",
}
assert "unit" not in monitor_config["schedule"]
celery_schedule = schedule(run_every=3)
monitor_config = _get_monitor_config(celery_schedule, app)
assert monitor_config == {
"schedule": {
"type": "interval",
"value": 1,
"unit": "minute",
},
"timezone": "Europe/Vienna",
}
unknown_celery_schedule = MagicMock()
monitor_config = _get_monitor_config(unknown_celery_schedule, app)
assert monitor_config == {}
def test_get_monitor_config_default_timezone():
app = MagicMock()
app.conf = MagicMock()
app.conf.timezone = None
celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10")
monitor_config = _get_monitor_config(celery_schedule, app)
assert monitor_config["timezone"] == "UTC"
@pytest.mark.parametrize(
"task_name,exclude_beat_tasks,task_in_excluded_beat_tasks",
[
["some_task_name", ["xxx", "some_task.*"], True],
["some_task_name", ["xxx", "some_other_task.*"], False],
],
)
def test_exclude_beat_tasks_option(
task_name, exclude_beat_tasks, task_in_excluded_beat_tasks
):
"""
Test excluding Celery Beat tasks from automatic instrumentation.
"""
fake_apply_entry = MagicMock()
fake_scheduler = MagicMock()
fake_scheduler.apply_entry = fake_apply_entry
fake_integration = MagicMock()
fake_integration.exclude_beat_tasks = exclude_beat_tasks
fake_schedule_entry = MagicMock()
fake_schedule_entry.name = task_name
fake_get_monitor_config = MagicMock()
with mock.patch(
"sentry_sdk.integrations.celery.Scheduler", fake_scheduler
) as Scheduler: # noqa: N806
with mock.patch(
"sentry_sdk.integrations.celery.Hub.current.get_integration",
return_value=fake_integration,
):
with mock.patch(
"sentry_sdk.integrations.celery._get_monitor_config",
fake_get_monitor_config,
) as _get_monitor_config:
# Mimic CeleryIntegration patching of Scheduler.apply_entry()
_patch_beat_apply_entry()
# Mimic Celery Beat calling a task from the Beat schedule
Scheduler.apply_entry(fake_scheduler, fake_schedule_entry)
if task_in_excluded_beat_tasks:
# Only the original Scheduler.apply_entry() is called, _get_monitor_config is NOT called.
assert fake_apply_entry.call_count == 1
_get_monitor_config.assert_not_called()
else:
# The original Scheduler.apply_entry() is called, AND _get_monitor_config is called.
assert fake_apply_entry.call_count == 1
assert _get_monitor_config.call_count == 1