blob: cd41fcf2e344d45a2c5daa3eaf40d9f725d4c962 [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
10import time
11
12# Constants describing TestStatus for ResultDB
13STATUS_PASS = 'PASS'
14STATUS_FAIL = 'FAIL'
15STATUS_CRASH = 'CRASH'
16STATUS_ABORT = 'ABORT'
17STATUS_SKIP = 'SKIP'
18
Scott Leecc2fe9b2020-11-19 19:38:06 +000019
Erik Staab9f38b632022-10-31 14:05:24 +000020# 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 Leecc2fe9b2020-11-19 19:38:06 +000028class ResultSink(object):
29 def __init__(self, session, url, prefix):
30 self._session = session
31 self._url = url
32 self._prefix = prefix
33
Erik Staab9f38b632022-10-31 14:05:24 +000034 def report(self, function_name, status, elapsed_time, failure_reason=None):
Scott Leecc2fe9b2020-11-19 19:38:06 +000035 """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 Staab9f38b632022-10-31 14:05:24 +000041 failure_reason (str or None): if set, the failure reason
Scott Leecc2fe9b2020-11-19 19:38:06 +000042 """
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 Staab9f38b632022-10-31 14:05:24 +000049 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 Leecc2fe9b2020-11-19 19:38:06 +000055 self._session.post(self._url, json={'testResults': [tr]})
56
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000057
58@contextlib.contextmanager
Scott Leecc2fe9b2020-11-19 19:38:06 +000059def 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 Sanghavi9949ab72020-07-20 20:56:40 +000065
66 Args:
Scott Leecc2fe9b2020-11-19 19:38:06 +000067 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 Sanghavi531d9922020-08-10 20:14:01 +000070 for example,
Scott Leecc2fe9b2020-11-19 19:38:06 +000071 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 Sanghavi9949ab72020-07-20 20:56:40 +000074 """
Scott Leecc2fe9b2020-11-19 19:38:06 +000075 luci_ctx = os.environ.get('LUCI_CONTEXT')
76 if not luci_ctx:
77 yield None
78 return
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000079
Scott Leecc2fe9b2020-11-19 19:38:06 +000080 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)