rdb_wrapper.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env vpython3
  2. # Copyright (c) 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import contextlib
  6. import json
  7. import os
  8. import requests
  9. import time
  10. # Constants describing TestStatus for ResultDB
  11. STATUS_PASS = 'PASS'
  12. STATUS_FAIL = 'FAIL'
  13. STATUS_CRASH = 'CRASH'
  14. STATUS_ABORT = 'ABORT'
  15. STATUS_SKIP = 'SKIP'
  16. # ResultDB limits failure reasons to 1024 characters.
  17. _FAILURE_REASON_LENGTH_LIMIT = 1024
  18. # Message to use at the end of a truncated failure reason.
  19. _FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.'
  20. class ResultSink(object):
  21. def __init__(self, session, url, prefix):
  22. self._session = session
  23. self._url = url
  24. self._prefix = prefix
  25. def report(self, function_name, status, elapsed_time, failure_reason=None):
  26. """Reports the result and elapsed time of a presubmit function call.
  27. Args:
  28. function_name (str): The name of the presubmit function
  29. status: the status to report the function call with
  30. elapsed_time: the time taken to invoke the presubmit function
  31. failure_reason (str or None): if set, the failure reason
  32. """
  33. tr = {
  34. 'testId': self._prefix + function_name,
  35. 'status': status,
  36. 'expected': status == STATUS_PASS,
  37. 'duration': '{:.9f}s'.format(elapsed_time)
  38. }
  39. if failure_reason:
  40. if len(failure_reason) > _FAILURE_REASON_LENGTH_LIMIT:
  41. failure_reason = failure_reason[
  42. :-len(_FAILURE_REASON_TRUNCATE_TEXT) - 1]
  43. failure_reason += _FAILURE_REASON_TRUNCATE_TEXT
  44. tr['failureReason'] = {'primaryErrorMessage': failure_reason}
  45. self._session.post(self._url, json={'testResults': [tr]})
  46. @contextlib.contextmanager
  47. def client(prefix):
  48. """Returns a client for ResultSink.
  49. This is a context manager that returns a client for ResultSink,
  50. if LUCI_CONTEXT with a section of result_sink is present. When the context
  51. is closed, all the connetions to the SinkServer are closed.
  52. Args:
  53. prefix: A prefix to be added to the test ID of reported function names.
  54. The format for this is
  55. presubmit:gerrit_host/folder/to/repo:path/to/file/
  56. for example,
  57. presubmit:chromium-review.googlesource.com/chromium/src/:services/viz/
  58. Returns:
  59. An instance of ResultSink() if the luci context is present. None, otherwise.
  60. """
  61. luci_ctx = os.environ.get('LUCI_CONTEXT')
  62. if not luci_ctx:
  63. yield None
  64. return
  65. sink_ctx = None
  66. with open(luci_ctx) as f:
  67. sink_ctx = json.load(f).get('result_sink')
  68. if not sink_ctx:
  69. yield None
  70. return
  71. url = 'http://{0}/prpc/luci.resultsink.v1.Sink/ReportTestResults'.format(
  72. sink_ctx['address'])
  73. with requests.Session() as s:
  74. s.headers = {
  75. 'Content-Type': 'application/json',
  76. 'Accept': 'application/json',
  77. 'Authorization': 'ResultSink {0}'.format(sink_ctx['auth_token'])
  78. }
  79. yield ResultSink(s, url, prefix)