blob: 26b9afa0adb19ae855d458240fd414ce2386da8c [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')
136 gtest_group.AddArgument('--retry_failed')
137 gtest_group.AddArgument('--gtest_color')
138 gtest_group.AddArgument('--gtest_filter')
139 gtest_group.AddArgument('--gtest_also_run_disabled_tests',
140 action='store_true',
141 default=None)
142 gtest_group.AddArgument('--timeout')
ehmaldonado76e60e92017-05-04 06:18:26 -0700143
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100144 # Syntax 'Nx' will be interpreted as N * number of cpu cores.
145 gtest_group.AddArgument('-w', '--workers', type=_ParseWorkersOption)
Yves Gereyea766fa2018-09-25 15:34:27 +0200146
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100147 # Needed when the test wants to store test artifacts, because it doesn't know
148 # what will be the swarming output dir.
149 parser.add_argument('--store-test-artifacts', action='store_true')
Oleh Prypin69c02222018-05-23 15:18:12 +0200150
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100151 # No-sandbox is a Chromium-specific flag, ignore it.
152 # TODO(oprypin): Remove (bugs.webrtc.org/8115)
153 parser.add_argument('--no-sandbox',
154 action='store_true',
155 help=argparse.SUPPRESS)
Oleh Prypin69c02222018-05-23 15:18:12 +0200156
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100157 parser.add_argument('executable')
158 parser.add_argument('executable_args', nargs='*')
Oleh Prypin69c02222018-05-23 15:18:12 +0200159
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100160 options, unrecognized_args = parser.parse_known_args(argv)
Oleh Prypin69c02222018-05-23 15:18:12 +0200161
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100162 args_to_pass = []
163 for arg in unrecognized_args:
164 if arg.startswith('--isolated-script-test-perf-output'):
165 arg_split = arg.split('=')
166 assert len(
167 arg_split) == 2, 'You must use the = syntax for this flag.'
168 args_to_pass.append('--isolated_script_test_perf_output=' +
169 arg_split[1])
170 else:
171 args_to_pass.append(arg)
172
173 executable_args = options.executable_args + args_to_pass
174
175 if options.store_test_artifacts:
176 assert options.output_dir, (
177 '--output_dir must be specified for storing test artifacts.')
178 test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts')
179
180 executable_args.insert(0,
181 '--test_artifacts_dir=%s' % test_artifacts_dir)
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100182 else:
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100183 test_artifacts_dir = None
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100184
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100185 gtest_parallel_args = gtest_group.RemakeCommandLine(options)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200186
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100187 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
188 # environment. Otherwise it will be picked up by the binary, causing a bug
189 # where only tests in the first shard are executed.
190 test_env = os.environ.copy()
191 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
192 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200193
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100194 gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index)
195 gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards)
Oleh Prypin69c02222018-05-23 15:18:12 +0200196
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100197 gtest_parallel_args.append(options.executable)
198 if executable_args:
199 gtest_parallel_args += ['--'] + executable_args
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200200
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100201 return Args(gtest_parallel_args, test_env, options.output_dir,
202 test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700203
204
205def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100206 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
207 gtest_parallel_path = os.path.join(webrtc_root, 'third_party',
208 'gtest-parallel', 'gtest-parallel')
ehmaldonado76e60e92017-05-04 06:18:26 -0700209
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100210 gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700211
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100212 command = [
213 sys.executable,
214 gtest_parallel_path,
215 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700216
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100217 if output_dir and not os.path.isdir(output_dir):
218 os.makedirs(output_dir)
219 if test_artifacts_dir and not os.path.isdir(test_artifacts_dir):
220 os.makedirs(test_artifacts_dir)
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100221
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100222 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
223 sys.stdout.flush()
ehmaldonado03184632017-03-30 03:33:19 -0700224
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100225 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700226
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100227 if output_dir:
228 for test_status in 'passed', 'failed', 'interrupted':
229 logs_dir = os.path.join(output_dir, 'gtest-parallel-logs',
230 test_status)
231 if not os.path.isdir(logs_dir):
232 continue
233 logs = [
234 os.path.join(logs_dir, log) for log in os.listdir(logs_dir)
235 ]
236 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
237 _CatFiles(logs, log_file)
238 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700239
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100240 if test_artifacts_dir:
241 shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir)
242 shutil.rmtree(test_artifacts_dir)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200243
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100244 return exit_code
ehmaldonado03184632017-03-30 03:33:19 -0700245
246
247if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100248 sys.exit(main())