Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 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 | |
| 6 | import contextlib |
| 7 | import json |
| 8 | import os |
| 9 | import requests |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 10 | |
| 11 | # Constants describing TestStatus for ResultDB |
| 12 | STATUS_PASS = 'PASS' |
| 13 | STATUS_FAIL = 'FAIL' |
| 14 | STATUS_CRASH = 'CRASH' |
| 15 | STATUS_ABORT = 'ABORT' |
| 16 | STATUS_SKIP = 'SKIP' |
| 17 | |
Erik Staab | 9f38b63 | 2022-10-31 14:05:24 +0000 | [diff] [blame] | 18 | # ResultDB limits failure reasons to 1024 characters. |
| 19 | _FAILURE_REASON_LENGTH_LIMIT = 1024 |
| 20 | |
Erik Staab | 9f38b63 | 2022-10-31 14:05:24 +0000 | [diff] [blame] | 21 | # Message to use at the end of a truncated failure reason. |
| 22 | _FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.' |
| 23 | |
| 24 | |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 25 | class ResultSink(object): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 26 | def __init__(self, session, url, prefix): |
| 27 | self._session = session |
| 28 | self._url = url |
| 29 | self._prefix = prefix |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 30 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 31 | def report(self, function_name, status, elapsed_time, failure_reason=None): |
| 32 | """Reports the result and elapsed time of a presubmit function call. |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 33 | |
| 34 | Args: |
| 35 | function_name (str): The name of the presubmit function |
| 36 | status: the status to report the function call with |
| 37 | elapsed_time: the time taken to invoke the presubmit function |
Erik Staab | 9f38b63 | 2022-10-31 14:05:24 +0000 | [diff] [blame] | 38 | failure_reason (str or None): if set, the failure reason |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 39 | """ |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 40 | tr = { |
| 41 | 'testId': self._prefix + function_name, |
| 42 | 'status': status, |
| 43 | 'expected': status == STATUS_PASS, |
| 44 | 'duration': '{:.9f}s'.format(elapsed_time) |
| 45 | } |
| 46 | if failure_reason: |
| 47 | if len(failure_reason) > _FAILURE_REASON_LENGTH_LIMIT: |
| 48 | failure_reason = failure_reason[:-len( |
| 49 | _FAILURE_REASON_TRUNCATE_TEXT) - 1] |
| 50 | failure_reason += _FAILURE_REASON_TRUNCATE_TEXT |
| 51 | tr['failureReason'] = {'primaryErrorMessage': failure_reason} |
| 52 | self._session.post(self._url, json={'testResults': [tr]}) |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 53 | |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 54 | |
| 55 | @contextlib.contextmanager |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 56 | def client(prefix): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 57 | """Returns a client for ResultSink. |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 58 | |
| 59 | This is a context manager that returns a client for ResultSink, |
| 60 | if LUCI_CONTEXT with a section of result_sink is present. When the context |
| 61 | is closed, all the connetions to the SinkServer are closed. |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 62 | |
| 63 | Args: |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 64 | prefix: A prefix to be added to the test ID of reported function names. |
| 65 | The format for this is |
| 66 | presubmit:gerrit_host/folder/to/repo:path/to/file/ |
Saagar Sanghavi | 531d992 | 2020-08-10 20:14:01 +0000 | [diff] [blame] | 67 | for example, |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 68 | presubmit:chromium-review.googlesource.com/chromium/src/:services/viz/ |
| 69 | Returns: |
| 70 | An instance of ResultSink() if the luci context is present. None, otherwise. |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 71 | """ |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 72 | luci_ctx = os.environ.get('LUCI_CONTEXT') |
| 73 | if not luci_ctx: |
| 74 | yield None |
| 75 | return |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 76 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 77 | sink_ctx = None |
| 78 | with open(luci_ctx) as f: |
| 79 | sink_ctx = json.load(f).get('result_sink') |
| 80 | if not sink_ctx: |
| 81 | yield None |
| 82 | return |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 83 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 84 | url = 'http://{0}/prpc/luci.resultsink.v1.Sink/ReportTestResults'.format( |
| 85 | sink_ctx['address']) |
| 86 | with requests.Session() as s: |
| 87 | s.headers = { |
| 88 | 'Content-Type': 'application/json', |
| 89 | 'Accept': 'application/json', |
| 90 | 'Authorization': 'ResultSink {0}'.format(sink_ctx['auth_token']) |
| 91 | } |
| 92 | yield ResultSink(s, url, prefix) |