blob: c3a0649d03fa6e67d56be25c56bf34e9b6ec7618 [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.
27For example:
ehmaldonado3ff7a952017-03-29 09:42:32 -070028
29 gtest-parallel-wrapper.py some_test \
ehmaldonado76e60e92017-05-04 06:18:26 -070030 --some_flag=some_value \
31 --another_flag \
ehmaldonado3ff7a952017-03-29 09:42:32 -070032 -- \
ehmaldonado76e60e92017-05-04 06:18:26 -070033 --isolated-script-test-output=some_dir \
34 --isolated-script-test-chartjson-output=some_other_dir \
35 --foo=bar \
36 --baz
ehmaldonado3ff7a952017-03-29 09:42:32 -070037
ehmaldonado76e60e92017-05-04 06:18:26 -070038Will be converted into:
ehmaldonado3ff7a952017-03-29 09:42:32 -070039
40 python gtest-parallel some_test \
41 --shard_count 1 \
42 --shard_index 0 \
ehmaldonado76e60e92017-05-04 06:18:26 -070043 --some_flag=some_value \
44 --another_flag \
45 --dump_json_test_results=some_dir \
ehmaldonado3ff7a952017-03-29 09:42:32 -070046 -- \
ehmaldonado76e60e92017-05-04 06:18:26 -070047 --foo=bar
48 --baz
49
ehmaldonado3ff7a952017-03-29 09:42:32 -070050"""
51
52import argparse
53import os
54import subprocess
55import sys
56
ehmaldonado3ff7a952017-03-29 09:42:32 -070057
kjellanderdd460e22017-04-12 12:06:13 -070058def CatFiles(file_list, output_file):
ehmaldonado03184632017-03-30 03:33:19 -070059 with open(output_file, 'w') as output_file:
60 for filename in file_list:
61 with open(filename) as input_file:
62 output_file.write(input_file.read())
63 os.remove(filename)
ehmaldonado3ff7a952017-03-29 09:42:32 -070064
ehmaldonado3ff7a952017-03-29 09:42:32 -070065
ehmaldonado76e60e92017-05-04 06:18:26 -070066def get_args_and_env():
67 if '--' not in sys.argv:
68 return sys.argv, os.environ
69
70 argv_index = sys.argv.index('--')
71
72 gtest_parallel_args = sys.argv[1:argv_index]
73 executable_args = sys.argv[argv_index + 1:]
ehmaldonado3ff7a952017-03-29 09:42:32 -070074
ehmaldonado03184632017-03-30 03:33:19 -070075 parser = argparse.ArgumentParser()
76 parser.add_argument('--isolated-script-test-output', type=str, default=None)
kjellander382f2b22017-04-11 04:07:01 -070077
ehmaldonado950614e2017-05-02 05:52:57 -070078 # We don't need to implement this flag, and possibly can't, since it's
79 # intended for results of Telemetry tests. See
80 # https://chromium.googlesource.com/external/github.com/catapult-project/catapult/+/HEAD/dashboard/docs/data-format.md
kjellander382f2b22017-04-11 04:07:01 -070081 parser.add_argument('--isolated-script-test-chartjson-output', type=str,
82 default=None)
83
ehmaldonado76e60e92017-05-04 06:18:26 -070084 # We have to do this, since --isolated-script-test-output is passed as an
85 # argument to the executable by the swarming scripts, and we want to pass it
86 # to gtest-parallel instead.
87 options, executable_args = parser.parse_known_args(executable_args)
88
89 # --isolated-script-test-output is used to upload results to the flakiness
90 # dashboard. This translation is made because gtest-parallel expects the flag
91 # to be called --dump_json_test_results instead.
92 if options.isolated_script_test_output:
93 gtest_parallel_args += [
94 '--dump_json_test_results',
95 options.isolated_script_test_output,
96 ]
ehmaldonado3ff7a952017-03-29 09:42:32 -070097
ehmaldonado03184632017-03-30 03:33:19 -070098 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the
99 # environment. Otherwise it will be picked up by the binary, causing a bug
100 # where only tests in the first shard are executed.
101 test_env = os.environ.copy()
102 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0')
103 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1')
ehmaldonado3ff7a952017-03-29 09:42:32 -0700104
ehmaldonado76e60e92017-05-04 06:18:26 -0700105 gtest_parallel_args += [
ehmaldonado03184632017-03-30 03:33:19 -0700106 '--shard_count',
107 gtest_total_shards,
108 '--shard_index',
109 gtest_shard_index,
ehmaldonado76e60e92017-05-04 06:18:26 -0700110 ] + ['--'] + executable_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700111
ehmaldonado76e60e92017-05-04 06:18:26 -0700112 return gtest_parallel_args, test_env
ehmaldonado3ff7a952017-03-29 09:42:32 -0700113
ehmaldonado3ff7a952017-03-29 09:42:32 -0700114
ehmaldonado76e60e92017-05-04 06:18:26 -0700115def get_output_dir(gtest_parallel_args):
116 parser = argparse.ArgumentParser()
117 parser.add_argument('--output_dir', type=str, default=None)
118 options, _ = parser.parse_known_args(gtest_parallel_args)
119 return options.output_dir
120
121
122def main():
123 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
124 gtest_parallel_path = os.path.join(
125 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel')
126
127 gtest_parallel_args, test_env = get_args_and_env()
ehmaldonado3ff7a952017-03-29 09:42:32 -0700128
ehmaldonado03184632017-03-30 03:33:19 -0700129 command = [
130 sys.executable,
131 gtest_parallel_path,
ehmaldonado76e60e92017-05-04 06:18:26 -0700132 ] + gtest_parallel_args
ehmaldonado3ff7a952017-03-29 09:42:32 -0700133
ehmaldonado03184632017-03-30 03:33:19 -0700134 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command)
135 sys.stdout.flush()
136
137 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd())
138
ehmaldonado76e60e92017-05-04 06:18:26 -0700139 output_dir = get_output_dir(gtest_parallel_args)
140 if output_dir:
ehmaldonado950614e2017-05-02 05:52:57 -0700141 for test_status in 'passed', 'failed', 'interrupted':
ehmaldonado76e60e92017-05-04 06:18:26 -0700142 logs_dir = os.path.join(output_dir, test_status)
ehmaldonado950614e2017-05-02 05:52:57 -0700143 if not os.path.isdir(logs_dir):
144 continue
145 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)]
ehmaldonado76e60e92017-05-04 06:18:26 -0700146 log_file = os.path.join(output_dir, '%s-tests.log' % test_status)
ehmaldonado950614e2017-05-02 05:52:57 -0700147 CatFiles(logs, log_file)
148 os.rmdir(logs_dir)
ehmaldonado03184632017-03-30 03:33:19 -0700149
150 return exit_code
151
152
153if __name__ == '__main__':
154 sys.exit(main())