buildbot_json_magic_substitutions.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright 2020 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """A set of functions to programmatically substitute test arguments.
  5. Arguments for a test that start with $$MAGIC_SUBSTITUTION_ will be replaced with
  6. the output of the corresponding function in this file. For example,
  7. $$MAGIC_SUBSTITUTION_Foo would be replaced with the return value of the Foo()
  8. function.
  9. This is meant as an alternative to many entries in test_suite_exceptions.pyl if
  10. the differentiation can be done programmatically.
  11. """
  12. MAGIC_SUBSTITUTION_PREFIX = '$$MAGIC_SUBSTITUTION_'
  13. def ChromeOSTelemetryRemote(test_config, _=None, __=None):
  14. """Substitutes the correct CrOS remote Telemetry arguments.
  15. VMs use a hard-coded remote address and port, while physical hardware use
  16. a magic hostname.
  17. Args:
  18. test_config: A dict containing a configuration for a specific test on a
  19. specific builder.
  20. """
  21. if _GetChromeOSBoardName(test_config) == 'amd64-generic':
  22. return [
  23. '--remote=127.0.0.1',
  24. # By default, CrOS VMs' ssh servers listen on local port 9222.
  25. '--remote-ssh-port=9222',
  26. ]
  27. return [
  28. # Magic hostname that resolves to a CrOS device in the test lab.
  29. '--remote=variable_chromeos_device_hostname',
  30. ]
  31. def ChromeOSGtestFilterFile(test_config, _=None, __=None):
  32. """Substitutes the correct CrOS filter file for gtests."""
  33. board = _GetChromeOSBoardName(test_config)
  34. test_name = test_config['name']
  35. filter_file = 'chromeos.%s.%s.filter' % (board, test_name)
  36. return [
  37. '--test-launcher-filter-file=../../testing/buildbot/filters/' +
  38. filter_file
  39. ]
  40. def _GetChromeOSBoardName(test_config):
  41. """Helper function to determine what ChromeOS board is being used."""
  42. def StringContainsSubstring(s, sub_strs):
  43. for sub_str in sub_strs:
  44. if sub_str in s:
  45. return True
  46. return False
  47. TEST_POOLS = [
  48. 'chrome.tests',
  49. 'chromium.tests',
  50. ]
  51. dimensions = test_config.get('swarming', {}).get('dimension_sets', [])
  52. assert len(dimensions)
  53. pool = dimensions[0].get('pool')
  54. if not pool:
  55. raise RuntimeError(
  56. 'No pool set for CrOS test, unable to determine whether running on '
  57. 'a VM or physical hardware.')
  58. if not StringContainsSubstring(pool, TEST_POOLS):
  59. raise RuntimeError('Unknown CrOS pool %s' % pool)
  60. return dimensions[0].get('device_type', 'amd64-generic')
  61. def GPUExpectedDeviceId(test_config, _=None, __=None):
  62. """Substitutes the correct expected GPU(s) for certain GPU tests.
  63. Most configurations only need one expected GPU, but heterogeneous pools (e.g.
  64. HD 630 and UHD 630 machines) require multiple.
  65. Args:
  66. test_config: A dict containing a configuration for a specific test on a
  67. specific builder.
  68. """
  69. dimensions = test_config.get('swarming', {}).get('dimension_sets', [])
  70. assert dimensions
  71. gpus = []
  72. for d in dimensions:
  73. # Split up multiple GPU/driver combinations if the swarming OR operator is
  74. # being used.
  75. if 'gpu' in d:
  76. gpus.extend(d['gpu'].split('|'))
  77. # We don't specify GPU on things like Android/CrOS devices, so default to 0.
  78. if not gpus:
  79. return ['--expected-device-id', '0']
  80. device_ids = set()
  81. for gpu_and_driver in gpus:
  82. # In the form vendor:device-driver.
  83. device = gpu_and_driver.split('-')[0].split(':')[1]
  84. device_ids.add(device)
  85. retval = []
  86. for device_id in sorted(device_ids):
  87. retval.extend(['--expected-device-id', device_id])
  88. return retval
  89. def GPUParallelJobs(_, __, tester_config):
  90. """Substitutes the correct number of jobs for GPU tests.
  91. Linux/Mac/Windows can run tests in parallel since multiple windows can be open
  92. but other platforms cannot.
  93. Args:
  94. tester_config: A dict containing the configuration for the builder
  95. that |test_config| is for.
  96. """
  97. os_type = tester_config.get('os_type')
  98. assert os_type
  99. if os_type in ['lacros', 'linux', 'mac', 'win']:
  100. return ['--jobs=4']
  101. return ['--jobs=1']
  102. def TestOnlySubstitution(_, __, ___):
  103. """Magic substitution used for unittests."""
  104. return ['--magic-substitution-success']