• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2020 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6#pylint: disable=protected-access
7
8import os
9import sys
10import unittest
11
12if sys.version_info[0] == 2:
13  import mock
14else:
15  import unittest.mock as mock
16
17from skia_gold_common import skia_gold_properties
18from skia_gold_common import unittest_utils
19
20createSkiaGoldArgs = unittest_utils.createSkiaGoldArgs
21
22
23class SkiaGoldPropertiesInitializationTest(unittest.TestCase):
24  """Tests that SkiaGoldProperties initializes (or doesn't) when expected."""
25
26  def verifySkiaGoldProperties(
27      self, instance: skia_gold_properties.SkiaGoldProperties,
28      expected: dict) -> None:
29    self.assertEqual(instance._local_pixel_tests,
30                     expected.get('local_pixel_tests'))
31    self.assertEqual(instance._no_luci_auth, expected.get('no_luci_auth'))
32    self.assertEqual(instance._code_review_system,
33                     expected.get('code_review_system'))
34    self.assertEqual(instance._continuous_integration_system,
35                     expected.get('continuous_integration_system'))
36    self.assertEqual(instance._git_revision, expected.get('git_revision'))
37    self.assertEqual(instance._issue, expected.get('gerrit_issue'))
38    self.assertEqual(instance._patchset, expected.get('gerrit_patchset'))
39    self.assertEqual(instance._job_id, expected.get('buildbucket_id'))
40    self.assertEqual(instance._bypass_skia_gold_functionality,
41                     expected.get('bypass_skia_gold_functionality'))
42
43  def test_initializeSkiaGoldAttributes_unsetLocal(self) -> None:
44    args = createSkiaGoldArgs()
45    sgp = skia_gold_properties.SkiaGoldProperties(args)
46    self.verifySkiaGoldProperties(sgp, {})
47
48  def test_initializeSkiaGoldAttributes_explicitLocal(self) -> None:
49    args = createSkiaGoldArgs(local_pixel_tests=True)
50    sgp = skia_gold_properties.SkiaGoldProperties(args)
51    self.verifySkiaGoldProperties(sgp, {'local_pixel_tests': True})
52
53  def test_initializeSkiaGoldAttributes_explicitNonLocal(self) -> None:
54    args = createSkiaGoldArgs(local_pixel_tests=False)
55    sgp = skia_gold_properties.SkiaGoldProperties(args)
56    self.verifySkiaGoldProperties(sgp, {'local_pixel_tests': False})
57
58  def test_initializeSkiaGoldAttributes_explicitNoLuciAuth(self) -> None:
59    args = createSkiaGoldArgs(no_luci_auth=True)
60    sgp = skia_gold_properties.SkiaGoldProperties(args)
61    self.verifySkiaGoldProperties(sgp, {'no_luci_auth': True})
62
63  def test_initializeSkiaGoldAttributes_explicitServiceAccount(self) -> None:
64    args = createSkiaGoldArgs(service_account='a')
65    sgp = skia_gold_properties.SkiaGoldProperties(args)
66    self.verifySkiaGoldProperties(sgp, {
67        'service_account': 'a',
68        'no_luci_auth': True
69    })
70
71  def test_initializeSkiaGoldAttributes_explicitCrs(self) -> None:
72    args = createSkiaGoldArgs(code_review_system='foo')
73    sgp = skia_gold_properties.SkiaGoldProperties(args)
74    self.verifySkiaGoldProperties(sgp, {'code_review_system': 'foo'})
75
76  def test_initializeSkiaGoldAttributes_explicitCis(self) -> None:
77    args = createSkiaGoldArgs(continuous_integration_system='foo')
78    sgp = skia_gold_properties.SkiaGoldProperties(args)
79    self.verifySkiaGoldProperties(sgp, {'continuous_integration_system': 'foo'})
80
81  def test_initializeSkiaGoldAttributes_bypassExplicitTrue(self) -> None:
82    args = createSkiaGoldArgs(bypass_skia_gold_functionality=True)
83    sgp = skia_gold_properties.SkiaGoldProperties(args)
84    self.verifySkiaGoldProperties(sgp, {'bypass_skia_gold_functionality': True})
85
86  def test_initializeSkiaGoldAttributes_explicitGitRevision(self) -> None:
87    args = createSkiaGoldArgs(git_revision='a')
88    sgp = skia_gold_properties.SkiaGoldProperties(args)
89    self.verifySkiaGoldProperties(sgp, {'git_revision': 'a'})
90
91  def test_initializeSkiaGoldAttributes_tryjobArgsIgnoredWithoutRevision(
92      self) -> None:
93    args = createSkiaGoldArgs(gerrit_issue=1,
94                              gerrit_patchset=2,
95                              buildbucket_id=3)
96    sgp = skia_gold_properties.SkiaGoldProperties(args)
97    self.verifySkiaGoldProperties(sgp, {})
98
99  def test_initializeSkiaGoldAttributes_tryjobArgs(self) -> None:
100    args = createSkiaGoldArgs(git_revision='a',
101                              gerrit_issue=1,
102                              gerrit_patchset=2,
103                              buildbucket_id=3)
104    sgp = skia_gold_properties.SkiaGoldProperties(args)
105    self.verifySkiaGoldProperties(
106        sgp, {
107            'git_revision': 'a',
108            'gerrit_issue': 1,
109            'gerrit_patchset': 2,
110            'buildbucket_id': 3
111        })
112
113  def test_initializeSkiaGoldAttributes_tryjobMissingPatchset(self) -> None:
114    args = createSkiaGoldArgs(git_revision='a',
115                              gerrit_issue=1,
116                              buildbucket_id=3)
117    with self.assertRaises(RuntimeError):
118      skia_gold_properties.SkiaGoldProperties(args)
119
120  def test_initializeSkiaGoldAttributes_tryjobMissingBuildbucket(self) -> None:
121    args = createSkiaGoldArgs(git_revision='a',
122                              gerrit_issue=1,
123                              gerrit_patchset=2)
124    with self.assertRaises(RuntimeError):
125      skia_gold_properties.SkiaGoldProperties(args)
126
127
128class SkiaGoldPropertiesCalculationTest(unittest.TestCase):
129  """Tests that SkiaGoldProperties properly calculates certain properties."""
130
131  def testLocalPixelTests_determineTrue(self) -> None:
132    args = createSkiaGoldArgs()
133    sgp = skia_gold_properties.SkiaGoldProperties(args)
134    with mock.patch.dict(os.environ, {}, clear=True):
135      self.assertTrue(sgp.local_pixel_tests)
136    with mock.patch.dict(os.environ, {'RUNNING_IN_SKYLAB': '0'}, clear=True):
137      self.assertTrue(sgp.local_pixel_tests)
138
139  def testLocalPixelTests_determineFalse(self) -> None:
140    args = createSkiaGoldArgs()
141    sgp = skia_gold_properties.SkiaGoldProperties(args)
142    with mock.patch.dict(os.environ, {'SWARMING_SERVER': ''}, clear=True):
143      self.assertFalse(sgp.local_pixel_tests)
144    with mock.patch.dict(os.environ, {'RUNNING_IN_SKYLAB': '1'}, clear=True):
145      self.assertFalse(sgp.local_pixel_tests)
146
147  def testIsTryjobRun_noIssue(self) -> None:
148    args = createSkiaGoldArgs()
149    sgp = skia_gold_properties.SkiaGoldProperties(args)
150    self.assertFalse(sgp.IsTryjobRun())
151
152  def testIsTryjobRun_issue(self) -> None:
153    args = createSkiaGoldArgs(git_revision='a',
154                              gerrit_issue=1,
155                              gerrit_patchset=2,
156                              buildbucket_id=3)
157    sgp = skia_gold_properties.SkiaGoldProperties(args)
158    self.assertTrue(sgp.IsTryjobRun())
159
160  def testGetGitRevision_revisionSet(self) -> None:
161    args = createSkiaGoldArgs(git_revision='a')
162    sgp = skia_gold_properties.SkiaGoldProperties(args)
163    self.assertEqual(sgp.git_revision, 'a')
164
165  def testGetGitRevision_findValidRevision(self) -> None:
166    args = createSkiaGoldArgs(local_pixel_tests=True)
167    sgp = skia_gold_properties.SkiaGoldProperties(args)
168    with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
169                           '_GetGitOriginMainHeadSha1') as patched_head:
170      expected = 'a' * 40
171      patched_head.return_value = expected
172      self.assertEqual(sgp.git_revision, expected)
173      # Should be cached.
174      self.assertEqual(sgp._git_revision, expected)
175
176  def testGetGitRevision_noExplicitOnBot(self) -> None:
177    args = createSkiaGoldArgs(local_pixel_tests=False)
178    sgp = skia_gold_properties.SkiaGoldProperties(args)
179    with self.assertRaises(RuntimeError):
180      _ = sgp.git_revision
181
182  def testGetGitRevision_findEmptyRevision(self) -> None:
183    args = createSkiaGoldArgs(local_pixel_tests=True)
184    sgp = skia_gold_properties.SkiaGoldProperties(args)
185    with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
186                           '_GetGitOriginMainHeadSha1') as patched_head:
187      patched_head.return_value = ''
188      with self.assertRaises(RuntimeError):
189        _ = sgp.git_revision
190
191  def testGetGitRevision_findMalformedRevision(self) -> None:
192    args = createSkiaGoldArgs(local_pixel_tests=True)
193    sgp = skia_gold_properties.SkiaGoldProperties(args)
194    with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
195                           '_GetGitOriginMainHeadSha1') as patched_head:
196      patched_head.return_value = 'a' * 39
197      with self.assertRaises(RuntimeError):
198        _ = sgp.git_revision
199
200
201if __name__ == '__main__':
202  unittest.main(verbosity=2)
203