Skip to content

Commit

Permalink
fix(test_results): handle parse_single_file failures
Browse files Browse the repository at this point in the history
we don't want to fail the entire upload parsing because one
test result file failed to be parsed.

Signed-off-by: joseph-sentry <joseph.sawaya@sentry.io>
  • Loading branch information
joseph-sentry committed Jan 25, 2024
1 parent e37e0e6 commit ea5afbf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
73 changes: 46 additions & 27 deletions tasks/test_results_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@


class ParserFailureError(Exception):
...
def __init__(self, err_msg, file_content, parser="", parser_err_msg=""):
self.err_msg = err_msg
self.file_content = file_content
self.parser = parser
self.parser_err_msg = parser_err_msg


class ParserNotSupportedError(Exception):
Expand Down Expand Up @@ -90,14 +94,18 @@ async def run_async(
def process_individual_upload(
self, db_session, repoid, commitid, upload_obj: Upload
):
try:
parsed_testruns: List[Testrun] = self.process_individual_arg(
upload_obj, upload_obj.report.commit.repository
)
except ParserFailureError:
log.warning(
f"Error parsing testruns",
extra=dict(repoid=repoid, commitid=commitid, uploadid=upload_obj.id),
upload_id = upload_obj.id
parsed_testruns: List[Testrun] = self.process_individual_arg(
upload_obj, upload_obj.report.commit.repository
)
if not parsed_testruns:
log.error(
"No test result files were successfully parsed for this upload",
extra=dict(
repoid=repoid,
commitid=commitid,
upload_id=upload_id,
),
)
return {
"successful": False,
Expand Down Expand Up @@ -151,34 +159,45 @@ def process_individual_arg(self, upload: Upload, repository) -> List[Testrun]:
for file in data["test_results_files"]:
file = file["data"]
file_bytes = BytesIO(zlib.decompress(base64.b64decode(file)))
testrun_list += self.parse_single_file(file_bytes)
try:
testrun_list += self.parse_single_file(file_bytes)
except ParserFailureError as exc:
log.error(
exc.err_msg,
extra=dict(
repoid=upload.report.commit.repoid,
commitid=upload.report.commit_id,
uploadid=upload.id,
file_content=exc.file_content,
parser=exc.parser,
parser_err_msg=exc.parser_err_msg,
),
)

return testrun_list

def parse_single_file(self, file_bytes):
def parse_single_file(
self,
file_bytes,
):
try:
parser, parsing_function = self.match_report(file_bytes)
except ParserNotSupportedError as e:
log.error(
"File did not match any parser format",
extra=dict(
file_content=file_bytes.read().decode()[:300],
),
)
raise ParserFailureError() from e
raise ParserFailureError(
err_msg="File did not match any parser format",
file_content=file_bytes.read().decode()[:300],
) from e

try:
file_content = file_bytes.read()
res = parsing_function(file_content)
except ParserError as e:
log.error(
"Error parsing test result file",
extra=dict(
file_content=file_content.decode()[:300],
parser=parser,
err_msg=str(e),
),
)
raise ParserFailureError() from e
raise ParserFailureError(

Check warning on line 195 in tasks/test_results_processor.py

View check run for this annotation

Codecov - Staging / codecov/patch

tasks/test_results_processor.py#L194-L195

Added lines #L194 - L195 were not covered by tests

Check warning on line 195 in tasks/test_results_processor.py

View check run for this annotation

Codecov - QA / codecov/patch

tasks/test_results_processor.py#L194-L195

Added lines #L194 - L195 were not covered by tests

Check warning on line 195 in tasks/test_results_processor.py

View check run for this annotation

Codecov Public QA / codecov/patch

tasks/test_results_processor.py#L194-L195

Added lines #L194 - L195 were not covered by tests

Check warning on line 195 in tasks/test_results_processor.py

View check run for this annotation

Codecov / codecov/patch

tasks/test_results_processor.py#L194-L195

Added lines #L194 - L195 were not covered by tests
err_msg="Error parsing file",
file_content=file_content.decode()[:300],
parser=parser,
parser_err_msg=str(e),
) from e

return res

Expand Down
11 changes: 6 additions & 5 deletions tasks/tests/unit/test_test_results_processor_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ async def test_test_result_processor_task_error_parsing(
mocker.patch.object(TestResultsProcessorTask, "app", celery_app)
mocker.patch.object(
TestResultsProcessorTask,
"process_individual_arg",
side_effect=ParserFailureError,
"parse_single_file",
side_effect=ParserFailureError(
err_msg="Test error message", file_content=""
),
)

commit = CommitFactory.create(
Expand All @@ -264,9 +266,8 @@ async def test_test_result_processor_task_error_parsing(
commit_yaml={"codecov": {"max_report_age": False}},
arguments_list=redis_queue,
)

assert result == [{"successful": False}]
assert "Error parsing testruns" in caplog.text
print(caplog.text)
assert "Test error message" in caplog.text

@pytest.mark.asyncio
@pytest.mark.integration
Expand Down

0 comments on commit ea5afbf

Please sign in to comment.