Skip to content

Commit

Permalink
Adding metrics for BA and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trent-codecov committed May 15, 2024
1 parent f5792c6 commit 6e5511a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 22 deletions.
15 changes: 15 additions & 0 deletions upload/tests/views/test_bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

def test_upload_bundle_analysis(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_sentry_metrics = mocker.patch(
"upload.views.bundle_analysis.sentry_metrics.incr"
)
create_presigned_put = mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
Expand All @@ -36,6 +39,7 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis):
"service": "test-service",
},
format="json",
headers={"User-Agent": "codecov-cli/0.4.7"},
)
assert res.status_code == 201

Expand Down Expand Up @@ -79,6 +83,17 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis):
report_code=None,
report_type="bundle_analysis",
)
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
},
)


def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
Expand Down
14 changes: 13 additions & 1 deletion upload/tests/views/test_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker):
private=False, author__username="codecov", name="the_repo"
)
mocked_call = mocker.patch.object(TaskService, "update_commit")
mock_sentry_metrics = mocker.patch("upload.views.commits.sentry_metrics.incr")
mock_jwt_decode.return_value = {
"repository": f"url/{repository.name}",
"repository_owner": repository.author.username,
Expand All @@ -315,7 +316,7 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker):
"pullid": "4",
},
format="json",
headers={"Authorization": f"token {token}"},
headers={"Authorization": f"token {token}", "User-Agent": "codecov-cli/0.4.7"},
)
assert response.status_code == 201
response_json = response.json()
Expand All @@ -340,3 +341,14 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker):
}
assert expected_response == response_json
mocked_call.assert_called_with(commitid="commit_sha", repoid=repository.repoid)
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "create_commit",
"repo_visibility": "public",
"is_using_shelter": "no",
},
)
16 changes: 13 additions & 3 deletions upload/tests/views/test_empty_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_empty_upload_with_yaml_ignored_files(
mocker.patch.object(
CanDoCoverageUploadsPermission, "has_permission", return_value=True
)
mock_sentry_metrics = mocker.patch("upload.views.empty_upload.sentry_metrics.incr")
mock_final_yaml.return_value = UserYaml(
{
"ignore": [
Expand Down Expand Up @@ -79,9 +80,7 @@ def test_empty_upload_with_yaml_ignored_files(
commit.commitid,
],
)
response = client.post(
url,
)
response = client.post(url, headers={"User-Agent": "codecov-cli/0.4.7"})
response_json = response.json()
assert response.status_code == 200
assert (
Expand All @@ -91,6 +90,17 @@ def test_empty_upload_with_yaml_ignored_files(
notify_mock.assert_called_once_with(
repoid=repository.repoid, commitid=commit.commitid, empty_upload="pass"
)
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "empty_upload",
"repo_visibility": "private",
"is_using_shelter": "no",
},
)


@patch("services.task.TaskService.notify")
Expand Down
16 changes: 15 additions & 1 deletion upload/tests/views/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_reports_get_not_allowed(client, mocker):

def test_reports_post(client, db, mocker):
mocked_call = mocker.patch.object(TaskService, "preprocess_upload")
mock_sentry_metrics = mocker.patch("upload.views.reports.sentry_metrics.incr")
repository = RepositoryFactory(
name="the_repo", author__username="codecov", author__service="github"
)
Expand All @@ -35,7 +36,9 @@ def test_reports_post(client, db, mocker):
"new_upload.reports",
args=["github", "codecov::::the_repo", commit.commitid],
)
response = client.post(url, data={"code": "code1"})
response = client.post(
url, data={"code": "code1"}, headers={"User-Agent": "codecov-cli/0.4.7"}
)

assert (
url == f"/upload/github/codecov::::the_repo/commits/{commit.commitid}/reports"
Expand All @@ -45,6 +48,17 @@ def test_reports_post(client, db, mocker):
commit_id=commit.id, code="code1", report_type=CommitReport.ReportType.COVERAGE
).exists()
mocked_call.assert_called_with(repository.repoid, commit.commitid, "code1")
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "create_report",
"repo_visibility": "private",
"is_using_shelter": "no",
},
)


