blob: 42e523343ed7d36ae18f1403e44dc3019f8d2b02 [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
ehmaldonado76e60e92017-05-04 06:18:26 -070021All flags before '--' will be passed as arguments to gtest-parallel, and
22(almost) all flags after '--' will be passed as arguments to the test
23executable.
24The exception is that --isolated-script-test-output and
25--isolated-script-test-chartson-output are expected to be after '--', so they
26are processed and removed from there.
Edward Lemurc2b6cf32017-10-10 14:00:35 +020027
28If the --store-test-artifacts flag is set, an --output_dir must be also
29specified.
30The test artifacts will then be stored in a 'test_artifacts' subdirectory of the
31output dir, and will be compressed into a zip file once the test finishes
32executing.
33This is useful when running the tests in swarming, since the output directory
34is not known beforehand.
35
ehmaldonado76e60e92017-05-04 06:18:26 -070036For example:
ehmaldonado3ff7a952017-03-29 09:42:32 -070037
38 gtest-parallel-wrapper.py some_test \
ehmaldonado76e60e92017-05-04 06:18:26 -070039 --some_flag=some_value \
40 --another_flag \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020041 --output_dir=SOME_OUTPUT_DIR
ehmaldonado3ff7a952017-03-29 09:42:32 -070042 -- \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020043 --store-test-artifacts
44 --isolated-script-test-output=SOME_DIR \
45 --isolated-script-test-perf-output=SOME_OTHER_DIR \
ehmaldonado76e60e92017-05-04 06:18:26 -070046 --foo=bar \
47 --baz
ehmaldonado3ff7a952017-03-29 09:42:32 -070048
ehmaldonado76e60e92017-05-04 06:18:26 -070049Will be converted into:
ehmaldonado3ff7a952017-03-29 09:42:32 -070050
51 python gtest-parallel some_test \
52 --shard_count 1 \
53 --shard_index 0 \
ehmaldonado76e60e92017-05-04 06:18:26 -070054 --some_flag=some_value \
55 --another_flag \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020056 --output_dir=SOME_OUTPUT_DIR \
57 --dump_json_test_results=SOME_DIR \
ehmaldonado3ff7a952017-03-29 09:42:32 -070058 -- \
Edward Lemurc2b6cf32017-10-10 14:00:35 +020059 --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \
60 --foo=bar \
ehmaldonado76e60e92017-05-04 06:18:26 -070061 --baz
62
ehmaldonado3ff7a952017-03-29 09:42:32 -070063"""
64
65import argparse
Edward Lemurc2b6cf32017-10-10 14:00:35 +020066import collections
ehmaldonado3ff7a952017-03-29 09:42:32 -070067import os
Edward Lemurc2b6cf32017-10-10 14:00:35 +020068import shutil
ehmaldonado3ff7a952017-03-29 09:42:32 -070069import subprocess
70import sys
71
ehmaldonado3ff7a952017-03-29 09:42:32 -070072
Edward Lemurc2b6cf32017-10-10 14:00:35 +020073Args = collections.namedtuple('Args',
74 ['gtest_parallel_args', 'test_env', 'output_dir',
75 'test_artifacts_dir'])
76
77
78def _CatFiles(file_list, output_file):
ehmaldonado03184632017-03-30 03:33:19 -070079 with open(output_file, 'w') as output_file:
80 for filename in file_list:
81 with open(filename) as input_file:
82 output_file.write(input_file.read())
83 os.remove(filename)
ehmaldonado3ff7a952017-03-29 09:42:32 -070084
ehmaldonado3ff7a952017-03-29 09:42:32 -070085
Edward Lemurc2b6cf32017-10-10 14:00:35 +020086def _GetOutputDir(gtest_parallel_args):
87 parser = argparse.ArgumentParser()
88 parser.add_argument('--output_dir', type=str, default=None)
89 options, _ = parser.parse_known_args(gtest_parallel_args)
90 return options.output_dir
91
92
93def _ParseArgs():
ehmaldonadof549dff2017-07-18 08:16:18 -070094 if '--' in sys.argv:
95 argv_index = sys.argv.index('--')
96 else:
97 argv_index = len(sys.argv)
ehmaldonado76e60e92017-05-04 06:18:26 -070098
99 gtest_parallel_args = sys.argv[1:argv_index]
100 executable_args = sys.argv[argv_index + 1:]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700101
ehmaldonado03184632017-03-30 03:33:19 -0700102 parser = argparse.ArgumentParser()
103 parser.add_argument('--isolated-script-test-output', type=str, default=None)
kjellander382f2b22017-04-11 04:07:01 -0700104
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200105 # Needed when the test wants to store test artifacts, because it doesn't know
106 # what will be the swarming output dir.
107 parser.add_argument('--store-test-artifacts', action='store_true',
108 default=False)
109
ehmaldonado950614e2017-05-02 05:52:57 -0700110 # We don't need to implement this flag, and possibly can't, since it's
111 # intended for results of Telemetry tests. See
112 # https://chromium.googlesource.com/external/github.com/catapult-project/catapult/+/HEAD/dashboard/docs/data-format.md
ehmaldonadoc4eee322017-09-07 00:34:17 -0700113 parser.add_argument('--isolated-script-test-perf-output', type=str,
kjellander382f2b22017-04-11 04:07:01 -0700114 default=None)
115
oprypin51d49b42017-08-22 10:55:47 -0700116 # No-sandbox is a Chromium-specific flag, ignore it.
117 # TODO(oprypin): Remove (bugs.webrtc.org/8115)
118 parser.add_argument('--no-sandbox', action='store_true', default=False)
119
ehmaldonado76e60e92017-05-04 06:18:26 -0700120 # We have to do this, since --isolated-script-test-output is passed as an
121 # argument to the executable by the swarming scripts, and we want to pass it
122 # to gtest-parallel instead.
123 options, executable_args = parser.parse_known_args(executable_args)
124
125 # --isolated-script-test-output is used to upload results to the flakiness
126 # dashboard. This translation is made because gtest-parallel expects the flag
127 # to be called --dump_json_test_results instead.
128 if options.isolated_script_test_output:
129 gtest_parallel_args += [
130 '--dump_json_test_results',
131 options.isolated_script_test_output,
132 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700133
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200134 output_dir = _GetOutputDir(gtest_parallel_args)
135 test_artifacts_dir = None
136
137 if options.store_test_artifacts:
138 assert output_dir, (
139 '--output_dir must be specified for storing test artifacts.')
140 test_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
141 if not os.path.isdir(test_artifacts_dir):
142 os.makedirs(test_artifacts_dir)
143
144 executable_args += [
145 '--test_artifacts_dir',
146 test_artifacts_dir,
147 ]
148
ehmaldonado03184632017-03-30 03:33:19 -0700149 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
150 # environment. Otherwise it will be picked up by the binary, causing a bug
151 # where only tests in the first shard are executed.
152 test_env = os.environ.copy()
153 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
154 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -0700155
ehmaldonado76e60e92017-05-04 06:18:26 -0700156 gtest_parallel_args += [
ehmaldonado03184632017-03-30 03:33:19 -0700157 '--shard_count',
158 gtest_total_shards,
159 '--shard_index',
160 gtest_shard_index,
ehmaldonado76e60e92017-05-04 06:18:26 -0700161 ] + ['--'] + executable_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700162
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200163 return Args(gtest_parallel_args, test_env, output_dir, test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700164
165
166def main():
167 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
168 gtest_parallel_path = os.path.join(
169 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
170
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200171 args = _ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700172
ehmaldonado03184632017-03-30 03:33:19 -0700173 command = [
174 sys.executable,
175 gtest_parallel_path,
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200176 ] + args.gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700177
ehmaldonado03184632017-03-30 03:33:19 -0700178 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
179 sys.stdout.flush()
180
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200181 exit_code = subprocess.call(command, env=args.test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700182
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200183 if args.output_dir:
ehmaldonado950614e2017-05-02 05:52:57 -0700184 for test_status in 'passed', 'failed', 'interrupted':
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200185 logs_dir = os.path.join(args.output_dir, 'gtest-parallel-logs',
186 test_status)
ehmaldonado950614e2017-05-02 05:52:57 -0700187 if not os.path.isdir(logs_dir):
188 continue
189 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200190 log_file = os.path.join(args.output_dir, '%s-tests.log' % test_status)
191 _CatFiles(logs, log_file)
ehmaldonado950614e2017-05-02 05:52:57 -0700192 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700193
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200194 if args.test_artifacts_dir:
195 shutil.make_archive(args.test_artifacts_dir, 'zip', args.test_artifacts_dir)
196 shutil.rmtree(args.test_artifacts_dir)
197
ehmaldonado03184632017-03-30 03:33:19 -0700198 return exit_code
199
200
201if __name__ == '__main__':
202 sys.exit(main())