-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_upload.py
More file actions
286 lines (244 loc) · 9.06 KB
/
Copy pathtest_upload.py
File metadata and controls
286 lines (244 loc) · 9.06 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
"""Tests for the upload API endpoints."""
import copy
UPLOAD_PAYLOAD = {
"metadata": {
"commit": {
"hexsha": "b" * 40,
"committed_date": "2025-06-16T10:00:00+00:00",
"message": "Benchmark commit",
"author": "Benchmark Author",
},
"version": {"major": 3, "minor": 14, "micro": 1},
"configure_vars": {
"CONFIG_ARGS": "'--enable-optimizations' '--prefix=/tmp/install'"
},
},
"benchmark_results": [
{
"benchmark_name": "test_bench",
"stats_json": {
"metadata": {"peak_memory": 2_000_000},
"allocation_size_histogram": [
{"min_bytes": 64, "count": 100},
{"min_bytes": 128, "count": 50},
],
"total_bytes_allocated": 3_000_000,
"top_allocations_by_size": [
{"location": "test_func", "count": 10, "size": 100_000}
],
},
"flamegraph_html": "<html>test flamegraph</html>",
}
],
"binary_id": "default",
"environment_id": "linux-x86_64",
}
async def test_upload_requires_auth(client, sample_binary, sample_environment):
response = await client.post("/api/upload-run", json=UPLOAD_PAYLOAD)
assert response.status_code in (401, 403)
async def test_upload_success(
client, auth_headers, sample_binary, sample_environment
):
response = await client.post(
"/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers
)
assert response.status_code == 200
data = response.json()
assert data["commit_sha"] == "b" * 40
assert data["binary_id"] == "default"
assert data["environment_id"] == "linux-x86_64"
assert data["results_created"] == 1
async def test_upload_missing_commit_sha(
client, auth_headers, sample_binary, sample_environment
):
payload = {
**UPLOAD_PAYLOAD,
"metadata": {"commit": {}, "version": {"major": 3, "minor": 14, "micro": 0}},
}
response = await client.post(
"/api/upload-run", json=payload, headers=auth_headers
)
assert response.status_code == 400
async def test_upload_invalid_binary(client, auth_headers, sample_environment):
payload = {**UPLOAD_PAYLOAD, "binary_id": "nonexistent"}
response = await client.post(
"/api/upload-run", json=payload, headers=auth_headers
)
assert response.status_code == 400
assert "not found" in response.json()["detail"].lower()
async def test_upload_invalid_environment(client, auth_headers, sample_binary):
payload = {**UPLOAD_PAYLOAD, "environment_id": "nonexistent"}
response = await client.post(
"/api/upload-run", json=payload, headers=auth_headers
)
assert response.status_code == 400
assert "not found" in response.json()["detail"].lower()
async def test_upload_flag_mismatch(
client, auth_headers, sample_binary, sample_environment
):
payload = {
**UPLOAD_PAYLOAD,
"metadata": {
**UPLOAD_PAYLOAD["metadata"],
"configure_vars": {"CONFIG_ARGS": "'--with-pydebug'"},
},
}
response = await client.post(
"/api/upload-run", json=payload, headers=auth_headers
)
assert response.status_code == 400
assert "configure flags" in response.json()["detail"].lower()
async def test_report_memray_failure_requires_auth(
client, sample_binary, sample_environment
):
payload = {
"commit_sha": "c" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "memray install failed",
}
response = await client.post("/api/report-memray-failure", json=payload)
assert response.status_code in (401, 403)
async def test_report_memray_failure_success(
client, auth_headers, sample_binary, sample_environment
):
payload = {
"commit_sha": "c" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "memray install failed",
}
response = await client.post(
"/api/report-memray-failure", json=payload, headers=auth_headers
)
assert response.status_code == 200
data = response.json()
assert data["message"] == "Memray failure reported successfully"
async def test_upload_duplicate_commit_binary_env(
client, auth_headers, sample_binary, sample_environment
):
"""Uploading the same commit+binary+environment twice should return 409."""
response = await client.post(
"/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers
)
assert response.status_code == 200
response = await client.post(
"/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers
)
assert response.status_code == 409
async def test_upload_existing_commit_new_binary(
client, auth_headers, db_session, sample_environment
):
"""Uploading the same commit with a different binary should succeed."""
from app.models import Binary
for bin_id in ("bin-a", "bin-b"):
db_session.add(Binary(
id=bin_id, name=bin_id, flags=[], display_order=0,
))
await db_session.commit()
payload_a = copy.deepcopy(UPLOAD_PAYLOAD)
payload_a["binary_id"] = "bin-a"
payload_a["metadata"]["configure_vars"]["CONFIG_ARGS"] = ""
payload_b = copy.deepcopy(UPLOAD_PAYLOAD)
payload_b["binary_id"] = "bin-b"
payload_b["metadata"]["configure_vars"]["CONFIG_ARGS"] = ""
resp_a = await client.post(
"/api/upload-run", json=payload_a, headers=auth_headers
)
assert resp_a.status_code == 200
resp_b = await client.post(
"/api/upload-run", json=payload_b, headers=auth_headers
)
assert resp_b.status_code == 200
async def test_upload_clears_memray_failure(
client, auth_headers, sample_binary, sample_environment
):
"""A successful upload should clear memray failures for that binary+env."""
# Report a failure
failure_payload = {
"commit_sha": "b" * 40,
"commit_timestamp": "2025-06-16T09:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "memray failed",
}
resp = await client.post(
"/api/report-memray-failure", json=failure_payload, headers=auth_headers
)
assert resp.status_code == 200
# Upload successfully — this should clear the failure
resp = await client.post(
"/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers
)
assert resp.status_code == 200
assert resp.json()["results_created"] == 1
# Verify the failure was cleared
status = await client.get("/api/memray-status")
data = status.json()
assert data["has_failures"] is False
assert data["failure_count"] == 0
async def test_memray_failure_update_newer(
client, auth_headers, sample_binary, sample_environment
):
"""Reporting a newer failure should update the existing record."""
older = {
"commit_sha": "a" * 40,
"commit_timestamp": "2025-06-15T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "old failure",
}
newer = {
"commit_sha": "b" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "new failure",
}
resp = await client.post(
"/api/report-memray-failure", json=older, headers=auth_headers
)
assert resp.status_code == 200
resp = await client.post(
"/api/report-memray-failure", json=newer, headers=auth_headers
)
assert resp.status_code == 200
assert resp.json()["message"] == "Memray failure reported successfully"
status = await client.get("/api/memray-status")
data = status.json()
assert data["failure_count"] == 1
assert data["affected_environments"][0]["commit_sha"] == "b" * 40
async def test_memray_failure_ignore_older(
client, auth_headers, sample_binary, sample_environment
):
"""Reporting an older failure should be ignored."""
newer = {
"commit_sha": "b" * 40,
"commit_timestamp": "2025-06-16T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "newer failure",
}
older = {
"commit_sha": "a" * 40,
"commit_timestamp": "2025-06-15T10:00:00",
"binary_id": "default",
"environment_id": "linux-x86_64",
"error_message": "older failure",
}
resp = await client.post(
"/api/report-memray-failure", json=newer, headers=auth_headers
)
assert resp.status_code == 200
resp = await client.post(
"/api/report-memray-failure", json=older, headers=auth_headers
)
assert resp.status_code == 200
assert "ignored" in resp.json()["message"].lower()
# Original failure should remain unchanged
status = await client.get("/api/memray-status")
data = status.json()
assert data["failure_count"] == 1
assert data["affected_environments"][0]["commit_sha"] == "b" * 40