• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2
3# Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS.  All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11import os
12import sys
13import unittest
14
15from unittest.mock import MagicMock
16
17
18# This tests requires the webrtc_dashboard_upload target to be built before
19# running the tests.
20def _ConfigurePythonPath():
21  # We just yank the python scripts we require into the PYTHONPATH. You could
22  # also imagine a solution where we use for instance
23  # protobuf:py_proto_runtime to copy catapult and protobuf code to out/.
24  # This is the convention in Chromium and WebRTC python scripts. We do need
25  # to build histogram_pb2 however, so that's why we add out/ to sys.path
26  # below.
27  #
28  # It would be better if there was an equivalent to py_binary in GN, but
29  # there's not.
30  script_dir = os.path.dirname(os.path.realpath(__file__))
31  checkout_root = os.path.abspath(os.path.join(script_dir, os.pardir,
32                                               os.pardir))
33
34  sys.path.insert(
35      0, os.path.join(checkout_root, 'third_party', 'catapult', 'tracing'))
36  sys.path.insert(
37      0, os.path.join(checkout_root, 'third_party', 'protobuf', 'python'))
38
39  # The webrtc_dashboard_upload gn rule will build the protobuf stub for
40  # python, so put it in the path for this script before we attempt to import
41  # it.
42  histogram_proto_path = os.path.join(os.path.join('../../out/Default'),
43                                      'pyproto', 'tracing', 'tracing', 'proto')
44  sys.path.insert(0, histogram_proto_path)
45
46  # Fail early in case the proto hasn't been built.
47  from tracing.proto import histogram_proto
48  if not histogram_proto.HAS_PROTO:
49    raise ImportError('Could not find histogram_pb2. You need to build the '
50                      'webrtc_dashboard_upload target before invoking this '
51                      'script. Expected to find '
52                      'histogram_pb2.py in %s.' % histogram_proto_path)
53
54
55def _CreateHistogram(name='hist',
56                     master=None,
57                     bot=None,
58                     benchmark=None,
59                     benchmark_description=None,
60                     commit_position=None,
61                     samples=None):
62  hists = [catapult_uploader.histogram.Histogram(name, 'count')]
63  if samples:
64    for s in samples:
65      hists[0].AddSample(s)
66  histograms = catapult_uploader.histogram_set.HistogramSet(hists)
67  if master:
68    histograms.AddSharedDiagnosticToAllHistograms(
69        catapult_uploader.reserved_infos.MASTERS.name,
70        catapult_uploader.generic_set.GenericSet([master]))
71  if bot:
72    histograms.AddSharedDiagnosticToAllHistograms(
73        catapult_uploader.reserved_infos.BOTS.name,
74        catapult_uploader.generic_set.GenericSet([bot]))
75  if commit_position:
76    histograms.AddSharedDiagnosticToAllHistograms(
77        catapult_uploader.reserved_infos.CHROMIUM_COMMIT_POSITIONS.name,
78        catapult_uploader.generic_set.GenericSet([commit_position]))
79  if benchmark:
80    histograms.AddSharedDiagnosticToAllHistograms(
81        catapult_uploader.reserved_infos.BENCHMARKS.name,
82        catapult_uploader.generic_set.GenericSet([benchmark]))
83  if benchmark_description:
84    histograms.AddSharedDiagnosticToAllHistograms(
85        catapult_uploader.reserved_infos.BENCHMARK_DESCRIPTIONS.name,
86        catapult_uploader.generic_set.GenericSet([benchmark_description]))
87  return histograms
88
89
90class CatapultUploaderTest(unittest.TestCase):
91  def setUp(self):
92    mock = MagicMock(return_value=[200, None])
93    catapult_uploader.httplib2.Http.request = mock
94
95    self.histogram = _CreateHistogram(
96        master='master',
97        bot='bot',
98        benchmark='benchmark',
99        commit_position=123,
100        benchmark_description='Benchmark description.',
101        samples=[1, 2, 3])
102
103  def testSendHistogramsSet(self):
104    url = 'http://notlocalhost'
105    # pylint: disable=protected-access
106    response, content = catapult_uploader._SendHistogramSet(url, self.histogram)
107    self.assertEqual(response, 200)
108    self.assertEqual(content, None)
109
110  def testSendHistogramsSetLocalhost(self):
111    url = 'http://localhost'
112    # pylint: disable=protected-access
113    response, content = catapult_uploader._SendHistogramSet(url, self.histogram)
114    self.assertEqual(response, 200)
115    self.assertEqual(content, None)
116
117
118if (__name__) == '__main__':
119  _ConfigurePythonPath()
120  import catapult_uploader
121
122  unittest.main()
123