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