Skip to content

Commit

Permalink
address feedback and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-sentry committed Jan 4, 2024
1 parent 941c10c commit b493ab3
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 27 deletions.
53 changes: 26 additions & 27 deletions tasks/test_results_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ async def run_async(
def save_report(
self, db_session: Session, testrun_list: List[Testrun], upload: Upload
):
repo_tests = (
db_session.query(Test).filter_by(repoid=upload.report.commit.repoid).all()
)
test_set = set()
for test in repo_tests:
test_set.add(f"{test.testsuite}::{test.name}")
for testrun in testrun_list:
test = (
db_session.query(Test)
.filter_by(name=testrun.name, testsuite=testrun.testsuite)
.first()
)
if not test:
if f"{testrun.testsuite}::{testrun.name}" not in test_set:
test = Test(
repoid=upload.report.commit.repoid,
name=testrun.name,
Expand Down Expand Up @@ -145,15 +146,17 @@ def process_individual_arg(self, upload: Upload, repository) -> List[Testrun]:
# TODO: improve report matching capabilities
# use file extensions?
# maybe do the matching in the testing result parser lib?
first_line = bytes("".join(first_line.decode("utf-8").split()), "utf-8")
try:
if first_line.startswith(b"<?xml"):
testrun_list = parse_junit_xml(file_content)
elif b"pytest" in first_line:
elif first_line.startswith(b'{"pytest_version": "7.4.3'):
testrun_list = parse_pytest_reportlog(file_content)
else:
elif first_line.startswith(b'"{"numTotalTestSuites":'):
testrun_list = parse_vitest_json(file_content)
except Exception:
except Exception as e:
log.warning(f"Error parsing: {file_content.decode()}")
raise Exception from e

return testrun_list

Expand All @@ -175,24 +178,20 @@ def should_delete_archive(self, commit_yaml):
def delete_archive(
self, commitid, repository, commit_yaml, uploads_to_delete: List[Upload]
):
if self.should_delete_archive(commit_yaml):
archive_service = ArchiveService(repository)
for upload in uploads_to_delete:
archive_url = upload.storage_path
if archive_url and not archive_url.startswith("http"):
log.info(
"Deleting uploaded file as requested",
extra=dict(
archive_url=archive_url,
commit=commitid,
upload=upload.external_id,
parent_task=self.request.parent_id,
),
)
archive_service.delete_file(archive_url)
return True

return False
archive_service = ArchiveService(repository)
for upload in uploads_to_delete:
archive_url = upload.storage_path
if archive_url and not archive_url.startswith("http"):
log.info(
"Deleting uploaded file as requested",
extra=dict(
archive_url=archive_url,
commit=commitid,
upload=upload.external_id,
parent_task=self.request.parent_id,
),
)
archive_service.delete_file(archive_url)


RegisteredTestResultsProcessorTask = celery_app.register_task(
Expand Down
295 changes: 295 additions & 0 deletions tasks/tests/unit/test_test_results_processor_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import pytest
from celery.exceptions import Retry
from shared.storage.exceptions import FileNotInStorageError

from database.models import CommitReport
from database.models.reports import Test
from database.tests.factories import CommitFactory, UploadFactory
from services.archive import ArchiveService
from tasks.test_results_processor import TestResultsProcessorTask
Expand Down Expand Up @@ -85,6 +87,7 @@ async def test_upload_processor_task_call(
}
assert expected_result == result
assert commit.message == "hello world"
assert 0 == 1

@pytest.mark.asyncio
@pytest.mark.integration
Expand Down Expand Up @@ -221,3 +224,295 @@ async def test_upload_processor_task_call_vitest(
}
assert expected_result == result
assert commit.message == "hello world"

@pytest.mark.asyncio
@pytest.mark.integration
async def test_test_result_processor_task_error_parsing(
self,
caplog,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
url = "v4/raw/2019-05-22/C3C4715CA57C910D11D5EB899FC86A7E/4c4e4654ac25037ae869caeb3619d485970b6304/a84d445c-9c1e-434f-8275-f18f1f320f81.txt"
with open(here.parent.parent / "samples" / "sample_vitest.txt") as f:
content = f.read()
mock_storage.write_file("archive", url, content)
upload = UploadFactory.create(storage_path=url)
dbsession.add(upload)
dbsession.flush()
redis_queue = [{"url": url, "upload_pk": upload.id_}]
mocker.patch.object(TestResultsProcessorTask, "app", celery_app)
mocker.patch.object(
TestResultsProcessorTask, "process_individual_arg", side_effect=Exception
)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(commit_id=commit.id_)
dbsession.add(current_report_row)
dbsession.flush()

result = await TestResultsProcessorTask().run_async(
dbsession,
repoid=commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)

assert result == {"successful": False}
assert "Error parsing testruns" in caplog.text

@pytest.mark.asyncio
@pytest.mark.integration
async def test_test_result_processor_task_delete_archive(
self,
caplog,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
url = "v4/raw/2019-05-22/C3C4715CA57C910D11D5EB899FC86A7E/4c4e4654ac25037ae869caeb3619d485970b6304/a84d445c-9c1e-434f-8275-f18f1f320f81.txt"
with open(here.parent.parent / "samples" / "sample_vitest.txt") as f:
content = f.read()
mock_storage.write_file("archive", url, content)
upload = UploadFactory.create(storage_path=url)
dbsession.add(upload)
dbsession.flush()
redis_queue = [{"url": url, "upload_pk": upload.id_}]
mocker.patch.object(TestResultsProcessorTask, "app", celery_app)
mocker.patch.object(
TestResultsProcessorTask, "should_delete_archive", return_value=True
)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(commit_id=commit.id_)
dbsession.add(current_report_row)
dbsession.flush()
result = await TestResultsProcessorTask().run_async(
dbsession,
repoid=commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)
expected_result = {
"successful": True,
"testrun_list": [
{
"duration_seconds": 0.009,
"name": " first test file 2 + 2 should equal 4",
"outcome": "Outcome.Failure",
"testsuite": "/root-directory/__tests__/test-file-1.test.ts",
},
{
"duration_seconds": 0.009,
"name": " first test file 2 + 2 should equal 4",
"outcome": "Outcome.Failure",
"testsuite": "/root-directory/__tests__/test-file-1.test.ts",
},
{
"duration_seconds": 0.009,
"name": " first test file 2 + 2 should equal 4",
"outcome": "Outcome.Failure",
"testsuite": "/root-directory/__tests__/test-file-1.test.ts",
},
{
"duration_seconds": 0.009,
"name": " first test file 2 + 2 should equal 4",
"outcome": "Outcome.Failure",
"testsuite": "/root-directory/__tests__/test-file-1.test.ts",
},
],
}

assert expected_result == result
assert "Deleting uploaded file as requested" in caplog.text
with pytest.raises(FileNotInStorageError):
mock_storage.read_file("archive", url)

@pytest.mark.asyncio
@pytest.mark.integration
async def test_test_result_processor_task_bad_file(
self,
caplog,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
url = "v4/raw/2019-05-22/C3C4715CA57C910D11D5EB899FC86A7E/4c4e4654ac25037ae869caeb3619d485970b6304/a84d445c-9c1e-434f-8275-f18f1f320f81.txt"
mock_storage.write_file(
"archive",
url,
b'{"test_results_files": [{"filename": "blah", "format": "blah", "data": "eJxLyknMSIJiAB8CBMY="}]}',
)
upload = UploadFactory.create(storage_path=url)
dbsession.add(upload)
dbsession.flush()
redis_queue = [{"url": url, "upload_pk": upload.id_}]
mocker.patch.object(TestResultsProcessorTask, "app", celery_app)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(commit_id=commit.id_)
dbsession.add(current_report_row)
dbsession.flush()
result = await TestResultsProcessorTask().run_async(
dbsession,
repoid=commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)
expected_result = {"successful": False}

assert expected_result == result
assert "Error parsing: blahblahblah" in caplog.text

@pytest.mark.asyncio
@pytest.mark.integration
async def test_upload_processor_task_call_multiple(
self,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
url = "v4/raw/2019-05-22/C3C4715CA57C910D11D5EB899FC86A7E/4c4e4654ac25037ae869caeb3619d485970b6304/a84d445c-9c1e-434f-8275-f18f1f320f81.txt"
with open(here.parent.parent / "samples" / "sample_test.txt") as f:
content = f.read()
mock_storage.write_file("archive", url, content)
upload = UploadFactory.create(storage_path=url)
upload_2 = UploadFactory.create(storage_path=url)
dbsession.add(upload)
dbsession.flush()
redis_queue = [{"url": url, "upload_pk": upload.id_}]
mocker.patch.object(TestResultsProcessorTask, "app", celery_app)

commit = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ad",
repository__owner__unencrypted_oauth_token="test7lk5ndmtqzxlx06rip65nac9c7epqopclnoy",
repository__owner__username="joseph-sentry",
repository__owner__service="github",
repository__name="codecov-demo",
)
commit_2 = CommitFactory.create(
message="hello world",
commitid="cd76b0821854a780b60012aed85af0a8263004ae",
)

commit_2.repository = commit.repository
dbsession.add(commit)
dbsession.add(commit_2)
dbsession.flush()
current_report_row = CommitReport(commit_id=commit.id_)
current_report_row_2 = CommitReport(commit_id=commit_2.id_)
dbsession.add(current_report_row)
dbsession.add(current_report_row_2)
dbsession.flush()
upload.report = current_report_row
upload_2.report = current_report_row_2

result = await TestResultsProcessorTask().run_async(
dbsession,
repoid=commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)
expected_result = {
"successful": True,
"testrun_list": [
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_add",
"outcome": "Outcome.Pass",
"testsuite": "pytest",
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_subtract",
"outcome": "Outcome.Pass",
"testsuite": "pytest",
},
{
"duration_seconds": 0.0,
"name": "api.temp.calculator.test_calculator::test_multiply",
"outcome": "Outcome.Pass",
"testsuite": "pytest",
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_divide",
"outcome": "Outcome.Failure",
"testsuite": "pytest",
},
],
}
assert expected_result == result
assert commit.message == "hello world"

url = "v4/raw/2019-05-22/C3C4715CA57C910D11D5EB899FC86A7E/4c4e4654ac25037ae869caeb3619d485970b6304/a84d445c-9c1e-434f-8275-f18f1f320f80.txt"
with open(here.parent.parent / "samples" / "sample_test.txt") as f:
content = f.read()
mock_storage.write_file("archive", url, content)
redis_queue = [{"url": url, "upload_pk": upload_2.id_}]

result = await TestResultsProcessorTask().run_async(
dbsession,
repoid=commit_2.repoid,
commitid=commit_2.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)

repo_tests = (
dbsession.query(Test).filter_by(repoid=upload.report.commit.repoid).all()
)

assert len(repo_tests) == 4

0 comments on commit b493ab3

Please sign in to comment.