blob: fd701f9b1aee73d1d92f42dca73aaa87da490b8d [file] [log] [blame]
[email protected]fbe29322013-07-09 09:03:261#!/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]181a5c92013-09-06 17:11:467"""Runs all types of tests from one unified interface."""
[email protected]fbe29322013-07-09 09:03:268
jama47ca85c2014-12-03 18:38:079import argparse
[email protected]fbe29322013-07-09 09:03:2610import collections
jbudorickeb7ea71c2015-09-28 16:40:2011import itertools
[email protected]f7148dd42013-08-20 14:24:5712import logging
[email protected]fbe29322013-07-09 09:03:2613import os
[email protected]83bb8152013-11-19 15:02:2114import signal
[email protected]fbe29322013-07-09 09:03:2615import sys
[email protected]83bb8152013-11-19 15:02:2116import threading
jbudorick256fd532014-10-24 01:50:1317import unittest
[email protected]fbe29322013-07-09 09:03:2618
jbudorick0c2a94a2015-12-04 14:27:4319import devil_chromium
jbudorick061629442015-09-03 18:00:5720from devil import base_error
jbudoricke29693be2015-12-07 15:53:2321from devil import devil_env
jbudorick061629442015-09-03 18:00:5722from devil.android import apk_helper
23from devil.android import device_blacklist
24from devil.android import device_errors
25from devil.android import device_utils
jbudorickd645efe82015-12-11 19:09:5926from devil.android import forwarder
jbudorick061629442015-09-03 18:00:5727from devil.android import ports
28from devil.utils import reraiser_thread
29from devil.utils import run_tests_helper
30
[email protected]fbe29322013-07-09 09:03:2631from pylib import constants
jbudorickd28554a2016-01-11 16:22:5932from pylib.constants import host_paths
[email protected]fbe29322013-07-09 09:03:2633from pylib.base import base_test_result
jbudorick66dc3722014-11-06 21:33:5134from pylib.base import environment_factory
[email protected]6bc1bda22013-07-19 22:08:3735from pylib.base import test_dispatcher
jbudorick66dc3722014-11-06 21:33:5136from pylib.base import test_instance_factory
37from pylib.base import test_run_factory
[email protected]6b6abac6d2013-10-03 11:56:3838from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1339from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3740from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2241from pylib.instrumentation import test_options as instrumentation_test_options
jbudorick9a6b7b332014-09-20 00:01:0742from pylib.junit import setup as junit_setup
43from pylib.junit import test_dispatcher as junit_dispatcher
[email protected]3dbdfa42013-08-08 01:08:1444from pylib.monkey import setup as monkey_setup
45from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4746from pylib.perf import setup as perf_setup
47from pylib.perf import test_options as perf_test_options
48from pylib.perf import test_runner as perf_test_runner
jbudorickb8c42072014-12-01 18:07:5449from pylib.results import json_results
50from pylib.results import report_results
[email protected]fbe29322013-07-09 09:03:2651
52
jbudorick0c2a94a2015-12-04 14:27:4353_DEVIL_STATIC_CONFIG_FILE = os.path.abspath(os.path.join(
jbudorickd28554a2016-01-11 16:22:5954 host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'devil_config.json'))
jbudorick0c2a94a2015-12-04 14:27:4355
56
jama47ca85c2014-12-03 18:38:0757def AddCommonOptions(parser):
58 """Adds all common options to |parser|."""
[email protected]fbe29322013-07-09 09:03:2659
jama47ca85c2014-12-03 18:38:0760 group = parser.add_argument_group('Common Options')
61
[email protected]dfffbcbc2013-09-17 22:06:0162 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
jama47ca85c2014-12-03 18:38:0763
64 debug_or_release_group = group.add_mutually_exclusive_group()
65 debug_or_release_group.add_argument(
66 '--debug', action='store_const', const='Debug', dest='build_type',
67 default=default_build_type,
68 help=('If set, run test suites under out/Debug. '
69 'Default is env var BUILDTYPE or Debug.'))
70 debug_or_release_group.add_argument(
71 '--release', action='store_const', const='Release', dest='build_type',
72 help=('If set, run test suites under out/Release. '
73 'Default is env var BUILDTYPE or Debug.'))
74
75 group.add_argument('--build-directory', dest='build_directory',
76 help=('Path to the directory in which build files are'
77 ' located (should not include build type)'))
78 group.add_argument('--output-directory', dest='output_directory',
79 help=('Path to the directory in which build files are'
80 ' located (must include build type). This will take'
81 ' precedence over --debug, --release and'
82 ' --build-directory'))
agrieveddb11f12015-10-23 17:03:4383 group.add_argument('--num_retries', '--num-retries', dest='num_retries',
84 type=int, default=2,
jama47ca85c2014-12-03 18:38:0785 help=('Number of retries for a test before '
86 'giving up (default: %(default)s).'))
87 group.add_argument('-v',
88 '--verbose',
89 dest='verbose_count',
90 default=0,
91 action='count',
92 help='Verbose level (multiple times for more)')
93 group.add_argument('--flakiness-dashboard-server',
94 dest='flakiness_dashboard_server',
95 help=('Address of the server that is hosting the '
96 'Chrome for Android flakiness dashboard.'))
97 group.add_argument('--enable-platform-mode', action='store_true',
98 help=('Run the test scripts in platform mode, which '
99 'conceptually separates the test runner from the '
100 '"device" (local or remote, real or emulated) on '
101 'which the tests are running. [experimental]'))
102 group.add_argument('-e', '--environment', default='local',
103 choices=constants.VALID_ENVIRONMENTS,
104 help='Test environment to run in (default: %(default)s).')
105 group.add_argument('--adb-path',
106 help=('Specify the absolute path of the adb binary that '
107 'should be used.'))
stipa5733b52015-12-02 08:17:19108 group.add_argument('--json-results-file', '--test-launcher-summary-output',
109 dest='json_results_file',
jama47ca85c2014-12-03 18:38:07110 help='If set, will dump results in JSON form '
111 'to specified file.')
mikecaseb0bafb02016-02-09 04:24:44112
113 logcat_output_group = group.add_mutually_exclusive_group()
114 logcat_output_group.add_argument(
115 '--logcat-output-dir',
116 help='If set, will dump logcats recorded during test run to directory. '
117 'File names will be the device ids with timestamps.')
118 logcat_output_group.add_argument(
119 '--logcat-output-file',
120 help='If set, will merge logcats recorded during test run and dump them '
121 'to the specified file.')
[email protected]fbe29322013-07-09 09:03:26122
jama47ca85c2014-12-03 18:38:07123def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26124 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07125 run_tests_helper.SetLogLevel(args.verbose_count)
126 constants.SetBuildType(args.build_type)
127 if args.build_directory:
128 constants.SetBuildDirectory(args.build_directory)
129 if args.output_directory:
mikecase0aea9c52015-04-30 00:12:33130 constants.SetOutputDirectory(args.output_directory)
jbudorick0c2a94a2015-12-04 14:27:43131
132 devil_custom_deps = None
jama47ca85c2014-12-03 18:38:07133 if args.adb_path:
jbudorick0c2a94a2015-12-04 14:27:43134 devil_custom_deps = {
135 'adb': {
jbudoricke29693be2015-12-07 15:53:23136 devil_env.GetPlatform(): [args.adb_path]
jbudorick0c2a94a2015-12-04 14:27:43137 }
138 }
139
140 devil_chromium.Initialize(
141 output_directory=constants.GetOutDirectory(),
142 custom_deps=devil_custom_deps)
143
mikecase48e16bf2014-11-19 22:46:45144 # Some things such as Forwarder require ADB to be in the environment path.
145 adb_dir = os.path.dirname(constants.GetAdbPath())
146 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
147 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26148
149
rnephew5c499782014-12-12 19:08:55150def AddRemoteDeviceOptions(parser):
151 group = parser.add_argument_group('Remote Device Options')
152
rnephewefe44b42015-02-04 04:45:15153 group.add_argument('--trigger',
jbudoricke6c560152015-01-13 23:49:28154 help=('Only triggers the test if set. Stores test_run_id '
155 'in given file path. '))
rnephewefe44b42015-02-04 04:45:15156 group.add_argument('--collect',
jbudoricke6c560152015-01-13 23:49:28157 help=('Only collects the test results if set. '
158 'Gets test_run_id from given file path.'))
rnephewefe44b42015-02-04 04:45:15159 group.add_argument('--remote-device', action='append',
jbudoricke6c560152015-01-13 23:49:28160 help='Device type to run test on.')
rnephewefe44b42015-02-04 04:45:15161 group.add_argument('--results-path',
jbudoricke6c560152015-01-13 23:49:28162 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11163 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28164 help='HTTP protocol to use. (http or https)')
rnephewefe44b42015-02-04 04:45:15165 group.add_argument('--api-address',
166 help='Address to send HTTP requests.')
167 group.add_argument('--api-port',
168 help='Port to send HTTP requests to.')
169 group.add_argument('--runner-type',
jbudoricke6c560152015-01-13 23:49:28170 help='Type of test to run as.')
rnephewefe44b42015-02-04 04:45:15171 group.add_argument('--runner-package',
172 help='Package name of test.')
173 group.add_argument('--device-type',
rnephewa46fc562015-01-23 16:00:14174 choices=constants.VALID_DEVICE_TYPES,
175 help=('Type of device to run on. iOS or android'))
rnephewefe44b42015-02-04 04:45:15176 group.add_argument('--device-oem', action='append',
177 help='Device OEM to run on.')
178 group.add_argument('--remote-device-file',
179 help=('File with JSON to select remote device. '
180 'Overrides all other flags.'))
rnephewc9ae8f52015-02-13 03:02:55181 group.add_argument('--remote-device-timeout', type=int,
182 help='Times to retry finding remote device')
mikecase520cbbb52015-04-21 18:51:18183 group.add_argument('--network-config', type=int,
184 help='Integer that specifies the network environment '
185 'that the tests will be run in.')
mikecaseddfa35d2015-10-28 01:14:27186 group.add_argument('--test-timeout', type=int,
187 help='Test run timeout in seconds.')
rnephewefe44b42015-02-04 04:45:15188
189 device_os_group = group.add_mutually_exclusive_group()
190 device_os_group.add_argument('--remote-device-minimum-os',
191 help='Minimum OS on device.')
192 device_os_group.add_argument('--remote-device-os', action='append',
193 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55194
195 api_secret_group = group.add_mutually_exclusive_group()
196 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28197 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55198 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28199 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55200
201 api_key_group = group.add_mutually_exclusive_group()
202 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28203 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55204 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28205 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55206
207
jama47ca85c2014-12-03 18:38:07208def AddDeviceOptions(parser):
209 """Adds device options to |parser|."""
210 group = parser.add_argument_group(title='Device Options')
jama47ca85c2014-12-03 18:38:07211 group.add_argument('--tool',
212 dest='tool',
213 help=('Run the test under a tool '
214 '(use --tool help to list them)'))
215 group.add_argument('-d', '--device', dest='test_device',
216 help=('Target device for the test suite '
217 'to run on.'))
jbudorickdde688fb2015-08-27 03:00:17218 group.add_argument('--blacklist-file', help='Device blacklist file.')
agrievea538a142015-10-09 15:45:56219 group.add_argument('--enable-device-cache', action='store_true',
220 help='Cache device state to disk between runs')
agrieve1a02e582015-10-15 21:35:39221 group.add_argument('--incremental-install', action='store_true',
222 help='Use an _incremental apk.')
agrieve8bcb52e2015-10-20 19:38:33223 group.add_argument('--enable-concurrent-adb', action='store_true',
224 help='Run multiple adb commands at the same time, even '
225 'for the same device.')
agrievecd243a22016-02-08 23:25:14226 group.add_argument('--skip-clear-data', action='store_true',
227 help='Do not wipe app data between tests. Use this to '
228 'speed up local development and never on bots '
229 '(increases flakiness)')
jbudorick256fd532014-10-24 01:50:13230
231
jama47ca85c2014-12-03 18:38:07232def AddGTestOptions(parser):
233 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26234
jama47ca85c2014-12-03 18:38:07235 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49236 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07237 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick277f2312015-09-24 16:37:43238 help='Executable name of the test suite to run.')
jama47ca85c2014-12-03 18:38:07239 group.add_argument('--gtest_also_run_disabled_tests',
240 '--gtest-also-run-disabled-tests',
241 dest='run_disabled', action='store_true',
242 help='Also run disabled tests if applicable.')
243 group.add_argument('-a', '--test-arguments', dest='test_arguments',
244 default='',
245 help='Additional arguments to pass to the test.')
jbudorick24616eb2015-10-06 02:40:57246 group.add_argument('-t', '--shard-timeout',
mikecase68bcfc82016-01-22 20:14:38247 dest='shard_timeout', type=int, default=120,
jama47ca85c2014-12-03 18:38:07248 help='Timeout to wait for each test '
249 '(default: %(default)s).')
250 group.add_argument('--isolate_file_path',
251 '--isolate-file-path',
252 dest='isolate_file_path',
253 help='.isolate file path to override the default '
254 'path')
jbudorick5ee45892015-06-10 18:46:22255 group.add_argument('--app-data-file', action='append', dest='app_data_files',
256 help='A file path relative to the app data directory '
257 'that should be saved to the host.')
258 group.add_argument('--app-data-file-dir',
259 help='Host directory to which app data files will be'
260 ' saved. Used with --app-data-file.')
mlliud7f9fe92015-06-15 19:36:56261 group.add_argument('--delete-stale-data', dest='delete_stale_data',
262 action='store_true',
263 help='Delete stale test data on the device.')
jbudorickeb7ea71c2015-09-28 16:40:20264 group.add_argument('--repeat', '--gtest_repeat', '--gtest-repeat',
265 dest='repeat', type=int, default=0,
266 help='Number of times to repeat the specified set of '
267 'tests.')
alexandermonta3f03bf2015-12-02 18:56:45268 group.add_argument('--break-on-failure', '--break_on_failure',
269 dest='break_on_failure', action='store_true',
270 help='Whether to break on failure.')
agrieve0f5e53e2016-02-04 03:47:37271 group.add_argument('--extract-test-list-from-filter',
272 action='store_true',
273 help='When a test filter is specified, and the list of '
274 'tests can be determined from it, skip querying the '
275 'device for the list of all tests. Speeds up local '
276 'development, but is not safe to use on bots ('
277 'http://crbug.com/549214')
jbudorick442a6932015-02-03 03:01:15278
279 filter_group = group.add_mutually_exclusive_group()
280 filter_group.add_argument('-f', '--gtest_filter', '--gtest-filter',
281 dest='test_filter',
282 help='googletest-style filter string.')
283 filter_group.add_argument('--gtest-filter-file', dest='test_filter_file',
284 help='Path to file that contains googletest-style '
285 'filter strings. (Lines will be joined with '
286 '":" to create a single filter string.)')
287
jama47ca85c2014-12-03 18:38:07288 AddDeviceOptions(parser)
289 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55290 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26291
292
jama47ca85c2014-12-03 18:38:07293def AddLinkerTestOptions(parser):
294 group = parser.add_argument_group('Linker Test Options')
295 group.add_argument('-f', '--gtest-filter', dest='test_filter',
296 help='googletest-style filter string.')
297 AddCommonOptions(parser)
298 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38299
300
jama47ca85c2014-12-03 18:38:07301def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26302 """Adds the Java test options to |option_parser|."""
303
jama47ca85c2014-12-03 18:38:07304 argument_group.add_argument(
305 '-f', '--test-filter', dest='test_filter',
306 help=('Test filter (if not fully qualified, will run all matches).'))
307 argument_group.add_argument(
jbudorickeb7ea71c2015-09-28 16:40:20308 '--repeat', dest='repeat', type=int, default=0,
309 help='Number of times to repeat the specified set of tests.')
310 argument_group.add_argument(
alexandermonta3f03bf2015-12-02 18:56:45311 '--break-on-failure', '--break_on_failure',
312 dest='break_on_failure', action='store_true',
313 help='Whether to break on failure.')
314 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26315 '-A', '--annotation', dest='annotation_str',
316 help=('Comma-separated list of annotations. Run only tests with any of '
317 'the given annotations. An annotation can be either a key or a '
318 'key-values pair. A test that has no annotation is considered '
319 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07320 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26321 '-E', '--exclude-annotation', dest='exclude_annotation_str',
322 help=('Comma-separated list of annotations. Exclude tests with these '
323 'annotations.'))
jama47ca85c2014-12-03 18:38:07324 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59325 '--screenshot', dest='screenshot_failures', action='store_true',
326 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07327 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59328 '--save-perf-json', action='store_true',
329 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07330 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59331 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07332 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59333 '--test_data', '--test-data', action='append', default=[],
334 help=('Each instance defines a directory of test data that should be '
335 'copied to the target(s) before running the tests. The argument '
336 'should be of the form <target>:<source>, <target> is relative to '
337 'the device data directory, and <source> is relative to the '
338 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21339 argument_group.add_argument(
340 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
341 default=True, help='Removes the dalvik.vm.enableassertions property')
342
[email protected]fbe29322013-07-09 09:03:26343
344
jama47ca85c2014-12-03 18:38:07345def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26346 """Processes options/arguments and populates |options| with defaults."""
347
jama47ca85c2014-12-03 18:38:07348 # TODO(jbudorick): Handle most of this function in argparse.
349 if args.annotation_str:
350 args.annotations = args.annotation_str.split(',')
351 elif args.test_filter:
352 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26353 else:
jama47ca85c2014-12-03 18:38:07354 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
355 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26356
jama47ca85c2014-12-03 18:38:07357 if args.exclude_annotation_str:
358 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26359 else:
jama47ca85c2014-12-03 18:38:07360 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26361
[email protected]fbe29322013-07-09 09:03:26362
jama47ca85c2014-12-03 18:38:07363def AddInstrumentationTestOptions(parser):
364 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26365
jama47ca85c2014-12-03 18:38:07366 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26367
jama47ca85c2014-12-03 18:38:07368 group = parser.add_argument_group('Instrumentation Test Options')
369 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26370
jama47ca85c2014-12-03 18:38:07371 java_or_python_group = group.add_mutually_exclusive_group()
372 java_or_python_group.add_argument(
373 '-j', '--java-only', action='store_false',
374 dest='run_python_tests', default=True, help='Run only the Java tests.')
375 java_or_python_group.add_argument(
376 '-p', '--python-only', action='store_false',
377 dest='run_java_tests', default=True,
378 help='Run only the host-driven tests.')
379
380 group.add_argument('--host-driven-root',
381 help='Root of the host-driven tests.')
382 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
383 action='store_true',
384 help='Wait for debugger.')
jbudorick911be58d2015-01-13 02:51:06385 group.add_argument('--apk-under-test', dest='apk_under_test',
386 help=('the name of the apk under test.'))
jama47ca85c2014-12-03 18:38:07387 group.add_argument('--test-apk', dest='test_apk', required=True,
388 help=('The name of the apk containing the tests '
389 '(without the .apk extension; '
390 'e.g. "ContentShellTest").'))
mikecasee7258622015-09-29 13:47:35391 group.add_argument('--additional-apk', action='append',
mikecase8c4ab302015-09-29 17:03:29392 dest='additional_apks', default=[],
mikecasee7258622015-09-29 13:47:35393 help='Additional apk that must be installed on '
394 'the device when the tests are run')
jama47ca85c2014-12-03 18:38:07395 group.add_argument('--coverage-dir',
396 help=('Directory in which to place all generated '
397 'EMMA coverage files.'))
398 group.add_argument('--device-flags', dest='device_flags', default='',
399 help='The relative filepath to a file containing '
400 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06401 group.add_argument('--device-flags-file', default='',
402 help='The relative filepath to a file containing '
403 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07404 group.add_argument('--isolate_file_path',
405 '--isolate-file-path',
406 dest='isolate_file_path',
407 help='.isolate file path to override the default '
408 'path')
mlliud7f9fe92015-06-15 19:36:56409 group.add_argument('--delete-stale-data', dest='delete_stale_data',
410 action='store_true',
411 help='Delete stale test data on the device.')
jbudorickede49722015-11-25 05:16:34412 group.add_argument('--timeout-scale', type=float,
413 help='Factor by which timeouts should be scaled.')
wnwen1d202892016-02-02 20:22:58414 group.add_argument('--strict-mode', dest='strict_mode', default='testing',
wnwen2b56c152016-01-12 16:40:10415 help='StrictMode command-line flag set on the device, '
416 'death/testing to kill the process, off to stop '
417 'checking, flash to flash only. Default testing.')
jama47ca85c2014-12-03 18:38:07418
419 AddCommonOptions(parser)
420 AddDeviceOptions(parser)
rnephewe416dff2015-01-21 21:26:37421 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26422
423
jama47ca85c2014-12-03 18:38:07424def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22425 """Processes options/arguments and populate |options| with defaults.
426
427 Args:
jama47ca85c2014-12-03 18:38:07428 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22429
430 Returns:
431 An InstrumentationOptions named tuple which contains all options relevant to
432 instrumentation tests.
433 """
[email protected]fbe29322013-07-09 09:03:26434
jama47ca85c2014-12-03 18:38:07435 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26436
jama47ca85c2014-12-03 18:38:07437 if not args.host_driven_root:
438 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13439
jbudorick9ef3f9552015-10-20 22:58:33440 if os.path.exists(args.test_apk):
441 args.test_apk_path = args.test_apk
442 args.test_apk, _ = os.path.splitext(os.path.basename(args.test_apk))
443 else:
444 args.test_apk_path = os.path.join(
445 constants.GetOutDirectory(),
446 constants.SDK_BUILD_APKS_DIR,
447 '%s.apk' % args.test_apk)
448
jama47ca85c2014-12-03 18:38:07449 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15450 constants.GetOutDirectory(),
451 constants.SDK_BUILD_TEST_JAVALIB_DIR,
jbudorick9ef3f9552015-10-20 22:58:33452 '%s.jar' % args.test_apk)
yusufo72c598c02015-07-16 23:40:20453 args.test_support_apk_path = '%sSupport%s' % (
454 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46455
jama47ca85c2014-12-03 18:38:07456 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46457
jama47ca85c2014-12-03 18:38:07458 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22459 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07460 args.tool,
jama47ca85c2014-12-03 18:38:07461 args.annotations,
462 args.exclude_annotations,
463 args.test_filter,
464 args.test_data,
465 args.save_perf_json,
466 args.screenshot_failures,
467 args.wait_for_debugger,
468 args.coverage_dir,
469 args.test_apk,
470 args.test_apk_path,
471 args.test_apk_jar_path,
472 args.test_runner,
473 args.test_support_apk_path,
474 args.device_flags,
davileen98efad12015-01-05 19:48:21475 args.isolate_file_path,
mlliud7f9fe92015-06-15 19:36:56476 args.set_asserts,
jbudorickede49722015-11-25 05:16:34477 args.delete_stale_data,
jbudorick248e31a2016-01-06 16:28:11478 args.timeout_scale,
479 args.apk_under_test,
wnwen2b56c152016-01-12 16:40:10480 args.additional_apks,
agrievecd243a22016-02-08 23:25:14481 args.strict_mode,
482 args.skip_clear_data)
[email protected]2a684222013-08-01 16:59:22483
[email protected]fbe29322013-07-09 09:03:26484
jama47ca85c2014-12-03 18:38:07485def AddUIAutomatorTestOptions(parser):
486 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26487
jama47ca85c2014-12-03 18:38:07488 group = parser.add_argument_group('UIAutomator Test Options')
489 AddJavaTestOptions(group)
490 group.add_argument(
491 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
492 metavar='PACKAGE', help='Package under test.')
493 group.add_argument(
494 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26495 help=('The name of the dexed jar containing the tests (without the '
496 '.dex.jar extension). Alternatively, this can be a full path '
497 'to the jar.'))
498
jama47ca85c2014-12-03 18:38:07499 AddCommonOptions(parser)
500 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26501
502
jama47ca85c2014-12-03 18:38:07503def AddJUnitTestOptions(parser):
504 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07505
jama47ca85c2014-12-03 18:38:07506 group = parser.add_argument_group('JUnit Test Options')
507 group.add_argument(
508 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07509 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07510 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07511 '-f', '--test-filter', dest='test_filter',
512 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07513 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07514 '--package-filter', dest='package_filter',
515 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07516 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07517 '--runner-filter', dest='runner_filter',
518 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07519 group.add_argument(
520 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07521 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07522 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07523
524
jama47ca85c2014-12-03 18:38:07525def AddMonkeyTestOptions(parser):
526 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07527
jama47ca85c2014-12-03 18:38:07528 group = parser.add_argument_group('Monkey Test Options')
529 group.add_argument(
530 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
531 metavar='PACKAGE', help='Package under test.')
532 group.add_argument(
533 '--event-count', default=10000, type=int,
534 help='Number of events to generate (default: %(default)s).')
535 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14536 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12537 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07538 group.add_argument(
539 '--throttle', default=100, type=int,
540 help='Delay between events (ms) (default: %(default)s). ')
541 group.add_argument(
542 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14543 help=('Seed value for pseudo-random generator. Same seed value generates '
544 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07545 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14546 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07547 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14548
jama47ca85c2014-12-03 18:38:07549 AddCommonOptions(parser)
550 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14551
jama47ca85c2014-12-03 18:38:07552def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14553 """Processes all monkey test options.
554
555 Args:
jama47ca85c2014-12-03 18:38:07556 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14557
558 Returns:
559 A MonkeyOptions named tuple which contains all options relevant to
560 monkey tests.
561 """
jama47ca85c2014-12-03 18:38:07562 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
563 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14564 if category:
jama47ca85c2014-12-03 18:38:07565 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14566
jama47ca85c2014-12-03 18:38:07567 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14568 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07569 args.verbose_count,
570 args.package,
571 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14572 category,
jama47ca85c2014-12-03 18:38:07573 args.throttle,
574 args.seed,
575 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14576
rnephew5c499782014-12-12 19:08:55577def AddUirobotTestOptions(parser):
578 """Adds uirobot test options to |option_parser|."""
579 group = parser.add_argument_group('Uirobot Test Options')
580
rnephewefe44b42015-02-04 04:45:15581 group.add_argument('--app-under-test', required=True,
582 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55583 group.add_argument(
mikecaseafa43842015-10-19 23:04:12584 '--repeat', dest='repeat', type=int, default=0,
585 help='Number of times to repeat the uirobot test.')
586 group.add_argument(
rnephew5c499782014-12-12 19:08:55587 '--minutes', default=5, type=int,
jbudorick676b1202015-02-06 22:02:27588 help='Number of minutes to run uirobot test [default: %(default)s].')
rnephew5c499782014-12-12 19:08:55589
590 AddCommonOptions(parser)
591 AddDeviceOptions(parser)
592 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14593
jama47ca85c2014-12-03 18:38:07594def AddPerfTestOptions(parser):
595 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47596
jama47ca85c2014-12-03 18:38:07597 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47598
jama47ca85c2014-12-03 18:38:07599 class SingleStepAction(argparse.Action):
600 def __call__(self, parser, namespace, values, option_string=None):
601 if values and not namespace.single_step:
602 parser.error('single step command provided, '
603 'but --single-step not specified.')
604 elif namespace.single_step and not values:
605 parser.error('--single-step specified, '
606 'but no single step command provided.')
607 setattr(namespace, self.dest, values)
608
609 step_group = group.add_mutually_exclusive_group(required=True)
610 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
611 # This requires removing "--" from client calls.
612 step_group.add_argument(
613 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52614 help='Execute the given command with retries, but only print the result '
615 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07616 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46617 '--steps',
[email protected]def4bce2013-11-12 12:59:52618 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07619 step_group.add_argument(
620 '--print-step',
621 help='The name of a previously executed perf step to print.')
622
623 group.add_argument(
peterbd4e73d2014-12-03 15:47:36624 '--output-json-list',
625 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07626 group.add_argument(
peterbd4e73d2014-12-03 15:47:36627 '--collect-chartjson-data',
628 action='store_true',
629 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07630 group.add_argument(
peterbd4e73d2014-12-03 15:47:36631 '--output-chartjson-data',
632 default='',
633 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07634 group.add_argument(
perezju67cf7f12015-09-29 11:39:05635 '--get-output-dir-archive', metavar='FILENAME',
636 help='Write the chached output directory archived by a step into the'
637 ' given ZIP file.')
638 group.add_argument(
jama47ca85c2014-12-03 18:38:07639 '--flaky-steps',
640 help=('A JSON file containing steps that are flaky '
641 'and will have its exit code ignored.'))
642 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46643 '--no-timeout', action='store_true',
644 help=('Do not impose a timeout. Each perf step is responsible for '
645 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07646 group.add_argument(
[email protected]650487c2013-09-30 11:40:49647 '-f', '--test-filter',
648 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07649 group.add_argument(
650 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49651 help='Just print the steps without executing.')
jbudorick5cfff872015-07-01 18:46:13652 # Uses 0.1 degrees C because that's what Android does.
653 group.add_argument(
654 '--max-battery-temp', type=int,
655 help='Only start tests when the battery is at or below the given '
656 'temperature (0.1 C)')
jama47ca85c2014-12-03 18:38:07657 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
658 help='If --single-step is specified, the command to run.')
rnephewdde05da82015-07-09 20:31:01659 group.add_argument('--min-battery-level', type=int,
660 help='Only starts tests when the battery is charged above '
661 'given level.')
jama47ca85c2014-12-03 18:38:07662 AddCommonOptions(parser)
663 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47664
665
jama47ca85c2014-12-03 18:38:07666def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47667 """Processes all perf test options.
668
669 Args:
jama47ca85c2014-12-03 18:38:07670 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47671
672 Returns:
673 A PerfOptions named tuple which contains all options relevant to
674 perf tests.
675 """
jama47ca85c2014-12-03 18:38:07676 # TODO(jbudorick): Move single_step handling down into the perf tests.
677 if args.single_step:
678 args.single_step = ' '.join(args.single_step_command)
679 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47680 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07681 args.steps, args.flaky_steps, args.output_json_list,
682 args.print_step, args.no_timeout, args.test_filter,
683 args.dry_run, args.single_step, args.collect_chartjson_data,
perezju67cf7f12015-09-29 11:39:05684 args.output_chartjson_data, args.get_output_dir_archive,
685 args.max_battery_temp, args.min_battery_level)
[email protected]ec3170b2013-08-14 14:39:47686
687
jama47ca85c2014-12-03 18:38:07688def AddPythonTestOptions(parser):
689 group = parser.add_argument_group('Python Test Options')
690 group.add_argument(
691 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
692 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
693 help='Name of the test suite to run.')
694 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13695
696
jama47ca85c2014-12-03 18:38:07697def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38698 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07699 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38700
701 results, exit_code = test_dispatcher.RunTests(
702 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07703 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38704
705 report_results.LogFull(
706 results=results,
707 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22708 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38709
jama47ca85c2014-12-03 18:38:07710 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20711 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54712
[email protected]6b6abac6d2013-10-03 11:56:38713 return exit_code
714
715
jama47ca85c2014-12-03 18:38:07716def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37717 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jbudorick58b4d362015-09-08 16:44:59718 logging.info('_RunInstrumentationTests(%s, %s)', str(args), str(devices))
[email protected]6bc1bda22013-07-19 22:08:37719
jama47ca85c2014-12-03 18:38:07720 instrumentation_options = ProcessInstrumentationOptions(args)
721
722 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57723 logging.warning('Debugger can not be sharded, using first available device')
724 devices = devices[:1]
725
[email protected]6bc1bda22013-07-19 22:08:37726 results = base_test_result.TestRunResults()
727 exit_code = 0
728
jama47ca85c2014-12-03 18:38:07729 if args.run_java_tests:
jbudorickeb7ea71c2015-09-28 16:40:20730 java_runner_factory, java_tests = instrumentation_setup.Setup(
mikecase526d68e2014-11-19 20:02:05731 instrumentation_options, devices)
jbudorickeb7ea71c2015-09-28 16:40:20732 else:
733 java_runner_factory = None
734 java_tests = None
[email protected]6bc1bda22013-07-19 22:08:37735
jama47ca85c2014-12-03 18:38:07736 if args.run_python_tests:
jbudorickeb7ea71c2015-09-28 16:40:20737 py_runner_factory, py_tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07738 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13739 instrumentation_options)
jbudorickeb7ea71c2015-09-28 16:40:20740 else:
741 py_runner_factory = None
742 py_tests = None
[email protected]37ee0c792013-08-06 19:10:13743
jbudorickeb7ea71c2015-09-28 16:40:20744 results = []
745 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
746 else itertools.count())
alexandermonta3f03bf2015-12-02 18:56:45747
alexandermonte2cbe022015-12-16 04:58:58748 code_counts = {constants.INFRA_EXIT_CODE: 0,
749 constants.ERROR_EXIT_CODE: 0,
750 constants.WARNING_EXIT_CODE: 0,
751 0: 0}
752
alexandermonta3f03bf2015-12-02 18:56:45753 def _escalate_code(old, new):
754 for x in (constants.INFRA_EXIT_CODE,
755 constants.ERROR_EXIT_CODE,
756 constants.WARNING_EXIT_CODE):
757 if x in (old, new):
758 return x
759 return 0
760
jbudorickeb7ea71c2015-09-28 16:40:20761 for _ in repetitions:
762 iteration_results = base_test_result.TestRunResults()
763 if java_tests:
[email protected]34020022013-08-06 23:35:34764 test_results, test_exit_code = test_dispatcher.RunTests(
jbudorickeb7ea71c2015-09-28 16:40:20765 java_tests, java_runner_factory, devices, shard=True,
766 test_timeout=None, num_retries=args.num_retries)
767 iteration_results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37768
alexandermonte2cbe022015-12-16 04:58:58769 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45770 exit_code = _escalate_code(exit_code, test_exit_code)
[email protected]6bc1bda22013-07-19 22:08:37771
jbudorickeb7ea71c2015-09-28 16:40:20772 if py_tests:
773 test_results, test_exit_code = test_dispatcher.RunTests(
774 py_tests, py_runner_factory, devices, shard=True, test_timeout=None,
775 num_retries=args.num_retries)
776 iteration_results.AddTestRunResults(test_results)
[email protected]4f777ca2014-08-08 01:45:59777
alexandermonte2cbe022015-12-16 04:58:58778 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45779 exit_code = _escalate_code(exit_code, test_exit_code)
jbudorickeb7ea71c2015-09-28 16:40:20780
781 results.append(iteration_results)
782 report_results.LogFull(
783 results=iteration_results,
784 test_type='Instrumentation',
785 test_package=os.path.basename(args.test_apk),
786 annotation=args.annotations,
787 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37788
alexandermonte2cbe022015-12-16 04:58:58789
alexandermonta3f03bf2015-12-02 18:56:45790 if args.break_on_failure and exit_code in (constants.ERROR_EXIT_CODE,
791 constants.INFRA_EXIT_CODE):
792 break
793
alexandermonte2cbe022015-12-16 04:58:58794 logging.critical('Instr tests: %s success, %s infra, %s errors, %s warnings',
795 str(code_counts[0]),
796 str(code_counts[constants.INFRA_EXIT_CODE]),
797 str(code_counts[constants.ERROR_EXIT_CODE]),
798 str(code_counts[constants.WARNING_EXIT_CODE]))
799
jama47ca85c2014-12-03 18:38:07800 if args.json_results_file:
801 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54802
[email protected]6bc1bda22013-07-19 22:08:37803 return exit_code
804
805
jama47ca85c2014-12-03 18:38:07806def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07807 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07808 runner_factory, tests = junit_setup.Setup(args)
mikecasec638a072015-04-01 16:35:35809 results, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
810
811 report_results.LogFull(
812 results=results,
813 test_type='JUnit',
814 test_package=args.test_suite)
815
mikecase572401b2015-04-09 02:28:57816 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20817 json_results.GenerateJsonResultsFile([results], args.json_results_file)
mikecase572401b2015-04-09 02:28:57818
jbudorick9a6b7b332014-09-20 00:01:07819 return exit_code
820
821
jama47ca85c2014-12-03 18:38:07822def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14823 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07824 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14825
826 runner_factory, tests = monkey_setup.Setup(monkey_options)
827
828 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46829 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07830 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14831
832 report_results.LogFull(
833 results=results,
834 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28835 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14836
jama47ca85c2014-12-03 18:38:07837 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20838 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54839
[email protected]3dbdfa42013-08-08 01:08:14840 return exit_code
841
842
jbudorickdde688fb2015-08-27 03:00:17843def _RunPerfTests(args, active_devices):
[email protected]ec3170b2013-08-14 14:39:47844 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07845 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56846
847 # Just save a simple json with a list of test names.
848 if perf_options.output_json_list:
849 return perf_test_runner.OutputJsonList(
850 perf_options.steps, perf_options.output_json_list)
851
[email protected]ad32f312013-11-13 04:03:29852 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47853 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42854 return perf_test_runner.PrintTestOutput(
perezju67cf7f12015-09-29 11:39:05855 perf_options.print_step, perf_options.output_chartjson_data,
856 perf_options.get_output_dir_archive)
[email protected]ec3170b2013-08-14 14:39:47857
jbudorickdde688fb2015-08-27 03:00:17858 runner_factory, tests, devices = perf_setup.Setup(
859 perf_options, active_devices)
[email protected]ec3170b2013-08-14 14:39:47860
[email protected]a72f0752014-06-03 23:52:34861 # shard=False means that each device will get the full list of tests
862 # and then each one will decide their own affinity.
863 # shard=True means each device will pop the next test available from a queue,
864 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57865 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34866 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07867 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47868
869 report_results.LogFull(
870 results=results,
871 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12872 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52873
jama47ca85c2014-12-03 18:38:07874 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20875 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54876
[email protected]def4bce2013-11-12 12:59:52877 if perf_options.single_step:
878 return perf_test_runner.PrintTestOutput('single_step')
879
[email protected]11ce8452014-02-17 10:55:03880 perf_test_runner.PrintSummary(tests)
881
[email protected]86184c7b2013-08-15 15:06:57882 # Always return 0 on the sharding stage. Individual tests exit_code
883 # will be returned on the print_step stage.
884 return 0
[email protected]ec3170b2013-08-14 14:39:47885
[email protected]3dbdfa42013-08-08 01:08:14886
jama47ca85c2014-12-03 18:38:07887def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13888 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07889 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13890 suite_path = suite_vars['path']
891 suite_test_modules = suite_vars['test_modules']
892
893 sys.path = [suite_path] + sys.path
894 try:
895 suite = unittest.TestSuite()
896 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
897 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07898 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13899 return 0 if runner.run(suite).wasSuccessful() else 1
900 finally:
901 sys.path = sys.path[1:]
902
903
agrievea538a142015-10-09 15:45:56904def _GetAttachedDevices(blacklist_file, test_device, enable_cache):
[email protected]f7148dd42013-08-20 14:24:57905 """Get all attached devices.
906
907 Args:
agrievea538a142015-10-09 15:45:56908 blacklist_file: Path to device blacklist.
[email protected]f7148dd42013-08-20 14:24:57909 test_device: Name of a specific device to use.
agrievea538a142015-10-09 15:45:56910 enable_cache: Whether to enable checksum caching.
[email protected]f7148dd42013-08-20 14:24:57911
912 Returns:
913 A list of attached devices.
914 """
jbudoricka583ba32015-09-11 17:23:19915 blacklist = (device_blacklist.Blacklist(blacklist_file)
916 if blacklist_file
917 else None)
jbudorickdde688fb2015-08-27 03:00:17918
agrievea538a142015-10-09 15:45:56919 attached_devices = device_utils.DeviceUtils.HealthyDevices(
920 blacklist, enable_device_files_cache=enable_cache)
aberent6a02a6182015-04-29 11:07:55921 if test_device:
jbudorick4551d0dc2015-04-29 16:07:06922 test_device = [d for d in attached_devices if d == test_device]
923 if not test_device:
924 raise device_errors.DeviceUnreachableError(
925 'Did not find device %s among attached device. Attached devices: %s'
926 % (test_device, ', '.join(attached_devices)))
927 return test_device
aberent6a02a6182015-04-29 11:07:55928
jbudorick4551d0dc2015-04-29 16:07:06929 else:
930 if not attached_devices:
931 raise device_errors.NoDevicesError()
932 return sorted(attached_devices)
[email protected]f7148dd42013-08-20 14:24:57933
934
agrieve63d20782016-02-04 17:47:26935def RunTestsCommand(args): # pylint: disable=too-many-return-statements
[email protected]fbe29322013-07-09 09:03:26936 """Checks test type and dispatches to the appropriate function.
937
938 Args:
jama47ca85c2014-12-03 18:38:07939 args: argparse.Namespace object.
[email protected]fbe29322013-07-09 09:03:26940
941 Returns:
942 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10943
944 Raises:
945 Exception: Unknown command name passed in, or an exception from an
946 individual test runner.
[email protected]fbe29322013-07-09 09:03:26947 """
jama47ca85c2014-12-03 18:38:07948 command = args.command
[email protected]fbe29322013-07-09 09:03:26949
jama47ca85c2014-12-03 18:38:07950 ProcessCommonOptions(args)
[email protected]d82f0252013-07-12 23:22:57951
jama47ca85c2014-12-03 18:38:07952 if args.enable_platform_mode:
agrieve63d20782016-02-04 17:47:26953 return RunTestsInPlatformMode(args)
jbudorick66dc3722014-11-06 21:33:51954
[email protected]c0662e092013-11-12 11:51:25955 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40956 if not ports.ResetTestServerPortAllocation():
957 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25958
agrieve18930bd2015-10-09 17:41:42959 def get_devices():
960 return _GetAttachedDevices(args.blacklist_file, args.test_device,
961 args.enable_device_cache)
962
[email protected]fbe29322013-07-09 09:03:26963 if command == 'gtest':
agrieve63d20782016-02-04 17:47:26964 return RunTestsInPlatformMode(args)
[email protected]6b6abac6d2013-10-03 11:56:38965 elif command == 'linker':
agrieve18930bd2015-10-09 17:41:42966 return _RunLinkerTests(args, get_devices())
[email protected]fbe29322013-07-09 09:03:26967 elif command == 'instrumentation':
agrieve18930bd2015-10-09 17:41:42968 return _RunInstrumentationTests(args, get_devices())
jbudorick9a6b7b332014-09-20 00:01:07969 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07970 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:14971 elif command == 'monkey':
agrieve18930bd2015-10-09 17:41:42972 return _RunMonkeyTests(args, get_devices())
[email protected]ec3170b2013-08-14 14:39:47973 elif command == 'perf':
agrieve18930bd2015-10-09 17:41:42974 return _RunPerfTests(args, get_devices())
jbudorick256fd532014-10-24 01:50:13975 elif command == 'python':
jama47ca85c2014-12-03 18:38:07976 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:26977 else:
[email protected]6bc1bda22013-07-19 22:08:37978 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26979
[email protected]fbe29322013-07-09 09:03:26980
jbudorick66dc3722014-11-06 21:33:51981_SUPPORTED_IN_PLATFORM_MODE = [
982 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:06983 'gtest',
984 'instrumentation',
985 'uirobot',
jbudorick66dc3722014-11-06 21:33:51986]
987
988
agrieve63d20782016-02-04 17:47:26989def RunTestsInPlatformMode(args):
jbudorick66dc3722014-11-06 21:33:51990
jbudorick566592ab2015-09-21 15:32:47991 def infra_error(message):
agrieve63d20782016-02-04 17:47:26992 logging.fatal(message)
993 sys.exit(constants.INFRA_EXIT_CODE)
jbudorickb9b0ada2015-09-17 22:52:58994
jbudorick566592ab2015-09-21 15:32:47995 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
996 infra_error('%s is not yet supported in platform mode' % args.command)
997
998 with environment_factory.CreateEnvironment(args, infra_error) as env:
999 with test_instance_factory.CreateTestInstance(args, infra_error) as test:
jbudorick66dc3722014-11-06 21:33:511000 with test_run_factory.CreateTestRun(
jbudorick566592ab2015-09-21 15:32:471001 args, env, test, infra_error) as test_run:
jbudorickeb7ea71c2015-09-28 16:40:201002 results = []
1003 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
1004 else itertools.count())
alexandermonte2cbe022015-12-16 04:58:581005 result_counts = collections.defaultdict(
1006 lambda: collections.defaultdict(int))
1007 iteration_count = 0
jbudorickeb7ea71c2015-09-28 16:40:201008 for _ in repetitions:
1009 iteration_results = test_run.RunTests()
jbudorickeb7ea71c2015-09-28 16:40:201010 if iteration_results is not None:
alexandermonte2cbe022015-12-16 04:58:581011 iteration_count += 1
jbudorickd4f77982015-09-28 21:09:181012 results.append(iteration_results)
alexandermonte2cbe022015-12-16 04:58:581013 for r in iteration_results.GetAll():
1014 result_counts[r.GetName()][r.GetType()] += 1
jbudorickeb7ea71c2015-09-28 16:40:201015 report_results.LogFull(
1016 results=iteration_results,
1017 test_type=test.TestType(),
1018 test_package=test_run.TestPackage(),
1019 annotation=getattr(args, 'annotations', None),
1020 flakiness_server=getattr(args, 'flakiness_dashboard_server',
1021 None))
alexandermonta3f03bf2015-12-02 18:56:451022 if args.break_on_failure and not iteration_results.DidRunPass():
1023 break
jbudorick66dc3722014-11-06 21:33:511024
alexandermonte2cbe022015-12-16 04:58:581025 if iteration_count > 1:
1026 # display summary results
1027 # only display results for a test if at least one test did not pass
1028 all_pass = 0
1029 tot_tests = 0
1030 for test_name in result_counts:
1031 tot_tests += 1
1032 if any(result_counts[test_name][x] for x in (
1033 base_test_result.ResultType.FAIL,
1034 base_test_result.ResultType.CRASH,
1035 base_test_result.ResultType.TIMEOUT,
1036 base_test_result.ResultType.UNKNOWN)):
1037 logging.critical(
1038 '%s: %s',
1039 test_name,
1040 ', '.join('%s %s' % (str(result_counts[test_name][i]), i)
1041 for i in base_test_result.ResultType.GetTypes()))
1042 else:
1043 all_pass += 1
1044
1045 logging.critical('%s of %s tests passed in all %s runs',
1046 str(all_pass),
1047 str(tot_tests),
1048 str(iteration_count))
1049
jama47ca85c2014-12-03 18:38:071050 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:541051 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:071052 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:541053
jbudorickeb7ea71c2015-09-28 16:40:201054 return (0 if all(r.DidRunPass() for r in results)
1055 else constants.ERROR_EXIT_CODE)
jbudorick66dc3722014-11-06 21:33:511056
1057
jama47ca85c2014-12-03 18:38:071058CommandConfigTuple = collections.namedtuple(
1059 'CommandConfigTuple',
1060 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:261061VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:071062 'gtest': CommandConfigTuple(
1063 AddGTestOptions,
1064 'googletest-based C++ tests'),
1065 'instrumentation': CommandConfigTuple(
1066 AddInstrumentationTestOptions,
1067 'InstrumentationTestCase-based Java tests'),
jama47ca85c2014-12-03 18:38:071068 'junit': CommandConfigTuple(
1069 AddJUnitTestOptions,
1070 'JUnit4-based Java tests'),
1071 'monkey': CommandConfigTuple(
1072 AddMonkeyTestOptions,
1073 "Tests based on Android's monkey"),
1074 'perf': CommandConfigTuple(
1075 AddPerfTestOptions,
1076 'Performance tests'),
1077 'python': CommandConfigTuple(
1078 AddPythonTestOptions,
1079 'Python tests based on unittest.TestCase'),
1080 'linker': CommandConfigTuple(
1081 AddLinkerTestOptions,
1082 'Linker tests'),
rnephew5c499782014-12-12 19:08:551083 'uirobot': CommandConfigTuple(
1084 AddUirobotTestOptions,
1085 'Uirobot test'),
jama47ca85c2014-12-03 18:38:071086}
[email protected]fbe29322013-07-09 09:03:261087
1088
[email protected]7c53a602014-03-24 16:21:441089def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:241090 for thread in threading.enumerate():
1091 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:211092
1093
[email protected]7c53a602014-03-24 16:21:441094def main():
[email protected]83bb8152013-11-19 15:02:211095 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:071096
1097 parser = argparse.ArgumentParser()
1098 command_parsers = parser.add_subparsers(title='test types',
1099 dest='command')
1100
1101 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
1102 key=lambda x: x[0]):
1103 subparser = command_parsers.add_parser(
1104 test_type, usage='%(prog)s [options]', help=config.help_txt)
1105 config.add_options_func(subparser)
1106
1107 args = parser.parse_args()
mikecasee74051022015-02-26 23:08:221108
1109 try:
agrieve63d20782016-02-04 17:47:261110 return RunTestsCommand(args)
mikecasee74051022015-02-26 23:08:221111 except base_error.BaseError as e:
1112 logging.exception('Error occurred.')
1113 if e.is_infra_error:
1114 return constants.INFRA_EXIT_CODE
mswecce6732015-06-06 00:31:331115 return constants.ERROR_EXIT_CODE
mikecasee74051022015-02-26 23:08:221116 except: # pylint: disable=W0702
1117 logging.exception('Unrecognized error occurred.')
1118 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:261119
[email protected]fbe29322013-07-09 09:03:261120
1121if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:441122 sys.exit(main())