OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 3 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
4 # | 4 # |
5 # Use of this source code is governed by a BSD-style license | 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 | 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 | 7 # tree. An additional intellectual property rights grant can be found |
8 # in the file PATENTS. All contributing project authors may | 8 # in the file PATENTS. All contributing project authors may |
9 # be found in the AUTHORS file in the root of the source tree. | 9 # be found in the AUTHORS file in the root of the source tree. |
10 | 10 |
11 # pylint: disable=invalid-name | 11 # pylint: disable=invalid-name |
12 """ | 12 """ |
13 This script acts as an interface between the Chromium infrastructure and | 13 This script acts as an interface between the Chromium infrastructure and |
14 gtest-parallel, renaming options and translating environment variables into | 14 gtest-parallel, renaming options and translating environment variables into |
15 flags. Developers should execute gtest-parallel directly. | 15 flags. Developers should execute gtest-parallel directly. |
16 | 16 |
17 In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS | 17 In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS |
18 environment variables to the --shard_index and --shard_count flags, and renames | 18 environment variables to the --shard_index and --shard_count flags, and renames |
19 the --isolated-script-test-output flag to --dump_json_test_results. | 19 the --isolated-script-test-output flag to --dump_json_test_results. |
20 | |
21 Note that the flags unprocessed by this script will passed as arguments to the | |
kjellander_webrtc
2017/05/04 09:30:06
I think we need to explain how flags are handled n
| |
22 test 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 | |
30 will 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 """ | 20 """ |
40 | 21 |
41 import argparse | 22 import argparse |
42 import os | 23 import os |
43 import subprocess | 24 import subprocess |
44 import sys | 25 import sys |
45 | 26 |
46 | 27 |
47 def CatFiles(file_list, output_file): | 28 def CatFiles(file_list, output_file): |
48 with open(output_file, 'w') as output_file: | 29 with open(output_file, 'w') as output_file: |
49 for filename in file_list: | 30 for filename in file_list: |
50 with open(filename) as input_file: | 31 with open(filename) as input_file: |
51 output_file.write(input_file.read()) | 32 output_file.write(input_file.read()) |
52 os.remove(filename) | 33 os.remove(filename) |
53 | 34 |
54 | 35 |
55 def main(): | 36 def get_args_and_env(): |
56 # Ignore '--'. Options unprocessed by this script will be passed to the test | 37 if '--' not in sys.argv: |
57 # as arguments. | 38 return sys.argv, os.environ |
58 if '--' in sys.argv: | 39 |
59 del sys.argv[sys.argv.index('--')] | 40 argv_index = sys.argv.index('--') |
41 | |
42 gtest_parallel_args = sys.argv[1:argv_index] | |
43 executable_args = sys.argv[argv_index + 1:] | |
60 | 44 |
61 parser = argparse.ArgumentParser() | 45 parser = argparse.ArgumentParser() |
62 parser.add_argument('--isolated-script-test-output', type=str, default=None) | 46 parser.add_argument('--isolated-script-test-output', type=str, default=None) |
63 | 47 |
64 # We don't need to implement this flag, and possibly can't, since it's | 48 # We don't need to implement this flag, and possibly can't, since it's |
65 # intended for results of Telemetry tests. See | 49 # intended for results of Telemetry tests. See |
66 # https://chromium.googlesource.com/external/github.com/catapult-project/catap ult/+/HEAD/dashboard/docs/data-format.md | 50 # https://chromium.googlesource.com/external/github.com/catapult-project/catap ult/+/HEAD/dashboard/docs/data-format.md |
67 parser.add_argument('--isolated-script-test-chartjson-output', type=str, | 51 parser.add_argument('--isolated-script-test-chartjson-output', type=str, |
68 default=None) | 52 default=None) |
69 | 53 |
70 # TODO(ehmaldonado): Figure out a way to avoid duplicating the flags in | 54 # We have to do this, since --isolated-script-test-output is passed as an |
71 # gtest-parallel. | 55 # argument to the executable by the swarming scripts, and we want to pass it |
72 parser.add_argument('--gtest_color', type=str, default='auto') | 56 # to gtest-parallel instead. |
73 parser.add_argument('--output_dir', type=str, default=None) | 57 options, executable_args = parser.parse_known_args(executable_args) |
74 parser.add_argument('--timeout', type=int, default=None) | 58 |
59 # --isolated-script-test-output is used to upload results to the flakiness | |
60 # dashboard. This translation is made because gtest-parallel expects the flag | |
61 # to be called --dump_json_test_results instead. | |
62 if options.isolated_script_test_output: | |
63 gtest_parallel_args += [ | |
64 '--dump_json_test_results', | |
65 options.isolated_script_test_output, | |
66 ] | |
75 | 67 |
76 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the | 68 # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the |
77 # environment. Otherwise it will be picked up by the binary, causing a bug | 69 # environment. Otherwise it will be picked up by the binary, causing a bug |
78 # where only tests in the first shard are executed. | 70 # where only tests in the first shard are executed. |
79 test_env = os.environ.copy() | 71 test_env = os.environ.copy() |
80 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') | 72 gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') |
81 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') | 73 gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') |
82 | 74 |
75 gtest_parallel_args += [ | |
76 '--shard_count', | |
77 gtest_total_shards, | |
78 '--shard_index', | |
79 gtest_shard_index, | |
80 ] + ['--'] + executable_args | |
81 | |
82 return gtest_parallel_args, test_env | |
83 | |
84 | |
85 def get_output_dir(gtest_parallel_args): | |
86 parser = argparse.ArgumentParser() | |
87 parser.add_argument('--output_dir', type=str, default=None) | |
88 options, _ = parser.parse_known_args(gtest_parallel_args) | |
89 return options.output_dir | |
90 | |
91 | |
92 def main(): | |
83 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 93 webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
84 gtest_parallel_path = os.path.join( | 94 gtest_parallel_path = os.path.join( |
85 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel') | 95 webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel') |
86 | 96 |
87 options, unprocessed = parser.parse_known_args() | 97 gtest_parallel_args, test_env = get_args_and_env() |
88 test_executable = unprocessed[0] | |
89 test_arguments = unprocessed[1:] | |
90 | |
91 gtest_args = [ | |
92 test_executable, | |
93 '--shard_count', | |
94 gtest_total_shards, | |
95 '--shard_index', | |
96 gtest_shard_index, | |
97 '--gtest_color', | |
98 options.gtest_color, | |
99 ] | |
100 | |
101 # --isolated-script-test-output is used to upload results to the flakiness | |
102 # dashboard. This translation is made because gtest-parallel expects the flag | |
103 # to be called --dump_json_test_results instead. | |
104 if options.isolated_script_test_output: | |
105 gtest_args += [ | |
106 '--dump_json_test_results', | |
107 options.isolated_script_test_output, | |
108 ] | |
109 | |
110 if options.output_dir: | |
111 gtest_args += [ | |
112 '--output_dir', | |
113 options.output_dir, | |
114 ] | |
115 | |
116 if options.timeout: | |
117 gtest_args += [ | |
118 '--timeout', | |
119 str(options.timeout), | |
120 ] | |
121 | 98 |
122 command = [ | 99 command = [ |
123 sys.executable, | 100 sys.executable, |
124 gtest_parallel_path, | 101 gtest_parallel_path, |
125 ] + gtest_args + ['--'] + test_arguments | 102 ] + gtest_parallel_args |
126 | 103 |
127 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) | 104 print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) |
128 sys.stdout.flush() | 105 sys.stdout.flush() |
129 | 106 |
130 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd()) | 107 exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd()) |
131 | 108 |
132 if options.output_dir: | 109 output_dir = get_output_dir(gtest_parallel_args) |
110 if output_dir: | |
133 for test_status in 'passed', 'failed', 'interrupted': | 111 for test_status in 'passed', 'failed', 'interrupted': |
134 logs_dir = os.path.join(options.output_dir, test_status) | 112 logs_dir = os.path.join(output_dir, test_status) |
135 if not os.path.isdir(logs_dir): | 113 if not os.path.isdir(logs_dir): |
136 continue | 114 continue |
137 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)] | 115 logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)] |
138 log_file = os.path.join(options.output_dir, '%s-tests.log' % test_status) | 116 log_file = os.path.join(output_dir, '%s-tests.log' % test_status) |
139 CatFiles(logs, log_file) | 117 CatFiles(logs, log_file) |
140 os.rmdir(logs_dir) | 118 os.rmdir(logs_dir) |
141 | 119 |
142 return exit_code | 120 return exit_code |
143 | 121 |
144 | 122 |
145 if __name__ == '__main__': | 123 if __name__ == '__main__': |
146 sys.exit(main()) | 124 sys.exit(main()) |
OLD | NEW |