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