blob: b6021e627ac0ee0a9172b38ccc93c75725485d26 [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
Scott Leecc2fe9b2020-11-19 19:38:06 +000018
Erik Staab9f38b632022-10-31 14:05:24 +000019# ResultDB limits failure reasons to 1024 characters.
20_FAILURE_REASON_LENGTH_LIMIT = 1024
21
22
23# Message to use at the end of a truncated failure reason.
24_FAILURE_REASON_TRUNCATE_TEXT = '\n...\nFailure reason was truncated.'
25
26
Scott Leecc2fe9b2020-11-19 19:38:06 +000027class ResultSink(object):
28 def __init__(self, session, url, prefix):
29 self._session = session
30 self._url = url
31 self._prefix = prefix
32
Erik Staab9f38b632022-10-31 14:05:24 +000033 def report(self, function_name, status, elapsed_time, failure_reason=None):
Scott Leecc2fe9b2020-11-19 19:38:06 +000034 """Reports the result and elapsed time of a presubmit function call.
35
36 Args:
37 function_name (str): The name of the presubmit function
38 status: the status to report the function call with
39 elapsed_time: the time taken to invoke the presubmit function
Erik Staab9f38b632022-10-31 14:05:24 +000040 failure_reason (str or None): if set, the failure reason
Scott Leecc2fe9b2020-11-19 19:38:06 +000041 """
42 tr = {
43 'testId': self._prefix + function_name,
44 'status': status,
45 'expected': status == STATUS_PASS,
46 'duration': '{:.9f}s'.format(elapsed_time)
47 }
Erik Staab9f38b632022-10-31 14:05:24 +000048 if failure_reason:
49 if len(failure_reason) > _FAILURE_REASON_LENGTH_LIMIT:
50 failure_reason = failure_reason[
51 :-len(_FAILURE_REASON_TRUNCATE_TEXT) - 1]
52 failure_reason += _FAILURE_REASON_TRUNCATE_TEXT
53 tr['failureReason'] = {'primaryErrorMessage': failure_reason}
Scott Leecc2fe9b2020-11-19 19:38:06 +000054 self._session.post(self._url, json={'testResults': [tr]})
55
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000056
57@contextlib.contextmanager
Scott Leecc2fe9b2020-11-19 19:38:06 +000058def client(prefix):
59 """Returns a client for ResultSink.
60
61 This is a context manager that returns a client for ResultSink,
62 if LUCI_CONTEXT with a section of result_sink is present. When the context
63 is closed, all the connetions to the SinkServer are closed.
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000064
65 Args:
Scott Leecc2fe9b2020-11-19 19:38:06 +000066 prefix: A prefix to be added to the test ID of reported function names.
67 The format for this is
68 presubmit:gerrit_host/folder/to/repo:path/to/file/
Saagar Sanghavi531d9922020-08-10 20:14:01 +000069 for example,
Scott Leecc2fe9b2020-11-19 19:38:06 +000070 presubmit:chromium-review.googlesource.com/chromium/src/:services/viz/
71 Returns:
72 An instance of ResultSink() if the luci context is present. None, otherwise.
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000073 """
Scott Leecc2fe9b2020-11-19 19:38:06 +000074 luci_ctx = os.environ.get('LUCI_CONTEXT')
75 if not luci_ctx:
76 yield None
77 return
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000078
Scott Leecc2fe9b2020-11-19 19:38:06 +000079 sink_ctx = None
80 with open(luci_ctx) as f:
81 sink_ctx = json.load(f).get('result_sink')
82 if not sink_ctx:
83 yield None
84 return
85
86 url = 'http://{0}/prpc/luci.resultsink.v1.Sink/ReportTestResults'.format(
87 sink_ctx['address'])
88 with requests.Session() as s:
89 s.headers = {
90 'Content-Type': 'application/json',
91 'Accept': 'application/json',
92 'Authorization': 'ResultSink {0}'.format(sink_ctx['auth_token'])
93 }
94 yield ResultSink(s, url, prefix)