Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 22fe17c

Browse files
Merge branch 'main' into spalmurray/account-api-tweaks
2 parents 6168faa + 1203a60 commit 22fe17c

File tree

3 files changed

+250
-27
lines changed

3 files changed

+250
-27
lines changed

docker/Dockerfile.requirements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN apt-get install -y \
2525
libpq-dev \
2626
make \
2727
curl \
28+
libexpat1 \
2829
&& pip install --upgrade pip
2930

3031
WORKDIR /pip-packages/

upload/tests/views/test_bundle_analysis.py

Lines changed: 201 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
@pytest.mark.django_db(databases={"default", "timeseries"})
1919
def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
2020
upload = mocker.patch.object(TaskService, "upload")
21-
mock_sentry_metrics = mocker.patch(
22-
"upload.views.bundle_analysis.sentry_metrics.incr"
21+
mock_metrics = mocker.patch(
22+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
2323
)
2424
create_presigned_put = mocker.patch(
2525
"services.archive.StorageService.create_presigned_put",
@@ -89,14 +89,101 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
8989
report_code=None,
9090
report_type="bundle_analysis",
9191
)
92-
mock_sentry_metrics.assert_called_with(
93-
"upload",
94-
tags={
92+
mock_metrics.assert_called_with(
93+
**{
94+
"agent": "cli",
95+
"version": "0.4.7",
96+
"action": "bundle_analysis",
97+
"endpoint": "bundle_analysis",
98+
"is_using_shelter": "no",
99+
"position": "end",
100+
},
101+
)
102+
103+
104+
@pytest.mark.django_db(databases={"default", "timeseries"})
105+
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")
106+
def test_upload_bundle_analysis_success_shelter(db, client, mocker, mock_redis):
107+
upload = mocker.patch.object(TaskService, "upload")
108+
mock_metrics = mocker.patch(
109+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
110+
)
111+
create_presigned_put = mocker.patch(
112+
"services.archive.StorageService.create_presigned_put",
113+
return_value="test-presigned-put",
114+
)
115+
116+
repository = RepositoryFactory.create()
117+
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
118+
119+
client = APIClient()
120+
client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}")
121+
122+
res = client.post(
123+
reverse("upload-bundle-analysis"),
124+
{
125+
"commit": commit_sha,
126+
"slug": f"{repository.author.username}::::{repository.name}",
127+
"build": "test-build",
128+
"buildURL": "test-build-url",
129+
"job": "test-job",
130+
"service": "test-service",
131+
"compareSha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
132+
"storage_path": "shelter/test/path.txt",
133+
"upload_external_id": "test-47078f85-2cee-4511-b38d-183c334ef43b",
134+
},
135+
format="json",
136+
headers={"User-Agent": "codecov-cli/0.4.7"},
137+
)
138+
assert res.status_code == 201
139+
140+
# returns presigned storage URL
141+
assert res.json() == {"url": "test-presigned-put"}
142+
143+
create_presigned_put.assert_called_once_with("bundle-analysis", ANY, 30)
144+
call = create_presigned_put.mock_calls[0]
145+
_, storage_path, _ = call.args
146+
match = re.match(r"v1/uploads/([\d\w\-]+)\.json", storage_path)
147+
assert match
148+
(reportid,) = match.groups()
149+
150+
# creates commit
151+
commit = Commit.objects.get(commitid=commit_sha)
152+
assert commit
153+
154+
# saves args in Redis
155+
redis = get_redis_connection()
156+
args = redis.rpop(f"uploads/{repository.repoid}/{commit_sha}/bundle_analysis")
157+
assert json.loads(args) == {
158+
"reportid": reportid,
159+
"build": "test-build",
160+
"build_url": "test-build-url",
161+
"job": "test-job",
162+
"service": "test-service",
163+
"url": f"v1/uploads/{reportid}.json",
164+
"commit": commit_sha,
165+
"report_code": None,
166+
"bundle_analysis_compare_sha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
167+
}
168+
169+
# sets latest upload timestamp
170+
ts = redis.get(f"latest_upload/{repository.repoid}/{commit_sha}/bundle_analysis")
171+
assert ts
172+
173+
# triggers upload task
174+
upload.assert_called_with(
175+
commitid=commit_sha,
176+
repoid=repository.repoid,
177+
countdown=4,
178+
report_code=None,
179+
report_type="bundle_analysis",
180+
)
181+
mock_metrics.assert_called_with(
182+
**{
95183
"agent": "cli",
96184
"version": "0.4.7",
97185
"action": "bundle_analysis",
98186
"endpoint": "bundle_analysis",
99-
"repo_visibility": "private",
100187
"is_using_shelter": "no",
101188
"position": "end",
102189
},
@@ -110,6 +197,9 @@ def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
110197
"services.archive.StorageService.create_presigned_put",
111198
return_value="test-presigned-put",
112199
)
200+
mock_metrics = mocker.patch(
201+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
202+
)
113203

114204
repository = RepositoryFactory.create()
115205
org_token = OrganizationLevelTokenFactory.create(owner=repository.author)
@@ -126,6 +216,16 @@ def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
126216
format="json",
127217
)
128218
assert res.status_code == 201
219+
mock_metrics.assert_called_with(
220+
**{
221+
"agent": "unknown-user-agent",
222+
"version": "unknown-user-agent",
223+
"action": "bundle_analysis",
224+
"endpoint": "bundle_analysis",
225+
"is_using_shelter": "no",
226+
"position": "end",
227+
},
228+
)
129229

130230

131231
@pytest.mark.django_db(databases={"default", "timeseries"})
@@ -135,6 +235,9 @@ def test_upload_bundle_analysis_existing_commit(db, client, mocker, mock_redis):
135235
"services.archive.StorageService.create_presigned_put",
136236
return_value="test-presigned-put",
137237
)
238+
mock_metrics = mocker.patch(
239+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
240+
)
138241

