ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 1 | #!/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 | """ |
| 12 | This script acts as an interface between the Chromium infrastructure and |
| 13 | gtest-parallel, renaming options and translating environment variables into |
| 14 | flags. Developers should execute gtest-parallel directly. |
| 15 | |
| 16 | In particular, this translates the GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS |
| 17 | environment variables to the --shard_index and --shard_count flags, and renames |
| 18 | the --isolated-script-test-output flag to --dump_json_test_results. |
| 19 | |
| 20 | Note that the flags unprocessed by this script will passed as arguments to the |
| 21 | test 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 | |
| 29 | will 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 | |
| 40 | import argparse |
| 41 | import os |
| 42 | import subprocess |
| 43 | import sys |
| 44 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 45 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 46 | def 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) |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 52 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 53 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 54 | def 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('--')] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 59 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 60 | 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) |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 64 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 65 | # 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') |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 71 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 72 | 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, |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 86 | ] |
| 87 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 88 | # --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 | ] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 96 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 97 | if options.output_dir: |
| 98 | gtest_args += [ |
| 99 | '--output_dir', |
| 100 | options.output_dir, |
| 101 | ] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 102 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 103 | if options.timeout: |
| 104 | gtest_args += [ |
| 105 | '--timeout', |
| 106 | str(options.timeout), |
| 107 | ] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 108 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 109 | command = [ |
| 110 | sys.executable, |
| 111 | gtest_parallel_path, |
| 112 | ] + gtest_args + ['--'] + test_arguments |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 113 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 114 | 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 | |
| 131 | if __name__ == '__main__': |
| 132 | sys.exit(main()) |