buildbot_json_magic_substitutions_unittest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python3
  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. import unittest
  6. import buildbot_json_magic_substitutions as magic_substitutions
  7. def CreateConfigWithPool(pool, device_type=None):
  8. dims = {
  9. 'name': 'test_name',
  10. 'swarming': {
  11. 'dimension_sets': [
  12. {
  13. 'pool': pool,
  14. },
  15. ],
  16. },
  17. }
  18. if device_type:
  19. dims['swarming']['dimension_sets'][0]['device_type'] = device_type
  20. return dims
  21. class ChromeOSTelemetryRemoteTest(unittest.TestCase):
  22. def testVirtualMachineSubstitutions(self):
  23. test_config = CreateConfigWithPool('chromium.tests.cros.vm')
  24. self.assertEqual(
  25. magic_substitutions.ChromeOSTelemetryRemote(test_config, None), [
  26. '--remote=127.0.0.1',
  27. '--remote-ssh-port=9222',
  28. ])
  29. def testPhysicalHardwareSubstitutions(self):
  30. test_config = CreateConfigWithPool('chromium.tests', device_type='eve')
  31. self.assertEqual(
  32. magic_substitutions.ChromeOSTelemetryRemote(test_config, None),
  33. ['--remote=variable_chromeos_device_hostname'])
  34. def testNoPool(self):
  35. test_config = CreateConfigWithPool(None)
  36. with self.assertRaisesRegex(RuntimeError, 'No pool *'):
  37. magic_substitutions.ChromeOSTelemetryRemote(test_config, None)
  38. def testUnknownPool(self):
  39. test_config = CreateConfigWithPool('totally-legit-pool')
  40. with self.assertRaisesRegex(RuntimeError, 'Unknown CrOS pool *'):
  41. magic_substitutions.ChromeOSTelemetryRemote(test_config, None)
  42. class ChromeOSGtestFilterFileTest(unittest.TestCase):
  43. def testVirtualMachineFile(self):
  44. test_config = CreateConfigWithPool('chromium.tests.cros.vm')
  45. self.assertEqual(
  46. magic_substitutions.ChromeOSGtestFilterFile(test_config, None), [
  47. '--test-launcher-filter-file=../../testing/buildbot/filters/'
  48. 'chromeos.amd64-generic.test_name.filter',
  49. ])
  50. def testPhysicalHardwareFile(self):
  51. test_config = CreateConfigWithPool('chromium.tests', device_type='eve')
  52. self.assertEqual(
  53. magic_substitutions.ChromeOSGtestFilterFile(test_config, None), [
  54. '--test-launcher-filter-file=../../testing/buildbot/filters/'
  55. 'chromeos.eve.test_name.filter',
  56. ])
  57. def testNoPool(self):
  58. test_config = CreateConfigWithPool(None)
  59. with self.assertRaisesRegex(RuntimeError, 'No pool *'):
  60. magic_substitutions.ChromeOSTelemetryRemote(test_config, None)
  61. def testUnknownPool(self):
  62. test_config = CreateConfigWithPool('totally-legit-pool')
  63. with self.assertRaisesRegex(RuntimeError, 'Unknown CrOS pool *'):
  64. magic_substitutions.ChromeOSTelemetryRemote(test_config, None)
  65. def CreateConfigWithGpus(gpus):
  66. dimension_sets = []
  67. for g in gpus:
  68. dimension_sets.append({'gpu': g})
  69. return {
  70. 'swarming': {
  71. 'dimension_sets': dimension_sets,
  72. },
  73. }
  74. class GPUExpectedDeviceId(unittest.TestCase):
  75. def assertDeviceIdCorrectness(self, retval, device_ids):
  76. self.assertEqual(len(retval), 2 * len(device_ids))
  77. for i in range(0, len(retval), 2):
  78. self.assertEqual(retval[i], '--expected-device-id')
  79. for d in device_ids:
  80. self.assertIn(d, retval)
  81. def testSingleGpuSingleDimension(self):
  82. test_config = CreateConfigWithGpus(['vendor:device1-driver'])
  83. self.assertDeviceIdCorrectness(
  84. magic_substitutions.GPUExpectedDeviceId(test_config, None), ['device1'])
  85. def testSingleGpuDoubleDimension(self):
  86. test_config = CreateConfigWithGpus(
  87. ['vendor:device1-driver', 'vendor:device2-driver'])
  88. self.assertDeviceIdCorrectness(
  89. magic_substitutions.GPUExpectedDeviceId(test_config, None),
  90. ['device1', 'device2'])
  91. def testDoubleGpuSingleDimension(self):
  92. test_config = CreateConfigWithGpus(
  93. ['vendor:device1-driver|vendor:device2-driver'])
  94. self.assertDeviceIdCorrectness(
  95. magic_substitutions.GPUExpectedDeviceId(test_config, None),
  96. ['device1', 'device2'])
  97. def testDoubleGpuDoubleDimension(self):
  98. test_config = CreateConfigWithGpus([
  99. 'vendor:device1-driver|vendor:device2-driver',
  100. 'vendor:device1-driver|vendor:device3-driver'
  101. ])
  102. self.assertDeviceIdCorrectness(
  103. magic_substitutions.GPUExpectedDeviceId(test_config, None),
  104. ['device1', 'device2', 'device3'])
  105. def testNoGpu(self):
  106. self.assertDeviceIdCorrectness(
  107. magic_substitutions.GPUExpectedDeviceId(
  108. {'swarming': {
  109. 'dimension_sets': [{}]
  110. }}, None), ['0'])
  111. def testNoDimensions(self):
  112. with self.assertRaises(AssertionError):
  113. magic_substitutions.GPUExpectedDeviceId({}, None)
  114. class GPUParallelJobs(unittest.TestCase):
  115. def testNoOsType(self):
  116. with self.assertRaises(AssertionError):
  117. magic_substitutions.GPUParallelJobs(None, None, {})
  118. def testParallelJobs(self):
  119. for os_type in ['lacros', 'linux', 'mac', 'win']:
  120. retval = magic_substitutions.GPUParallelJobs(None, None,
  121. {'os_type': os_type})
  122. self.assertEqual(retval, ['--jobs=4'])
  123. def testSerialJobs(self):
  124. for os_type in ['android', 'chromeos', 'fuchsia']:
  125. retval = magic_substitutions.GPUParallelJobs(None, None,
  126. {'os_type': os_type})
  127. self.assertEqual(retval, ['--jobs=1'])
  128. if __name__ == '__main__':
  129. unittest.main()