blob: a64c7736382f0b4896b5b38d3ff2e426911ef404 [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 \
Jeremy Lecontec6ae33f2022-11-22 11:06:47 +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 parser.add_argument('executable')
159 parser.add_argument('executable_args', nargs='*')
Oleh Prypin69c02222018-05-23 15:18:12 +0200160
Jeremy Leconte994bf452022-01-12 10:51:16 +0100161 options, unrecognized_args = parser.parse_known_args(argv)
Oleh Prypin69c02222018-05-23 15:18:12 +0200162
Jeremy Lecontec6ae33f2022-11-22 11:06:47 +0100163 executable_args = options.executable_args + unrecognized_args
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200164
Jeremy Leconte994bf452022-01-12 10:51:16 +0100165 if options.store_test_artifacts:
166 assert options.output_dir, (
167 '--output_dir must be specified for storing test artifacts.')
168 test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts')
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200169
Jeremy Leconte994bf452022-01-12 10:51:16 +0100170 executable_args.insert(0, '--test_artifacts_dir=%s' % test_artifacts_dir)
171 else:
172 test_artifacts_dir = None
Oleh Prypin69c02222018-05-23 15:18:12 +0200173
Jeremy Leconte994bf452022-01-12 10:51:16 +0100174 gtest_parallel_args = gtest_group.RemakeCommandLine(options)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200175
Jeremy Leconte994bf452022-01-12 10:51:16 +0100176 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
177 # environment. Otherwise it will be picked up by the binary, causing a bug
178 # where only tests in the first shard are executed.
179 test_env = os.environ.copy()
180 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
181 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
182
183 gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index)
184 gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards)
185
186 gtest_parallel_args.append(options.executable)
187 if executable_args:
188 gtest_parallel_args += ['--'] + executable_args
189
190 return Args(gtest_parallel_args, test_env, options.output_dir,
191 test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700192
193
194def main():
Jeremy Leconte994bf452022-01-12 10:51:16 +0100195 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
196 gtest_parallel_path = os.path.join(webrtc_root, 'third_party',
197 'gtest-parallel', 'gtest-parallel')
ehmaldonado76e60e92017-05-04 06:18:26 -0700198
Jeremy Leconte994bf452022-01-12 10:51:16 +0100199 gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700200
Jeremy Leconte994bf452022-01-12 10:51:16 +0100201 command = [
202 sys.executable,
203 gtest_parallel_path,
204 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700205
Jeremy Leconte994bf452022-01-12 10:51:16 +0100206 if output_dir and not os.path.isdir(output_dir):
207 os.makedirs(output_dir)
208 if test_artifacts_dir and not os.path.isdir(test_artifacts_dir):
209 os.makedirs(test_artifacts_dir)
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100210
Mirko Bonadei5d9ae862022-01-27 20:18:16 +0100211 print('gtest-parallel-wrapper: Executing command %s' % ' '.join(command))
Jeremy Leconte994bf452022-01-12 10:51:16 +0100212 sys.stdout.flush()
ehmaldonado03184632017-03-30 03:33:19 -0700213
Jeremy Leconte994bf452022-01-12 10:51:16 +0100214 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700215
Jeremy Leconte994bf452022-01-12 10:51:16 +0100216 if output_dir:
217 for test_status in 'passed', 'failed', 'interrupted':
218 logs_dir = os.path.join(output_dir, 'gtest-parallel-logs', test_status)
219 if not os.path.isdir(logs_dir):
220 continue
221 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
222 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
223 _CatFiles(logs, log_file)
224 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700225
Jeremy Leconte994bf452022-01-12 10:51:16 +0100226 if test_artifacts_dir:
227 shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir)
228 shutil.rmtree(test_artifacts_dir)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200229
Jeremy Leconte994bf452022-01-12 10:51:16 +0100230 return exit_code
ehmaldonado03184632017-03-30 03:33:19 -0700231
232
233if __name__ == '__main__':
Jeremy Leconte994bf452022-01-12 10:51:16 +0100234 sys.exit(main())