blob: 3d6f03d7a93193b5fc851d5ce23eaa94fd13c24c [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
oprypin51d49b42017-08-22 10:55:47 -0700110 # No-sandbox is a Chromium-specific flag, ignore it.
111 # TODO(oprypin): Remove (bugs.webrtc.org/8115)
112 parser.add_argument('--no-sandbox', action='store_true', default=False)
113
ehmaldonado76e60e92017-05-04 06:18:26 -0700114 # We have to do this, since --isolated-script-test-output is passed as an
115 # argument to the executable by the swarming scripts, and we want to pass it
116 # to gtest-parallel instead.
117 options, executable_args = parser.parse_known_args(executable_args)
118
119 # --isolated-script-test-output is used to upload results to the flakiness
120 # dashboard. This translation is made because gtest-parallel expects the flag
121 # to be called --dump_json_test_results instead.
122 if options.isolated_script_test_output:
123 gtest_parallel_args += [
124 '--dump_json_test_results',
125 options.isolated_script_test_output,
126 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700127
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200128 output_dir = _GetOutputDir(gtest_parallel_args)
129 test_artifacts_dir = None
130
131 if options.store_test_artifacts:
132 assert output_dir, (
133 '--output_dir must be specified for storing test artifacts.')
134 test_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
135 if not os.path.isdir(test_artifacts_dir):
136 os.makedirs(test_artifacts_dir)
137
138 executable_args += [
139 '--test_artifacts_dir',
140 test_artifacts_dir,
141 ]
142
ehmaldonado03184632017-03-30 03:33:19 -0700143 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
144 # environment. Otherwise it will be picked up by the binary, causing a bug
145 # where only tests in the first shard are executed.
146 test_env = os.environ.copy()
147 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
148 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -0700149
ehmaldonado76e60e92017-05-04 06:18:26 -0700150 gtest_parallel_args += [
ehmaldonado03184632017-03-30 03:33:19 -0700151 '--shard_count',
152 gtest_total_shards,
153 '--shard_index',
154 gtest_shard_index,
ehmaldonado76e60e92017-05-04 06:18:26 -0700155 ] + ['--'] + executable_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700156
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200157 return Args(gtest_parallel_args, test_env, output_dir, test_artifacts_dir)
ehmaldonado76e60e92017-05-04 06:18:26 -0700158
159
160def main():
161 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
162 gtest_parallel_path = os.path.join(
163 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
164
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200165 args = _ParseArgs()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700166
ehmaldonado03184632017-03-30 03:33:19 -0700167 command = [
168 sys.executable,
169 gtest_parallel_path,
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200170 ] + args.gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700171
Mirko Bonadei738d11b2018-02-19 12:15:45 +0100172 if args.output_dir and not os.path.isdir(args.output_dir):
173 os.makedirs(args.output_dir)
174
ehmaldonado03184632017-03-30 03:33:19 -0700175 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
176 sys.stdout.flush()
177
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200178 exit_code = subprocess.call(command, env=args.test_env, cwd=os.getcwd())
ehmaldonado03184632017-03-30 03:33:19 -0700179
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200180 if args.output_dir:
ehmaldonado950614e2017-05-02 05:52:57 -0700181 for test_status in 'passed', 'failed', 'interrupted':
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200182 logs_dir = os.path.join(args.output_dir, 'gtest-parallel-logs',
183 test_status)
ehmaldonado950614e2017-05-02 05:52:57 -0700184 if not os.path.isdir(logs_dir):
185 continue
186 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200187 log_file = os.path.join(args.output_dir, '%s-tests.log' % test_status)
188 _CatFiles(logs, log_file)
ehmaldonado950614e2017-05-02 05:52:57 -0700189 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700190
Edward Lemurc2b6cf32017-10-10 14:00:35 +0200191 if args.test_artifacts_dir:
192 shutil.make_archive(args.test_artifacts_dir, 'zip', args.test_artifacts_dir)
193 shutil.rmtree(args.test_artifacts_dir)
194
ehmaldonado03184632017-03-30 03:33:19 -0700195 return exit_code
196
197
198if __name__ == '__main__':
199 sys.exit(main())