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 | |
| 20 | class ResultSink(object): |
| 21 | def __init__(self, session, url, prefix): |
| 22 | self._session = session |
| 23 | self._url = url |
| 24 | self._prefix = prefix |
| 25 | |
| 26 | def report(self, function_name, status, elapsed_time): |
| 27 | """Reports the result and elapsed time of a presubmit function call. |
| 28 | |
| 29 | Args: |
| 30 | function_name (str): The name of the presubmit function |
| 31 | status: the status to report the function call with |
| 32 | elapsed_time: the time taken to invoke the presubmit function |
| 33 | """ |
| 34 | tr = { |
| 35 | 'testId': self._prefix + function_name, |
| 36 | 'status': status, |
| 37 | 'expected': status == STATUS_PASS, |
| 38 | 'duration': '{:.9f}s'.format(elapsed_time) |
| 39 | } |
| 40 | self._session.post(self._url, json={'testResults': [tr]}) |
| 41 | |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 42 | |
| 43 | @contextlib.contextmanager |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 44 | def client(prefix): |
| 45 | """Returns a client for ResultSink. |
| 46 | |
| 47 | This is a context manager that returns a client for ResultSink, |
| 48 | if LUCI_CONTEXT with a section of result_sink is present. When the context |
| 49 | is closed, all the connetions to the SinkServer are closed. |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 50 | |
| 51 | Args: |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 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/ |
Saagar Sanghavi | 531d992 | 2020-08-10 20:14:01 +0000 | [diff] [blame] | 55 | for example, |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 56 | presubmit:chromium-review.googlesource.com/chromium/src/:services/viz/ |
| 57 | Returns: |
| 58 | An instance of ResultSink() if the luci context is present. None, otherwise. |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 59 | """ |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 60 | luci_ctx = os.environ.get('LUCI_CONTEXT') |
| 61 | if not luci_ctx: |
| 62 | yield None |
| 63 | return |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 64 | |
Scott Lee | cc2fe9b | 2020-11-19 19:38:06 +0000 | [diff] [blame] | 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 | |
| 72 | url = 'http://{0}/prpc/luci.resultsink.v1.Sink/ReportTestResults'.format( |
| 73 | sink_ctx['address']) |
| 74 | with requests.Session() as s: |
| 75 | s.headers = { |
| 76 | 'Content-Type': 'application/json', |
| 77 | 'Accept': 'application/json', |
| 78 | 'Authorization': 'ResultSink {0}'.format(sink_ctx['auth_token']) |
| 79 | } |
| 80 | yield ResultSink(s, url, prefix) |