blob: 875e0885efee74dae97654f174b2a7204dc8fb10 [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
18environment variables to the --shard_index and --shard_count flags, and renames
19the --isolated-script-test-output flag to --dump_json_test_results.
20
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
25If the --store-test-artifacts flag is set, an --output_dir must be also
26specified.
27The test artifacts will then be stored in a 'test_artifacts' subdirectory of the
28output dir, and will be compressed into a zip file once the test finishes
29executing.
30This is useful when running the tests in swarming, since the output directory
31is not known beforehand.
32
ehmaldonado76e60e92017-05-04 06:18:26 -070033For example:
ehmaldonado3ff7a952017-03-29 09:42:32 -070034
35 gtest-parallel-wrapper.py some_test \
ehmaldonado76e60e92017-05-04 06:18:26 -070036 --some_flag=some_value \
37 --another_flag \
Oleh Prypin69c02222018-05-23 15:18:12 +020038 --output_dir=SOME_OUTPUT_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020039 --store-test-artifacts
40 --isolated-script-test-output=SOME_DIR \
41 --isolated-script-test-perf-output=SOME_OTHER_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020042 -- \
ehmaldonado76e60e92017-05-04 06:18:26 -070043 --foo=bar \
44 --baz
ehmaldonado3ff7a952017-03-29 09:42:32 -070045
ehmaldonado76e60e92017-05-04 06:18:26 -070046Will be converted into:
ehmaldonado3ff7a952017-03-29 09:42:32 -070047
Oleh Prypin69c02222018-05-23 15:18:12 +020048 python gtest-parallel \
ehmaldonado3ff7a952017-03-29 09:42:32 -070049 --shard_index 0 \
Oleh Prypin69c02222018-05-23 15:18:12 +020050 --shard_count 1 \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020051 --output_dir=SOME_OUTPUT_DIR \
52 --dump_json_test_results=SOME_DIR \
Oleh Prypin69c02222018-05-23 15:18:12 +020053 some_test \
ehmaldonado3ff7a952017-03-29 09:42:32 -070054 -- \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020055 --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \
Oleh Prypin69c02222018-05-23 15:18:12 +020056 --some_flag=some_value \
57 --another_flag \
58 --isolated-script-test-perf-output=SOME_OTHER_DIR \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020059 --foo=bar \
ehmaldonado76e60e92017-05-04 06:18:26 -070060 --baz
61
ehmaldonado3ff7a952017-03-29 09:42:32 -070062"""
63
64import argparse
Edward Lemurc2b6cf32017-10-10 14:00:35 +020065import collections
ehmaldonado3ff7a952017-03-29 09:42:32 -070066import os
Edward Lemurc2b6cf32017-10-10 14:00:35 +020067import shutil
ehmaldonado3ff7a952017-03-29 09:42:32 -070068import subprocess
69import sys
70
ehmaldonado3ff7a952017-03-29 09:42:32 -070071
Edward Lemurc2b6cf32017-10-10 14:00:35 +020072Args = collections.namedtuple('Args',
73 ['gtest_parallel_args', 'test_env', 'output_dir',
74 'test_artifacts_dir'])
75
76
77def _CatFiles(file_list, output_file):
ehmaldonado03184632017-03-30 03:33:19 -070078 with open(output_file, 'w') as output_file:
79 for filename in file_list:
80 with open(filename) as input_file:
81 output_file.write(input_file.read())
82 os.remove(filename)
ehmaldonado3ff7a952017-03-29 09:42:32 -070083
ehmaldonado3ff7a952017-03-29 09:42:32 -070084
Oleh Prypin69c02222018-05-23 15:18:12 +020085class ReconstructibleArgumentGroup(object):
86 """An argument group that can be converted back into a command line.
87
88 This acts like ArgumentParser.add_argument_group, but names of arguments added
89 to it are also kept in a list, so that parsed options from
90 ArgumentParser.parse_args can be reconstructed back into a command line (list
91 of args) based on the list of wanted keys."""
92 def __init__(self, parser, *args, **kwargs):
93 self._group = parser.add_argument_group(*args, **kwargs)
94 self._keys = []
95
96 def AddArgument(self, *args, **kwargs):
97 arg = self._group.add_argument(*args, **kwargs)
98 self._keys.append(arg.dest)
99
100 def RemakeCommandLine(self, options):
101 result = []
102 for key in self._keys:
103 value = getattr(options, key)
104 if value is True:
105 result.append('--%s' % key)
106 elif value is not None:
107 result.append('--%s=%s' % (key, value))
108 return result
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200109
110
Oleh Prypin69c02222018-05-23 15:18:12 +0200111def ParseArgs(argv=None):
112 parser = argparse.ArgumentParser(argv)
ehmaldonado76e60e92017-05-04 06:18:26 -0700113
Oleh Prypin69c02222018-05-23 15:18:12 +0200114 gtest_group = ReconstructibleArgumentGroup(parser,
115 'Arguments to gtest-parallel')
116 # These options will be passed unchanged to gtest-parallel.
117 gtest_group.AddArgument('-d', '--output_dir')
118 gtest_group.AddArgument('-r', '--repeat')
119 gtest_group.AddArgument('--retry_failed')
120 gtest_group.AddArgument('-w', '--workers')
121 gtest_group.AddArgument('--gtest_color')
122 gtest_group.AddArgument('--gtest_filter')
123 gtest_group.AddArgument('--gtest_also_run_disabled_tests',
124 action='store_true', default=None)
125 gtest_group.AddArgument('--timeout')
ehmaldonado76e60e92017-05-04 06:18:26 -0700126
127 # --isolated-script-test-output is used to upload results to the flakiness
128 # dashboard. This translation is made because gtest-parallel expects the flag
129 # to be called --dump_json_test_results instead.
Oleh Prypin69c02222018-05-23 15:18:12 +0200130 gtest_group.AddArgument('--isolated-script-test-output',
131 dest='dump_json_test_results')
ehmaldonado3ff7a952017-03-29 09:42:32 -0700132
Oleh Prypin69c02222018-05-23 15:18:12 +0200133 # Needed when the test wants to store test artifacts, because it doesn't know
134 # what will be the swarming output dir.
135 parser.add_argument('--store-test-artifacts', action='store_true')
136
137 # No-sandbox is a Chromium-specific flag, ignore it.
138 # TODO(oprypin): Remove (bugs.webrtc.org/8115)
139 parser.add_argument('--no-sandbox', action='store_true',
140 help=argparse.SUPPRESS)
141
142 parser.add_argument('executable')
143 parser.add_argument('executable_args', nargs='*')
144
145 options, unrecognized_args = parser.parse_known_args(argv)
146
147 executable_args = options.executable_args + unrecognized_args
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200148
149 if options.store_test_artifacts:
Oleh Prypin69c02222018-05-23 15:18:12 +0200150 assert options.output_dir, (
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200151 '--output_dir must be specified for storing test artifacts.')
Oleh Prypin69c02222018-05-23 15:18:12 +0200152 test_artifacts_dir = os.path.join(options.output_dir, 'test_artifacts')
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200153
Oleh Prypin69c02222018-05-23 15:18:12 +0200154 executable_args.insert(0, '--test_artifacts_dir=%s' % test_artifacts_dir)
155 else:
156 test_artifacts_dir = None
157
158 gtest_parallel_args = gtest_group.RemakeCommandLine(options)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200159
ehmaldonado03184632017-03-30 03:33:19 -0700160 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
161 # environment. Otherwise it will be picked up by the binary, causing a bug
162 # where only tests in the first shard are executed.
163 test_env = os.environ.copy()
164 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
165 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -0700166
Oleh Prypin69c02222018-05-23 15:18:12 +0200167 gtest_parallel_args.insert(0, '--shard_index=%s' % gtest_shard_index)
168 gtest_parallel_args.insert(1, '--shard_count=%s' % gtest_total_shards)
ehmaldonado3ff7a952017-03-29 09:42:32 -0700169
Oleh Prypin69c02222018-05-23 15:18:12 +0200170 gtest_parallel_args.append(options.executable)
171 if executable_args:
172 gtest_parallel_args += ['--'] + executable_args
173
174 return Args(gtest_parallel_args, test_env, options.output_dir,
175 test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700176
177
178def main():
179 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
180 gtest_parallel_path = os.path.join(
181 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
182
Oleh Prypin69c02222018-05-23 15:18:12 +0200183 gtest_parallel_args, test_env, output_dir, test_artifacts_dir = ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700184
ehmaldonado03184632017-03-30 03:33:19 -0700185 command = [
186 sys.executable,
187 gtest_parallel_path,
Oleh Prypin69c02222018-05-23 15:18:12 +0200188 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700189
Oleh Prypin69c02222018-05-23 15:18:12 +0200190 if output_dir and not os.path.isdir(output_dir):
191 os.makedirs(output_dir)
192 if test_artifacts_dir and not os.path.isdir(test_artifacts_dir):
193 os.makedirs(test_artifacts_dir)
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100194
ehmaldonado03184632017-03-30 03:33:19 -0700195 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
196 sys.stdout.flush()
197
Oleh Prypin69c02222018-05-23 15:18:12 +0200198 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700199
Oleh Prypin69c02222018-05-23 15:18:12 +0200200 if output_dir:
ehmaldonado950614e2017-05-02 05:52:57 -0700201 for test_status in 'passed', 'failed', 'interrupted':
Oleh Prypin69c02222018-05-23 15:18:12 +0200202 logs_dir = os.path.join(output_dir, 'gtest-parallel-logs', test_status)
ehmaldonado950614e2017-05-02 05:52:57 -0700203 if not os.path.isdir(logs_dir):
204 continue
205 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
Oleh Prypin69c02222018-05-23 15:18:12 +0200206 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200207 _CatFiles(logs, log_file)
ehmaldonado950614e2017-05-02 05:52:57 -0700208 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700209
Oleh Prypin69c02222018-05-23 15:18:12 +0200210 if test_artifacts_dir:
211 shutil.make_archive(test_artifacts_dir, 'zip', test_artifacts_dir)
212 shutil.rmtree(test_artifacts_dir)
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200213
ehmaldonado03184632017-03-30 03:33:19 -0700214 return exit_code
215
216
217if __name__ == '__main__':
218 sys.exit(main())