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