forked from getsentry/sentry-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration_cache.py
More file actions
365 lines (313 loc) · 9.88 KB
/
Copy pathtest_integration_cache.py
File metadata and controls
365 lines (313 loc) · 9.88 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
import time
import pytest
from . import make_dsn, run
from .conditions import has_breakpad, has_files, has_http, is_qemu
pytestmark = [
pytest.mark.skipif(not has_files, reason="tests need local filesystem"),
pytest.mark.skipif(not has_http, reason="tests need http transport"),
]
@pytest.mark.parametrize(
"cache_args,expect_cache",
[
([], False),
(["cache-keep"], True),
(["cache-keep-always"], True),
],
)
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad or is_qemu, reason="breakpad backend not available"
),
),
],
)
def test_cache_keep(cmake, backend, cache_args, expect_cache, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
# capture
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "flush", "crash"] + cache_args,
expect_failure=True,
env=env,
)
assert not cache_dir.exists() or len(list(cache_dir.glob("*.envelope"))) == 0
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "flush", "no-setup"] + cache_args,
env=env,
)
assert cache_dir.exists() or expect_cache is False
if expect_cache:
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 1
if backend != "inproc":
dmp_files = list(cache_dir.glob("*.dmp"))
assert len(dmp_files) == 1
assert cache_files[0].stem == dmp_files[0].stem
def test_cache_keep_always(cmake, httpserver):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "inproc"})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
httpserver.expect_oneshot_request("/api/123456/envelope/").respond_with_data("OK")
with httpserver.wait(timeout=10) as waiting:
run(
tmp_path,
"sentry_example",
["log", "cache-keep-always", "flush", "capture-event"],
env=env,
)
assert waiting.result
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 1
assert len(cache_files[0].stem) == 36
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad or is_qemu, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_size(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(5):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# 5 x 4mb
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
for f in cache_files:
with open(f, "r+b") as file:
file.truncate(4 * 1024 * 1024)
# max 16mb
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "no-setup"],
env=env,
)
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) <= 4
assert sum(f.stat().st_size for f in cache_files) <= 16 * 1024 * 1024
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad or is_qemu, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_age(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(5):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# 2,4,6,8,10 days old
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
for i, f in enumerate(cache_files):
mtime = time.time() - ((i + 1) * 2 * 24 * 60 * 60)
os.utime(str(f), (mtime, mtime))
# max 5 days
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "no-setup"],
env=env,
)
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 2
for f in cache_files:
assert time.time() - f.stat().st_mtime <= 5 * 24 * 60 * 60
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad or is_qemu, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_items(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
for i in range(6):
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "no-http-retry", "cache-keep", "flush", "no-setup"],
env=env,
)
# max 5 items
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 5
@pytest.mark.parametrize(
"backend",
[
"inproc",
pytest.param(
"breakpad",
marks=pytest.mark.skipif(
not has_breakpad or is_qemu, reason="breakpad backend not available"
),
),
],
)
def test_cache_max_items_with_retry(cmake, backend, unreachable_dsn):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": backend})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
# Create cache files via crash+restart cycles
for i in range(4):
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "flush", "crash"],
expect_failure=True,
env=env,
)
# flush + cache
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "flush", "no-setup"],
env=env,
)
# Pre-populate cache/ with retry-format envelope files
cache_dir.mkdir(parents=True, exist_ok=True)
for i in range(4):
ts = int(time.time() * 1000)
f = cache_dir / f"{ts}-00-00000000-0000-0000-0000-{i:012x}.envelope"
f.write_text("dummy envelope content")
# Trigger sentry_init which runs cleanup
run(
tmp_path,
"sentry_example",
["log", "cache-keep", "no-setup"],
env=env,
)
# max 5 items total in cache/
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) <= 5
def test_cache_consent_revoke(cmake, unreachable_dsn):
"""With consent revoked and cache_keep, envelopes are cached to disk."""
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
run(
tmp_path,
"sentry_example",
[
"log",
"cache-keep",
"require-user-consent",
"user-consent-revoke",
"capture-event",
"flush",
],
env=env,
)
assert cache_dir.exists()
cache_files = list(cache_dir.glob("*.envelope"))
assert len(cache_files) == 1
def test_cache_consent_discard(cmake, unreachable_dsn):
"""With consent revoked but no cache_keep, envelopes are discarded."""
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=unreachable_dsn)
run(
tmp_path,
"sentry_example",
[
"log",
"require-user-consent",
"user-consent-revoke",
"capture-event",
"flush",
],
env=env,
)
assert not cache_dir.exists() or len(list(cache_dir.glob("*.envelope"))) == 0
def test_cache_consent_flush(cmake, httpserver):
"""Giving consent after capturing flushes cached envelopes immediately."""
from . import make_dsn
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})
cache_dir = tmp_path.joinpath(".sentry-native/cache")
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
httpserver.expect_request("/api/123456/envelope/").respond_with_data("OK")
run(
tmp_path,
"sentry_example",
[
"log",
"http-retry",
"require-user-consent",
"user-consent-revoke",
"capture-event",
"user-consent-give",
],
env=env,
)
assert len(httpserver.log) >= 1
assert not cache_dir.exists() or len(list(cache_dir.glob("*.envelope"))) == 0