Эх сурвалжийг харах

Add snippets to presubmit failures in resultdb

Fixed: 1174446
Change-Id: Ie57818846704ed24fe1af49a70e24ad7a118d44c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3965229
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Erik Staab <estaab@chromium.org>
Erik Staab 2 жил өмнө
parent
commit
9f38b63b4e

+ 10 - 1
presubmit_support.py

@@ -1666,10 +1666,19 @@ class PresubmitExecuter(object):
       sys.stdout.write('%6.1fs to run %s from %s.\n' %
                        (elapsed_time, function_name, presubmit_path))
     if sink:
+      failure_reason = None
       status = rdb_wrapper.STATUS_PASS
       if any(r.fatal for r in result):
         status = rdb_wrapper.STATUS_FAIL
-      sink.report(function_name, status, elapsed_time)
+        failure_reasons = []
+        for r in result:
+          fields = r.json_format()
+          message = fields['message']
+          items = '\n'.join('  %s' % item for item in fields['items'])
+          failure_reasons.append('\n'.join([message, items]))
+        if failure_reasons:
+          failure_reason = '\n'.join(failure_reasons)
+      sink.report(function_name, status, elapsed_time, failure_reason)
 
     return result
 

+ 16 - 1
rdb_wrapper.py

@@ -17,19 +17,28 @@ STATUS_ABORT = 'ABORT'
 STATUS_SKIP = 'SKIP'
 
 
+# ResultDB limits failure reasons to 1024 characters.
+_FAILURE_REASON_LENGTH_LIMIT = 1024
+
+
+# Message to use at the end of a truncated failure reason.
+_FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.'
+
+
 class ResultSink(object):
   def __init__(self, session, url, prefix):
     self._session = session
     self._url = url
     self._prefix = prefix
 
-  def report(self, function_name, status, elapsed_time):
+  def report(self, function_name, status, elapsed_time, failure_reason=None):
     """Reports the result and elapsed time of a presubmit function call.
 
     Args:
       function_name (str): The name of the presubmit function
       status: the status to report the function call with
       elapsed_time: the time taken to invoke the presubmit function
+      failure_reason (str or None): if set, the failure reason
     """
     tr = {
         'testId': self._prefix + function_name,
@@ -37,6 +46,12 @@ class ResultSink(object):
         'expected': status == STATUS_PASS,
         'duration': '{:.9f}s'.format(elapsed_time)
     }
+    if failure_reason:
+      if len(failure_reason) > _FAILURE_REASON_LENGTH_LIMIT:
+        failure_reason = failure_reason[
+            :-len(_FAILURE_REASON_TRUNCATE_TEXT) - 1]
+        failure_reason += _FAILURE_REASON_TRUNCATE_TEXT
+      tr['failureReason'] = {'primaryErrorMessage': failure_reason}
     self._session.post(self._url, json={'testResults': [tr]})
 
 

+ 2 - 2
tests/presubmit_unittest.py

@@ -568,7 +568,7 @@ class PresubmitUnittest(PresubmitTestsBase):
         'def CheckChangeOnCommit(input_api, output_api):\n'
         '  return [output_api.PresubmitResult("test")]\n', fake_presubmit)
     sink.report.assert_called_with('CheckChangeOnCommit',
-                                   rdb_wrapper.STATUS_PASS, 0)
+                                   rdb_wrapper.STATUS_PASS, 0, None)
 
     # STATUS_FAIL on fatal error
     sink.reset_mock()
@@ -577,7 +577,7 @@ class PresubmitUnittest(PresubmitTestsBase):
         'def CheckChangeOnCommit(input_api, output_api):\n'
         '  return [output_api.PresubmitError("error")]\n', fake_presubmit)
     sink.report.assert_called_with('CheckChangeOnCommit',
-                                   rdb_wrapper.STATUS_FAIL, 0)
+                                   rdb_wrapper.STATUS_FAIL, 0, "error\n")
 
   def testExecPresubmitScriptTemporaryFilesRemoval(self):
     tempfile.NamedTemporaryFile.side_effect = [

+ 39 - 0
tests/rdb_wrapper_test.py

@@ -96,6 +96,45 @@ class TestResultSink(unittest.TestCase):
         json={'testResults': [expected]},
     )
 
+  def test_report_failure_reason(self):
+    session = mock.MagicMock()
+    sink = rdb_wrapper.ResultSink(session, 'http://host', 'test_id_prefix/')
+    sink.report("function_foo", rdb_wrapper.STATUS_PASS, 123, 'Bad CL.')
+    expected = {
+        'testId': 'test_id_prefix/function_foo',
+        'status': rdb_wrapper.STATUS_PASS,
+        'expected': True,
+        'duration': '123.000000000s',
+        'failureReason': {
+            'primaryErrorMessage': 'Bad CL.',
+        },
+    }
+    session.post.assert_called_once_with(
+        'http://host',
+        json={'testResults': [expected]},
+    )
+
+  def test_report_failure_reason_truncated(self):
+    session = mock.MagicMock()
+    sink = rdb_wrapper.ResultSink(session, 'http://host', 'test_id_prefix/')
+    sink.report("function_foo", rdb_wrapper.STATUS_PASS, 123, 'X' * 1025)
+    trunc_text = rdb_wrapper._FAILURE_REASON_TRUNCATE_TEXT
+    limit = rdb_wrapper._FAILURE_REASON_LENGTH_LIMIT
+    expected_truncated_error = 'X' * (limit - len(trunc_text)) + trunc_text
+    expected = {
+        'testId': 'test_id_prefix/function_foo',
+        'status': rdb_wrapper.STATUS_PASS,
+        'expected': True,
+        'duration': '123.000000000s',
+        'failureReason': {
+            'primaryErrorMessage': expected_truncated_error,
+        },
+    }
+    session.post.assert_called_once_with(
+        'http://host',
+        json={'testResults': [expected]},
+    )
+
 
 if __name__ == '__main__':
   logging.basicConfig(