blob: d4460ae6258d1b0e1c53dadac4920706694eb8eb [file] [log] [blame]
ehmaldonado3ff7a952017-03-29 09:42:32 -07001#!/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
kjellanderdd460e22017-04-12 12:06:13 -070011# pylint: disable=invalid-name
ehmaldonado3ff7a952017-03-29 09:42:32 -070012"""
13This script acts as an interface between the Chromium infrastructure and
14gtest-parallel, renaming options and translating environment variables into
15flags. Developers should execute gtest-parallel directly.
16
17In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010018environment variables to the --shard_index and --shard_count flags
19and interprets e.g. --workers=2x as 2 workers per core.
ehmaldonado3ff7a952017-03-29 09:42:32 -070020
Oleh Prypin69c02222018-05-23 15:18:12 +020021Flags before '--' will be attempted to be understood as arguments to
22gtest-parallel. If gtest-parallel doesn't recognize the flag or the flag is
23after '--', the flag will be passed on to the test executable.
Edward Lemurc2b6cf32017-10-10 14:00:35 +020024
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010025--isolated-script-test-perf-output is renamed to
26--isolated_script_test_perf_output. The Android test runner needs the flag to
27be in the former form, but our tests require the latter, so this is the only
28place we can do it.
29
Edward Lemurc2b6cf32017-10-10 14:00:35 +020030If the --store-test-artifacts flag is set, an --output_dir must be also
31specified.
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010032
Edward Lemurc2b6cf32017-10-10 14:00:35 +020033The test artifacts will then be stored in a 'test_artifacts' subdirectory of the
34output dir, and will be compressed into a zip file once the test finishes
35executing.
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010036
Edward Lemurc2b6cf32017-10-10 14:00:35 +020037This is useful when running the tests in swarming, since the output directory
38is not known beforehand.
39
ehmaldonado76e60e92017-05-04 06:18:26 -070040For example:
ehmaldonado3ff7a952017-03-29 09:42:32 -070041
42 gtest-parallel-wrapper.py some_test \
ehmaldonado76e60e92017-05-04 06:18:26 -070043 --some_flag=some_value \
44 --another_flag \
Oleh Prypin69c02222018-05-23 15:18:12 +020045 --output_dir=SOME_OUTPUT_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020046 --store-test-artifacts
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010047 --isolated-script-test-perf-output=SOME_OTHER_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020048 -- \
ehmaldonado76e60e92017-05-04 06:18:26 -070049 --foo=bar \
50 --baz
ehmaldonado3ff7a952017-03-29 09:42:32 -070051
ehmaldonado76e60e92017-05-04 06:18:26 -070052Will be converted into:
ehmaldonado3ff7a952017-03-29 09:42:32 -070053
Oleh Prypin69c02222018-05-23 15:18:12 +020054 python gtest-parallel \
ehmaldonado3ff7a952017-03-29 09:42:32 -070055 --shard_index 0 \
Oleh Prypin69c02222018-05-23 15:18:12 +020056 --shard_count 1 \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020057 --output_dir=SOME_OUTPUT_DIR \
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010058 --dump_json_test_results=SOME_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020059 some_test \
ehmaldonado3ff7a952017-03-29 09:42:32 -070060 -- \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020061 --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \
Oleh Prypin69c02222018-05-23 15:18:12 +020062 --some_flag=some_value \
63 --another_flag \
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010064 --isolated_script_test_perf_output=SOME_OTHER_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020065 --foo=bar \
ehmaldonado76e60e92017-05-04 06:18:26 -070066 --baz
67
ehmaldonado3ff7a952017-03-29 09:42:32 -070068"""
69
70import argparse
Edward Lemurc2b6cf32017-10-10 14:00:35 +020071import collections
Yves Gereyea766fa2018-09-25 15:34:27 +020072import multiprocessing
ehmaldonado3ff7a952017-03-29 09:42:32 -070073import os
Edward Lemurc2b6cf32017-10-10 14:00:35 +020074import shutil
ehmaldonado3ff7a952017-03-29 09:42:32 -070075import subprocess
76import sys
77
Mirko Bonadei8cc66952020-10-30 10:13:45 +010078Args = collections.namedtuple(
79 'Args',
80 ['gtest_parallel_args', 'test_env', 'output_dir', 'test_artifacts_dir'])
Edward Lemurc2b6cf32017-10-10 14:00:35 +020081
82
83def _CatFiles(file_list, output_file):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010084 with open(output_file, 'w') as output_file:
85 for filename in file_list:
86 with open(filename) as input_file:
87 output_file.write(input_file.read())
88 os.remove(filename)
89
ehmaldonado3ff7a952017-03-29 09:42:32 -070090
Yves Gereyea766fa2018-09-25 15:34:27 +020091def _ParseWorkersOption(workers):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010092 """Interpret Nx syntax as N * cpu_count. Int value is left as is."""
93 base = float(workers.rstrip('x'))
94 if workers.endswith('x'):
95 result = int(base * multiprocessing.cpu_count())
96 else:
97 result = int(base)
98 return max(result, 1) # Sanitize when using e.g. '0.5x'.
Yves Gereyea766fa2018-09-25 15:34:27 +020099
ehmaldonado3ff7a952017-03-29 09:42:32 -0700100
Oleh Prypin69c02222018-05-23 15:18:12 +0200101class ReconstructibleArgumentGroup(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102 """An argument group that can be converted back into a command line.
Oleh Prypin69c02222018-05-23 15:18:12 +0200103
104 This acts like ArgumentParser.add_argument_group, but names of arguments added
105 to it are also kept in a list, so that parsed options from
106 ArgumentParser.parse_args can be reconstructed back into a command line (list
107 of args) based on the list of wanted keys."""
Oleh Prypin69c02222018-05-23 15:18:12 +0200108
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100109 def __init__(self, parser, *args, **kwargs):
110 self._group = parser.add_argument_group(*args, **kwargs)
111 self._keys = []
Oleh Prypin69c02222018-05-23 15:18:12 +0200112
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100113 def AddArgument(self, *args, **kwargs):
114 arg = self._group.add_argument(*args, **kwargs)
115 self._keys.append(arg.dest)
116
117 def RemakeCommandLine(self, options):
118 result = []
119 for key in self._keys:
120 value = getattr(options, key)
121 if value is True:
122 result.append('--%s' % key)
123 elif value is not None:
124 result.append('--%s=%s' % (key, value))
125 return result
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200126
127
Oleh Prypin69c02222018-05-23 15:18:12 +0200128def ParseArgs(argv=None):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100129 parser = argparse.ArgumentParser(argv)
ehmaldonado76e60e92017-05-04 06:18:26 -0700130
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100131 gtest_group = ReconstructibleArgumentGroup(parser,
132 'Arguments to gtest-parallel')
133 # These options will be passed unchanged to gtest-parallel.
134 gtest_group.AddArgument('-d', '--output_dir')
135 gtest_group.AddArgument('-r', '--repeat')
Jeremy Leconte20d03232021-11-19 11:24:47 +0100136 gtest_group.AddArgument('--dump_json_test_results')
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100137 gtest_group.AddArgument('--retry_failed')
138 gtest_group.AddArgument('--gtest_color')
139 gtest_group.AddArgument('--gtest_filter')
140 gtest_group.AddArgument('--gtest_also_run_disabled_tests',
141 action='store_true',
142 default=None)
143 gtest_group.AddArgument('--timeout')
ehmaldonado76e60e92017-05-04 06:18:26 -0700144
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100145 # Syntax 'Nx' will be interpreted as N * number of cpu cores.
146 gtest_group.AddArgument('-w', '--workers', type=_ParseWorkersOption)
Yves Gereyea766fa2018-09-25 15:34:27 +0200147
Jeremy Leconte20d03232021-11-19 11:24:47 +0100148 # Needed when the test wants to store test artifacts, because it doesn't
149 # know what will be the swarming output dir.
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100150 parser.add_argument('--store-test-artifacts', action='store_true')
Oleh Prypin69c02222018-05-23 15:18:12 +0200151
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100152 # No-sandbox is a Chromium-specific flag, ignore it.
153 # TODO(oprypin): Remove (bugs.webrtc.org/8115)
154 parser.add_argument('--no-sandbox',
155 action='store_true',
156 help=argparse.SUPPRESS)
Oleh Prypin69c02222018-05-23 15:18:12 +0200157
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100158 parser.add_argument('executable')
159 parser.add_argument('executable_args', nargs='*')
Oleh Prypin69c02222018-05-23 15:18:12 +0200160
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100161 options, unrecognized_args = parser.parse_known_args(argv)
Oleh Prypin69c02222018-05-23 15:18:12 +0200162
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100163 args_to_pass = []
164 for arg in unrecognized_args:
165 if arg.startswith('--isolated-script-test-perf-output'):
166 arg_split = arg.split('=')
167 assert len(
168 arg_split) == 2, 'You must use the = syntax for this flag.'
169 args_to_pass.append('--isolated_script_test_perf_output=' +
170 arg_split[1])
171 else:
172 args_to_pass.append(arg)
173
174 executable_args = options.executable_args + args_to_pass
175
176 if options.store_test_artifacts:
177 assert options.output_dir, (
178 '--output_dir must be specified for storing test artifacts.')
179 test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts')
180
181 executable_args.insert(0,
182 '--test_artifacts_dir=%s' % test_artifacts_dir)
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100183 else:
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100184 test_artifacts_dir = None
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100185
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100186 gtest_parallel_args = gtest_group.RemakeCommandLine(options)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200187
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100188 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
189 # environment. Otherwise it will be picked up by the binary, causing a bug
190 # where only tests in the first shard are executed.
191 test_env = os.environ.copy()
192 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
193 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200194
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100195 gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index)
196 gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards)
Oleh Prypin69c02222018-05-23 15:18:12 +0200197
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100198 gtest_parallel_args.append(options.executable)
199 if executable_args:
200 gtest_parallel_args += ['--'] + executable_args
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200201
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100202 return Args(gtest_parallel_args, test_env, options.output_dir,
203 test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700204
205
206def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100207 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
208 gtest_parallel_path = os.path.join(webrtc_root, 'third_party',
209 'gtest-parallel', 'gtest-parallel')
ehmaldonado76e60e92017-05-04 06:18:26 -0700210
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100211 gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700212
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100213 command = [
214 sys.executable,
215 gtest_parallel_path,
216 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700217
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100218 if output_dir and not os.path.isdir(output_dir):
219 os.makedirs(output_dir)
220 if test_artifacts_dir and not os.path.isdir(test_artifacts_dir):
221 os.makedirs(test_artifacts_dir)
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100222
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100223 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
224 sys.stdout.flush()
ehmaldonado03184632017-03-30 03:33:19 -0700225
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100226 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700227
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100228 if output_dir:
229 for test_status in 'passed', 'failed', 'interrupted':
230 logs_dir = os.path.join(output_dir, 'gtest-parallel-logs',
231 test_status)
232 if not os.path.isdir(logs_dir):
233 continue
234 logs = [
235 os.path.join(logs_dir, log) for log in os.listdir(logs_dir)
236 ]
237 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
238 _CatFiles(logs, log_file)
239 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700240
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100241 if test_artifacts_dir:
242 shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir)
243 shutil.rmtree(test_artifacts_dir)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200244
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100245 return exit_code
ehmaldonado03184632017-03-30 03:33:19 -0700246
247
248if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100249 sys.exit(main())