blob: bfdef096d5b7740e9539028ffb812a0d6d4d8696 [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
21Note that the flags unprocessed by this script will passed as arguments to the
22test executable, i.e.
23
24 gtest-parallel-wrapper.py some_test \
25 --isolated-script-test-output=some_dir \
26 --unprocessed_arg_1
27 -- \
28 --unprocessed_arg_2
29
30will be converted into
31
32 python gtest-parallel some_test \
33 --shard_count 1 \
34 --shard_index 0 \
35 --dump_json_test_results some_dir \
36 -- \
37 --unprocessed_arg_1
38 --unprocessed_arg_2
39"""
40
41import argparse
42import os
43import subprocess
44import sys
45
ehmaldonado3ff7a952017-03-29 09:42:32 -070046
kjellanderdd460e22017-04-12 12:06:13 -070047def CatFiles(file_list, output_file):
ehmaldonado03184632017-03-30 03:33:19 -070048 with open(output_file, 'w') as output_file:
49 for filename in file_list:
50 with open(filename) as input_file:
51 output_file.write(input_file.read())
52 os.remove(filename)
ehmaldonado3ff7a952017-03-29 09:42:32 -070053
ehmaldonado3ff7a952017-03-29 09:42:32 -070054
ehmaldonado03184632017-03-30 03:33:19 -070055def main():
56 # Ignore '--'. Options unprocessed by this script will be passed to the test
57 # as arguments.
58 if '--' in sys.argv:
59 del sys.argv[sys.argv.index('--')]
ehmaldonado3ff7a952017-03-29 09:42:32 -070060
ehmaldonado03184632017-03-30 03:33:19 -070061 parser = argparse.ArgumentParser()
62 parser.add_argument('--isolated-script-test-output', type=str, default=None)
kjellander382f2b22017-04-11 04:07:01 -070063
64 # TODO(ehmaldonado): Implement this flag instead of just "eating" it.
65 parser.add_argument('--isolated-script-test-chartjson-output', type=str,
66 default=None)
67
ehmaldonado03184632017-03-30 03:33:19 -070068 parser.add_argument('--output_dir', type=str, default=None)
69 parser.add_argument('--timeout', type=int, default=None)
ehmaldonado3ff7a952017-03-29 09:42:32 -070070
ehmaldonado03184632017-03-30 03:33:19 -070071 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
72 # environment. Otherwise it will be picked up by the binary, causing a bug
73 # where only tests in the first shard are executed.
74 test_env = os.environ.copy()
75 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
76 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -070077
ehmaldonado03184632017-03-30 03:33:19 -070078 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
79 gtest_parallel_path = os.path.join(
80 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
81
82 options, unprocessed = parser.parse_known_args()
83 test_executable = unprocessed[0]
84 test_arguments = unprocessed[1:]
85
86 gtest_args = [
87 test_executable,
88 '--shard_count',
89 gtest_total_shards,
90 '--shard_index',
91 gtest_shard_index,
ehmaldonado3ff7a952017-03-29 09:42:32 -070092 ]
93
ehmaldonado03184632017-03-30 03:33:19 -070094 # --isolated-script-test-output is used to upload results to the flakiness
95 # dashboard. This translation is made because gtest-parallel expects the flag
96 # to be called --dump_json_test_results instead.
97 if options.isolated_script_test_output:
98 gtest_args += [
99 '--dump_json_test_results',
100 options.isolated_script_test_output,
101 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700102
ehmaldonado03184632017-03-30 03:33:19 -0700103 if options.output_dir:
104 gtest_args += [
105 '--output_dir',
106 options.output_dir,
107 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700108
ehmaldonado03184632017-03-30 03:33:19 -0700109 if options.timeout:
110 gtest_args += [
111 '--timeout',
112 str(options.timeout),
113 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700114
ehmaldonado03184632017-03-30 03:33:19 -0700115 command = [
116 sys.executable,
117 gtest_parallel_path,
118 ] + gtest_args + ['--'] + test_arguments
ehmaldonado3ff7a952017-03-29 09:42:32 -0700119
ehmaldonado03184632017-03-30 03:33:19 -0700120 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
121 sys.stdout.flush()
122
123 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
124
125 for test_status in 'passed', 'failed', 'interrupted':
126 logs_dir = os.path.join(options.output_dir, test_status)
127 if not os.path.isdir(logs_dir):
128 continue
129 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
130 log_file = os.path.join(options.output_dir, '%s-tests.log' % test_status)
kjellanderdd460e22017-04-12 12:06:13 -0700131 CatFiles(logs, log_file)
ehmaldonado03184632017-03-30 03:33:19 -0700132 os.rmdir(logs_dir)
133
134 return exit_code
135
136
137if __name__ == '__main__':
138 sys.exit(main())