[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 7 | """Runs all types of tests from one unified interface.""" |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 8 | |
| 9 | import collections |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 10 | import logging |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 11 | import optparse |
| 12 | import os |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 13 | import shutil |
[email protected] | 83bb815 | 2013-11-19 15:02:21 | [diff] [blame] | 14 | import signal |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 15 | import sys |
[email protected] | 83bb815 | 2013-11-19 15:02:21 | [diff] [blame] | 16 | import threading |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 17 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 18 | from pylib import android_commands |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 19 | from pylib import constants |
[email protected] | c0662e09 | 2013-11-12 11:51:25 | [diff] [blame] | 20 | from pylib import forwarder |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 21 | from pylib import ports |
| 22 | from pylib.base import base_test_result |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 23 | from pylib.base import test_dispatcher |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 24 | from pylib.gtest import gtest_config |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 25 | from pylib.gtest import setup as gtest_setup |
| 26 | from pylib.gtest import test_options as gtest_test_options |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 27 | from pylib.linker import setup as linker_setup |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 28 | from pylib.host_driven import setup as host_driven_setup |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 29 | from pylib.instrumentation import setup as instrumentation_setup |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 30 | from pylib.instrumentation import test_options as instrumentation_test_options |
jbudorick | 9a6b7b33 | 2014-09-20 00:01:07 | [diff] [blame] | 31 | from pylib.junit import setup as junit_setup |
| 32 | from pylib.junit import test_dispatcher as junit_dispatcher |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 33 | from pylib.monkey import setup as monkey_setup |
| 34 | from pylib.monkey import test_options as monkey_test_options |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 35 | from pylib.perf import setup as perf_setup |
| 36 | from pylib.perf import test_options as perf_test_options |
| 37 | from pylib.perf import test_runner as perf_test_runner |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 38 | from pylib.uiautomator import setup as uiautomator_setup |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 39 | from pylib.uiautomator import test_options as uiautomator_test_options |
[email protected] | 2eea487 | 2014-07-28 23:06:17 | [diff] [blame] | 40 | from pylib.utils import apk_helper |
[email protected] | 803f65a7 | 2013-08-20 19:11:30 | [diff] [blame] | 41 | from pylib.utils import command_option_parser |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 42 | from pylib.utils import report_results |
[email protected] | 71aec4b | 2013-11-20 00:35:24 | [diff] [blame] | 43 | from pylib.utils import reraiser_thread |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 44 | from pylib.utils import run_tests_helper |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 45 | |
| 46 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 47 | def AddCommonOptions(option_parser): |
| 48 | """Adds all common options to |option_parser|.""" |
| 49 | |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 50 | group = optparse.OptionGroup(option_parser, 'Common Options') |
| 51 | default_build_type = os.environ.get('BUILDTYPE', 'Debug') |
| 52 | group.add_option('--debug', action='store_const', const='Debug', |
| 53 | dest='build_type', default=default_build_type, |
| 54 | help=('If set, run test suites under out/Debug. ' |
| 55 | 'Default is env var BUILDTYPE or Debug.')) |
| 56 | group.add_option('--release', action='store_const', |
| 57 | const='Release', dest='build_type', |
| 58 | help=('If set, run test suites under out/Release.' |
| 59 | ' Default is env var BUILDTYPE or Debug.')) |
r.kasibhatla | 3d57cba | 2014-10-09 10:55:57 | [diff] [blame^] | 60 | group.add_option('--build-directory', dest='build_directory', |
| 61 | help=('Path to the directory in which build files are' |
| 62 | ' located (should not include build type)')) |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 63 | group.add_option('-c', dest='cleanup_test_files', |
| 64 | help='Cleanup test files on the device after run', |
| 65 | action='store_true') |
| 66 | group.add_option('--num_retries', dest='num_retries', type='int', |
| 67 | default=2, |
| 68 | help=('Number of retries for a test before ' |
| 69 | 'giving up.')) |
| 70 | group.add_option('-v', |
| 71 | '--verbose', |
| 72 | dest='verbose_count', |
| 73 | default=0, |
| 74 | action='count', |
| 75 | help='Verbose level (multiple times for more)') |
| 76 | group.add_option('--tool', |
| 77 | dest='tool', |
| 78 | help=('Run the test under a tool ' |
| 79 | '(use --tool help to list them)')) |
| 80 | group.add_option('--flakiness-dashboard-server', |
| 81 | dest='flakiness_dashboard_server', |
| 82 | help=('Address of the server that is hosting the ' |
| 83 | 'Chrome for Android flakiness dashboard.')) |
| 84 | group.add_option('--skip-deps-push', dest='push_deps', |
| 85 | action='store_false', default=True, |
| 86 | help=('Do not push dependencies to the device. ' |
| 87 | 'Use this at own risk for speeding up test ' |
| 88 | 'execution on local machine.')) |
| 89 | group.add_option('-d', '--device', dest='test_device', |
| 90 | help=('Target device for the test suite ' |
| 91 | 'to run on.')) |
| 92 | option_parser.add_option_group(group) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def ProcessCommonOptions(options): |
| 96 | """Processes and handles all common options.""" |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 97 | run_tests_helper.SetLogLevel(options.verbose_count) |
[email protected] | 14b3b120 | 2013-08-15 22:25:28 | [diff] [blame] | 98 | constants.SetBuildType(options.build_type) |
r.kasibhatla | 3d57cba | 2014-10-09 10:55:57 | [diff] [blame^] | 99 | if options.build_directory: |
| 100 | constants.SetBuildDirectory(options.build_directory) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 101 | |
| 102 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 103 | def AddGTestOptions(option_parser): |
| 104 | """Adds gtest options to |option_parser|.""" |
| 105 | |
| 106 | option_parser.usage = '%prog gtest [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 107 | option_parser.commands_dict = {} |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 108 | option_parser.example = '%prog gtest -s base_unittests' |
| 109 | |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 110 | # TODO(gkanwar): Make this option required |
| 111 | option_parser.add_option('-s', '--suite', dest='suite_name', |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 112 | help=('Executable name of the test suite to run ' |
| 113 | '(use -s help to list them).')) |
[email protected] | c53dc433 | 2013-11-20 04:38:03 | [diff] [blame] | 114 | option_parser.add_option('-f', '--gtest_filter', '--gtest-filter', |
| 115 | dest='test_filter', |
[email protected] | 9e68925 | 2013-07-30 20:14:36 | [diff] [blame] | 116 | help='googletest-style filter string.') |
[email protected] | c53dc433 | 2013-11-20 04:38:03 | [diff] [blame] | 117 | option_parser.add_option('--gtest_also_run_disabled_tests', |
| 118 | '--gtest-also-run-disabled-tests', |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 119 | dest='run_disabled', action='store_true', |
| 120 | help='Also run disabled tests if applicable.') |
| 121 | option_parser.add_option('-a', '--test-arguments', dest='test_arguments', |
| 122 | default='', |
[email protected] | 9e68925 | 2013-07-30 20:14:36 | [diff] [blame] | 123 | help='Additional arguments to pass to the test.') |
| 124 | option_parser.add_option('-t', dest='timeout', |
| 125 | help='Timeout to wait for each test', |
| 126 | type='int', |
| 127 | default=60) |
[email protected] | 5b8b874 | 2014-05-22 08:18:50 | [diff] [blame] | 128 | option_parser.add_option('--isolate_file_path', |
| 129 | '--isolate-file-path', |
| 130 | dest='isolate_file_path', |
| 131 | help='.isolate file path to override the default ' |
| 132 | 'path') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 133 | # TODO(gkanwar): Move these to Common Options once we have the plumbing |
| 134 | # in our other test types to handle these commands |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 135 | AddCommonOptions(option_parser) |
| 136 | |
| 137 | |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 138 | def AddLinkerTestOptions(option_parser): |
| 139 | option_parser.usage = '%prog linker' |
| 140 | option_parser.commands_dict = {} |
| 141 | option_parser.example = '%prog linker' |
| 142 | |
[email protected] | 98c4feef | 2013-10-08 01:19:05 | [diff] [blame] | 143 | option_parser.add_option('-f', '--gtest-filter', dest='test_filter', |
| 144 | help='googletest-style filter string.') |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 145 | AddCommonOptions(option_parser) |
| 146 | |
| 147 | |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 148 | def ProcessGTestOptions(options): |
| 149 | """Intercept test suite help to list test suites. |
| 150 | |
| 151 | Args: |
| 152 | options: Command line options. |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 153 | """ |
| 154 | if options.suite_name == 'help': |
| 155 | print 'Available test suites are:' |
[email protected] | 9e68925 | 2013-07-30 20:14:36 | [diff] [blame] | 156 | for test_suite in (gtest_config.STABLE_TEST_SUITES + |
| 157 | gtest_config.EXPERIMENTAL_TEST_SUITES): |
| 158 | print test_suite |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 159 | sys.exit(0) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 160 | |
| 161 | # Convert to a list, assuming all test suites if nothing was specified. |
| 162 | # TODO(gkanwar): Require having a test suite |
| 163 | if options.suite_name: |
| 164 | options.suite_name = [options.suite_name] |
| 165 | else: |
[email protected] | 9e68925 | 2013-07-30 20:14:36 | [diff] [blame] | 166 | options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES] |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 167 | |
| 168 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 169 | def AddJavaTestOptions(option_parser): |
| 170 | """Adds the Java test options to |option_parser|.""" |
| 171 | |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 172 | option_parser.add_option('-f', '--test-filter', dest='test_filter', |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 173 | help=('Test filter (if not fully qualified, ' |
| 174 | 'will run all matches).')) |
| 175 | option_parser.add_option( |
| 176 | '-A', '--annotation', dest='annotation_str', |
| 177 | help=('Comma-separated list of annotations. Run only tests with any of ' |
| 178 | 'the given annotations. An annotation can be either a key or a ' |
| 179 | 'key-values pair. A test that has no annotation is considered ' |
| 180 | '"SmallTest".')) |
| 181 | option_parser.add_option( |
| 182 | '-E', '--exclude-annotation', dest='exclude_annotation_str', |
| 183 | help=('Comma-separated list of annotations. Exclude tests with these ' |
| 184 | 'annotations.')) |
jbudorick | cbcc115d | 2014-09-18 17:50:59 | [diff] [blame] | 185 | option_parser.add_option( |
| 186 | '--screenshot', dest='screenshot_failures', action='store_true', |
| 187 | help='Capture screenshots of test failures') |
| 188 | option_parser.add_option( |
| 189 | '--save-perf-json', action='store_true', |
| 190 | help='Saves the JSON file for each UI Perf test.') |
| 191 | option_parser.add_option( |
| 192 | '--official-build', action='store_true', help='Run official build tests.') |
| 193 | option_parser.add_option( |
| 194 | '--test_data', '--test-data', action='append', default=[], |
| 195 | help=('Each instance defines a directory of test data that should be ' |
| 196 | 'copied to the target(s) before running the tests. The argument ' |
| 197 | 'should be of the form <target>:<source>, <target> is relative to ' |
| 198 | 'the device data directory, and <source> is relative to the ' |
| 199 | 'chromium build directory.')) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 200 | |
| 201 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 202 | def ProcessJavaTestOptions(options): |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 203 | """Processes options/arguments and populates |options| with defaults.""" |
| 204 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 205 | if options.annotation_str: |
| 206 | options.annotations = options.annotation_str.split(',') |
| 207 | elif options.test_filter: |
| 208 | options.annotations = [] |
| 209 | else: |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 210 | options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', |
[email protected] | 4f777ca | 2014-08-08 01:45:59 | [diff] [blame] | 211 | 'EnormousTest', 'IntegrationTest'] |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 212 | |
| 213 | if options.exclude_annotation_str: |
| 214 | options.exclude_annotations = options.exclude_annotation_str.split(',') |
| 215 | else: |
| 216 | options.exclude_annotations = [] |
| 217 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 218 | |
| 219 | def AddInstrumentationTestOptions(option_parser): |
| 220 | """Adds Instrumentation test options to |option_parser|.""" |
| 221 | |
| 222 | option_parser.usage = '%prog instrumentation [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 223 | option_parser.commands_dict = {} |
[email protected] | fb7ab5e8 | 2013-07-26 18:31:20 | [diff] [blame] | 224 | option_parser.example = ('%prog instrumentation ' |
[email protected] | efeb59e | 2014-03-12 01:31:26 | [diff] [blame] | 225 | '--test-apk=ChromeShellTest') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 226 | |
| 227 | AddJavaTestOptions(option_parser) |
| 228 | AddCommonOptions(option_parser) |
| 229 | |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 230 | option_parser.add_option('-j', '--java-only', action='store_true', |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 231 | default=False, help='Run only the Java tests.') |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 232 | option_parser.add_option('-p', '--python-only', action='store_true', |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 233 | default=False, |
| 234 | help='Run only the host-driven tests.') |
[email protected] | a69e85bc | 2013-08-16 18:07:26 | [diff] [blame] | 235 | option_parser.add_option('--host-driven-root', |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 236 | help='Root of the host-driven tests.') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 237 | option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger', |
| 238 | action='store_true', |
| 239 | help='Wait for debugger.') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 240 | option_parser.add_option( |
| 241 | '--test-apk', dest='test_apk', |
| 242 | help=('The name of the apk containing the tests ' |
[email protected] | ae68d4a | 2013-09-24 21:57:15 | [diff] [blame] | 243 | '(without the .apk extension; e.g. "ContentShellTest").')) |
[email protected] | 803f65a7 | 2013-08-20 19:11:30 | [diff] [blame] | 244 | option_parser.add_option('--coverage-dir', |
| 245 | help=('Directory in which to place all generated ' |
| 246 | 'EMMA coverage files.')) |
[email protected] | 4f777ca | 2014-08-08 01:45:59 | [diff] [blame] | 247 | option_parser.add_option('--device-flags', dest='device_flags', default='', |
| 248 | help='The relative filepath to a file containing ' |
| 249 | 'command-line flags to set on the device') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 250 | |
| 251 | |
| 252 | def ProcessInstrumentationOptions(options, error_func): |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 253 | """Processes options/arguments and populate |options| with defaults. |
| 254 | |
| 255 | Args: |
| 256 | options: optparse.Options object. |
| 257 | error_func: Function to call with the error message in case of an error. |
| 258 | |
| 259 | Returns: |
| 260 | An InstrumentationOptions named tuple which contains all options relevant to |
| 261 | instrumentation tests. |
| 262 | """ |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 263 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 264 | ProcessJavaTestOptions(options) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 265 | |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 266 | if options.java_only and options.python_only: |
| 267 | error_func('Options java_only (-j) and python_only (-p) ' |
| 268 | 'are mutually exclusive.') |
| 269 | options.run_java_tests = True |
| 270 | options.run_python_tests = True |
| 271 | if options.java_only: |
| 272 | options.run_python_tests = False |
| 273 | elif options.python_only: |
| 274 | options.run_java_tests = False |
| 275 | |
[email protected] | 67954f82 | 2013-08-14 18:09:08 | [diff] [blame] | 276 | if not options.host_driven_root: |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 277 | options.run_python_tests = False |
| 278 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 279 | if not options.test_apk: |
| 280 | error_func('--test-apk must be specified.') |
| 281 | |
[email protected] | ae68d4a | 2013-09-24 21:57:15 | [diff] [blame] | 282 | |
[email protected] | 2eea487 | 2014-07-28 23:06:17 | [diff] [blame] | 283 | options.test_apk_path = os.path.join( |
| 284 | constants.GetOutDirectory(), |
| 285 | constants.SDK_BUILD_APKS_DIR, |
| 286 | '%s.apk' % options.test_apk) |
[email protected] | ae68d4a | 2013-09-24 21:57:15 | [diff] [blame] | 287 | options.test_apk_jar_path = os.path.join( |
| 288 | constants.GetOutDirectory(), |
| 289 | constants.SDK_BUILD_TEST_JAVALIB_DIR, |
| 290 | '%s.jar' % options.test_apk) |
[email protected] | 5e2f3f6 | 2014-06-23 12:31:46 | [diff] [blame] | 291 | options.test_support_apk_path = '%sSupport%s' % ( |
[email protected] | 2eea487 | 2014-07-28 23:06:17 | [diff] [blame] | 292 | os.path.splitext(options.test_apk_path)) |
[email protected] | 5e2f3f6 | 2014-06-23 12:31:46 | [diff] [blame] | 293 | |
[email protected] | 2eea487 | 2014-07-28 23:06:17 | [diff] [blame] | 294 | options.test_runner = apk_helper.GetInstrumentationName(options.test_apk_path) |
[email protected] | 5e2f3f6 | 2014-06-23 12:31:46 | [diff] [blame] | 295 | |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 296 | return instrumentation_test_options.InstrumentationOptions( |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 297 | options.tool, |
| 298 | options.cleanup_test_files, |
| 299 | options.push_deps, |
| 300 | options.annotations, |
| 301 | options.exclude_annotations, |
| 302 | options.test_filter, |
| 303 | options.test_data, |
| 304 | options.save_perf_json, |
| 305 | options.screenshot_failures, |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 306 | options.wait_for_debugger, |
[email protected] | 803f65a7 | 2013-08-20 19:11:30 | [diff] [blame] | 307 | options.coverage_dir, |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 308 | options.test_apk, |
| 309 | options.test_apk_path, |
[email protected] | 5e2f3f6 | 2014-06-23 12:31:46 | [diff] [blame] | 310 | options.test_apk_jar_path, |
[email protected] | 65bd8fb | 2014-08-02 17:02:02 | [diff] [blame] | 311 | options.test_runner, |
[email protected] | 4f777ca | 2014-08-08 01:45:59 | [diff] [blame] | 312 | options.test_support_apk_path, |
| 313 | options.device_flags |
[email protected] | 5e2f3f6 | 2014-06-23 12:31:46 | [diff] [blame] | 314 | ) |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 315 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 316 | |
| 317 | def AddUIAutomatorTestOptions(option_parser): |
| 318 | """Adds UI Automator test options to |option_parser|.""" |
| 319 | |
| 320 | option_parser.usage = '%prog uiautomator [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 321 | option_parser.commands_dict = {} |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 322 | option_parser.example = ( |
[email protected] | efeb59e | 2014-03-12 01:31:26 | [diff] [blame] | 323 | '%prog uiautomator --test-jar=chrome_shell_uiautomator_tests' |
| 324 | ' --package=chrome_shell') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 325 | option_parser.add_option( |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 326 | '--package', |
| 327 | help=('Package under test. Possible values: %s' % |
| 328 | constants.PACKAGE_INFO.keys())) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 329 | option_parser.add_option( |
| 330 | '--test-jar', dest='test_jar', |
| 331 | help=('The name of the dexed jar containing the tests (without the ' |
| 332 | '.dex.jar extension). Alternatively, this can be a full path ' |
| 333 | 'to the jar.')) |
| 334 | |
| 335 | AddJavaTestOptions(option_parser) |
| 336 | AddCommonOptions(option_parser) |
| 337 | |
| 338 | |
| 339 | def ProcessUIAutomatorOptions(options, error_func): |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 340 | """Processes UIAutomator options/arguments. |
| 341 | |
| 342 | Args: |
| 343 | options: optparse.Options object. |
| 344 | error_func: Function to call with the error message in case of an error. |
| 345 | |
| 346 | Returns: |
| 347 | A UIAutomatorOptions named tuple which contains all options relevant to |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 348 | uiautomator tests. |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 349 | """ |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 350 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 351 | ProcessJavaTestOptions(options) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 352 | |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 353 | if not options.package: |
| 354 | error_func('--package is required.') |
| 355 | |
| 356 | if options.package not in constants.PACKAGE_INFO: |
| 357 | error_func('Invalid package.') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 358 | |
| 359 | if not options.test_jar: |
| 360 | error_func('--test-jar must be specified.') |
| 361 | |
| 362 | if os.path.exists(options.test_jar): |
| 363 | # The dexed JAR is fully qualified, assume the info JAR lives along side. |
| 364 | options.uiautomator_jar = options.test_jar |
| 365 | else: |
| 366 | options.uiautomator_jar = os.path.join( |
[email protected] | ae68d4a | 2013-09-24 21:57:15 | [diff] [blame] | 367 | constants.GetOutDirectory(), |
| 368 | constants.SDK_BUILD_JAVALIB_DIR, |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 369 | '%s.dex.jar' % options.test_jar) |
| 370 | options.uiautomator_info_jar = ( |
| 371 | options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] + |
| 372 | '_java.jar') |
| 373 | |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 374 | return uiautomator_test_options.UIAutomatorOptions( |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 375 | options.tool, |
| 376 | options.cleanup_test_files, |
| 377 | options.push_deps, |
| 378 | options.annotations, |
| 379 | options.exclude_annotations, |
| 380 | options.test_filter, |
| 381 | options.test_data, |
| 382 | options.save_perf_json, |
| 383 | options.screenshot_failures, |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 384 | options.uiautomator_jar, |
| 385 | options.uiautomator_info_jar, |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 386 | options.package) |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 387 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 388 | |
jbudorick | 9a6b7b33 | 2014-09-20 00:01:07 | [diff] [blame] | 389 | def AddJUnitTestOptions(option_parser): |
| 390 | """Adds junit test options to |option_parser|.""" |
| 391 | option_parser.usage = '%prog junit -s [test suite name]' |
| 392 | option_parser.commands_dict = {} |
| 393 | |
| 394 | option_parser.add_option( |
| 395 | '-s', '--test-suite', dest='test_suite', |
| 396 | help=('JUnit test suite to run.')) |
| 397 | option_parser.add_option( |
| 398 | '-f', '--test-filter', dest='test_filter', |
| 399 | help='Filters tests googletest-style.') |
| 400 | option_parser.add_option( |
| 401 | '--package-filter', dest='package_filter', |
| 402 | help='Filters tests by package.') |
| 403 | option_parser.add_option( |
| 404 | '--runner-filter', dest='runner_filter', |
| 405 | help='Filters tests by runner class. Must be fully qualified.') |
| 406 | option_parser.add_option( |
| 407 | '--sdk-version', dest='sdk_version', type="int", |
| 408 | help='The Android SDK version.') |
| 409 | AddCommonOptions(option_parser) |
| 410 | |
| 411 | |
| 412 | def ProcessJUnitTestOptions(options, error_func): |
| 413 | """Processes all JUnit test options.""" |
| 414 | if not options.test_suite: |
| 415 | error_func('No test suite specified.') |
| 416 | return options |
| 417 | |
| 418 | |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 419 | def AddMonkeyTestOptions(option_parser): |
| 420 | """Adds monkey test options to |option_parser|.""" |
[email protected] | fb81b98 | 2013-08-09 00:07:12 | [diff] [blame] | 421 | |
| 422 | option_parser.usage = '%prog monkey [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 423 | option_parser.commands_dict = {} |
[email protected] | fb81b98 | 2013-08-09 00:07:12 | [diff] [blame] | 424 | option_parser.example = ( |
[email protected] | efeb59e | 2014-03-12 01:31:26 | [diff] [blame] | 425 | '%prog monkey --package=chrome_shell') |
[email protected] | fb81b98 | 2013-08-09 00:07:12 | [diff] [blame] | 426 | |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 427 | option_parser.add_option( |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 428 | '--package', |
| 429 | help=('Package under test. Possible values: %s' % |
| 430 | constants.PACKAGE_INFO.keys())) |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 431 | option_parser.add_option( |
| 432 | '--event-count', default=10000, type='int', |
| 433 | help='Number of events to generate [default: %default].') |
| 434 | option_parser.add_option( |
| 435 | '--category', default='', |
[email protected] | fb81b98 | 2013-08-09 00:07:12 | [diff] [blame] | 436 | help='A list of allowed categories.') |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 437 | option_parser.add_option( |
| 438 | '--throttle', default=100, type='int', |
| 439 | help='Delay between events (ms) [default: %default]. ') |
| 440 | option_parser.add_option( |
| 441 | '--seed', type='int', |
| 442 | help=('Seed value for pseudo-random generator. Same seed value generates ' |
| 443 | 'the same sequence of events. Seed is randomized by default.')) |
| 444 | option_parser.add_option( |
| 445 | '--extra-args', default='', |
| 446 | help=('String of other args to pass to the command verbatim ' |
| 447 | '[default: "%default"].')) |
| 448 | |
| 449 | AddCommonOptions(option_parser) |
| 450 | |
| 451 | |
| 452 | def ProcessMonkeyTestOptions(options, error_func): |
| 453 | """Processes all monkey test options. |
| 454 | |
| 455 | Args: |
| 456 | options: optparse.Options object. |
| 457 | error_func: Function to call with the error message in case of an error. |
| 458 | |
| 459 | Returns: |
| 460 | A MonkeyOptions named tuple which contains all options relevant to |
| 461 | monkey tests. |
| 462 | """ |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 463 | if not options.package: |
| 464 | error_func('--package is required.') |
| 465 | |
| 466 | if options.package not in constants.PACKAGE_INFO: |
| 467 | error_func('Invalid package.') |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 468 | |
| 469 | category = options.category |
| 470 | if category: |
| 471 | category = options.category.split(',') |
| 472 | |
| 473 | return monkey_test_options.MonkeyOptions( |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 474 | options.verbose_count, |
[email protected] | a8886c8a9 | 2013-10-08 17:29:30 | [diff] [blame] | 475 | options.package, |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 476 | options.event_count, |
| 477 | category, |
| 478 | options.throttle, |
| 479 | options.seed, |
| 480 | options.extra_args) |
| 481 | |
| 482 | |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 483 | def AddPerfTestOptions(option_parser): |
| 484 | """Adds perf test options to |option_parser|.""" |
| 485 | |
| 486 | option_parser.usage = '%prog perf [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 487 | option_parser.commands_dict = {} |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 488 | option_parser.example = ('%prog perf ' |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 489 | '[--single-step -- command args] or ' |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 490 | '[--steps perf_steps.json] or ' |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 491 | '[--print-step step]') |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 492 | |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 493 | option_parser.add_option( |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 494 | '--single-step', |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 495 | action='store_true', |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 496 | help='Execute the given command with retries, but only print the result ' |
| 497 | 'for the "most successful" round.') |
| 498 | option_parser.add_option( |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 499 | '--steps', |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 500 | help='JSON file containing the list of commands to run.') |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 501 | option_parser.add_option( |
| 502 | '--flaky-steps', |
| 503 | help=('A JSON file containing steps that are flaky ' |
| 504 | 'and will have its exit code ignored.')) |
| 505 | option_parser.add_option( |
[email protected] | 61487ed | 2014-06-09 12:33:56 | [diff] [blame] | 506 | '--output-json-list', |
| 507 | help='Write a simple list of names from --steps into the given file.') |
| 508 | option_parser.add_option( |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 509 | '--print-step', |
| 510 | help='The name of a previously executed perf step to print.') |
| 511 | option_parser.add_option( |
| 512 | '--no-timeout', action='store_true', |
| 513 | help=('Do not impose a timeout. Each perf step is responsible for ' |
| 514 | 'implementing the timeout logic.')) |
[email protected] | 650487c | 2013-09-30 11:40:49 | [diff] [blame] | 515 | option_parser.add_option( |
| 516 | '-f', '--test-filter', |
| 517 | help=('Test filter (will match against the names listed in --steps).')) |
| 518 | option_parser.add_option( |
| 519 | '--dry-run', |
| 520 | action='store_true', |
| 521 | help='Just print the steps without executing.') |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 522 | AddCommonOptions(option_parser) |
| 523 | |
| 524 | |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 525 | def ProcessPerfTestOptions(options, args, error_func): |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 526 | """Processes all perf test options. |
| 527 | |
| 528 | Args: |
| 529 | options: optparse.Options object. |
| 530 | error_func: Function to call with the error message in case of an error. |
| 531 | |
| 532 | Returns: |
| 533 | A PerfOptions named tuple which contains all options relevant to |
| 534 | perf tests. |
| 535 | """ |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 536 | # Only one of steps, print_step or single_step must be provided. |
| 537 | count = len(filter(None, |
| 538 | [options.steps, options.print_step, options.single_step])) |
| 539 | if count != 1: |
| 540 | error_func('Please specify one of: --steps, --print-step, --single-step.') |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 541 | single_step = None |
| 542 | if options.single_step: |
| 543 | single_step = ' '.join(args[2:]) |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 544 | return perf_test_options.PerfOptions( |
[email protected] | 61487ed | 2014-06-09 12:33:56 | [diff] [blame] | 545 | options.steps, options.flaky_steps, options.output_json_list, |
| 546 | options.print_step, options.no_timeout, options.test_filter, |
| 547 | options.dry_run, single_step) |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 548 | |
| 549 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 550 | def _RunGTests(options, devices): |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 551 | """Subcommand of RunTestsCommands which runs gtests.""" |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 552 | ProcessGTestOptions(options) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 553 | |
| 554 | exit_code = 0 |
| 555 | for suite_name in options.suite_name: |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 556 | # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for |
| 557 | # the gtest command. |
| 558 | gtest_options = gtest_test_options.GTestOptions( |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 559 | options.tool, |
| 560 | options.cleanup_test_files, |
| 561 | options.push_deps, |
| 562 | options.test_filter, |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 563 | options.run_disabled, |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 564 | options.test_arguments, |
| 565 | options.timeout, |
[email protected] | 5b8b874 | 2014-05-22 08:18:50 | [diff] [blame] | 566 | options.isolate_file_path, |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 567 | suite_name) |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 568 | runner_factory, tests = gtest_setup.Setup(gtest_options, devices) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 569 | |
| 570 | results, test_exit_code = test_dispatcher.RunTests( |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 571 | tests, runner_factory, devices, shard=True, test_timeout=None, |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 572 | num_retries=options.num_retries) |
| 573 | |
| 574 | if test_exit_code and exit_code != constants.ERROR_EXIT_CODE: |
| 575 | exit_code = test_exit_code |
| 576 | |
| 577 | report_results.LogFull( |
| 578 | results=results, |
| 579 | test_type='Unit test', |
| 580 | test_package=suite_name, |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 581 | flakiness_server=options.flakiness_dashboard_server) |
| 582 | |
| 583 | if os.path.isdir(constants.ISOLATE_DEPS_DIR): |
| 584 | shutil.rmtree(constants.ISOLATE_DEPS_DIR) |
| 585 | |
| 586 | return exit_code |
| 587 | |
| 588 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 589 | def _RunLinkerTests(options, devices): |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 590 | """Subcommand of RunTestsCommands which runs linker tests.""" |
| 591 | runner_factory, tests = linker_setup.Setup(options, devices) |
| 592 | |
| 593 | results, exit_code = test_dispatcher.RunTests( |
| 594 | tests, runner_factory, devices, shard=True, test_timeout=60, |
| 595 | num_retries=options.num_retries) |
| 596 | |
| 597 | report_results.LogFull( |
| 598 | results=results, |
| 599 | test_type='Linker test', |
[email protected] | 93c9f9b | 2014-02-10 16:19:22 | [diff] [blame] | 600 | test_package='ChromiumLinkerTest') |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 601 | |
| 602 | return exit_code |
| 603 | |
| 604 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 605 | def _RunInstrumentationTests(options, error_func, devices): |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 606 | """Subcommand of RunTestsCommands which runs instrumentation tests.""" |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 607 | instrumentation_options = ProcessInstrumentationOptions(options, error_func) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 608 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 609 | if len(devices) > 1 and options.wait_for_debugger: |
| 610 | logging.warning('Debugger can not be sharded, using first available device') |
| 611 | devices = devices[:1] |
| 612 | |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 613 | results = base_test_result.TestRunResults() |
| 614 | exit_code = 0 |
| 615 | |
| 616 | if options.run_java_tests: |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 617 | runner_factory, tests = instrumentation_setup.Setup(instrumentation_options) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 618 | |
| 619 | test_results, exit_code = test_dispatcher.RunTests( |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 620 | tests, runner_factory, devices, shard=True, test_timeout=None, |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 621 | num_retries=options.num_retries) |
| 622 | |
| 623 | results.AddTestRunResults(test_results) |
| 624 | |
| 625 | if options.run_python_tests: |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 626 | runner_factory, tests = host_driven_setup.InstrumentationSetup( |
[email protected] | 67954f82 | 2013-08-14 18:09:08 | [diff] [blame] | 627 | options.host_driven_root, options.official_build, |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 628 | instrumentation_options) |
| 629 | |
[email protected] | 3402002 | 2013-08-06 23:35:34 | [diff] [blame] | 630 | if tests: |
| 631 | test_results, test_exit_code = test_dispatcher.RunTests( |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 632 | tests, runner_factory, devices, shard=True, test_timeout=None, |
[email protected] | 3402002 | 2013-08-06 23:35:34 | [diff] [blame] | 633 | num_retries=options.num_retries) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 634 | |
[email protected] | 3402002 | 2013-08-06 23:35:34 | [diff] [blame] | 635 | results.AddTestRunResults(test_results) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 636 | |
[email protected] | 3402002 | 2013-08-06 23:35:34 | [diff] [blame] | 637 | # Only allow exit code escalation |
| 638 | if test_exit_code and exit_code != constants.ERROR_EXIT_CODE: |
| 639 | exit_code = test_exit_code |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 640 | |
[email protected] | 4f777ca | 2014-08-08 01:45:59 | [diff] [blame] | 641 | if options.device_flags: |
| 642 | options.device_flags = os.path.join(constants.DIR_SOURCE_ROOT, |
| 643 | options.device_flags) |
| 644 | |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 645 | report_results.LogFull( |
| 646 | results=results, |
| 647 | test_type='Instrumentation', |
| 648 | test_package=os.path.basename(options.test_apk), |
| 649 | annotation=options.annotations, |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 650 | flakiness_server=options.flakiness_dashboard_server) |
| 651 | |
| 652 | return exit_code |
| 653 | |
| 654 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 655 | def _RunUIAutomatorTests(options, error_func, devices): |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 656 | """Subcommand of RunTestsCommands which runs uiautomator tests.""" |
[email protected] | 2a68422 | 2013-08-01 16:59:22 | [diff] [blame] | 657 | uiautomator_options = ProcessUIAutomatorOptions(options, error_func) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 658 | |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 659 | runner_factory, tests = uiautomator_setup.Setup(uiautomator_options) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 660 | |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 661 | results, exit_code = test_dispatcher.RunTests( |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 662 | tests, runner_factory, devices, shard=True, test_timeout=None, |
[email protected] | 37ee0c79 | 2013-08-06 19:10:13 | [diff] [blame] | 663 | num_retries=options.num_retries) |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 664 | |
| 665 | report_results.LogFull( |
| 666 | results=results, |
| 667 | test_type='UIAutomator', |
| 668 | test_package=os.path.basename(options.test_jar), |
| 669 | annotation=options.annotations, |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 670 | flakiness_server=options.flakiness_dashboard_server) |
| 671 | |
| 672 | return exit_code |
| 673 | |
| 674 | |
jbudorick | 9a6b7b33 | 2014-09-20 00:01:07 | [diff] [blame] | 675 | def _RunJUnitTests(options, error_func): |
| 676 | """Subcommand of RunTestsCommand which runs junit tests.""" |
| 677 | junit_options = ProcessJUnitTestOptions(options, error_func) |
| 678 | runner_factory, tests = junit_setup.Setup(junit_options) |
| 679 | _, exit_code = junit_dispatcher.RunTests(tests, runner_factory) |
| 680 | |
| 681 | return exit_code |
| 682 | |
| 683 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 684 | def _RunMonkeyTests(options, error_func, devices): |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 685 | """Subcommand of RunTestsCommands which runs monkey tests.""" |
| 686 | monkey_options = ProcessMonkeyTestOptions(options, error_func) |
| 687 | |
| 688 | runner_factory, tests = monkey_setup.Setup(monkey_options) |
| 689 | |
| 690 | results, exit_code = test_dispatcher.RunTests( |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 691 | tests, runner_factory, devices, shard=False, test_timeout=None, |
| 692 | num_retries=options.num_retries) |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 693 | |
| 694 | report_results.LogFull( |
| 695 | results=results, |
| 696 | test_type='Monkey', |
[email protected] | 14b3b120 | 2013-08-15 22:25:28 | [diff] [blame] | 697 | test_package='Monkey') |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 698 | |
| 699 | return exit_code |
| 700 | |
| 701 | |
[email protected] | a72f075 | 2014-06-03 23:52:34 | [diff] [blame] | 702 | def _RunPerfTests(options, args, error_func): |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 703 | """Subcommand of RunTestsCommands which runs perf tests.""" |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 704 | perf_options = ProcessPerfTestOptions(options, args, error_func) |
[email protected] | 61487ed | 2014-06-09 12:33:56 | [diff] [blame] | 705 | |
| 706 | # Just save a simple json with a list of test names. |
| 707 | if perf_options.output_json_list: |
| 708 | return perf_test_runner.OutputJsonList( |
| 709 | perf_options.steps, perf_options.output_json_list) |
| 710 | |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 711 | # Just print the results from a single previously executed step. |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 712 | if perf_options.print_step: |
| 713 | return perf_test_runner.PrintTestOutput(perf_options.print_step) |
| 714 | |
[email protected] | a72f075 | 2014-06-03 23:52:34 | [diff] [blame] | 715 | runner_factory, tests, devices = perf_setup.Setup(perf_options) |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 716 | |
[email protected] | a72f075 | 2014-06-03 23:52:34 | [diff] [blame] | 717 | # shard=False means that each device will get the full list of tests |
| 718 | # and then each one will decide their own affinity. |
| 719 | # shard=True means each device will pop the next test available from a queue, |
| 720 | # which increases throughput but have no affinity. |
[email protected] | 86184c7b | 2013-08-15 15:06:57 | [diff] [blame] | 721 | results, _ = test_dispatcher.RunTests( |
[email protected] | a72f075 | 2014-06-03 23:52:34 | [diff] [blame] | 722 | tests, runner_factory, devices, shard=False, test_timeout=None, |
[email protected] | 181a5c9 | 2013-09-06 17:11:46 | [diff] [blame] | 723 | num_retries=options.num_retries) |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 724 | |
| 725 | report_results.LogFull( |
| 726 | results=results, |
| 727 | test_type='Perf', |
[email protected] | 865a47a | 2013-08-16 14:01:12 | [diff] [blame] | 728 | test_package='Perf') |
[email protected] | def4bce | 2013-11-12 12:59:52 | [diff] [blame] | 729 | |
| 730 | if perf_options.single_step: |
| 731 | return perf_test_runner.PrintTestOutput('single_step') |
| 732 | |
[email protected] | 11ce845 | 2014-02-17 10:55:03 | [diff] [blame] | 733 | perf_test_runner.PrintSummary(tests) |
| 734 | |
[email protected] | 86184c7b | 2013-08-15 15:06:57 | [diff] [blame] | 735 | # Always return 0 on the sharding stage. Individual tests exit_code |
| 736 | # will be returned on the print_step stage. |
| 737 | return 0 |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 738 | |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 739 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 740 | def _GetAttachedDevices(test_device=None): |
| 741 | """Get all attached devices. |
| 742 | |
| 743 | Args: |
| 744 | test_device: Name of a specific device to use. |
| 745 | |
| 746 | Returns: |
| 747 | A list of attached devices. |
| 748 | """ |
| 749 | attached_devices = [] |
| 750 | |
| 751 | attached_devices = android_commands.GetAttachedDevices() |
| 752 | if test_device: |
| 753 | assert test_device in attached_devices, ( |
| 754 | 'Did not find device %s among attached device. Attached devices: %s' |
| 755 | % (test_device, ', '.join(attached_devices))) |
| 756 | attached_devices = [test_device] |
| 757 | |
| 758 | assert attached_devices, 'No devices attached.' |
| 759 | |
| 760 | return sorted(attached_devices) |
| 761 | |
| 762 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 763 | def RunTestsCommand(command, options, args, option_parser): |
| 764 | """Checks test type and dispatches to the appropriate function. |
| 765 | |
| 766 | Args: |
| 767 | command: String indicating the command that was received to trigger |
| 768 | this function. |
| 769 | options: optparse options dictionary. |
| 770 | args: List of extra args from optparse. |
| 771 | option_parser: optparse.OptionParser object. |
| 772 | |
| 773 | Returns: |
| 774 | Integer indicated exit code. |
[email protected] | b387389 | 2013-07-10 04:57:10 | [diff] [blame] | 775 | |
| 776 | Raises: |
| 777 | Exception: Unknown command name passed in, or an exception from an |
| 778 | individual test runner. |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 779 | """ |
| 780 | |
[email protected] | d82f025 | 2013-07-12 23:22:57 | [diff] [blame] | 781 | # Check for extra arguments |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 782 | if len(args) > 2 and command != 'perf': |
[email protected] | d82f025 | 2013-07-12 23:22:57 | [diff] [blame] | 783 | option_parser.error('Unrecognized arguments: %s' % (' '.join(args[2:]))) |
| 784 | return constants.ERROR_EXIT_CODE |
[email protected] | ad32f31 | 2013-11-13 04:03:29 | [diff] [blame] | 785 | if command == 'perf': |
| 786 | if ((options.single_step and len(args) <= 2) or |
| 787 | (not options.single_step and len(args) > 2)): |
| 788 | option_parser.error('Unrecognized arguments: %s' % (' '.join(args))) |
| 789 | return constants.ERROR_EXIT_CODE |
[email protected] | d82f025 | 2013-07-12 23:22:57 | [diff] [blame] | 790 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 791 | ProcessCommonOptions(options) |
| 792 | |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 793 | devices = _GetAttachedDevices(options.test_device) |
| 794 | |
[email protected] | c0662e09 | 2013-11-12 11:51:25 | [diff] [blame] | 795 | forwarder.Forwarder.RemoveHostLog() |
[email protected] | 6b11583b | 2013-11-21 16:18:40 | [diff] [blame] | 796 | if not ports.ResetTestServerPortAllocation(): |
| 797 | raise Exception('Failed to reset test server port.') |
[email protected] | c0662e09 | 2013-11-12 11:51:25 | [diff] [blame] | 798 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 799 | if command == 'gtest': |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 800 | return _RunGTests(options, devices) |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 801 | elif command == 'linker': |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 802 | return _RunLinkerTests(options, devices) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 803 | elif command == 'instrumentation': |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 804 | return _RunInstrumentationTests(options, option_parser.error, devices) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 805 | elif command == 'uiautomator': |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 806 | return _RunUIAutomatorTests(options, option_parser.error, devices) |
jbudorick | 9a6b7b33 | 2014-09-20 00:01:07 | [diff] [blame] | 807 | elif command == 'junit': |
| 808 | return _RunJUnitTests(options, option_parser.error) |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 809 | elif command == 'monkey': |
[email protected] | f7148dd4 | 2013-08-20 14:24:57 | [diff] [blame] | 810 | return _RunMonkeyTests(options, option_parser.error, devices) |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 811 | elif command == 'perf': |
[email protected] | a72f075 | 2014-06-03 23:52:34 | [diff] [blame] | 812 | return _RunPerfTests(options, args, option_parser.error) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 813 | else: |
[email protected] | 6bc1bda2 | 2013-07-19 22:08:37 | [diff] [blame] | 814 | raise Exception('Unknown test type.') |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 815 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 816 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 817 | def HelpCommand(command, _options, args, option_parser): |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 818 | """Display help for a certain command, or overall help. |
| 819 | |
| 820 | Args: |
| 821 | command: String indicating the command that was received to trigger |
| 822 | this function. |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 823 | options: optparse options dictionary. unused. |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 824 | args: List of extra args from optparse. |
| 825 | option_parser: optparse.OptionParser object. |
| 826 | |
| 827 | Returns: |
| 828 | Integer indicated exit code. |
| 829 | """ |
| 830 | # If we don't have any args, display overall help |
| 831 | if len(args) < 3: |
| 832 | option_parser.print_help() |
| 833 | return 0 |
[email protected] | d82f025 | 2013-07-12 23:22:57 | [diff] [blame] | 834 | # If we have too many args, print an error |
| 835 | if len(args) > 3: |
| 836 | option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:]))) |
| 837 | return constants.ERROR_EXIT_CODE |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 838 | |
| 839 | command = args[2] |
| 840 | |
| 841 | if command not in VALID_COMMANDS: |
| 842 | option_parser.error('Unrecognized command.') |
| 843 | |
| 844 | # Treat the help command as a special case. We don't care about showing a |
| 845 | # specific help page for itself. |
| 846 | if command == 'help': |
| 847 | option_parser.print_help() |
| 848 | return 0 |
| 849 | |
| 850 | VALID_COMMANDS[command].add_options_func(option_parser) |
| 851 | option_parser.usage = '%prog ' + command + ' [options]' |
[email protected] | dfffbcbc | 2013-09-17 22:06:01 | [diff] [blame] | 852 | option_parser.commands_dict = {} |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 853 | option_parser.print_help() |
| 854 | |
| 855 | return 0 |
| 856 | |
| 857 | |
| 858 | # Define a named tuple for the values in the VALID_COMMANDS dictionary so the |
| 859 | # syntax is a bit prettier. The tuple is two functions: (add options, run |
| 860 | # command). |
| 861 | CommandFunctionTuple = collections.namedtuple( |
| 862 | 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) |
| 863 | VALID_COMMANDS = { |
| 864 | 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand), |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 865 | 'instrumentation': CommandFunctionTuple( |
| 866 | AddInstrumentationTestOptions, RunTestsCommand), |
| 867 | 'uiautomator': CommandFunctionTuple( |
| 868 | AddUIAutomatorTestOptions, RunTestsCommand), |
jbudorick | 9a6b7b33 | 2014-09-20 00:01:07 | [diff] [blame] | 869 | 'junit': CommandFunctionTuple( |
| 870 | AddJUnitTestOptions, RunTestsCommand), |
[email protected] | 3dbdfa4 | 2013-08-08 01:08:14 | [diff] [blame] | 871 | 'monkey': CommandFunctionTuple( |
| 872 | AddMonkeyTestOptions, RunTestsCommand), |
[email protected] | ec3170b | 2013-08-14 14:39:47 | [diff] [blame] | 873 | 'perf': CommandFunctionTuple( |
| 874 | AddPerfTestOptions, RunTestsCommand), |
[email protected] | 6b6abac6d | 2013-10-03 11:56:38 | [diff] [blame] | 875 | 'linker': CommandFunctionTuple( |
| 876 | AddLinkerTestOptions, RunTestsCommand), |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 877 | 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand) |
| 878 | } |
| 879 | |
| 880 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 881 | def DumpThreadStacks(_signal, _frame): |
[email protected] | 71aec4b | 2013-11-20 00:35:24 | [diff] [blame] | 882 | for thread in threading.enumerate(): |
| 883 | reraiser_thread.LogThreadStack(thread) |
[email protected] | 83bb815 | 2013-11-19 15:02:21 | [diff] [blame] | 884 | |
| 885 | |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 886 | def main(): |
[email protected] | 83bb815 | 2013-11-19 15:02:21 | [diff] [blame] | 887 | signal.signal(signal.SIGUSR1, DumpThreadStacks) |
[email protected] | 803f65a7 | 2013-08-20 19:11:30 | [diff] [blame] | 888 | option_parser = command_option_parser.CommandOptionParser( |
| 889 | commands_dict=VALID_COMMANDS) |
| 890 | return command_option_parser.ParseAndExecute(option_parser) |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 891 | |
[email protected] | fbe2932 | 2013-07-09 09:03:26 | [diff] [blame] | 892 | |
| 893 | if __name__ == '__main__': |
[email protected] | 7c53a60 | 2014-03-24 16:21:44 | [diff] [blame] | 894 | sys.exit(main()) |