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 | |
| 45 | # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the environment |
| 46 | # otherwise it will be picked up by the binary, causing a bug where only tests |
| 47 | # in the firsh shard are executed. |
| 48 | test_env = os.environ.copy() |
| 49 | gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') |
| 50 | gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') |
| 51 | |
| 52 | webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 53 | gtest_parallel_path = os.path.join(webrtc_root, 'third_party', 'gtest-parallel', |
| 54 | 'gtest-parallel') |
| 55 | |
| 56 | # Ignore '--'. Options unprocessed by this script will be passed to the test as |
| 57 | # arguments. |
| 58 | if '--' in sys.argv: |
| 59 | del sys.argv[sys.argv.index('--')] |
| 60 | |
| 61 | parser = argparse.ArgumentParser() |
| 62 | parser.add_argument('--isolated-script-test-output', type=str, default=None) |
| 63 | parser.add_argument('--output_dir', type=str, default=None) |
| 64 | parser.add_argument('--timeout', type=int, default=None) |
| 65 | |
| 66 | options, unprocessed = parser.parse_known_args() |
| 67 | test_executable = unprocessed[0] |
| 68 | test_arguments = unprocessed[1:] |
| 69 | |
| 70 | gtest_args = [ |
| 71 | test_executable, |
| 72 | '--shard_count', |
| 73 | gtest_total_shards, |
| 74 | '--shard_index', |
| 75 | gtest_shard_index, |
| 76 | ] |
| 77 | |
| 78 | # --isolated-script-test-output is used to upload results to the flakiness |
| 79 | # dashboard. This translation is made because gtest-parallel expects the flag to |
| 80 | # be called --dump_json_test_results instead. |
| 81 | if options.isolated_script_test_output: |
| 82 | gtest_args += [ |
| 83 | '--dump_json_test_results', |
| 84 | options.isolated_script_test_output, |
| 85 | ] |
| 86 | |
| 87 | if options.output_dir: |
| 88 | gtest_args += [ |
| 89 | '--output_dir', |
| 90 | options.output_dir, |
| 91 | ] |
| 92 | |
| 93 | if options.timeout: |
| 94 | gtest_args += [ |
| 95 | '--timeout', |
| 96 | str(options.timeout), |
| 97 | ] |
| 98 | |
| 99 | command = [ |
| 100 | sys.executable, |
| 101 | gtest_parallel_path, |
| 102 | ] + gtest_args + ['--'] + test_arguments |
| 103 | |
| 104 | print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) |
| 105 | sys.stdout.flush() |
| 106 | |
| 107 | sys.exit(subprocess.call(command, env=test_env, cwd=os.getcwd())) |