Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
tradefed/tasks.py: check if testrun exists
Browse files Browse the repository at this point in the history
There could be a scenario where a task takes a bit long to run
and the user might have deleted the testjob and therefore its
testrun along with it. The plugin must check for such case.

Signed-off-by: Charles Oliveira <[email protected]>
  • Loading branch information
chaws committed Jul 17, 2024
1 parent 580662e commit ceed542
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions test/test_tradefed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from io import StringIO, BytesIO
from unittest.mock import PropertyMock, MagicMock, Mock, patch
from tradefed import Tradefed, ResultFiles, ExtractedResult
from tradefed.tasks import update_build_status
from collections import defaultdict


Expand Down Expand Up @@ -1295,3 +1296,19 @@ def test_get_tradefed_url_from_tuxsuite(self):

url = self.plugin._get_tradefed_url_from_tuxsuite(testjob)
self.assertEqual(tradefed_url, url)

@patch("tradefed.update_testjob_status.delay")
@patch("tradefed.tasks.TestRun")
def test_update_build_status_testrun_not_found(self, mock_testrun, mock_update_testjob_status):
class does_not_exist_mock(Exception):
pass

mock_testrun.DoesNotExist = does_not_exist_mock

objects = MagicMock()
objects.get.side_effect = does_not_exist_mock()

mock_testrun.objects = objects

update_build_status(None, None, None, None)
mock_update_testjob_status.assert_not_called()
11 changes: 10 additions & 1 deletion tradefed/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

@celery.task(queue='ci_fetch')
def update_build_status(results_list, testrun_id, job_id, job_status):
testrun = TestRun.objects.get(pk=testrun_id)

"""
There could be a scenario where the test job, and its test run as consequence, gets deleted
by the user. Since this function is invoked upon task from the queue, by the time it actually
gets invoked, the test run might not exist anymore.
"""
try:
testrun = TestRun.objects.get(pk=testrun_id)
except TestRun.DoesNotExist:
return

# Compute stats all at once
Status.objects.filter(test_run=testrun).all().delete()
Expand Down

0 comments on commit ceed542

Please sign in to comment.