ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2016 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 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 11 | # pylint: disable=invalid-name |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 12 | """ |
| 13 | This script acts as an interface between the Chromium infrastructure and |
| 14 | gtest-parallel, renaming options and translating environment variables into |
| 15 | flags. Developers should execute gtest-parallel directly. |
| 16 | |
| 17 | In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 18 | environment variables to the --shard_index and --shard_count flags, renames |
| 19 | the --isolated-script-test-output flag to --dump_json_test_results, |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 20 | and interprets e.g. --workers=2x as 2 workers per core. |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 21 | |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 22 | Flags before '--' will be attempted to be understood as arguments to |
| 23 | gtest-parallel. If gtest-parallel doesn't recognize the flag or the flag is |
| 24 | after '--', the flag will be passed on to the test executable. |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 25 | |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 26 | --isolated-script-test-perf-output is renamed to |
| 27 | --isolated_script_test_perf_output. The Android test runner needs the flag to |
| 28 | be in the former form, but our tests require the latter, so this is the only |
| 29 | place we can do it. |
| 30 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 31 | If the --store-test-artifacts flag is set, an --output_dir must be also |
| 32 | specified. |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 33 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 34 | The test artifacts will then be stored in a 'test_artifacts' subdirectory of the |
| 35 | output dir, and will be compressed into a zip file once the test finishes |
| 36 | executing. |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 37 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 38 | This is useful when running the tests in swarming, since the output directory |
| 39 | is not known beforehand. |
| 40 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 41 | For example: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 42 | |
| 43 | gtest-parallel-wrapper.py some_test \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 44 | --some_flag=some_value \ |
| 45 | --another_flag \ |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 46 | --output_dir=SOME_OUTPUT_DIR \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 47 | --store-test-artifacts |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 48 | --isolated-script-test-output=SOME_DIR \ |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 49 | --isolated-script-test-perf-output=SOME_OTHER_DIR \ |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 50 | -- \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 51 | --foo=bar \ |
| 52 | --baz |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 53 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 54 | Will be converted into: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 55 | |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 56 | python gtest-parallel \ |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 57 | --shard_index 0 \ |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 58 | --shard_count 1 \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 59 | --output_dir=SOME_OUTPUT_DIR \ |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 60 | --dump_json_test_results=SOME_DIR \ |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 61 | some_test \ |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 62 | -- \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 63 | --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \ |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 64 | --some_flag=some_value \ |
| 65 | --another_flag \ |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 66 | --isolated_script_test_perf_output=SOME_OTHER_DIR \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 67 | --foo=bar \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 68 | --baz |
| 69 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 70 | """ |
| 71 | |
| 72 | import argparse |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 73 | import collections |
Yves Gerey | ea766fa | 2018-09-25 15:34:27 +0200 | [diff] [blame] | 74 | import multiprocessing |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 75 | import os |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 76 | import shutil |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 77 | import subprocess |
| 78 | import sys |
| 79 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 80 | Args = collections.namedtuple( |
| 81 | 'Args', |
| 82 | ['gtest_parallel_args', 'test_env', 'output_dir', 'test_artifacts_dir']) |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def _CatFiles(file_list, output_file): |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 86 | with open(output_file, 'w') as output_file: |
| 87 | for filename in file_list: |
| 88 | with open(filename) as input_file: |
| 89 | output_file.write(input_file.read()) |
| 90 | os.remove(filename) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 91 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 92 | |
Yves Gerey | ea766fa | 2018-09-25 15:34:27 +0200 | [diff] [blame] | 93 | def _ParseWorkersOption(workers): |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 94 | """Interpret Nx syntax as N * cpu_count. Int value is left as is.""" |
| 95 | base = float(workers.rstrip('x')) |
| 96 | if workers.endswith('x'): |
| 97 | result = int(base * multiprocessing.cpu_count()) |
| 98 | else: |
| 99 | result = int(base) |
| 100 | return max(result, 1) # Sanitize when using e.g. '0.5x'. |
Yves Gerey | ea766fa | 2018-09-25 15:34:27 +0200 | [diff] [blame] | 101 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 102 | |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 103 | class ReconstructibleArgumentGroup(object): |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 104 | """An argument group that can be converted back into a command line. |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 105 | |
| 106 | This acts like ArgumentParser.add_argument_group, but names of arguments added |
| 107 | to it are also kept in a list, so that parsed options from |
| 108 | ArgumentParser.parse_args can be reconstructed back into a command line (list |
| 109 | of args) based on the list of wanted keys.""" |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 110 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 111 | def __init__(self, parser, *args, **kwargs): |
| 112 | self._group = parser.add_argument_group(*args, **kwargs) |
| 113 | self._keys = [] |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 114 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 115 | def AddArgument(self, *args, **kwargs): |
| 116 | arg = self._group.add_argument(*args, **kwargs) |
| 117 | self._keys.append(arg.dest) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 118 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 119 | def RemakeCommandLine(self, options): |
| 120 | result = [] |
| 121 | for key in self._keys: |
| 122 | value = getattr(options, key) |
| 123 | if value is True: |
| 124 | result.append('--%s' % key) |
| 125 | elif value is not None: |
| 126 | result.append('--%s=%s' % (key, value)) |
| 127 | return result |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 128 | |
| 129 | |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 130 | def ParseArgs(argv=None): |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 131 | parser = argparse.ArgumentParser(argv) |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 132 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 133 | gtest_group = ReconstructibleArgumentGroup(parser, |
| 134 | 'Arguments to gtest-parallel') |
| 135 | # These options will be passed unchanged to gtest-parallel. |
| 136 | gtest_group.AddArgument('-d', '--output_dir') |
| 137 | gtest_group.AddArgument('-r', '--repeat') |
| 138 | # TODO(webrtc:13556): use isolated-script-test-output argument instead |
| 139 | # of dump_json_test_results as it was done prior to chromium:1051927. |
| 140 | gtest_group.AddArgument('--dump_json_test_results') |
| 141 | gtest_group.AddArgument('--retry_failed') |
| 142 | gtest_group.AddArgument('--gtest_color') |
| 143 | gtest_group.AddArgument('--gtest_filter') |
| 144 | gtest_group.AddArgument('--gtest_also_run_disabled_tests', |
| 145 | action='store_true', |
| 146 | default=None) |
| 147 | gtest_group.AddArgument('--timeout') |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 148 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 149 | # Syntax 'Nx' will be interpreted as N * number of cpu cores. |
| 150 | gtest_group.AddArgument('-w', '--workers', type=_ParseWorkersOption) |
Yves Gerey | ea766fa | 2018-09-25 15:34:27 +0200 | [diff] [blame] | 151 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 152 | # Needed when the test wants to store test artifacts, because it doesn't |
| 153 | # know what will be the swarming output dir. |
| 154 | parser.add_argument('--store-test-artifacts', action='store_true') |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 155 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 156 | # No-sandbox is a Chromium-specific flag, ignore it. |
| 157 | # TODO(oprypin): Remove (bugs.webrtc.org/8115) |
| 158 | parser.add_argument('--no-sandbox', |
| 159 | action='store_true', |
| 160 | help=argparse.SUPPRESS) |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 161 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 162 | parser.add_argument('executable') |
| 163 | parser.add_argument('executable_args', nargs='*') |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 164 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 165 | options, unrecognized_args = parser.parse_known_args(argv) |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 166 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 167 | webrtc_flags_to_change = { |
| 168 | '--isolated-script-test-perf-output': |
| 169 | '--isolated_script_test_perf_output', |
| 170 | '--isolated-script-test-output': '--isolated_script_test_output', |
| 171 | } |
| 172 | args_to_pass = [] |
| 173 | for arg in unrecognized_args: |
| 174 | if any(arg.startswith(k) for k in webrtc_flags_to_change.keys()): |
| 175 | arg_split = arg.split('=') |
| 176 | args_to_pass.append(webrtc_flags_to_change[arg_split[0]] + '=' + |
| 177 | arg_split[1]) |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 178 | else: |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 179 | args_to_pass.append(arg) |
Patrik Höglund | 28b8a0b | 2020-03-26 20:30:50 +0100 | [diff] [blame] | 180 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 181 | executable_args = options.executable_args + args_to_pass |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 182 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 183 | if options.store_test_artifacts: |
| 184 | assert options.output_dir, ( |
| 185 | '--output_dir must be specified for storing test artifacts.') |
| 186 | test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts') |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 187 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 188 | executable_args.insert(0, '--test_artifacts_dir=%s' % test_artifacts_dir) |
| 189 | else: |
| 190 | test_artifacts_dir = None |
Oleh Prypin | 69c0222 | 2018-05-23 15:18:12 +0200 | [diff] [blame] | 191 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 192 | gtest_parallel_args = gtest_group.RemakeCommandLine(options) |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 193 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 194 | # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the |
| 195 | # environment. Otherwise it will be picked up by the binary, causing a bug |
| 196 | # where only tests in the first shard are executed. |
| 197 | test_env = os.environ.copy() |
| 198 | gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') |
| 199 | gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') |
| 200 | |
| 201 | gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index) |
| 202 | gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards) |
| 203 | |
| 204 | gtest_parallel_args.append(options.executable) |
| 205 | if executable_args: |
| 206 | gtest_parallel_args += ['--'] + executable_args |
| 207 | |
| 208 | return Args(gtest_parallel_args, test_env, options.output_dir, |
| 209 | test_artifacts_dir) |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def main(): |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 213 | webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 214 | gtest_parallel_path = os.path.join(webrtc_root, 'third_party', |
| 215 | 'gtest-parallel', 'gtest-parallel') |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 216 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 217 | gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs() |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 218 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 219 | command = [ |
| 220 | sys.executable, |
| 221 | gtest_parallel_path, |
| 222 | ] + gtest_parallel_args |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 223 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 224 | if output_dir and not os.path.isdir(output_dir): |
| 225 | os.makedirs(output_dir) |
| 226 | if test_artifacts_dir and not os.path.isdir(test_artifacts_dir): |
| 227 | os.makedirs(test_artifacts_dir) |
Mirko Bonadei | 738d11b | 2018-02-19 12:15:45 +0100 | [diff] [blame] | 228 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 229 | print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) |
| 230 | sys.stdout.flush() |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 231 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 232 | exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd()) |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 233 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 234 | if output_dir: |
| 235 | for test_status in 'passed', 'failed', 'interrupted': |
| 236 | logs_dir = os.path.join(output_dir, 'gtest-parallel-logs', test_status) |
| 237 | if not os.path.isdir(logs_dir): |
| 238 | continue |
| 239 | logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)] |
| 240 | log_file = os.path.join(output_dir, '%s-tests.log' % test_status) |
| 241 | _CatFiles(logs, log_file) |
| 242 | os.rmdir(logs_dir) |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 243 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 244 | if test_artifacts_dir: |
| 245 | shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir) |
| 246 | shutil.rmtree(test_artifacts_dir) |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 247 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 248 | return exit_code |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 249 | |
| 250 | |
| 251 | if __name__ == '__main__': |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 252 | sys.exit(main()) |