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 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 11 | # pylint: disable=invalid-name |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 12 | """ |
| 13 | This script acts as an interface between the Chromium infrastructure and |
| 14 | gtest-parallel, renaming options and translating environment variables into |
| 15 | flags. Developers should execute gtest-parallel directly. |
| 16 | |
| 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 |
| 19 | the --isolated-script-test-output flag to --dump_json_test_results. |
| 20 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 21 | All flags before '--' will be passed as arguments to gtest-parallel, and |
| 22 | (almost) all flags after '--' will be passed as arguments to the test |
| 23 | executable. |
| 24 | The exception is that --isolated-script-test-output and |
| 25 | --isolated-script-test-chartson-output are expected to be after '--', so they |
| 26 | are processed and removed from there. |
| 27 | For example: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 28 | |
| 29 | gtest-parallel-wrapper.py some_test \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 30 | --some_flag=some_value \ |
| 31 | --another_flag \ |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 32 | -- \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 33 | --isolated-script-test-output=some_dir \ |
ehmaldonado | c4eee32 | 2017-09-07 00:34:17 -0700 | [diff] [blame] | 34 | --isolated-script-test-perf-output=some_other_dir \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 35 | --foo=bar \ |
| 36 | --baz |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 37 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 38 | Will be converted into: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 39 | |
| 40 | python gtest-parallel some_test \ |
| 41 | --shard_count 1 \ |
| 42 | --shard_index 0 \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 43 | --some_flag=some_value \ |
| 44 | --another_flag \ |
| 45 | --dump_json_test_results=some_dir \ |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 46 | -- \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 47 | --foo=bar |
| 48 | --baz |
| 49 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 50 | """ |
| 51 | |
| 52 | import argparse |
| 53 | import os |
| 54 | import subprocess |
| 55 | import sys |
| 56 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 57 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 58 | def CatFiles(file_list, output_file): |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 59 | 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) |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 64 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 65 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 66 | def get_args_and_env(): |
ehmaldonado | f549dff | 2017-07-18 08:16:18 -0700 | [diff] [blame] | 67 | if '--' in sys.argv: |
| 68 | argv_index = sys.argv.index('--') |
| 69 | else: |
| 70 | argv_index = len(sys.argv) |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 71 | |
| 72 | gtest_parallel_args = sys.argv[1:argv_index] |
| 73 | executable_args = sys.argv[argv_index + 1:] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 74 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 75 | parser = argparse.ArgumentParser() |
| 76 | parser.add_argument('--isolated-script-test-output', type=str, default=None) |
kjellander | 382f2b2 | 2017-04-11 04:07:01 -0700 | [diff] [blame] | 77 | |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 78 | # 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 |
ehmaldonado | c4eee32 | 2017-09-07 00:34:17 -0700 | [diff] [blame] | 81 | parser.add_argument('--isolated-script-test-perf-output', type=str, |
kjellander | 382f2b2 | 2017-04-11 04:07:01 -0700 | [diff] [blame] | 82 | default=None) |
| 83 | |
oprypin | 51d49b4 | 2017-08-22 10:55:47 -0700 | [diff] [blame] | 84 | # No-sandbox is a Chromium-specific flag, ignore it. |
| 85 | # TODO(oprypin): Remove (bugs.webrtc.org/8115) |
| 86 | parser.add_argument('--no-sandbox', action='store_true', default=False) |
| 87 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 88 | # We have to do this, since --isolated-script-test-output is passed as an |
| 89 | # argument to the executable by the swarming scripts, and we want to pass it |
| 90 | # to gtest-parallel instead. |
| 91 | options, executable_args = parser.parse_known_args(executable_args) |
| 92 | |
| 93 | # --isolated-script-test-output is used to upload results to the flakiness |
| 94 | # dashboard. This translation is made because gtest-parallel expects the flag |
| 95 | # to be called --dump_json_test_results instead. |
| 96 | if options.isolated_script_test_output: |
| 97 | gtest_parallel_args += [ |
| 98 | '--dump_json_test_results', |
| 99 | options.isolated_script_test_output, |
| 100 | ] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 101 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 102 | # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the |
| 103 | # environment. Otherwise it will be picked up by the binary, causing a bug |
| 104 | # where only tests in the first shard are executed. |
| 105 | test_env = os.environ.copy() |
| 106 | gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') |
| 107 | gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 108 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 109 | gtest_parallel_args += [ |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 110 | '--shard_count', |
| 111 | gtest_total_shards, |
| 112 | '--shard_index', |
| 113 | gtest_shard_index, |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 114 | ] + ['--'] + executable_args |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 115 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 116 | return gtest_parallel_args, test_env |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 117 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 118 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 119 | def get_output_dir(gtest_parallel_args): |
| 120 | parser = argparse.ArgumentParser() |
| 121 | parser.add_argument('--output_dir', type=str, default=None) |
| 122 | options, _ = parser.parse_known_args(gtest_parallel_args) |
| 123 | return options.output_dir |
| 124 | |
| 125 | |
| 126 | def main(): |
| 127 | webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 128 | gtest_parallel_path = os.path.join( |
| 129 | webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel') |
| 130 | |
| 131 | gtest_parallel_args, test_env = get_args_and_env() |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 132 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 133 | command = [ |
| 134 | sys.executable, |
| 135 | gtest_parallel_path, |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 136 | ] + gtest_parallel_args |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 137 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 138 | print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) |
| 139 | sys.stdout.flush() |
| 140 | |
| 141 | exit_code = subprocess.call(command, env=test_env, cwd=os.getcwd()) |
| 142 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 143 | output_dir = get_output_dir(gtest_parallel_args) |
| 144 | if output_dir: |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 145 | for test_status in 'passed', 'failed', 'interrupted': |
ehmaldonado | 6e6289d | 2017-09-08 07:25:51 -0700 | [diff] [blame] | 146 | logs_dir = os.path.join(output_dir, 'gtest-parallel-logs', test_status) |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 147 | if not os.path.isdir(logs_dir): |
| 148 | continue |
| 149 | logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)] |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 150 | log_file = os.path.join(output_dir, '%s-tests.log' % test_status) |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 151 | CatFiles(logs, log_file) |
| 152 | os.rmdir(logs_dir) |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 153 | |
| 154 | return exit_code |
| 155 | |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | sys.exit(main()) |