139242
repository = RepositoryFactory.create()
140243
commit = CommitFactory.create(repository=repository)
@@ -159,6 +262,16 @@ def test_upload_bundle_analysis_existing_commit(db, client, mocker, mock_redis):
159262
report_code=None,
160263
report_type="bundle_analysis",
161264
)
265+
mock_metrics.assert_called_with(
266+
**{
267+
"agent": "unknown-user-agent",
268+
"version": "unknown-user-agent",
269+
"action": "bundle_analysis",
270+
"endpoint": "bundle_analysis",
271+
"is_using_shelter": "no",
272+
"position": "end",
273+
},
274+
)
162275

163276

164277
def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
@@ -167,6 +280,9 @@ def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
167280
"services.archive.StorageService.create_presigned_put",
168281
return_value="test-presigned-put",
169282
)
283+
mock_metrics = mocker.patch(
284+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
285+
)
170286

171287
repository = RepositoryFactory.create()
172288
commit = CommitFactory.create(repository=repository)
@@ -195,6 +311,16 @@ def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
195311
assert res.status_code == 400
196312
assert res.json() == {"commit": ["This field is required."]}
197313
assert not upload.called
314+
mock_metrics.assert_called_with(
315+
**{
316+
"agent": "unknown-user-agent",
317+
"version": "unknown-user-agent",
318+
"action": "bundle_analysis",
319+
"endpoint": "bundle_analysis",
320+
"is_using_shelter": "no",
321+
"position": "start",
322+
},
323+
)
198324

199325

