blob: 2e0843a92caf07e53f4a9aca8af291176c0fba02 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env vpython3
Saagar Sanghavi9949ab72020-07-20 20:56:40 +00002# 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
6import contextlib
7import json
8import os
9import requests
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000010
11# Constants describing TestStatus for ResultDB
12STATUS_PASS = 'PASS'
13STATUS_FAIL = 'FAIL'
14STATUS_CRASH = 'CRASH'
15STATUS_ABORT = 'ABORT'
16STATUS_SKIP = 'SKIP'
17
Erik Staab9f38b632022-10-31 14:05:24 +000018# ResultDB limits failure reasons to 1024 characters.
19_FAILURE_REASON_LENGTH_LIMIT = 1024
20
Erik Staab9f38b632022-10-31 14:05:24 +000021# Message to use at the end of a truncated failure reason.
22_FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.'
23
24
Scott Leecc2fe9b2020-11-19 19:38:06 +000025class ResultSink(object):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000026 def __init__(self, session, url, prefix):
27 self._session = session
28 self._url = url
29 self._prefix = prefix
Scott Leecc2fe9b2020-11-19 19:38:06 +000030
Mike Frysinger124bb8e2023-09-06 05:48:55 +000031 def report(self, function_name, status, elapsed_time, failure_reason=None):
32 """Reports the result and elapsed time of a presubmit function call.
Scott Leecc2fe9b2020-11-19 19:38:06 +000033
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 Staab9f38b632022-10-31 14:05:24 +000038 failure_reason (str or None): if set, the failure reason
Scott Leecc2fe9b2020-11-19 19:38:06 +000039 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +000040 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 Leecc2fe9b2020-11-19 19:38:06 +000053
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000054
55@contextlib.contextmanager
Scott Leecc2fe9b2020-11-19 19:38:06 +000056def client(prefix):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000057 """Returns a client for ResultSink.
Scott Leecc2fe9b2020-11-19 19:38:06 +000058
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 Sanghavi9949ab72020-07-20 20:56:40 +000062
63 Args:
Scott Leecc2fe9b2020-11-19 19:38:06 +000064 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 Sanghavi531d9922020-08-10 20:14:01 +000067 for example,
Scott Leecc2fe9b2020-11-19 19:38:06 +000068 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 Sanghavi9949ab72020-07-20 20:56:40 +000071 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +000072 luci_ctx = os.environ.get('LUCI_CONTEXT')
73 if not luci_ctx:
74 yield None
75 return
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000076
Mike Frysinger124bb8e2023-09-06 05:48:55 +000077 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 Leecc2fe9b2020-11-19 19:38:06 +000083
Mike Frysinger124bb8e2023-09-06 05:48:55 +000084 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)