blob: 2972e6c9bf09e17a7575efd2ee7b5cea20d5abe6 [file] [log] [blame]
Mirko Bonadei5d9ae862022-01-27 20:18:16 +01001#!/usr/bin/env vpython3
ehmaldonado3ff7a952017-03-29 09:42:32 -07002
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
Jeremy Leconte994bf452022-01-12 10:51:16 +010018environment variables to the --shard_index and --shard_count flags, renames
19the --isolated-script-test-output flag to --dump_json_test_results,
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010020and interprets e.g. --workers=2x as 2 workers per core.
ehmaldonado3ff7a952017-03-29 09:42:32 -070021
Oleh Prypin69c02222018-05-23 15:18:12 +020022Flags before '--' will be attempted to be understood as arguments to
23gtest-parallel. If gtest-parallel doesn't recognize the flag or the flag is
24after '--', the flag will be passed on to the test executable.
Edward Lemurc2b6cf32017-10-10 14:00:35 +020025
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010026--isolated-script-test-perf-output is renamed to
27--isolated_script_test_perf_output. The Android test runner needs the flag to
28be in the former form, but our tests require the latter, so this is the only
29place we can do it.
30
Edward Lemurc2b6cf32017-10-10 14:00:35 +020031If the --store-test-artifacts flag is set, an --output_dir must be also
32specified.
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010033
Edward Lemurc2b6cf32017-10-10 14:00:35 +020034The test artifacts will then be stored in a 'test_artifacts' subdirectory of the
35output dir, and will be compressed into a zip file once the test finishes
36executing.
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010037
Edward Lemurc2b6cf32017-10-10 14:00:35 +020038This is useful when running the tests in swarming, since the output directory
39is not known beforehand.
40
ehmaldonado76e60e92017-05-04 06:18:26 -070041For example:
ehmaldonado3ff7a952017-03-29 09:42:32 -070042
43 gtest-parallel-wrapper.py some_test \
ehmaldonado76e60e92017-05-04 06:18:26 -070044 --some_flag=some_value \
45 --another_flag \
Oleh Prypin69c02222018-05-23 15:18:12 +020046 --output_dir=SOME_OUTPUT_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020047 --store-test-artifacts
Jeremy Leconte994bf452022-01-12 10:51:16 +010048 --isolated-script-test-output=SOME_DIR \
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010049 --isolated-script-test-perf-output=SOME_OTHER_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020050 -- \
ehmaldonado76e60e92017-05-04 06:18:26 -070051 --foo=bar \
52 --baz
ehmaldonado3ff7a952017-03-29 09:42:32 -070053
ehmaldonado76e60e92017-05-04 06:18:26 -070054Will be converted into:
ehmaldonado3ff7a952017-03-29 09:42:32 -070055
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010056 vpython3 gtest-parallel \
ehmaldonado3ff7a952017-03-29 09:42:32 -070057 --shard_index 0 \
Oleh Prypin69c02222018-05-23 15:18:12 +020058 --shard_count 1 \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020059 --output_dir=SOME_OUTPUT_DIR \
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010060 --dump_json_test_results=SOME_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020061 some_test \
ehmaldonado3ff7a952017-03-29 09:42:32 -070062 -- \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020063 --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \
Oleh Prypin69c02222018-05-23 15:18:12 +020064 --some_flag=some_value \
65 --another_flag \
Patrik Höglund28b8a0b2020-03-26 20:30:50 +010066 --isolated_script_test_perf_output=SOME_OTHER_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020067 --foo=bar \
ehmaldonado76e60e92017-05-04 06:18:26 -070068 --baz
69
ehmaldonado3ff7a952017-03-29 09:42:32 -070070"""
71
72import argparse
Edward Lemurc2b6cf32017-10-10 14:00:35 +020073import collections
Yves Gereyea766fa2018-09-25 15:34:27 +020074import multiprocessing
ehmaldonado3ff7a952017-03-29 09:42:32 -070075import os
Edward Lemurc2b6cf32017-10-10 14:00:35 +020076import shutil
ehmaldonado3ff7a952017-03-29 09:42:32 -070077import subprocess
78import sys
79
Mirko Bonadei8cc66952020-10-30 10:13:45 +010080Args = collections.namedtuple(
81 'Args',
82 ['gtest_parallel_args', 'test_env', 'output_dir', 'test_artifacts_dir'])
Edward Lemurc2b6cf32017-10-10 14:00:35 +020083
84
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010085def _CatFiles(file_list, output_file_destination):
86 with open(output_file_destination, 'w') as output_file:
Jeremy Leconte994bf452022-01-12 10:51:16 +010087 for filename in file_list:
88 with open(filename) as input_file:
89 output_file.write(input_file.read())
90 os.remove(filename)
Mirko Bonadei8cc66952020-10-30 10:13:45 +010091
ehmaldonado3ff7a952017-03-29 09:42:32 -070092
Yves Gereyea766fa2018-09-25 15:34:27 +020093def _ParseWorkersOption(workers):
Jeremy Leconte994bf452022-01-12 10:51:16 +010094 """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 Gereyea766fa2018-09-25 15:34:27 +0200101
ehmaldonado3ff7a952017-03-29 09:42:32 -0700102
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100103class ReconstructibleArgumentGroup:
Jeremy Leconte994bf452022-01-12 10:51:16 +0100104 """An argument group that can be converted back into a command line.
Oleh Prypin69c02222018-05-23 15:18:12 +0200105
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 Prypin69c02222018-05-23 15:18:12 +0200110
Jeremy Leconte994bf452022-01-12 10:51:16 +0100111 def __init__(self, parser, *args, **kwargs):
112 self._group = parser.add_argument_group(*args, **kwargs)
113 self._keys = []
Oleh Prypin69c02222018-05-23 15:18:12 +0200114
Jeremy Leconte994bf452022-01-12 10:51:16 +0100115 def AddArgument(self, *args, **kwargs):
116 arg = self._group.add_argument(*args, **kwargs)
117 self._keys.append(arg.dest)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100118
Jeremy Leconte994bf452022-01-12 10:51:16 +0100119 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 Lemurc2b6cf32017-10-10 14:00:35 +0200128
129
Oleh Prypin69c02222018-05-23 15:18:12 +0200130def ParseArgs(argv=None):
Jeremy Leconte994bf452022-01-12 10:51:16 +0100131 parser = argparse.ArgumentParser(argv)
ehmaldonado76e60e92017-05-04 06:18:26 -0700132
Jeremy Leconte994bf452022-01-12 10:51:16 +0100133 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')
Jeremy Leconte63472e52022-02-11 13:46:02 +0100138 # --isolated-script-test-output is used to upload results to the flakiness
139 # dashboard. This translation is made because gtest-parallel expects the flag
140 # to be called --dump_json_test_results instead.
141 gtest_group.AddArgument('--isolated-script-test-output',
142 dest='dump_json_test_results')
Jeremy Leconte994bf452022-01-12 10:51:16 +0100143 gtest_group.AddArgument('--retry_failed')
144 gtest_group.AddArgument('--gtest_color')
145 gtest_group.AddArgument('--gtest_filter')
146 gtest_group.AddArgument('--gtest_also_run_disabled_tests',
147 action='store_true',
148 default=None)
149 gtest_group.AddArgument('--timeout')
ehmaldonado76e60e92017-05-04 06:18:26 -0700150
Jeremy Leconte994bf452022-01-12 10:51:16 +0100151 # Syntax 'Nx' will be interpreted as N * number of cpu cores.
152 gtest_group.AddArgument('-w', '--workers', type=_ParseWorkersOption)
Yves Gereyea766fa2018-09-25 15:34:27 +0200153
Jeremy Leconte994bf452022-01-12 10:51:16 +0100154 # Needed when the test wants to store test artifacts, because it doesn't
155 # know what will be the swarming output dir.
156 parser.add_argument('--store-test-artifacts', action='store_true')
Oleh Prypin69c02222018-05-23 15:18:12 +0200157
Jeremy Leconte994bf452022-01-12 10:51:16 +0100158 # No-sandbox is a Chromium-specific flag, ignore it.
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100159 # TODO(bugs.webrtc.org/8115): Remove workaround when fixed.
Jeremy Leconte994bf452022-01-12 10:51:16 +0100160 parser.add_argument('--no-sandbox',
161 action='store_true',
162 help=argparse.SUPPRESS)
Oleh Prypin69c02222018-05-23 15:18:12 +0200163
Jeremy Leconte994bf452022-01-12 10:51:16 +0100164 parser.add_argument('executable')
165 parser.add_argument('executable_args', nargs='*')
Oleh Prypin69c02222018-05-23 15:18:12 +0200166
Jeremy Leconte994bf452022-01-12 10:51:16 +0100167 options, unrecognized_args = parser.parse_known_args(argv)
Oleh Prypin69c02222018-05-23 15:18:12 +0200168
Jeremy Leconte994bf452022-01-12 10:51:16 +0100169 webrtc_flags_to_change = {
170 '--isolated-script-test-perf-output':
171 '--isolated_script_test_perf_output',
172 '--isolated-script-test-output': '--isolated_script_test_output',
173 }
174 args_to_pass = []
175 for arg in unrecognized_args:
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100176 if any(arg.startswith(k) for k in list(webrtc_flags_to_change.keys())):
Jeremy Leconte994bf452022-01-12 10:51:16 +0100177 arg_split = arg.split('=')
178 args_to_pass.append(webrtc_flags_to_change[arg_split[0]] + '=' +
179 arg_split[1])
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100180 else:
Jeremy Leconte994bf452022-01-12 10:51:16 +0100181 args_to_pass.append(arg)
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100182
Jeremy Leconte994bf452022-01-12 10:51:16 +0100183 executable_args = options.executable_args + args_to_pass
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200184
Jeremy Leconte994bf452022-01-12 10:51:16 +0100185 if options.store_test_artifacts:
186 assert options.output_dir, (
187 '--output_dir must be specified for storing test artifacts.')
188 test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts')
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200189
Jeremy Leconte994bf452022-01-12 10:51:16 +0100190 executable_args.insert(0, '--test_artifacts_dir=%s' % test_artifacts_dir)
191 else:
192 test_artifacts_dir = None
Oleh Prypin69c02222018-05-23 15:18:12 +0200193
Jeremy Leconte994bf452022-01-12 10:51:16 +0100194 gtest_parallel_args = gtest_group.RemakeCommandLine(options)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200195
Jeremy Leconte994bf452022-01-12 10:51:16 +0100196 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
197 # environment. Otherwise it will be picked up by the binary, causing a bug
198 # where only tests in the first shard are executed.
199 test_env = os.environ.copy()
200 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
201 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
202
203 gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index)
204 gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards)
205
206 gtest_parallel_args.append(options.executable)
207 if executable_args:
208 gtest_parallel_args += ['--'] + executable_args
209
210 return Args(gtest_parallel_args, test_env, options.output_dir,
211 test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700212
213
214def main():
Jeremy Leconte994bf452022-01-12 10:51:16 +0100215 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
216 gtest_parallel_path = os.path.join(webrtc_root, 'third_party',
217 'gtest-parallel', 'gtest-parallel')
ehmaldonado76e60e92017-05-04 06:18:26 -0700218
Jeremy Leconte994bf452022-01-12 10:51:16 +0100219 gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700220
Jeremy Leconte994bf452022-01-12 10:51:16 +0100221 command = [
222 sys.executable,
223 gtest_parallel_path,
224 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700225
Jeremy Leconte994bf452022-01-12 10:51:16 +0100226 if output_dir and not os.path.isdir(output_dir):
227 os.makedirs(output_dir)
228 if test_artifacts_dir and not os.path.isdir(test_artifacts_dir):
229 os.makedirs(test_artifacts_dir)
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100230
Mirko Bonadei5d9ae862022-01-27 20:18:16 +0100231 print('gtest-parallel-wrapper: Executing command %s' % ' '.join(command))
Jeremy Leconte994bf452022-01-12 10:51:16 +0100232 sys.stdout.flush()
ehmaldonado03184632017-03-30 03:33:19 -0700233
Jeremy Leconte994bf452022-01-12 10:51:16 +0100234 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700235
Jeremy Leconte994bf452022-01-12 10:51:16 +0100236 if output_dir:
237 for test_status in 'passed', 'failed', 'interrupted':
238 logs_dir = os.path.join(output_dir, 'gtest-parallel-logs', test_status)
239 if not os.path.isdir(logs_dir):
240 continue
241 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
242 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
243 _CatFiles(logs, log_file)
244 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700245
Jeremy Leconte994bf452022-01-12 10:51:16 +0100246 if test_artifacts_dir:
247 shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir)
248 shutil.rmtree(test_artifacts_dir)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200249
Jeremy Leconte994bf452022-01-12 10:51:16 +0100250 return exit_code
ehmaldonado03184632017-03-30 03:33:19 -0700251
252
253if __name__ == '__main__':
Jeremy Leconte994bf452022-01-12 10:51:16 +0100254 sys.exit(main())