File: result_sink.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (287 lines) | stat: -rw-r--r-- 10,085 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import base64
import json
import logging
import os

import requests  # pylint: disable=import-error
from lib.results import result_types

HTML_SUMMARY_MAX = 4096

_HTML_SUMMARY_ARTIFACT = '<text-artifact artifact-id="HTML Summary" />'
_TEST_LOG_ARTIFACT = '<text-artifact artifact-id="Test Log" />'

# Maps result_types to the luci test-result.proto.
# https://godoc.org/go.chromium.org/luci/resultdb/proto/v1#TestStatus
RESULT_MAP = {
    result_types.UNKNOWN: 'ABORT',
    result_types.PASS: 'PASS',
    result_types.FAIL: 'FAIL',
    result_types.CRASH: 'CRASH',
    result_types.TIMEOUT: 'ABORT',
    result_types.SKIP: 'SKIP',
    result_types.NOTRUN: 'SKIP',
}


def TryInitClient():
  """Tries to initialize a result_sink_client object.

  Assumes that rdb stream is already running.

  Returns:
    A ResultSinkClient for the result_sink server else returns None.
  """
  try:
    with open(os.environ['LUCI_CONTEXT']) as f:
      sink = json.load(f)['result_sink']
      return ResultSinkClient(sink)
  except KeyError:
    return None


