rdb_wrapper.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # Constants describing TestStatus for ResultDB
  10. STATUS_PASS = 'PASS'
  11. STATUS_FAIL = 'FAIL'
  12. STATUS_CRASH = 'CRASH'
  13. STATUS_ABORT = 'ABORT'
  14. STATUS_SKIP = 'SKIP'
  15. # ResultDB limits failure reasons to 1024 characters.
  16. _FAILURE_REASON_LENGTH_LIMIT = 1024
  17. # Message to use at the end of a truncated failure reason.
  18. _FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.'
  19. class ResultSink(object):
  20. def __init__(self, session, url, prefix):
  21. self._session = session
  22. self._url = url
  23. self._prefix = prefix
  24. def report(self, function_name, status, elapsed_time, failure_reason=None):
  25. """Reports the result and elapsed time of a presubmit function call.
  26. Args:
  27. function_name (str): The name of the presubmit function
  28. status: the status to report the function call with
  29. elapsed_time: the time taken to invoke the presubmit function
  30. failure_reason (str or None): if set, the failure reason
  31. """
  32. tr = {
  33. 'testId': self._prefix + function_name,
  34. 'status': status,
  35. 'expected': status == STATUS_PASS,
  36. 'duration': '{:.9f}s'.format(elapsed_time)
  37. }
  38. if failure_reason:
  39. if len(failure_reason) > _FAILURE_REASON_LENGTH_LIMIT:
  40. failure_reason = failure_reason[:-len(
  41. _FAILURE_REASON_TRUNCATE_TEXT) - 1]
  42. failure_reason += _FAILURE_REASON_TRUNCATE_TEXT
  43. tr['failureReason'] = {'primaryErrorMessage': failure_reason}
  44. self._session.post(self._url, json={'testResults': [tr]})
  45. @contextlib.contextmanager
  46. def client(prefix):
  47. """Returns a client for ResultSink.
  48. This is a context manager that returns a client for ResultSink,
  49. if LUCI_CONTEXT with a section of result_sink is present. When the context
  50. is closed, all the connetions to the SinkServer are closed.
  51. Args:
  52. prefix: A prefix to be added to the test ID of reported function names.
  53. The format for this is
  54. presubmit:gerrit_host/folder/to/repo:path/to/file/
  55. for example,
  56. presubmit:chromium-review.googlesource.com/chromium/src/:services/viz/ # pylint: disable=line-too-long
  57. Returns:
  58. An instance of ResultSink() if the luci context is present. None,
  59. 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)