blob: 0a31f63a729ae59530990c6943d2035b0e1347fa [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)
62 parser.add_argument('--output_dir', type=str, default=None)
63 parser.add_argument('--timeout', type=int, default=None)
ehmaldonado3ff7a952017-03-29 09:42:32 -070064
ehmaldonado03184632017-03-30 03:33:19 -070065 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
66 # environment. Otherwise it will be picked up by the binary, causing a bug
67 # where only tests in the first shard are executed.
68 test_env = os.environ.copy()
69 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
70 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -070071
ehmaldonado03184632017-03-30 03:33:19 -070072 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
73 gtest_parallel_path = os.path.join(
74 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
75
76 options, unprocessed = parser.parse_known_args()
77 test_executable = unprocessed[0]
78 test_arguments = unprocessed[1:]
79
80 gtest_args = [
81 test_executable,
82 '--shard_count',
83 gtest_total_shards,
84 '--shard_index',
85 gtest_shard_index,
ehmaldonado3ff7a952017-03-29 09:42:32 -070086 ]
87
ehmaldonado03184632017-03-30 03:33:19 -070088 # --isolated-script-test-output is used to upload results to the flakiness
89 # dashboard. This translation is made because gtest-parallel expects the flag
90 # to be called --dump_json_test_results instead.
91 if options.isolated_script_test_output:
92 gtest_args += [
93 '--dump_json_test_results',
94 options.isolated_script_test_output,
95 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -070096
ehmaldonado03184632017-03-30 03:33:19 -070097 if options.output_dir:
98 gtest_args += [
99 '--output_dir',
100 options.output_dir,
101 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700102
ehmaldonado03184632017-03-30 03:33:19 -0700103 if options.timeout:
104 gtest_args += [
105 '--timeout',
106 str(options.timeout),
107 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -0700108
ehmaldonado03184632017-03-30 03:33:19 -0700109 command = [
110 sys.executable,
111 gtest_parallel_path,
112 ] + gtest_args + ['--'] + test_arguments
ehmaldonado3ff7a952017-03-29 09:42:32 -0700113
ehmaldonado03184632017-03-30 03:33:19 -0700114 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
115 sys.stdout.flush()
116
117 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
118
119 for test_status in 'passed', 'failed', 'interrupted':
120 logs_dir = os.path.join(options.output_dir, test_status)
121 if not os.path.isdir(logs_dir):
122 continue
123 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
124 log_file = os.path.join(options.output_dir, '%s-tests.log' % test_status)
125 cat_files(logs, log_file)
126 os.rmdir(logs_dir)
127
128 return exit_code
129
130
131if __name__ == '__main__':
132 sys.exit(main())