class ResultSinkClient(object):
  """A class to store the sink's post configurations and make post requests.

  This assumes that the rdb stream has been called already and that the
  server is listening.
  """

  def __init__(self, context):
    base_url = 'http://%s/prpc/luci.resultsink.v1.Sink' % context['address']
    self.test_results_url = base_url + '/ReportTestResults'
    self.report_artifacts_url = base_url + '/ReportInvocationLevelArtifacts'
    self.update_invocation_url = base_url + '/UpdateInvocation'

    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'ResultSink %s' % context['auth_token'],
    }
    self.session = requests.Session()
    self.session.headers.update(headers)

  def __enter__(self):
    return self

  def __exit__(self, exc_type, exc_value, traceback):
    self.close()

  def close(self):
    """Closes the session backing the sink."""
    self.session.close()

  def Post(self,
           test_id,
           status,
           duration,
           test_log,
           test_file,
           test_id_structured=None,
           variant=None,
           artifacts=None,
           failure_reason=None,
           html_artifact=None,
           tags=None):
    """Uploads the test result to the ResultSink server.

    This assumes that the rdb stream has been called already and that
    server is ready listening.

    Args:
      test_id: A string representing the test's name.
      status: A string representing if the test passed, failed, etc...
      duration: An int representing time in ms.
      test_log: A string representing the test's output.
      test_file: A string representing the file location of the test.
      test_id_structured: A dictionary containing structured test id fields.
      variant: An optional dict of variant key value pairs as the
          additional variant sent from test runners, which can override
          or add to the variants passed to `rdb stream` command.
      artifacts: An optional dict of artifacts to attach to the test.
      failure_reason: An optional string with the reason why the test failed.
          Should be None if the test did not fail.
      html_artifact: An optional html-formatted string to prepend to the test's
          log. Useful to encode click-able URL links in the test log, since that
          won't be formatted in the test_log.
      tags: An optional list of tuple of key name and value to prepend to the
          test's tags.

    Returns:
      N/A
    """
    assert status in RESULT_MAP
    expected = status in (result_types.PASS, result_types.SKIP)
    result_db_status = RESULT_MAP[status]

    tr = {
        'expected':
        expected,
        'status':
        result_db_status,
        'tags': [
            {
                'key': 'test_name',
                'value': test_id,
            },
            {
                # Status before getting mapped to result_db statuses.
                'key': 'raw_status',
                'value': status,
            }
        ],
        'testId':
        test_id,
        'testMetadata': {
            'name': test_id,
        }
    }

    if test_id_structured:
      tr['testIdStructured'] = test_id_structured

    if tags:
      tr['tags'].extend({
          'key': key_name,
          'value': value
      } for (key_name, value) in tags)

    if variant:
      tr['variant'] = {'def': variant}

    artifacts = artifacts or {}
    tr['summaryHtml'] = html_artifact if html_artifact else ''

    # If over max supported length of html summary, replace with artifact
    # upload.
    if (test_log
        and len(tr['summaryHtml']) + len(_TEST_LOG_ARTIFACT) > HTML_SUMMARY_MAX
        or len(tr['summaryHtml']) > HTML_SUMMARY_MAX):
      b64_summary = base64.b64encode(tr['summaryHtml'].encode()).decode()
      artifacts.update({'HTML Summary': {'contents': b64_summary}})
      tr['summaryHtml'] = _HTML_SUMMARY_ARTIFACT

    if test_log:
      # Upload the original log without any modifications.
      b64_log = base64.b64encode(test_log.encode()).decode()
      artifacts.update({'Test Log': {'contents': b64_log}})
      tr['summaryHtml'] += _TEST_LOG_ARTIFACT

    if artifacts:
      tr['artifacts'] = artifacts
    if failure_reason:
      tr['failureReason'] = {
          'primaryErrorMessage': _TruncateToUTF8Bytes(failure_reason, 1024)
      }

    if duration is not None:
      # Duration must be formatted to avoid scientific notation in case
      # number is too small or too large. Result_db takes seconds, not ms.
      # Need to use float() otherwise it does substitution first then divides.
      tr['duration'] = '%.9fs' % float(duration / 1000.0)

    if test_file and str(test_file).startswith('//'):
      tr['testMetadata']['location'] = {
          'file_name': test_file,
          'repo': 'https://chromium.googlesource.com/chromium/src',
      }

    res = self.session.post(url=self.test_results_url,
                            data=json.dumps({'testResults': [tr]}))
    res.raise_for_status()

  def ReportInvocationLevelArtifacts(self, artifacts):
    """Uploads invocation-level artifacts to the ResultSink server.

    This is for artifacts that don't apply to a single test but to the test
    invocation as a whole (eg: system logs).

    Args:
      artifacts: A dict of artifacts to attach to the invocation.
    """
    req = {'artifacts': artifacts}
    res = self.session.post(url=self.report_artifacts_url, data=json.dumps(req))
    res.raise_for_status()

  def UpdateInvocation(self, invocation, update_mask):
    """Update the invocation to the ResultSink server.

    Details can be found in the proto luci.resultsink.v1.UpdateInvocationRequest

    Args:
      invocation: a dict representation of luci.resultsink.v1.Invocation proto
      update_mask: a string representation of google.protobuf.FieldMask proto
    """
    req = {
        'invocation': invocation,
        'updateMask': update_mask,
    }
    res = self.session.post(url=self.update_invocation_url,
                            data=json.dumps(req))
    res.raise_for_status()

  def UpdateInvocationExtendedProperties(self, extended_properties, keys=None):
    """Update the extended_properties field of an invocation.

    Details can be found in the "extended_properties" field of the proto
    luci.resultdb.v1.Invocation.

    Args:
      extended_properties: a dict containing the content of extended_properties.
        The value in the dict shall be a dict containing a "@type" key
        representing the data schema, and corresponding data.
      keys: (Optional) a list of keys in extended_properties to add, replace,
        or remove. If a key exists in "keys", but not in "extended_properties",
        this is considered as deleting the key from the resultdb record side
        If None, the keys in "extended_properties" dict will be used.
    """
    # Sink server by default decodes payload with protojson, i.e. codecJSONV2
    # in https://source.chromium.org/search?q=f:server.go%20func:requestCodec
    # which requires loweCamelCase names in the json request.
    # For the value for update mask, see "JSON Encoding of Field Masks" in
    # https://protobuf.dev/reference/protobuf/google.protobuf/#field-masks
    invocation = {'extendedProperties': extended_properties}
    if not keys:
      keys = extended_properties.keys()
    mask_paths = ['extendedProperties.%s' % _ToCamelCase(key) for key in keys]
    update_mask = ','.join(mask_paths)
    self.UpdateInvocation(invocation, update_mask)


def _ToCamelCase(s):
  """Converts the string s from snake_case to lowerCamelCase."""

  elems = s.split('_')
  return elems[0] + ''.join(elem.capitalize() for elem in elems[1:])


def _TruncateToUTF8Bytes(s, length):
  """ Truncates a string to a given number of bytes when encoded as UTF-8.

  Ensures the given string does not take more than length bytes when encoded
  as UTF-8. Adds trailing ellipsis (...) if truncation occurred. A truncated
  string may end up encoding to a length slightly shorter than length because
  only whole Unicode codepoints are dropped.

  Args:
    s: The string to truncate.
    length: the length (in bytes) to truncate to.
  """
  try:
    encoded = s.encode('utf-8')
  # When encode throws UnicodeDecodeError in py2, it usually means the str is
  # already encoded and has non-ascii chars. So skip re-encoding it.
  except UnicodeDecodeError:
    encoded = s
  if len(encoded) > length:
    # Truncate, leaving space for trailing ellipsis (...).
    encoded = encoded[:length - 3]
    # Truncating the string encoded as UTF-8 may have left the final codepoint
    # only partially present. Pass 'ignore' to acknowledge and ensure this is
    # dropped.
    return encoded.decode('utf-8', 'ignore') + "..."
  return s