@patch("upload.helpers.jwt.decode")
Expand Down
13 changes: 13 additions & 0 deletions upload/tests/views/test_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

def test_upload_test_results(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_sentry_metrics = mocker.patch("upload.views.test_results.metrics.incr")
create_presigned_put = mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
Expand All @@ -37,6 +38,7 @@ def test_upload_test_results(db, client, mocker, mock_redis):
"service": "test-service",
},
format="json",
headers={"User-Agent": "codecov-cli/0.4.7"},
)
assert res.status_code == 201

Expand Down Expand Up @@ -89,6 +91,17 @@ def test_upload_test_results(db, client, mocker, mock_redis):
report_code=None,
report_type="test_results",
)
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "test_results",
"endpoint": "test_results",
"repo_visibility": "private",
"is_using_shelter": "no",
},
)


def test_test_results_org_token(db, client, mocker, mock_redis):
Expand Down
19 changes: 15 additions & 4 deletions upload/tests/views/test_upload_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc
mocker.patch.object(
CanDoCoverageUploadsPermission, "has_permission", return_value=True
)

mock_sentry_metrics = mocker.patch(
"upload.views.upload_completion.sentry_metrics.incr"
)
repository = RepositoryFactory(
name="the_repo", author__username="codecov", author__service="github"
)
Expand All @@ -74,9 +76,7 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc
commit.commitid,
],
)
response = client.post(
url,
)
response = client.post(url, headers={"User-Agent": "codecov-cli/0.4.7"})
response_json = response.json()
assert response.status_code == 200
assert response_json == {
Expand All @@ -86,6 +86,17 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc
"uploads_error": 0,
}
mocked_manual_trigger.assert_called_once_with(repository.repoid, commit.commitid)
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "upload_complete",
"repo_visibility": "private",
"is_using_shelter": "no",
},
)


@patch("services.task.TaskService.manual_upload_completion_trigger")
Expand Down
2 changes: 2 additions & 0 deletions upload/tests/views/test_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def test_uploads_post_shelter(db, mocker, mock_redis):
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "create_upload",
"repo_visibility": "private",
"is_using_shelter": "yes",
},
Expand All @@ -667,6 +668,7 @@ def test_uploads_post_shelter(db, mocker, mock_redis):
"agent": "cli",
"version": "0.4.7",
"action": "coverage",
"endpoint": "create_upload",
"repo_visibility": "private",
"is_using_shelter": "yes",
},
Expand Down
4 changes: 2 additions & 2 deletions upload/views/bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def post(self, request):
sentry_metrics.incr(
"upload",
tags=generate_upload_sentry_metrics_tags(
action="coverage",
endpoint="create_report",
action="bundle_analysis",
endpoint="bundle_analysis",
request=self.request,
repository=repo,
is_shelter_request=self.is_shelter_request(),
Expand Down
22 changes: 11 additions & 11 deletions upload/views/empty_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ def post(self, request, *args, **kwargs):
)
)
]

sentry_metrics.incr(
"upload",
tags=generate_upload_sentry_metrics_tags(
action="coverage",
endpoint="empty_upload",
request=self.request,
repository=repo,
is_shelter_request=self.is_shelter_request(),
),
)
if set(changed_files) == set(ignored_changed_files):
TaskService().notify(
repoid=repo.repoid, commitid=commit.commitid, empty_upload="pass"
Expand All @@ -149,16 +158,7 @@ def post(self, request, *args, **kwargs):
TaskService().notify(
repoid=repo.repoid, commitid=commit.commitid, empty_upload="fail"
)
sentry_metrics.incr(
"upload",
tags=generate_upload_sentry_metrics_tags(
action="coverage",
endpoint="empty_upload",
request=self.request,
repository=repo,
is_shelter_request=self.is_shelter_request(),
),
)

return Response(
data={
"result": "Some files cannot be ignored. Triggering failing notifications.",
Expand Down

0 comments on commit 6e5511a

Please sign in to comment.