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