tansell | 0e6baaf2 | 2017-02-16 09:14:53 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Runs a script that can run as an isolate (or not). |
| 7 | |
| 8 | The main requirement is that |
| 9 | |
| 10 | --isolated-script-test-output=[FILENAME] |
| 11 | |
| 12 | is passed on the command line to run_isolated_script_tests. This gets |
| 13 | remapped to the command line argument --write-full-results-to. |
| 14 | |
| 15 | json is written to that file in the format produced by |
| 16 | common.parse_common_test_results. |
| 17 | |
Kenneth Russell | 4027405 | 2017-11-14 00:57:44 | [diff] [blame] | 18 | Optional argument: |
| 19 | |
Kenneth Russell | a649a4612 | 2017-11-21 06:39:59 | [diff] [blame] | 20 | --isolated-script-test-filter=[TEST_NAMES] |
Kenneth Russell | 4027405 | 2017-11-14 00:57:44 | [diff] [blame] | 21 | |
Kenneth Russell | a649a4612 | 2017-11-21 06:39:59 | [diff] [blame] | 22 | is a double-colon-separated ("::") list of test names, to run just that subset |
| 23 | of tests. This list is parsed by this harness and sent down via the --test-list |
| 24 | argument. |
Kenneth Russell | 4027405 | 2017-11-14 00:57:44 | [diff] [blame] | 25 | |
tansell | 0e6baaf2 | 2017-02-16 09:14:53 | [diff] [blame] | 26 | This script is intended to be the base command invoked by the isolate, |
| 27 | followed by a subsequent Python script. It could be generalized to |
| 28 | invoke an arbitrary executable. |
| 29 | """ |
| 30 | |
| 31 | # TODO(tansell): Remove this script once LayoutTests can accept the isolated |
| 32 | # arguments and start xvfb itself. |
| 33 | |
| 34 | import argparse |
| 35 | import json |
| 36 | import os |
| 37 | import pprint |
| 38 | import sys |
Kenneth Russell | a649a4612 | 2017-11-21 06:39:59 | [diff] [blame] | 39 | import tempfile |
tansell | 0e6baaf2 | 2017-02-16 09:14:53 | [diff] [blame] | 40 | |
| 41 | |
| 42 | import common |
| 43 | |
| 44 | # Add src/testing/ into sys.path for importing xvfb. |
| 45 | sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 46 | import xvfb |
| 47 | |
| 48 | |
Kenneth Russell | 1df696c4 | 2019-01-30 02:44:24 | [diff] [blame] | 49 | # Some harnesses understand the --isolated-script-test arguments |
| 50 | # directly and prefer that they be passed through. |
| 51 | KNOWN_ISOLATED_SCRIPT_TEST_RUNNERS = {'run_web_tests.py'} |
| 52 | |
| 53 | |
Stephen Martinis | 9bcb5a3 | 2018-06-21 23:11:31 | [diff] [blame] | 54 | # Known typ test runners this script wraps. They need a different argument name |
| 55 | # when selecting which tests to run. |
| 56 | # TODO(dpranke): Detect if the wrapped test suite uses typ better. |
Robert Sesek | b842129 | 2019-05-06 18:13:27 | [diff] [blame] | 57 | KNOWN_TYP_TEST_RUNNERS = { |
| 58 | 'run_blinkpy_tests.py', |
| 59 | 'metrics_python_tests.py', |
| 60 | 'run_mac_signing_tests.py', |
rbpotter | d8ca34c | 2019-07-25 18:48:05 | [diff] [blame] | 61 | 'run_polymer_tools_tests.py', |
Peter Kotwicz | c10750c | 2020-10-07 20:57:45 | [diff] [blame] | 62 | 'monochrome_python_tests.py', |
Robert Sesek | b842129 | 2019-05-06 18:13:27 | [diff] [blame] | 63 | } |
Stephen Martinis | 9bcb5a3 | 2018-06-21 23:11:31 | [diff] [blame] | 64 | |
| 65 | |
Kenneth Russell | 1df696c4 | 2019-01-30 02:44:24 | [diff] [blame] | 66 | class IsolatedScriptTestAdapter(common.BaseIsolatedScriptArgsAdapter): |
| 67 | def __init__(self): |
| 68 | super(IsolatedScriptTestAdapter, self).__init__() |
| 69 | |
| 70 | def generate_sharding_args(self, total_shards, shard_index): |
| 71 | # This script only uses environment variable for sharding. |
| 72 | del total_shards, shard_index # unused |
| 73 | return [] |
| 74 | |
| 75 | def generate_test_also_run_disabled_tests_args(self): |
| 76 | return ['--isolated-script-test-also-run-disabled-tests'] |
| 77 | |
| 78 | def generate_test_filter_args(self, test_filter_str): |
| 79 | return ['--isolated-script-test-filter=%s' % test_filter_str] |
| 80 | |
| 81 | def generate_test_output_args(self, output): |
| 82 | return ['--isolated-script-test-output=%s' % output] |
| 83 | |
| 84 | def generate_test_launcher_retry_limit_args(self, retry_limit): |
Kenneth Russell | d87758a | 2019-01-31 23:19:32 | [diff] [blame] | 85 | return ['--isolated-script-test-launcher-retry-limit=%d' % retry_limit] |
Kenneth Russell | 1df696c4 | 2019-01-30 02:44:24 | [diff] [blame] | 86 | |
| 87 | def generate_test_repeat_args(self, repeat_count): |
| 88 | return ['--isolated-script-test-repeat=%d' % repeat_count] |
| 89 | |
| 90 | |
Ned Nguyen | 09dcd00 | 2018-10-16 06:01:50 | [diff] [blame] | 91 | class TypUnittestAdapter(common.BaseIsolatedScriptArgsAdapter): |
| 92 | |
| 93 | def __init__(self): |
| 94 | super(TypUnittestAdapter, self).__init__() |
| 95 | self._temp_filter_file = None |
| 96 | |
| 97 | def generate_sharding_args(self, total_shards, shard_index): |
| 98 | # This script only uses environment variable for sharding. |
| 99 | del total_shards, shard_index # unused |
| 100 | return [] |
| 101 | |
| 102 | def generate_test_output_args(self, output): |
| 103 | return ['--write-full-results-to', output] |
| 104 | |
| 105 | def generate_test_filter_args(self, test_filter_str): |
| 106 | filter_list = common.extract_filter_list(test_filter_str) |
| 107 | self._temp_filter_file = tempfile.NamedTemporaryFile( |
| 108 | mode='w', delete=False) |
| 109 | self._temp_filter_file.write('\n'.join(filter_list)) |
| 110 | self._temp_filter_file.close() |
| 111 | arg_name = 'test-list' |
Peter Kotwicz | 72fc99f | 2020-10-06 05:09:24 | [diff] [blame] | 112 | if any(r in self.rest_args[0] for r in KNOWN_TYP_TEST_RUNNERS): |
Ned Nguyen | 09dcd00 | 2018-10-16 06:01:50 | [diff] [blame] | 113 | arg_name = 'file-list' |
| 114 | |
| 115 | return ['--%s=' % arg_name + self._temp_filter_file.name] |
| 116 | |
Ned Nguyen | 68c3498 | 2018-11-01 01:07:58 | [diff] [blame] | 117 | def clean_up_after_test_run(self): |
| 118 | if self._temp_filter_file: |
| 119 | os.unlink(self._temp_filter_file.name) |
| 120 | |
Ned Nguyen | 09dcd00 | 2018-10-16 06:01:50 | [diff] [blame] | 121 | def run_test(self): |
Ned Nguyen | 68c3498 | 2018-11-01 01:07:58 | [diff] [blame] | 122 | return super(TypUnittestAdapter, self).run_test() |
| 123 | |
Ned Nguyen | 09dcd00 | 2018-10-16 06:01:50 | [diff] [blame] | 124 | |
tansell | 0e6baaf2 | 2017-02-16 09:14:53 | [diff] [blame] | 125 | def main(): |
Kenneth Russell | 1df696c4 | 2019-01-30 02:44:24 | [diff] [blame] | 126 | if any(r in sys.argv[1] for r in KNOWN_ISOLATED_SCRIPT_TEST_RUNNERS): |
| 127 | adapter = IsolatedScriptTestAdapter() |
| 128 | else: |
| 129 | adapter = TypUnittestAdapter() |
Stephen Martinis | 0865005 | 2018-10-22 18:50:37 | [diff] [blame] | 130 | return adapter.run_test() |
tansell | 0e6baaf2 | 2017-02-16 09:14:53 | [diff] [blame] | 131 | |
| 132 | # This is not really a "script test" so does not need to manually add |
| 133 | # any additional compile targets. |
| 134 | def main_compile_targets(args): |
| 135 | json.dump([], args.output) |
| 136 | |
| 137 | |
| 138 | if __name__ == '__main__': |
| 139 | # Conform minimally to the protocol defined by ScriptTest. |
| 140 | if 'compile_targets' in sys.argv: |
| 141 | funcs = { |
| 142 | 'run': None, |
| 143 | 'compile_targets': main_compile_targets, |
| 144 | } |
| 145 | sys.exit(common.run_script(sys.argv[1:], funcs)) |
| 146 | sys.exit(main()) |