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