skia_gold_properties_unittest.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env vpython3
  2. # Copyright 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. #pylint: disable=protected-access
  6. import os
  7. import sys
  8. import unittest
  9. if sys.version_info[0] == 2:
  10. import mock
  11. else:
  12. import unittest.mock as mock
  13. from skia_gold_common import skia_gold_properties
  14. from skia_gold_common import unittest_utils
  15. createSkiaGoldArgs = unittest_utils.createSkiaGoldArgs
  16. class SkiaGoldPropertiesInitializationTest(unittest.TestCase):
  17. """Tests that SkiaGoldProperties initializes (or doesn't) when expected."""
  18. def verifySkiaGoldProperties(
  19. self, instance: skia_gold_properties.SkiaGoldProperties,
  20. expected: dict) -> None:
  21. self.assertEqual(instance._local_pixel_tests,
  22. expected.get('local_pixel_tests'))
  23. self.assertEqual(instance._no_luci_auth, expected.get('no_luci_auth'))
  24. self.assertEqual(instance._code_review_system,
  25. expected.get('code_review_system'))
  26. self.assertEqual(instance._continuous_integration_system,
  27. expected.get('continuous_integration_system'))
  28. self.assertEqual(instance._git_revision, expected.get('git_revision'))
  29. self.assertEqual(instance._issue, expected.get('gerrit_issue'))
  30. self.assertEqual(instance._patchset, expected.get('gerrit_patchset'))
  31. self.assertEqual(instance._job_id, expected.get('buildbucket_id'))
  32. self.assertEqual(instance._bypass_skia_gold_functionality,
  33. expected.get('bypass_skia_gold_functionality'))
  34. def test_initializeSkiaGoldAttributes_unsetLocal(self) -> None:
  35. args = createSkiaGoldArgs()
  36. sgp = skia_gold_properties.SkiaGoldProperties(args)
  37. self.verifySkiaGoldProperties(sgp, {})
  38. def test_initializeSkiaGoldAttributes_explicitLocal(self) -> None:
  39. args = createSkiaGoldArgs(local_pixel_tests=True)
  40. sgp = skia_gold_properties.SkiaGoldProperties(args)
  41. self.verifySkiaGoldProperties(sgp, {'local_pixel_tests': True})
  42. def test_initializeSkiaGoldAttributes_explicitNonLocal(self) -> None:
  43. args = createSkiaGoldArgs(local_pixel_tests=False)
  44. sgp = skia_gold_properties.SkiaGoldProperties(args)
  45. self.verifySkiaGoldProperties(sgp, {'local_pixel_tests': False})
  46. def test_initializeSkiaGoldAttributes_explicitNoLuciAuth(self) -> None:
  47. args = createSkiaGoldArgs(no_luci_auth=True)
  48. sgp = skia_gold_properties.SkiaGoldProperties(args)
  49. self.verifySkiaGoldProperties(sgp, {'no_luci_auth': True})
  50. def test_initializeSkiaGoldAttributes_explicitCrs(self) -> None:
  51. args = createSkiaGoldArgs(code_review_system='foo')
  52. sgp = skia_gold_properties.SkiaGoldProperties(args)
  53. self.verifySkiaGoldProperties(sgp, {'code_review_system': 'foo'})
  54. def test_initializeSkiaGoldAttributes_explicitCis(self) -> None:
  55. args = createSkiaGoldArgs(continuous_integration_system='foo')
  56. sgp = skia_gold_properties.SkiaGoldProperties(args)
  57. self.verifySkiaGoldProperties(sgp, {'continuous_integration_system': 'foo'})
  58. def test_initializeSkiaGoldAttributes_bypassExplicitTrue(self) -> None:
  59. args = createSkiaGoldArgs(bypass_skia_gold_functionality=True)
  60. sgp = skia_gold_properties.SkiaGoldProperties(args)
  61. self.verifySkiaGoldProperties(sgp, {'bypass_skia_gold_functionality': True})
  62. def test_initializeSkiaGoldAttributes_explicitGitRevision(self) -> None:
  63. args = createSkiaGoldArgs(git_revision='a')
  64. sgp = skia_gold_properties.SkiaGoldProperties(args)
  65. self.verifySkiaGoldProperties(sgp, {'git_revision': 'a'})
  66. def test_initializeSkiaGoldAttributes_tryjobArgsIgnoredWithoutRevision(
  67. self) -> None:
  68. args = createSkiaGoldArgs(gerrit_issue=1,
  69. gerrit_patchset=2,
  70. buildbucket_id=3)
  71. sgp = skia_gold_properties.SkiaGoldProperties(args)
  72. self.verifySkiaGoldProperties(sgp, {})
  73. def test_initializeSkiaGoldAttributes_tryjobArgs(self) -> None:
  74. args = createSkiaGoldArgs(git_revision='a',
  75. gerrit_issue=1,
  76. gerrit_patchset=2,
  77. buildbucket_id=3)
  78. sgp = skia_gold_properties.SkiaGoldProperties(args)
  79. self.verifySkiaGoldProperties(
  80. sgp, {
  81. 'git_revision': 'a',
  82. 'gerrit_issue': 1,
  83. 'gerrit_patchset': 2,
  84. 'buildbucket_id': 3
  85. })
  86. def test_initializeSkiaGoldAttributes_tryjobMissingPatchset(self) -> None:
  87. args = createSkiaGoldArgs(git_revision='a',
  88. gerrit_issue=1,
  89. buildbucket_id=3)
  90. with self.assertRaises(RuntimeError):
  91. skia_gold_properties.SkiaGoldProperties(args)
  92. def test_initializeSkiaGoldAttributes_tryjobMissingBuildbucket(self) -> None:
  93. args = createSkiaGoldArgs(git_revision='a',
  94. gerrit_issue=1,
  95. gerrit_patchset=2)
  96. with self.assertRaises(RuntimeError):
  97. skia_gold_properties.SkiaGoldProperties(args)
  98. class SkiaGoldPropertiesCalculationTest(unittest.TestCase):
  99. """Tests that SkiaGoldProperties properly calculates certain properties."""
  100. def testLocalPixelTests_determineTrue(self) -> None:
  101. args = createSkiaGoldArgs()
  102. sgp = skia_gold_properties.SkiaGoldProperties(args)
  103. with mock.patch.dict(os.environ, {}, clear=True):
  104. self.assertTrue(sgp.local_pixel_tests)
  105. def testLocalPixelTests_determineFalse(self) -> None:
  106. args = createSkiaGoldArgs()
  107. sgp = skia_gold_properties.SkiaGoldProperties(args)
  108. with mock.patch.dict(os.environ, {'SWARMING_SERVER': ''}, clear=True):
  109. self.assertFalse(sgp.local_pixel_tests)
  110. def testIsTryjobRun_noIssue(self) -> None:
  111. args = createSkiaGoldArgs()
  112. sgp = skia_gold_properties.SkiaGoldProperties(args)
  113. self.assertFalse(sgp.IsTryjobRun())
  114. def testIsTryjobRun_issue(self) -> None:
  115. args = createSkiaGoldArgs(git_revision='a',
  116. gerrit_issue=1,
  117. gerrit_patchset=2,
  118. buildbucket_id=3)
  119. sgp = skia_gold_properties.SkiaGoldProperties(args)
  120. self.assertTrue(sgp.IsTryjobRun())
  121. def testGetGitRevision_revisionSet(self) -> None:
  122. args = createSkiaGoldArgs(git_revision='a')
  123. sgp = skia_gold_properties.SkiaGoldProperties(args)
  124. self.assertEqual(sgp.git_revision, 'a')
  125. def testGetGitRevision_findValidRevision(self) -> None:
  126. args = createSkiaGoldArgs(local_pixel_tests=True)
  127. sgp = skia_gold_properties.SkiaGoldProperties(args)
  128. with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
  129. '_GetGitOriginMainHeadSha1') as patched_head:
  130. expected = 'a' * 40
  131. patched_head.return_value = expected
  132. self.assertEqual(sgp.git_revision, expected)
  133. # Should be cached.
  134. self.assertEqual(sgp._git_revision, expected)
  135. def testGetGitRevision_noExplicitOnBot(self) -> None:
  136. args = createSkiaGoldArgs(local_pixel_tests=False)
  137. sgp = skia_gold_properties.SkiaGoldProperties(args)
  138. with self.assertRaises(RuntimeError):
  139. _ = sgp.git_revision
  140. def testGetGitRevision_findEmptyRevision(self) -> None:
  141. args = createSkiaGoldArgs(local_pixel_tests=True)
  142. sgp = skia_gold_properties.SkiaGoldProperties(args)
  143. with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
  144. '_GetGitOriginMainHeadSha1') as patched_head:
  145. patched_head.return_value = ''
  146. with self.assertRaises(RuntimeError):
  147. _ = sgp.git_revision
  148. def testGetGitRevision_findMalformedRevision(self) -> None:
  149. args = createSkiaGoldArgs(local_pixel_tests=True)
  150. sgp = skia_gold_properties.SkiaGoldProperties(args)
  151. with mock.patch.object(skia_gold_properties.SkiaGoldProperties,
  152. '_GetGitOriginMainHeadSha1') as patched_head:
  153. patched_head.return_value = 'a' * 39
  154. with self.assertRaises(RuntimeError):
  155. _ = sgp.git_revision
  156. if __name__ == '__main__':
  157. unittest.main(verbosity=2)