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. |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 27 | |
| 28 | If the --store-test-artifacts flag is set, an --output_dir must be also |
| 29 | specified. |
| 30 | The test artifacts will then be stored in a 'test_artifacts' subdirectory of the |
| 31 | output dir, and will be compressed into a zip file once the test finishes |
| 32 | executing. |
| 33 | This is useful when running the tests in swarming, since the output directory |
| 34 | is not known beforehand. |
| 35 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 36 | For example: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 37 | |
| 38 | gtest-parallel-wrapper.py some_test \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 39 | --some_flag=some_value \ |
| 40 | --another_flag \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 41 | --output_dir=SOME_OUTPUT_DIR |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 42 | -- \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 43 | --store-test-artifacts |
| 44 | --isolated-script-test-output=SOME_DIR \ |
| 45 | --isolated-script-test-perf-output=SOME_OTHER_DIR \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 46 | --foo=bar \ |
| 47 | --baz |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 48 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 49 | Will be converted into: |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 50 | |
| 51 | python gtest-parallel some_test \ |
| 52 | --shard_count 1 \ |
| 53 | --shard_index 0 \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 54 | --some_flag=some_value \ |
| 55 | --another_flag \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 56 | --output_dir=SOME_OUTPUT_DIR \ |
| 57 | --dump_json_test_results=SOME_DIR \ |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 58 | -- \ |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 59 | --test_artifacts_dir=SOME_OUTPUT_DIR/test_artifacts \ |
| 60 | --foo=bar \ |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 61 | --baz |
| 62 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 63 | """ |
| 64 | |
| 65 | import argparse |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 66 | import collections |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 67 | import os |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 68 | import shutil |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 69 | import subprocess |
| 70 | import sys |
| 71 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 72 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 73 | Args = collections.namedtuple('Args', |
| 74 | ['gtest_parallel_args', 'test_env', 'output_dir', |
| 75 | 'test_artifacts_dir']) |
| 76 | |
| 77 | |
| 78 | def _CatFiles(file_list, output_file): |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 79 | with open(output_file, 'w') as output_file: |
| 80 | for filename in file_list: |
| 81 | with open(filename) as input_file: |
| 82 | output_file.write(input_file.read()) |
| 83 | os.remove(filename) |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 84 | |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 85 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 86 | def _GetOutputDir(gtest_parallel_args): |
| 87 | parser = argparse.ArgumentParser() |
| 88 | parser.add_argument('--output_dir', type=str, default=None) |
| 89 | options, _ = parser.parse_known_args(gtest_parallel_args) |
| 90 | return options.output_dir |
| 91 | |
| 92 | |
| 93 | def _ParseArgs(): |
ehmaldonado | f549dff | 2017-07-18 08:16:18 -0700 | [diff] [blame] | 94 | if '--' in sys.argv: |
| 95 | argv_index = sys.argv.index('--') |
| 96 | else: |
| 97 | argv_index = len(sys.argv) |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 98 | |
| 99 | gtest_parallel_args = sys.argv[1:argv_index] |
| 100 | executable_args = sys.argv[argv_index + 1:] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 101 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 102 | parser = argparse.ArgumentParser() |
| 103 | parser.add_argument('--isolated-script-test-output', type=str, default=None) |
kjellander | 382f2b2 | 2017-04-11 04:07:01 -0700 | [diff] [blame] | 104 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 105 | # Needed when the test wants to store test artifacts, because it doesn't know |
| 106 | # what will be the swarming output dir. |
| 107 | parser.add_argument('--store-test-artifacts', action='store_true', |
| 108 | default=False) |
| 109 | |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 110 | # We don't need to implement this flag, and possibly can't, since it's |
| 111 | # intended for results of Telemetry tests. See |
| 112 | # 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] | 113 | parser.add_argument('--isolated-script-test-perf-output', type=str, |
kjellander | 382f2b2 | 2017-04-11 04:07:01 -0700 | [diff] [blame] | 114 | default=None) |
| 115 | |
oprypin | 51d49b4 | 2017-08-22 10:55:47 -0700 | [diff] [blame] | 116 | # No-sandbox is a Chromium-specific flag, ignore it. |
| 117 | # TODO(oprypin): Remove (bugs.webrtc.org/8115) |
| 118 | parser.add_argument('--no-sandbox', action='store_true', default=False) |
| 119 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 120 | # We have to do this, since --isolated-script-test-output is passed as an |
| 121 | # argument to the executable by the swarming scripts, and we want to pass it |
| 122 | # to gtest-parallel instead. |
| 123 | options, executable_args = parser.parse_known_args(executable_args) |
| 124 | |
| 125 | # --isolated-script-test-output is used to upload results to the flakiness |
| 126 | # dashboard. This translation is made because gtest-parallel expects the flag |
| 127 | # to be called --dump_json_test_results instead. |
| 128 | if options.isolated_script_test_output: |
| 129 | gtest_parallel_args += [ |
| 130 | '--dump_json_test_results', |
| 131 | options.isolated_script_test_output, |
| 132 | ] |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 133 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 134 | output_dir = _GetOutputDir(gtest_parallel_args) |
| 135 | test_artifacts_dir = None |
| 136 | |
| 137 | if options.store_test_artifacts: |
| 138 | assert output_dir, ( |
| 139 | '--output_dir must be specified for storing test artifacts.') |
| 140 | test_artifacts_dir = os.path.join(output_dir, 'test_artifacts') |
| 141 | if not os.path.isdir(test_artifacts_dir): |
| 142 | os.makedirs(test_artifacts_dir) |
| 143 | |
| 144 | executable_args += [ |
| 145 | '--test_artifacts_dir', |
| 146 | test_artifacts_dir, |
| 147 | ] |
| 148 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 149 | # GTEST_SHARD_INDEX and GTEST_TOTAL_SHARDS must be removed from the |
| 150 | # environment. Otherwise it will be picked up by the binary, causing a bug |
| 151 | # where only tests in the first shard are executed. |
| 152 | test_env = os.environ.copy() |
| 153 | gtest_shard_index = test_env.pop('GTEST_SHARD_INDEX', '0') |
| 154 | gtest_total_shards = test_env.pop('GTEST_TOTAL_SHARDS', '1') |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 155 | |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 156 | gtest_parallel_args += [ |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 157 | '--shard_count', |
| 158 | gtest_total_shards, |
| 159 | '--shard_index', |
| 160 | gtest_shard_index, |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 161 | ] + ['--'] + executable_args |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 162 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 163 | return Args(gtest_parallel_args, test_env, output_dir, test_artifacts_dir) |
ehmaldonado | 76e60e9 | 2017-05-04 06:18:26 -0700 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def main(): |
| 167 | webrtc_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 168 | gtest_parallel_path = os.path.join( |
| 169 | webrtc_root, 'third_party', 'gtest-parallel', 'gtest-parallel') |
| 170 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 171 | args = _ParseArgs() |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 172 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 173 | command = [ |
| 174 | sys.executable, |
| 175 | gtest_parallel_path, |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 176 | ] + args.gtest_parallel_args |
ehmaldonado | 3ff7a95 | 2017-03-29 09:42:32 -0700 | [diff] [blame] | 177 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 178 | print 'gtest-parallel-wrapper: Executing command %s' % ' '.join(command) |
| 179 | sys.stdout.flush() |
| 180 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 181 | exit_code = subprocess.call(command, env=args.test_env, cwd=os.getcwd()) |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 182 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 183 | if args.output_dir: |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 184 | for test_status in 'passed', 'failed', 'interrupted': |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 185 | logs_dir = os.path.join(args.output_dir, 'gtest-parallel-logs', |
| 186 | test_status) |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 187 | if not os.path.isdir(logs_dir): |
| 188 | continue |
| 189 | logs = [os.path.join(logs_dir, log) for log in os.listdir(logs_dir)] |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 190 | log_file = os.path.join(args.output_dir, '%s-tests.log' % test_status) |
| 191 | _CatFiles(logs, log_file) |
ehmaldonado | 950614e | 2017-05-02 05:52:57 -0700 | [diff] [blame] | 192 | os.rmdir(logs_dir) |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 193 | |
Edward Lemur | c2b6cf3 | 2017-10-10 14:00:35 +0200 | [diff] [blame] | 194 | if args.test_artifacts_dir: |
| 195 | shutil.make_archive(args.test_artifacts_dir, 'zip', args.test_artifacts_dir) |
| 196 | shutil.rmtree(args.test_artifacts_dir) |
| 197 | |
ehmaldonado | 0318463 | 2017-03-30 03:33:19 -0700 | [diff] [blame] | 198 | return exit_code |
| 199 | |
| 200 | |
| 201 | if __name__ == '__main__': |
| 202 | sys.exit(main()) |