Skip to content

Commit

Permalink
fix: make some test result finisher fixes
Browse files Browse the repository at this point in the history
Signed-off-by: joseph-sentry <joseph.sawaya@sentry.io>
  • Loading branch information
joseph-sentry committed Jan 16, 2024
1 parent 59330a6 commit 6facf1f
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 8 deletions.
24 changes: 17 additions & 7 deletions tasks/test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from services.lock_manager import LockManager, LockRetry, LockType
from services.test_results import TestResultsNotifier
from tasks.base import BaseCodecovTask
from tasks.notify import notify_task_name

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -146,6 +147,12 @@ async def process_async_within_lock(
testrun_list += existing_test_instance_by_test.values()

if self.check_if_no_failures(testrun_list):
self.app.tasks[notify_task_name].apply_async(
args=None,
kwargs=dict(
repoid=repoid, commitid=commitid, current_yaml=commit_yaml.to_dict()
),
)
return {"notify_attempted": False, "notify_succeeded": False}

success = None
Expand Down Expand Up @@ -199,21 +206,24 @@ def try_overwrite_old_test_instance(
outcome,
failure_message,
):
existing_test_instance = test_map[test_id]
existing_run_number = existing_test_instance.upload.build_code
existing_run_number = test_map[test_id].upload.build_code

try:
if int(run_number) > int(existing_run_number):
existing_test_instance.upload_id = upload_id
existing_test_instance.duration_seconds = duration_seconds
existing_test_instance.outcome = outcome
existing_test_instance.failure_message = failure_message
test_map[test_id].upload_id = upload_id
test_map[test_id].duration_seconds = duration_seconds
test_map[test_id].outcome = outcome
test_map[test_id].failure_message = failure_message

except ValueError:
pass
except TypeError:
pass

Check warning on line 221 in tasks/test_results_finisher.py

View check run for this annotation

Codecov - Staging / codecov/patch

tasks/test_results_finisher.py#L218-L221

Added lines #L218 - L221 were not covered by tests

Check warning on line 221 in tasks/test_results_finisher.py

View check run for this annotation

Codecov - QA / codecov/patch

tasks/test_results_finisher.py#L218-L221

Added lines #L218 - L221 were not covered by tests

Check warning on line 221 in tasks/test_results_finisher.py

View check run for this annotation

Codecov Public QA / codecov/patch

tasks/test_results_finisher.py#L218-L221

Added lines #L218 - L221 were not covered by tests

Check warning on line 221 in tasks/test_results_finisher.py

View check run for this annotation

Codecov / codecov/patch

tasks/test_results_finisher.py#L218-L221

Added lines #L218 - L221 were not covered by tests

def check_if_no_failures(self, testrun_list):
return all([instance.outcome != Outcome.Failure for instance in testrun_list])
return all(
[instance.outcome != int(Outcome.Failure) for instance in testrun_list]
)

def get_existing_test_instance_by_test(self, db_session, commit):
existing_test_instances = (
Expand Down
182 changes: 181 additions & 1 deletion tasks/tests/unit/test_test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,12 @@ async def test_upload_finisher_task_call_no_failures(
dbsession.add(upload)
dbsession.flush()

mocker.patch.object(TestResultsFinisherTask, "app", celery_app)
mocked_app = mocker.patch.object(
TestResultsFinisherTask,
"app",
tasks={"app.tasks.notify.Notify": mocker.MagicMock()},
)
mocker.patch.object(TestResultsFinisherTask, "hard_time_limit_task", 0)

commit = CommitFactory.create(
message="hello world",
Expand Down Expand Up @@ -734,3 +739,178 @@ async def test_upload_finisher_task_call_edit_comment(
)

assert expected_result == result

@pytest.mark.asyncio
@pytest.mark.integration
async def test_upload_finisher_task_call_update_comment_when_failures_resolved(
self,
mocker,
mock_configuration,
dbsession,
codecov_vcr,
mock_storage,
mock_redis,
celery_app,
):
upload = UploadFactory.create()
upload.build_code = 1
dbsession.add(upload)
dbsession.flush()
upload2 = UploadFactory.create()
upload2.build_code = 2
dbsession.add(upload2)
dbsession.flush()

mocked_app = mocker.patch.object(
TestResultsFinisherTask,
"app",
tasks={"app.tasks.notify.Notify": mocker.MagicMock()},
)
mocker.patch.object(TestResultsFinisherTask, "hard_time_limit_task", 0)

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",
)

pull = PullFactory.create(repository=commit.repository, head=commit.commitid)

_ = mocker.patch(
"services.test_results.fetch_and_update_pull_request_information_from_commit",
return_value=EnrichedPull(
database_pull=pull,
provider_pull={},
),
)
m = mocker.MagicMock(
edit_comment=AsyncMock(return_value=True),
post_comment=AsyncMock(return_value={"id": 1}),
)
mocked_repo_provider = mocker.patch(
"services.test_results.get_repo_provider_service",
return_value=m,
)

dbsession.add(commit)
dbsession.flush()
current_report_row = CommitReport(
commit_id=commit.id_, report_type=ReportType.TEST_RESULTS.value
)
dbsession.add(current_report_row)
dbsession.flush()
upload.report = current_report_row
upload2.report = current_report_row
dbsession.flush()

result = await TestResultsFinisherTask().run_async(
dbsession,
[
[
{
"successful": True,
"upload_id": upload.id,
"env": "",
"run_number": 1,
"testrun_list": [
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_add",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_subtract",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.0,
"name": "api.temp.calculator.test_calculator::test_multiply",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "hello world",
"outcome": int(Outcome.Failure),
"testsuite": "hello world testsuite",
"failure_message": "bad failure",
},
],
}
],
],
repoid=upload.report.commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
)
expected_result = {"notify_attempted": True, "notify_succeeded": True}
m.post_comment.assert_called_with(
pull.pullid,
"## [Codecov](url) Report\n\n**Test Failures Detected**: Due to failing tests, we cannot provide coverage reports at this time.\n\n### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n<details><summary>View the full list of failed tests</summary>\n\n| **File path** | **Failure message** |\n| :-- | :-- |\n| hello world testsuite::hello world | <pre>bad failure</pre> |",
)

result = await TestResultsFinisherTask().run_async(
dbsession,
[
[
{
"successful": True,
"upload_id": upload2.id,
"env": "",
"run_number": 2,
"testrun_list": [
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_add",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "api.temp.calculator.test_calculator::test_subtract",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.0,
"name": "api.temp.calculator.test_calculator::test_multiply",
"outcome": int(Outcome.Pass),
"testsuite": "pytest",
"failure_message": None,
},
{
"duration_seconds": 0.001,
"name": "hello world",
"outcome": int(Outcome.Pass),
"testsuite": "hello world testsuite",
"failure_message": None,
},
],
}
],
],
repoid=upload.report.commit.repoid,
commitid=commit.commitid,
commit_yaml={"codecov": {"max_report_age": False}},
)
mocked_app.tasks["app.tasks.notify.Notify"].apply_async.assert_called_with(
args=None,
kwargs={
"commitid": commit.commitid,
"current_yaml": {"codecov": {"max_report_age": False}},
"repoid": commit.repoid,
},
)
expected_result = {"notify_attempted": False, "notify_succeeded": False}
assert expected_result == result

0 comments on commit 6facf1f

Please sign in to comment.