Skip to content

Commit c840c34

Browse files
sleauxMongoDB Bot
authored andcommitted
SERVER-99725 Extend a resmoke test's log when its outcome is changed to a failure (#32022)
GitOrigin-RevId: 9570c53
1 parent 8fdcab0 commit c840c34

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

buildscripts/resmokelib/testing/job.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ def _execute_test(self, test: TestCase, hook_failure_flag: Optional[threading.Ev
262262
"%s marked as a failure because the fixture crashed during the test.",
263263
test.short_description(),
264264
)
265-
self.report.setFailure(test, return_code=2)
265+
self.report.setFailure(
266+
test, return_code=2, reason="the fixture crashed during the test"
267+
)
266268
# Always fail fast if the fixture fails.
267269
raise errors.StopExecution(
268270
"%s not running after %s" % (self.fixture, test.short_description())
@@ -397,10 +399,14 @@ def _run_hooks_after_tests(
397399
self.fixture.stop_balancer()
398400
except:
399401
self.logger.exception(
400-
"%s failed while stopping the balancer for end-test hooks",
402+
"%s failed while stopping the balancer for after-test hooks",
401403
test.short_description(),
402404
)
403-
self.report.setFailure(test, return_code=2)
405+
self.report.setFailure(
406+
test,
407+
return_code=2,
408+
reason="the balancer failed to stop before running after-test hooks",
409+
)
404410
if self.archival:
405411
result = TestResult(test=test, hook=None, success=False)
406412
self.archival.archive(self.logger, result, self.manager)
@@ -418,14 +424,18 @@ def _run_hooks_after_tests(
418424
self.logger.exception(
419425
"%s marked as a failure by a hook's after_test.", test.short_description()
420426
)
421-
self.report.setFailure(test, return_code=2)
427+
self.report.setFailure(
428+
test, return_code=2, reason=f"The hook {hook.REGISTERED_NAME} failed."
429+
)
422430
raise errors.StopExecution("A hook's after_test failed")
423431

424432
except errors.TestFailure:
425433
self.logger.exception(
426434
"%s marked as a failure by a hook's after_test.", test.short_description()
427435
)
428-
self.report.setFailure(test, return_code=1)
436+
self.report.setFailure(
437+
test, return_code=1, reason=f"The hook {hook.REGISTERED_NAME} failed."
438+
)
429439
if self.suite_options.fail_fast:
430440
raise errors.StopExecution("A hook's after_test failed")
431441

@@ -442,7 +452,11 @@ def _run_hooks_after_tests(
442452
"%s failed while re-starting the balancer after end-test hooks",
443453
test.short_description(),
444454
)
445-
self.report.setFailure(test, return_code=2)
455+
self.report.setFailure(
456+
test,
457+
return_code=2,
458+
reason="the balancer failed to restart after running after test hooks",
459+
)
446460
if self.archival:
447461
result = TestResult(test=test, hook=None, success=False)
448462
self.archival.archive(self.logger, result, self.manager)

buildscripts/resmokelib/testing/report.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def setError(self, test, err):
218218
if test_info.end_time is None:
219219
raise ValueError("stopTest was not called on %s" % (test.basename()))
220220

221+
changed = test_info.status != "error"
221222
# We don't distinguish between test failures and Python errors in Evergreen.
222223
test_info.status = "error"
223224
test_info.evergreen_status = "fail"
@@ -230,6 +231,9 @@ def setError(self, test, err):
230231
self.num_errored = len(self.get_errored())
231232
self.num_interrupted = len(self.get_interrupted())
232233

234+
if changed:
235+
self._log_outcome_change(test, "error")
236+
233237
def addFailure(self, test, err):
234238
"""Call when a failureException was raised during the execution of 'test'."""
235239

@@ -243,14 +247,15 @@ def addFailure(self, test, err):
243247
test_info.evergreen_status = "fail"
244248
test_info.return_code = test.return_code
245249

246-
def setFailure(self, test, return_code=1):
250+
def setFailure(self, test, return_code=1, reason=""):
247251
"""Change the outcome of an existing test to a failure."""
248252

249253
with self._lock:
250254
test_info = self.find_test_info(test)
251255
if test_info.end_time is None:
252256
raise ValueError("stopTest was not called on %s" % (test.basename()))
253257

258+
changed = test_info.status != "fail"
254259
test_info.status = "fail"
255260
test_info.evergreen_status = "fail"
256261
test_info.return_code = return_code
@@ -261,6 +266,9 @@ def setFailure(self, test, return_code=1):
261266
self.num_errored = len(self.get_errored())
262267
self.num_interrupted = len(self.get_interrupted())
263268

269+
if changed:
270+
self._log_outcome_change(test, "fail", reason)
271+
264272
def addSuccess(self, test):
265273
"""Call when 'test' executed successfully."""
266274

@@ -397,6 +405,24 @@ def find_test_info(self, test):
397405

398406
raise ValueError("Details for %s not found in the report" % (test.basename()))
399407

408+
def _log_outcome_change(self, test, outcome, reason=""):
409+
# Recreate the test logger for this test in order to append to the existing log.
410+
logger = logging.loggers.new_test_logger(
411+
test.short_name(),
412+
test.basename(),
413+
None,
414+
test.logger,
415+
self.job_num,
416+
test.id(),
417+
self.job_logger,
418+
)
419+
logger.info(
420+
f'Sometime after completion of {test.short_description()}, the test outcome was changed to "{outcome}"'
421+
+ (f" because: {reason}" if reason else "."),
422+
)
423+
for handler in logger.handlers:
424+
logging.flush.close_later(handler)
425+
400426

401427
class TestInfo(object):
402428
"""Holder for the test status and timing information."""

0 commit comments

Comments
 (0)