200326
def test_upload_bundle_analysis_invalid_token(db, client, mocker, mock_redis):
@@ -233,6 +359,9 @@ def test_upload_bundle_analysis_github_oidc_auth(
233359
"services.archive.StorageService.create_presigned_put",
234360
return_value="test-presigned-put",
235361
)
362+
mock_metrics = mocker.patch(
363+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
364+
)
236365
repository = RepositoryFactory()
237366
mock_jwt_decode.return_value = {
238367
"repository": f"url/{repository.name}",
@@ -253,18 +382,30 @@ def test_upload_bundle_analysis_github_oidc_auth(
253382
format="json",
254383
)
255384
assert res.status_code == 201
385+
mock_metrics.assert_called_with(
386+
**{
387+
"agent": "unknown-user-agent",
388+
"version": "unknown-user-agent",
389+
"action": "bundle_analysis",
390+
"endpoint": "bundle_analysis",
391+
"is_using_shelter": "no",
392+
"position": "end",
393+
},
394+
)
256395

257396

258397
@pytest.mark.django_db(databases={"default", "timeseries"})
259398
def test_upload_bundle_analysis_measurement_datasets_created(
260399
db, client, mocker, mock_redis
261400
):
262401
mocker.patch.object(TaskService, "upload")
263-
mocker.patch("upload.views.bundle_analysis.sentry_metrics.incr")
264402
mocker.patch(
265403
"services.archive.StorageService.create_presigned_put",
266404
return_value="test-presigned-put",
267405
)
406+
mock_metrics = mocker.patch(
407+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
408+
)
268409

269410
repository = RepositoryFactory.create()
270411
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
@@ -301,18 +442,31 @@ def test_upload_bundle_analysis_measurement_datasets_created(
301442
repository_id=repository.pk,
302443
).exists()
303444

445+
mock_metrics.assert_called_with(
446+
**{
447+
"agent": "cli",
448+
"version": "0.4.7",
449+
"action": "bundle_analysis",
450+
"endpoint": "bundle_analysis",
451+
"is_using_shelter": "no",
452+
"position": "end",
453+
},
454+
)
455+
304456

305457
@override_settings(TIMESERIES_ENABLED=False)
306458
@pytest.mark.django_db(databases={"default", "timeseries"})
307459
def test_upload_bundle_analysis_measurement_timeseries_disabled(
308460
db, client, mocker, mock_redis
309461
):
310462
mocker.patch.object(TaskService, "upload")
311-
mocker.patch("upload.views.bundle_analysis.sentry_metrics.incr")
312463
mocker.patch(
313464
"services.archive.StorageService.create_presigned_put",
314465
return_value="test-presigned-put",
315466
)
467+
mock_metrics = mocker.patch(
468+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
469+
)
316470

317471
repository = RepositoryFactory.create()
318472
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
@@ -349,6 +503,17 @@ def test_upload_bundle_analysis_measurement_timeseries_disabled(
349503
repository_id=repository.pk,
350504
).exists()
351505

506+
mock_metrics.assert_called_with(
507+
**{
508+
"agent": "cli",
509+
"version": "0.4.7",
510+
"action": "bundle_analysis",
511+
"endpoint": "bundle_analysis",
512+
"is_using_shelter": "no",
513+
"position": "end",
514+
},
515+
)
516+
352517

353518
@pytest.mark.django_db(databases={"default", "timeseries"})
354519
def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
@@ -358,6 +523,9 @@ def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
358523
"services.archive.StorageService.create_presigned_put",
359524
return_value="test-presigned-put",
360525
)
526+
mock_metrics = mocker.patch(
527+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
528+
)
361529

362530
repository = RepositoryFactory.create()
363531
org_token = OrganizationLevelTokenFactory.create(owner=repository.author)
@@ -377,10 +545,24 @@ def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
377545
assert res.json() == {"detail": "Repository not found."}
378546
assert not upload.called
379547

548+
mock_metrics.assert_called_with(
549+
**{
550+
"agent": "unknown-user-agent",
551+
"version": "unknown-user-agent",
552+
"action": "bundle_analysis",
553+
"endpoint": "bundle_analysis",
554+
"is_using_shelter": "no",
555+
"position": "start",
556+
},
557+
)
558+
380559

381560
@pytest.mark.django_db(databases={"default", "timeseries"})
382561
def test_upload_bundle_analysis_tokenless_success(db, client, mocker, mock_redis):
383562
upload = mocker.patch.object(TaskService, "upload")
563+
mock_metrics = mocker.patch(
564+
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
565+
)
384566

385567
create_presigned_put = mocker.patch(
386568
"services.archive.StorageService.create_presigned_put",
@@ -417,6 +599,17 @@ def test_upload_bundle_analysis_tokenless_success(db, client, mocker, mock_redis
417599
assert upload.called
418600
create_presigned_put.assert_called_once_with("bundle-analysis", ANY, 30)
419601

602+
mock_metrics.assert_called_with(
603+
**{
604+
"agent": "cli",
605+
"version": "0.4.7",
606+
"action": "bundle_analysis",
607+
"endpoint": "bundle_analysis",
608+
"is_using_shelter": "no",
609+
"position": "end",
610+
},
611+
)
612+
420613

421614
@pytest.mark.django_db(databases={"default", "timeseries"})
422615
def test_upload_bundle_analysis_tokenless_no_repo(db, client, mocker, mock_redis):

0 commit comments

Comments
 (0)