blob: cb83f1907d676cfa259d0162913352d329d20537 [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
11"""
12This script acts as an interface between the Chromium infrastructure and
13gtest-parallel, renaming options and translating environment variables into
14flags. Developers should execute gtest-parallel directly.
15
16In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS
17environment variables to the --shard_index and --shard_count flags, and renames
18the --isolated-script-test-output flag to --dump_json_test_results.
19
20Note that the flags unprocessed by this script will passed as arguments to the
21test executable, i.e.
22
23 gtest-parallel-wrapper.py some_test \
24 --isolated-script-test-output=some_dir \
25 --unprocessed_arg_1
26 -- \
27 --unprocessed_arg_2
28
29will be converted into
30
31 python gtest-parallel some_test \
32 --shard_count 1 \
33 --shard_index 0 \
34 --dump_json_test_results some_dir \
35 -- \
36 --unprocessed_arg_1
37 --unprocessed_arg_2
38"""
39
40import argparse
41import os
42import subprocess
43import sys
44
ehmaldonado3ff7a952017-03-29 09:42:32 -070045
ehmaldonado03184632017-03-30 03:33:19 -070046def cat_files(file_list, output_file):
47 with open(output_file, 'w') as output_file:
48 for filename in file_list:
49 with open(filename) as input_file:
50 output_file.write(input_file.read())
51 os.remove(filename)
ehmaldonado3ff7a952017-03-29 09:42:32 -070052
ehmaldonado3ff7a952017-03-29 09:42:32 -070053
ehmaldonado03184632017-03-30 03:33:19 -070054def main():
55 # Ignore '--'. Options unprocessed by this script will be passed to the test
56 # as arguments.
57 if '--' in sys.argv:
58 del sys.argv[sys.argv.index('--')]
ehmaldonado3ff7a952017-03-29 09:42:32 -070059
ehmaldonado03184632017-03-30 03:33:19 -070060 parser = argparse.ArgumentParser()
61 parser.add_argument('--isolated-script-test-output', type=str, default=None)
kjellander382f2b22017-04-11 04:07:01 -070062
63 # TODO(ehmaldonado): Implement this flag instead of just "eating" it.
64 parser.add_argument('--isolated-script-test-chartjson-output', type=str,
65 default=None)
66
ehmaldonado03184632017-03-30 03:33:19 -070067 parser.add_argument('--output_dir', type=str, default=None)
68 parser.add_argument('--timeout', type=int, default=None)
ehmaldonado3ff7a952017-03-29 09:42:32 -070069
ehmaldonado03184632017-03-30 03:33:19 -070070 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
71 # environment. Otherwise it will be picked up by the binary, causing a bug
72 # where only tests in the first shard are executed.
73 test_env = os.environ.copy()
74 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
75 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -070076
ehmaldonado03184632017-03-30 03:33:19 -070077 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
78 gtest_parallel_path = os.path.join(
79 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
80
81 options, unprocessed = parser.parse_known_args()
82 test_executable = unprocessed[0]
83 test_arguments = unprocessed[1:]
84
85 gtest_args = [
86 test_executable,
87 '--shard_count',
88 gtest_total_shards,
89 '--shard_index',
90 gtest_shard_index,
ehmaldonado3ff7a952017-03-29 09:42:32 -070091 ]
92
ehmaldonado03184632017-03-30 03:33:19 -070093 # --isolated-script-test-output is used to upload results to the flakiness
94 # dashboard. This translation is made because gtest-parallel expects the flag
95 # to be called --dump_json_test_results instead.
96 if options.isolated_script_test_output:
97 gtest_args += [
98 '--dump_json_test_results',
99 options.isolated_script_test_output,
100 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700101
ehmaldonado03184632017-03-30 03:33:19 -0700102 if options.output_dir:
103 gtest_args += [
104 '--output_dir',
105 options.output_dir,
106 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700107
ehmaldonado03184632017-03-30 03:33:19 -0700108 if options.timeout:
109 gtest_args += [
110 '--timeout',
111 str(options.timeout),
112 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700113
ehmaldonado03184632017-03-30 03:33:19 -0700114 command = [
115 sys.executable,
116 gtest_parallel_path,
117 ] + gtest_args + ['--'] + test_arguments
ehmaldonado3ff7a952017-03-29 09:42:32 -0700118
ehmaldonado03184632017-03-30 03:33:19 -0700119 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
120 sys.stdout.flush()
121
122 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
123
124 for test_status in 'passed', 'failed', 'interrupted':
125 logs_dir = os.path.join(options.output_dir, test_status)
126 if not os.path.isdir(logs_dir):
127 continue
128 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
129 log_file = os.path.join(options.output_dir, '%s-tests.log' % test_status)
130 cat_files(logs, log_file)
131 os.rmdir(logs_dir)
132
133 return exit_code
134
135
136if __name__ == '__main__':
137 sys.exit(main())