blob: 35ce8d5d342a95a3c1503baac9e2dd0055d38f49 [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
20class 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 Sanghavi9949ab72020-07-20 20:56:40 +000042
43@contextlib.contextmanager
Scott Leecc2fe9b2020-11-19 19:38:06 +000044def 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 Sanghavi9949ab72020-07-20 20:56:40 +000050
51 Args:
Scott Leecc2fe9b2020-11-19 19:38:06 +000052 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 Sanghavi531d9922020-08-10 20:14:01 +000055 for example,
Scott Leecc2fe9b2020-11-19 19:38:06 +000056 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 Sanghavi9949ab72020-07-20 20:56:40 +000059 """
Scott Leecc2fe9b2020-11-19 19:38:06 +000060 luci_ctx = os.environ.get('LUCI_CONTEXT')
61 if not luci_ctx:
62 yield None
63 return
Saagar Sanghavi9949ab72020-07-20 20:56:40 +000064
Scott Leecc2fe9b2020-11-19 19:38:06 +000065 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)