blob: 390babab099279866cb550f9456a5bad75337dfd [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
agrieve7acf04a2016-02-09 18:36:38123 class FastLocalDevAction(argparse.Action):
124 def __call__(self, parser, namespace, values, option_string=None):
125 namespace.verbose_count = max(namespace.verbose_count, 1)
126 namespace.num_retries = 0
127 namespace.enable_device_cache = True
agrieve2e8027602016-02-10 22:26:22128 namespace.enable_concurrent_adb = True
agrieve7acf04a2016-02-09 18:36:38129 namespace.skip_clear_data = True
130 namespace.extract_test_list_from_filter = True
131
132 group.add_argument('--fast-local-dev', type=bool, nargs=0,
133 action=FastLocalDevAction,
134 help='Alias for: --verbose --num-retries=0 '
agrieve2e8027602016-02-10 22:26:22135 '--enable-device-cache --enable-concurrent-adb '
136 '--skip-clear-data --extract-test-list-from-filter')
agrieve7acf04a2016-02-09 18:36:38137
jama47ca85c2014-12-03 18:38:07138def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26139 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07140 run_tests_helper.SetLogLevel(args.verbose_count)
141 constants.SetBuildType(args.build_type)
142 if args.build_directory:
143 constants.SetBuildDirectory(args.build_directory)
144 if args.output_directory:
mikecase0aea9c52015-04-30 00:12:33145 constants.SetOutputDirectory(args.output_directory)
jbudorick0c2a94a2015-12-04 14:27:43146
147 devil_custom_deps = None
jama47ca85c2014-12-03 18:38:07148 if args.adb_path:
jbudorick0c2a94a2015-12-04 14:27:43149 devil_custom_deps = {
150 'adb': {
jbudoricke29693be2015-12-07 15:53:23151 devil_env.GetPlatform(): [args.adb_path]
jbudorick0c2a94a2015-12-04 14:27:43152 }
153 }
154
155 devil_chromium.Initialize(
156 output_directory=constants.GetOutDirectory(),
157 custom_deps=devil_custom_deps)
158
mikecase48e16bf2014-11-19 22:46:45159 # Some things such as Forwarder require ADB to be in the environment path.
160 adb_dir = os.path.dirname(constants.GetAdbPath())
161 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
162 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26163
164
rnephew5c499782014-12-12 19:08:55165def AddRemoteDeviceOptions(parser):
166 group = parser.add_argument_group('Remote Device Options')
167
rnephewefe44b42015-02-04 04:45:15168 group.add_argument('--trigger',
jbudoricke6c560152015-01-13 23:49:28169 help=('Only triggers the test if set. Stores test_run_id '
170 'in given file path. '))
rnephewefe44b42015-02-04 04:45:15171 group.add_argument('--collect',
jbudoricke6c560152015-01-13 23:49:28172 help=('Only collects the test results if set. '
173 'Gets test_run_id from given file path.'))
rnephewefe44b42015-02-04 04:45:15174 group.add_argument('--remote-device', action='append',
jbudoricke6c560152015-01-13 23:49:28175 help='Device type to run test on.')
rnephewefe44b42015-02-04 04:45:15176 group.add_argument('--results-path',
jbudoricke6c560152015-01-13 23:49:28177 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11178 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28179 help='HTTP protocol to use. (http or https)')
rnephewefe44b42015-02-04 04:45:15180 group.add_argument('--api-address',
181 help='Address to send HTTP requests.')
182 group.add_argument('--api-port',
183 help='Port to send HTTP requests to.')
184 group.add_argument('--runner-type',
jbudoricke6c560152015-01-13 23:49:28185 help='Type of test to run as.')
rnephewefe44b42015-02-04 04:45:15186 group.add_argument('--runner-package',
187 help='Package name of test.')
188 group.add_argument('--device-type',
rnephewa46fc562015-01-23 16:00:14189 choices=constants.VALID_DEVICE_TYPES,
190 help=('Type of device to run on. iOS or android'))
rnephewefe44b42015-02-04 04:45:15191 group.add_argument('--device-oem', action='append',
192 help='Device OEM to run on.')
193 group.add_argument('--remote-device-file',
194 help=('File with JSON to select remote device. '
195 'Overrides all other flags.'))
rnephewc9ae8f52015-02-13 03:02:55196 group.add_argument('--remote-device-timeout', type=int,
197 help='Times to retry finding remote device')
mikecase520cbbb52015-04-21 18:51:18198 group.add_argument('--network-config', type=int,
199 help='Integer that specifies the network environment '
200 'that the tests will be run in.')
mikecaseddfa35d2015-10-28 01:14:27201 group.add_argument('--test-timeout', type=int,
202 help='Test run timeout in seconds.')
rnephewefe44b42015-02-04 04:45:15203
204 device_os_group = group.add_mutually_exclusive_group()
205 device_os_group.add_argument('--remote-device-minimum-os',
206 help='Minimum OS on device.')
207 device_os_group.add_argument('--remote-device-os', action='append',
208 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55209
210 api_secret_group = group.add_mutually_exclusive_group()
211 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28212 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55213 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28214 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55215
216 api_key_group = group.add_mutually_exclusive_group()
217 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28218 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55219 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28220 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55221
222
jama47ca85c2014-12-03 18:38:07223def AddDeviceOptions(parser):
224 """Adds device options to |parser|."""
225 group = parser.add_argument_group(title='Device Options')
jama47ca85c2014-12-03 18:38:07226 group.add_argument('--tool',
227 dest='tool',
228 help=('Run the test under a tool '
229 '(use --tool help to list them)'))
230 group.add_argument('-d', '--device', dest='test_device',
231 help=('Target device for the test suite '
232 'to run on.'))
jbudorickdde688fb2015-08-27 03:00:17233 group.add_argument('--blacklist-file', help='Device blacklist file.')
agrievea538a142015-10-09 15:45:56234 group.add_argument('--enable-device-cache', action='store_true',
235 help='Cache device state to disk between runs')
agrieve8bcb52e2015-10-20 19:38:33236 group.add_argument('--enable-concurrent-adb', action='store_true',
237 help='Run multiple adb commands at the same time, even '
238 'for the same device.')
agrievecd243a22016-02-08 23:25:14239 group.add_argument('--skip-clear-data', action='store_true',
240 help='Do not wipe app data between tests. Use this to '
241 'speed up local development and never on bots '
242 '(increases flakiness)')
jbudorick256fd532014-10-24 01:50:13243
244
jama47ca85c2014-12-03 18:38:07245def AddGTestOptions(parser):
246 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26247
jama47ca85c2014-12-03 18:38:07248 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49249 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07250 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick277f2312015-09-24 16:37:43251 help='Executable name of the test suite to run.')
agrieve62ab00282016-04-05 02:03:45252 group.add_argument('--executable-dist-dir',
253 help="Path to executable's dist directory for native"
254 " (non-apk) tests.")
agrieve4931af8c2016-02-10 19:25:26255 group.add_argument('--test-apk-incremental-install-script',
256 help='Path to install script for the test apk.')
jama47ca85c2014-12-03 18:38:07257 group.add_argument('--gtest_also_run_disabled_tests',
258 '--gtest-also-run-disabled-tests',
259 dest='run_disabled', action='store_true',
260 help='Also run disabled tests if applicable.')
261 group.add_argument('-a', '--test-arguments', dest='test_arguments',
262 default='',
263 help='Additional arguments to pass to the test.')
jbudorick24616eb2015-10-06 02:40:57264 group.add_argument('-t', '--shard-timeout',
mikecase68bcfc82016-01-22 20:14:38265 dest='shard_timeout', type=int, default=120,
jama47ca85c2014-12-03 18:38:07266 help='Timeout to wait for each test '
267 '(default: %(default)s).')
268 group.add_argument('--isolate_file_path',
269 '--isolate-file-path',
270 dest='isolate_file_path',
271 help='.isolate file path to override the default '
272 'path')
jbudorick5ee45892015-06-10 18:46:22273 group.add_argument('--app-data-file', action='append', dest='app_data_files',
274 help='A file path relative to the app data directory '
275 'that should be saved to the host.')
276 group.add_argument('--app-data-file-dir',
277 help='Host directory to which app data files will be'
278 ' saved. Used with --app-data-file.')
mlliud7f9fe92015-06-15 19:36:56279 group.add_argument('--delete-stale-data', dest='delete_stale_data',
280 action='store_true',
281 help='Delete stale test data on the device.')
jbudorickeb7ea71c2015-09-28 16:40:20282 group.add_argument('--repeat', '--gtest_repeat', '--gtest-repeat',
283 dest='repeat', type=int, default=0,
284 help='Number of times to repeat the specified set of '
285 'tests.')
alexandermonta3f03bf2015-12-02 18:56:45286 group.add_argument('--break-on-failure', '--break_on_failure',
287 dest='break_on_failure', action='store_true',
288 help='Whether to break on failure.')
agrieve0f5e53e2016-02-04 03:47:37289 group.add_argument('--extract-test-list-from-filter',
290 action='store_true',
291 help='When a test filter is specified, and the list of '
292 'tests can be determined from it, skip querying the '
293 'device for the list of all tests. Speeds up local '
294 'development, but is not safe to use on bots ('
295 'http://crbug.com/549214')
jbudorick442a6932015-02-03 03:01:15296
297 filter_group = group.add_mutually_exclusive_group()
298 filter_group.add_argument('-f', '--gtest_filter', '--gtest-filter',
299 dest='test_filter',
300 help='googletest-style filter string.')
301 filter_group.add_argument('--gtest-filter-file', dest='test_filter_file',
302 help='Path to file that contains googletest-style '
303 'filter strings. (Lines will be joined with '
304 '":" to create a single filter string.)')
305
jama47ca85c2014-12-03 18:38:07306 AddDeviceOptions(parser)
307 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55308 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26309
310
jama47ca85c2014-12-03 18:38:07311def AddLinkerTestOptions(parser):
312 group = parser.add_argument_group('Linker Test Options')
313 group.add_argument('-f', '--gtest-filter', dest='test_filter',
314 help='googletest-style filter string.')
315 AddCommonOptions(parser)
316 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38317
318
jama47ca85c2014-12-03 18:38:07319def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26320 """Adds the Java test options to |option_parser|."""
321
jama47ca85c2014-12-03 18:38:07322 argument_group.add_argument(
stipf09861b682016-04-06 13:51:36323 '-f', '--test-filter', '--gtest_filter', '--gtest-filter',
324 dest='test_filter',
jama47ca85c2014-12-03 18:38:07325 help=('Test filter (if not fully qualified, will run all matches).'))
326 argument_group.add_argument(
jbudorickeb7ea71c2015-09-28 16:40:20327 '--repeat', dest='repeat', type=int, default=0,
328 help='Number of times to repeat the specified set of tests.')
329 argument_group.add_argument(
alexandermonta3f03bf2015-12-02 18:56:45330 '--break-on-failure', '--break_on_failure',
331 dest='break_on_failure', action='store_true',
332 help='Whether to break on failure.')
333 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26334 '-A', '--annotation', dest='annotation_str',
335 help=('Comma-separated list of annotations. Run only tests with any of '
336 'the given annotations. An annotation can be either a key or a '
337 'key-values pair. A test that has no annotation is considered '
338 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07339 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26340 '-E', '--exclude-annotation', dest='exclude_annotation_str',
341 help=('Comma-separated list of annotations. Exclude tests with these '
342 'annotations.'))
jama47ca85c2014-12-03 18:38:07343 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59344 '--screenshot', dest='screenshot_failures', action='store_true',
345 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07346 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59347 '--save-perf-json', action='store_true',
348 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07349 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59350 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07351 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59352 '--test_data', '--test-data', action='append', default=[],
353 help=('Each instance defines a directory of test data that should be '
354 'copied to the target(s) before running the tests. The argument '
355 'should be of the form <target>:<source>, <target> is relative to '
356 'the device data directory, and <source> is relative to the '
357 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21358 argument_group.add_argument(
359 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
360 default=True, help='Removes the dalvik.vm.enableassertions property')
361
[email protected]fbe29322013-07-09 09:03:26362
363
jama47ca85c2014-12-03 18:38:07364def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26365 """Processes options/arguments and populates |options| with defaults."""
366
jama47ca85c2014-12-03 18:38:07367 # TODO(jbudorick): Handle most of this function in argparse.
368 if args.annotation_str:
369 args.annotations = args.annotation_str.split(',')
370 elif args.test_filter:
371 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26372 else:
jama47ca85c2014-12-03 18:38:07373 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
374 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26375
jama47ca85c2014-12-03 18:38:07376 if args.exclude_annotation_str:
377 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26378 else:
jama47ca85c2014-12-03 18:38:07379 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26380
[email protected]fbe29322013-07-09 09:03:26381
jama47ca85c2014-12-03 18:38:07382def AddInstrumentationTestOptions(parser):
383 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26384
jama47ca85c2014-12-03 18:38:07385 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26386
jama47ca85c2014-12-03 18:38:07387 group = parser.add_argument_group('Instrumentation Test Options')
388 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26389
jama47ca85c2014-12-03 18:38:07390 java_or_python_group = group.add_mutually_exclusive_group()
391 java_or_python_group.add_argument(
392 '-j', '--java-only', action='store_false',
393 dest='run_python_tests', default=True, help='Run only the Java tests.')
394 java_or_python_group.add_argument(
395 '-p', '--python-only', action='store_false',
396 dest='run_java_tests', default=True,
397 help='Run only the host-driven tests.')
398
399 group.add_argument('--host-driven-root',
400 help='Root of the host-driven tests.')
401 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
402 action='store_true',
403 help='Wait for debugger.')
agrieve4931af8c2016-02-10 19:25:26404 group.add_argument('--apk-under-test',
405 help='Path or name of the apk under test.')
406 group.add_argument('--apk-under-test-incremental-install-script',
407 help='Path to install script for the --apk-under-test.')
408 group.add_argument('--test-apk', required=True,
409 help='Path or name of the apk containing the tests '
410 '(name is without the .apk extension; '
411 'e.g. "ContentShellTest").')
412 group.add_argument('--test-apk-incremental-install-script',
413 help='Path to install script for the --test-apk.')
mikecasee7258622015-09-29 13:47:35414 group.add_argument('--additional-apk', action='append',
mikecase8c4ab302015-09-29 17:03:29415 dest='additional_apks', default=[],
mikecasee7258622015-09-29 13:47:35416 help='Additional apk that must be installed on '
417 'the device when the tests are run')
jama47ca85c2014-12-03 18:38:07418 group.add_argument('--coverage-dir',
419 help=('Directory in which to place all generated '
420 'EMMA coverage files.'))
421 group.add_argument('--device-flags', dest='device_flags', default='',
422 help='The relative filepath to a file containing '
423 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06424 group.add_argument('--device-flags-file', default='',
425 help='The relative filepath to a file containing '
426 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07427 group.add_argument('--isolate_file_path',
428 '--isolate-file-path',
429 dest='isolate_file_path',
430 help='.isolate file path to override the default '
431 'path')
mlliud7f9fe92015-06-15 19:36:56432 group.add_argument('--delete-stale-data', dest='delete_stale_data',
433 action='store_true',
434 help='Delete stale test data on the device.')
jbudorickede49722015-11-25 05:16:34435 group.add_argument('--timeout-scale', type=float,
436 help='Factor by which timeouts should be scaled.')
wnwen1d202892016-02-02 20:22:58437 group.add_argument('--strict-mode', dest='strict_mode', default='testing',
wnwen2b56c152016-01-12 16:40:10438 help='StrictMode command-line flag set on the device, '
439 'death/testing to kill the process, off to stop '
440 'checking, flash to flash only. Default testing.')
jama47ca85c2014-12-03 18:38:07441
442 AddCommonOptions(parser)
443 AddDeviceOptions(parser)
rnephewe416dff2015-01-21 21:26:37444 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26445
446
jama47ca85c2014-12-03 18:38:07447def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22448 """Processes options/arguments and populate |options| with defaults.
449
450 Args:
jama47ca85c2014-12-03 18:38:07451 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22452
453 Returns:
454 An InstrumentationOptions named tuple which contains all options relevant to
455 instrumentation tests.
456 """
[email protected]fbe29322013-07-09 09:03:26457
jama47ca85c2014-12-03 18:38:07458 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26459
jama47ca85c2014-12-03 18:38:07460 if not args.host_driven_root:
461 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13462
jbudorick9ef3f9552015-10-20 22:58:33463 if os.path.exists(args.test_apk):
464 args.test_apk_path = args.test_apk
465 args.test_apk, _ = os.path.splitext(os.path.basename(args.test_apk))
466 else:
467 args.test_apk_path = os.path.join(
468 constants.GetOutDirectory(),
469 constants.SDK_BUILD_APKS_DIR,
470 '%s.apk' % args.test_apk)
471
agrieve4931af8c2016-02-10 19:25:26472 jar_basename = args.test_apk
473 if jar_basename.endswith('_incremental'):
474 jar_basename = jar_basename[:-len('_incremental')]
475
jama47ca85c2014-12-03 18:38:07476 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15477 constants.GetOutDirectory(),
478 constants.SDK_BUILD_TEST_JAVALIB_DIR,
agrieve4931af8c2016-02-10 19:25:26479 '%s.jar' % jar_basename)
yusufo72c598c02015-07-16 23:40:20480 args.test_support_apk_path = '%sSupport%s' % (
481 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46482
jama47ca85c2014-12-03 18:38:07483 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46484
jama47ca85c2014-12-03 18:38:07485 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22486 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07487 args.tool,
jama47ca85c2014-12-03 18:38:07488 args.annotations,
489 args.exclude_annotations,
490 args.test_filter,
491 args.test_data,
492 args.save_perf_json,
493 args.screenshot_failures,
494 args.wait_for_debugger,
495 args.coverage_dir,
496 args.test_apk,
497 args.test_apk_path,
498 args.test_apk_jar_path,
499 args.test_runner,
500 args.test_support_apk_path,
501 args.device_flags,
davileen98efad12015-01-05 19:48:21502 args.isolate_file_path,
mlliud7f9fe92015-06-15 19:36:56503 args.set_asserts,
jbudorickede49722015-11-25 05:16:34504 args.delete_stale_data,
jbudorick248e31a2016-01-06 16:28:11505 args.timeout_scale,
506 args.apk_under_test,
wnwen2b56c152016-01-12 16:40:10507 args.additional_apks,
agrievecd243a22016-02-08 23:25:14508 args.strict_mode,
agrieve4931af8c2016-02-10 19:25:26509 args.skip_clear_data,
510 args.test_apk_incremental_install_script,
511 args.apk_under_test_incremental_install_script)
[email protected]2a684222013-08-01 16:59:22512
[email protected]fbe29322013-07-09 09:03:26513
jama47ca85c2014-12-03 18:38:07514def AddUIAutomatorTestOptions(parser):
515 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26516
jama47ca85c2014-12-03 18:38:07517 group = parser.add_argument_group('UIAutomator Test Options')
518 AddJavaTestOptions(group)
519 group.add_argument(
520 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
521 metavar='PACKAGE', help='Package under test.')
522 group.add_argument(
523 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26524 help=('The name of the dexed jar containing the tests (without the '
525 '.dex.jar extension). Alternatively, this can be a full path '
526 'to the jar.'))
527
jama47ca85c2014-12-03 18:38:07528 AddCommonOptions(parser)
529 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26530
531
jama47ca85c2014-12-03 18:38:07532def AddJUnitTestOptions(parser):
533 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07534
jama47ca85c2014-12-03 18:38:07535 group = parser.add_argument_group('JUnit Test Options')
536 group.add_argument(
537 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07538 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07539 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07540 '-f', '--test-filter', dest='test_filter',
541 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07542 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07543 '--package-filter', dest='package_filter',
544 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07545 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07546 '--runner-filter', dest='runner_filter',
547 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07548 group.add_argument(
549 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07550 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07551 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07552
553
jama47ca85c2014-12-03 18:38:07554def AddMonkeyTestOptions(parser):
555 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07556
jama47ca85c2014-12-03 18:38:07557 group = parser.add_argument_group('Monkey Test Options')
558 group.add_argument(
559 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
560 metavar='PACKAGE', help='Package under test.')
561 group.add_argument(
562 '--event-count', default=10000, type=int,
563 help='Number of events to generate (default: %(default)s).')
564 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14565 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12566 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07567 group.add_argument(
568 '--throttle', default=100, type=int,
569 help='Delay between events (ms) (default: %(default)s). ')
570 group.add_argument(
571 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14572 help=('Seed value for pseudo-random generator. Same seed value generates '
573 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07574 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14575 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07576 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14577
jama47ca85c2014-12-03 18:38:07578 AddCommonOptions(parser)
579 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14580
jama47ca85c2014-12-03 18:38:07581def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14582 """Processes all monkey test options.
583
584 Args:
jama47ca85c2014-12-03 18:38:07585 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14586
587 Returns:
588 A MonkeyOptions named tuple which contains all options relevant to
589 monkey tests.
590 """
jama47ca85c2014-12-03 18:38:07591 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
592 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14593 if category:
jama47ca85c2014-12-03 18:38:07594 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14595
jama47ca85c2014-12-03 18:38:07596 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14597 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07598 args.verbose_count,
599 args.package,
600 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14601 category,
jama47ca85c2014-12-03 18:38:07602 args.throttle,
603 args.seed,
604 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14605
rnephew5c499782014-12-12 19:08:55606def AddUirobotTestOptions(parser):
607 """Adds uirobot test options to |option_parser|."""
608 group = parser.add_argument_group('Uirobot Test Options')
609
rnephewefe44b42015-02-04 04:45:15610 group.add_argument('--app-under-test', required=True,
611 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55612 group.add_argument(
mikecaseafa43842015-10-19 23:04:12613 '--repeat', dest='repeat', type=int, default=0,
614 help='Number of times to repeat the uirobot test.')
615 group.add_argument(
rnephew5c499782014-12-12 19:08:55616 '--minutes', default=5, type=int,
jbudorick676b1202015-02-06 22:02:27617 help='Number of minutes to run uirobot test [default: %(default)s].')
rnephew5c499782014-12-12 19:08:55618
619 AddCommonOptions(parser)
620 AddDeviceOptions(parser)
621 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14622
jama47ca85c2014-12-03 18:38:07623def AddPerfTestOptions(parser):
624 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47625
jama47ca85c2014-12-03 18:38:07626 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47627
jama47ca85c2014-12-03 18:38:07628 class SingleStepAction(argparse.Action):
629 def __call__(self, parser, namespace, values, option_string=None):
630 if values and not namespace.single_step:
631 parser.error('single step command provided, '
632 'but --single-step not specified.')
633 elif namespace.single_step and not values:
634 parser.error('--single-step specified, '
635 'but no single step command provided.')
636 setattr(namespace, self.dest, values)
637
638 step_group = group.add_mutually_exclusive_group(required=True)
639 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
640 # This requires removing "--" from client calls.
641 step_group.add_argument(
642 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52643 help='Execute the given command with retries, but only print the result '
644 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07645 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46646 '--steps',
[email protected]def4bce2013-11-12 12:59:52647 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07648 step_group.add_argument(
649 '--print-step',
650 help='The name of a previously executed perf step to print.')
651
652 group.add_argument(
peterbd4e73d2014-12-03 15:47:36653 '--output-json-list',
654 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07655 group.add_argument(
peterbd4e73d2014-12-03 15:47:36656 '--collect-chartjson-data',
657 action='store_true',
658 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07659 group.add_argument(
peterbd4e73d2014-12-03 15:47:36660 '--output-chartjson-data',
661 default='',
662 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07663 group.add_argument(
perezju67cf7f12015-09-29 11:39:05664 '--get-output-dir-archive', metavar='FILENAME',
665 help='Write the chached output directory archived by a step into the'
666 ' given ZIP file.')
667 group.add_argument(
jama47ca85c2014-12-03 18:38:07668 '--flaky-steps',
669 help=('A JSON file containing steps that are flaky '
670 'and will have its exit code ignored.'))
671 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46672 '--no-timeout', action='store_true',
673 help=('Do not impose a timeout. Each perf step is responsible for '
674 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07675 group.add_argument(
[email protected]650487c2013-09-30 11:40:49676 '-f', '--test-filter',
677 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07678 group.add_argument(
679 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49680 help='Just print the steps without executing.')
jbudorick5cfff872015-07-01 18:46:13681 # Uses 0.1 degrees C because that's what Android does.
682 group.add_argument(
683 '--max-battery-temp', type=int,
684 help='Only start tests when the battery is at or below the given '
685 'temperature (0.1 C)')
jama47ca85c2014-12-03 18:38:07686 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
687 help='If --single-step is specified, the command to run.')
rnephewdde05da82015-07-09 20:31:01688 group.add_argument('--min-battery-level', type=int,
689 help='Only starts tests when the battery is charged above '
690 'given level.')
rnephewa66f59992016-02-26 18:29:05691 group.add_argument('--known-devices-file', help='Path to known device list.')
jama47ca85c2014-12-03 18:38:07692 AddCommonOptions(parser)
693 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47694
695
jama47ca85c2014-12-03 18:38:07696def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47697 """Processes all perf test options.
698
699 Args:
jama47ca85c2014-12-03 18:38:07700 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47701
702 Returns:
703 A PerfOptions named tuple which contains all options relevant to
704 perf tests.
705 """
jama47ca85c2014-12-03 18:38:07706 # TODO(jbudorick): Move single_step handling down into the perf tests.
707 if args.single_step:
708 args.single_step = ' '.join(args.single_step_command)
709 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47710 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07711 args.steps, args.flaky_steps, args.output_json_list,
712 args.print_step, args.no_timeout, args.test_filter,
713 args.dry_run, args.single_step, args.collect_chartjson_data,
perezju67cf7f12015-09-29 11:39:05714 args.output_chartjson_data, args.get_output_dir_archive,
rnephewa66f59992016-02-26 18:29:05715 args.max_battery_temp, args.min_battery_level,
716 args.known_devices_file)
[email protected]ec3170b2013-08-14 14:39:47717
718
jama47ca85c2014-12-03 18:38:07719def AddPythonTestOptions(parser):
720 group = parser.add_argument_group('Python Test Options')
721 group.add_argument(
722 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
723 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
724 help='Name of the test suite to run.')
725 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13726
727
jama47ca85c2014-12-03 18:38:07728def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38729 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07730 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38731
732 results, exit_code = test_dispatcher.RunTests(
733 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07734 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38735
736 report_results.LogFull(
737 results=results,
738 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22739 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38740
jama47ca85c2014-12-03 18:38:07741 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20742 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54743
[email protected]6b6abac6d2013-10-03 11:56:38744 return exit_code
745
746
jama47ca85c2014-12-03 18:38:07747def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37748 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jbudorick58b4d362015-09-08 16:44:59749 logging.info('_RunInstrumentationTests(%s, %s)', str(args), str(devices))
[email protected]6bc1bda22013-07-19 22:08:37750
jama47ca85c2014-12-03 18:38:07751 instrumentation_options = ProcessInstrumentationOptions(args)
752
753 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57754 logging.warning('Debugger can not be sharded, using first available device')
755 devices = devices[:1]
756
[email protected]6bc1bda22013-07-19 22:08:37757 results = base_test_result.TestRunResults()
758 exit_code = 0
759
jama47ca85c2014-12-03 18:38:07760 if args.run_java_tests:
jbudorickeb7ea71c2015-09-28 16:40:20761 java_runner_factory, java_tests = instrumentation_setup.Setup(
mikecase526d68e2014-11-19 20:02:05762 instrumentation_options, devices)
jbudorickeb7ea71c2015-09-28 16:40:20763 else:
764 java_runner_factory = None
765 java_tests = None
[email protected]6bc1bda22013-07-19 22:08:37766
jama47ca85c2014-12-03 18:38:07767 if args.run_python_tests:
jbudorickeb7ea71c2015-09-28 16:40:20768 py_runner_factory, py_tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07769 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13770 instrumentation_options)
jbudorickeb7ea71c2015-09-28 16:40:20771 else:
772 py_runner_factory = None
773 py_tests = None
[email protected]37ee0c792013-08-06 19:10:13774
jbudorickeb7ea71c2015-09-28 16:40:20775 results = []
776 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
777 else itertools.count())
alexandermonta3f03bf2015-12-02 18:56:45778
alexandermonte2cbe022015-12-16 04:58:58779 code_counts = {constants.INFRA_EXIT_CODE: 0,
780 constants.ERROR_EXIT_CODE: 0,
781 constants.WARNING_EXIT_CODE: 0,
782 0: 0}
783
alexandermonta3f03bf2015-12-02 18:56:45784 def _escalate_code(old, new):
785 for x in (constants.INFRA_EXIT_CODE,
786 constants.ERROR_EXIT_CODE,
787 constants.WARNING_EXIT_CODE):
788 if x in (old, new):
789 return x
790 return 0
791
jbudorickeb7ea71c2015-09-28 16:40:20792 for _ in repetitions:
793 iteration_results = base_test_result.TestRunResults()
794 if java_tests:
[email protected]34020022013-08-06 23:35:34795 test_results, test_exit_code = test_dispatcher.RunTests(
jbudorickeb7ea71c2015-09-28 16:40:20796 java_tests, java_runner_factory, devices, shard=True,
797 test_timeout=None, num_retries=args.num_retries)
798 iteration_results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37799
alexandermonte2cbe022015-12-16 04:58:58800 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45801 exit_code = _escalate_code(exit_code, test_exit_code)
[email protected]6bc1bda22013-07-19 22:08:37802
jbudorickeb7ea71c2015-09-28 16:40:20803 if py_tests:
804 test_results, test_exit_code = test_dispatcher.RunTests(
805 py_tests, py_runner_factory, devices, shard=True, test_timeout=None,
806 num_retries=args.num_retries)
807 iteration_results.AddTestRunResults(test_results)
[email protected]4f777ca2014-08-08 01:45:59808
alexandermonte2cbe022015-12-16 04:58:58809 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45810 exit_code = _escalate_code(exit_code, test_exit_code)
jbudorickeb7ea71c2015-09-28 16:40:20811
812 results.append(iteration_results)
813 report_results.LogFull(
814 results=iteration_results,
815 test_type='Instrumentation',
816 test_package=os.path.basename(args.test_apk),
817 annotation=args.annotations,
818 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37819
alexandermonte2cbe022015-12-16 04:58:58820
alexandermonta3f03bf2015-12-02 18:56:45821 if args.break_on_failure and exit_code in (constants.ERROR_EXIT_CODE,
822 constants.INFRA_EXIT_CODE):
823 break
824
alexandermonte2cbe022015-12-16 04:58:58825 logging.critical('Instr tests: %s success, %s infra, %s errors, %s warnings',
826 str(code_counts[0]),
827 str(code_counts[constants.INFRA_EXIT_CODE]),
828 str(code_counts[constants.ERROR_EXIT_CODE]),
829 str(code_counts[constants.WARNING_EXIT_CODE]))
830
jama47ca85c2014-12-03 18:38:07831 if args.json_results_file:
832 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54833
[email protected]6bc1bda22013-07-19 22:08:37834 return exit_code
835
836
jama47ca85c2014-12-03 18:38:07837def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07838 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07839 runner_factory, tests = junit_setup.Setup(args)
mikecasec638a072015-04-01 16:35:35840 results, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
841
842 report_results.LogFull(
843 results=results,
844 test_type='JUnit',
845 test_package=args.test_suite)
846
mikecase572401b2015-04-09 02:28:57847 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20848 json_results.GenerateJsonResultsFile([results], args.json_results_file)
mikecase572401b2015-04-09 02:28:57849
jbudorick9a6b7b332014-09-20 00:01:07850 return exit_code
851
852
jama47ca85c2014-12-03 18:38:07853def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14854 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07855 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14856
857 runner_factory, tests = monkey_setup.Setup(monkey_options)
858
859 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46860 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07861 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14862
863 report_results.LogFull(
864 results=results,
865 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28866 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14867
jama47ca85c2014-12-03 18:38:07868 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20869 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54870
[email protected]3dbdfa42013-08-08 01:08:14871 return exit_code
872
873
jbudorickdde688fb2015-08-27 03:00:17874def _RunPerfTests(args, active_devices):
[email protected]ec3170b2013-08-14 14:39:47875 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07876 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56877
878 # Just save a simple json with a list of test names.
879 if perf_options.output_json_list:
880 return perf_test_runner.OutputJsonList(
881 perf_options.steps, perf_options.output_json_list)
882
[email protected]ad32f312013-11-13 04:03:29883 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47884 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42885 return perf_test_runner.PrintTestOutput(
perezju67cf7f12015-09-29 11:39:05886 perf_options.print_step, perf_options.output_chartjson_data,
887 perf_options.get_output_dir_archive)
[email protected]ec3170b2013-08-14 14:39:47888
jbudorickdde688fb2015-08-27 03:00:17889 runner_factory, tests, devices = perf_setup.Setup(
890 perf_options, active_devices)
[email protected]ec3170b2013-08-14 14:39:47891
[email protected]a72f0752014-06-03 23:52:34892 # shard=False means that each device will get the full list of tests
893 # and then each one will decide their own affinity.
894 # shard=True means each device will pop the next test available from a queue,
895 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57896 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34897 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07898 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47899
900 report_results.LogFull(
901 results=results,
902 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12903 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52904
jama47ca85c2014-12-03 18:38:07905 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20906 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54907
[email protected]def4bce2013-11-12 12:59:52908 if perf_options.single_step:
909 return perf_test_runner.PrintTestOutput('single_step')
910
[email protected]11ce8452014-02-17 10:55:03911 perf_test_runner.PrintSummary(tests)
912
[email protected]86184c7b2013-08-15 15:06:57913 # Always return 0 on the sharding stage. Individual tests exit_code
914 # will be returned on the print_step stage.
915 return 0
[email protected]ec3170b2013-08-14 14:39:47916
[email protected]3dbdfa42013-08-08 01:08:14917
jama47ca85c2014-12-03 18:38:07918def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13919 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07920 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13921 suite_path = suite_vars['path']
922 suite_test_modules = suite_vars['test_modules']
923
924 sys.path = [suite_path] + sys.path
925 try:
926 suite = unittest.TestSuite()
927 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
928 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07929 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13930 return 0 if runner.run(suite).wasSuccessful() else 1
931 finally:
932 sys.path = sys.path[1:]
933
934
agrieveebc6bfa2016-04-05 19:29:25935def _GetAttachedDevices(blacklist_file, test_device, enable_cache, num_retries):
[email protected]f7148dd42013-08-20 14:24:57936 """Get all attached devices.
937
938 Args:
agrievea538a142015-10-09 15:45:56939 blacklist_file: Path to device blacklist.
[email protected]f7148dd42013-08-20 14:24:57940 test_device: Name of a specific device to use.
agrievea538a142015-10-09 15:45:56941 enable_cache: Whether to enable checksum caching.
[email protected]f7148dd42013-08-20 14:24:57942
943 Returns:
944 A list of attached devices.
945 """
jbudoricka583ba32015-09-11 17:23:19946 blacklist = (device_blacklist.Blacklist(blacklist_file)
947 if blacklist_file
948 else None)
jbudorickdde688fb2015-08-27 03:00:17949
agrievea538a142015-10-09 15:45:56950 attached_devices = device_utils.DeviceUtils.HealthyDevices(
agrieveebc6bfa2016-04-05 19:29:25951 blacklist, enable_device_files_cache=enable_cache,
952 default_retries=num_retries)
aberent6a02a6182015-04-29 11:07:55953 if test_device:
jbudorick4551d0dc2015-04-29 16:07:06954 test_device = [d for d in attached_devices if d == test_device]
955 if not test_device:
956 raise device_errors.DeviceUnreachableError(
957 'Did not find device %s among attached device. Attached devices: %s'
958 % (test_device, ', '.join(attached_devices)))
959 return test_device
aberent6a02a6182015-04-29 11:07:55960
jbudorick4551d0dc2015-04-29 16:07:06961 else:
962 if not attached_devices:
963 raise device_errors.NoDevicesError()
964 return sorted(attached_devices)
[email protected]f7148dd42013-08-20 14:24:57965
966
agrieve63d20782016-02-04 17:47:26967def RunTestsCommand(args): # pylint: disable=too-many-return-statements
[email protected]fbe29322013-07-09 09:03:26968 """Checks test type and dispatches to the appropriate function.
969
970 Args:
jama47ca85c2014-12-03 18:38:07971 args: argparse.Namespace object.
[email protected]fbe29322013-07-09 09:03:26972
973 Returns:
974 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10975
976 Raises:
977 Exception: Unknown command name passed in, or an exception from an
978 individual test runner.
[email protected]fbe29322013-07-09 09:03:26979 """
jama47ca85c2014-12-03 18:38:07980 command = args.command
[email protected]fbe29322013-07-09 09:03:26981
jama47ca85c2014-12-03 18:38:07982 ProcessCommonOptions(args)
jbudorick2be28ee2016-02-16 15:53:23983 logging.info('command: %s', ' '.join(sys.argv))
[email protected]d82f0252013-07-12 23:22:57984
jbudorickb71170d02016-03-30 01:32:08985 if args.enable_platform_mode or command in ('gtest', 'instrumentation'):
agrieve63d20782016-02-04 17:47:26986 return RunTestsInPlatformMode(args)
jbudorick66dc3722014-11-06 21:33:51987
[email protected]c0662e092013-11-12 11:51:25988 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40989 if not ports.ResetTestServerPortAllocation():
990 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25991
agrieve18930bd2015-10-09 17:41:42992 def get_devices():
993 return _GetAttachedDevices(args.blacklist_file, args.test_device,
agrieveebc6bfa2016-04-05 19:29:25994 args.enable_device_cache, args.num_retries)
agrieve18930bd2015-10-09 17:41:42995
jbudorickb71170d02016-03-30 01:32:08996 if command == 'linker':
agrieve18930bd2015-10-09 17:41:42997 return _RunLinkerTests(args, get_devices())
jbudorick9a6b7b332014-09-20 00:01:07998 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07999 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:141000 elif command == 'monkey':
agrieve18930bd2015-10-09 17:41:421001 return _RunMonkeyTests(args, get_devices())
[email protected]ec3170b2013-08-14 14:39:471002 elif command == 'perf':
agrieve18930bd2015-10-09 17:41:421003 return _RunPerfTests(args, get_devices())
jbudorick256fd532014-10-24 01:50:131004 elif command == 'python':
jama47ca85c2014-12-03 18:38:071005 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:261006 else:
[email protected]6bc1bda22013-07-19 22:08:371007 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:261008
[email protected]fbe29322013-07-09 09:03:261009
jbudorick66dc3722014-11-06 21:33:511010_SUPPORTED_IN_PLATFORM_MODE = [
1011 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:061012 'gtest',
1013 'instrumentation',
1014 'uirobot',
jbudorick66dc3722014-11-06 21:33:511015]
1016
1017
agrieve63d20782016-02-04 17:47:261018def RunTestsInPlatformMode(args):
jbudorick66dc3722014-11-06 21:33:511019
jbudorick566592ab2015-09-21 15:32:471020 def infra_error(message):
agrieve63d20782016-02-04 17:47:261021 logging.fatal(message)
1022 sys.exit(constants.INFRA_EXIT_CODE)
jbudorickb9b0ada2015-09-17 22:52:581023
jbudorick566592ab2015-09-21 15:32:471024 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
1025 infra_error('%s is not yet supported in platform mode' % args.command)
1026
1027 with environment_factory.CreateEnvironment(args, infra_error) as env:
1028 with test_instance_factory.CreateTestInstance(args, infra_error) as test:
jbudorick66dc3722014-11-06 21:33:511029 with test_run_factory.CreateTestRun(
jbudorick566592ab2015-09-21 15:32:471030 args, env, test, infra_error) as test_run:
jbudorickeb7ea71c2015-09-28 16:40:201031 results = []
1032 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
1033 else itertools.count())
alexandermonte2cbe022015-12-16 04:58:581034 result_counts = collections.defaultdict(
1035 lambda: collections.defaultdict(int))
1036 iteration_count = 0
jbudorickeb7ea71c2015-09-28 16:40:201037 for _ in repetitions:
1038 iteration_results = test_run.RunTests()
jbudorickeb7ea71c2015-09-28 16:40:201039 if iteration_results is not None:
alexandermonte2cbe022015-12-16 04:58:581040 iteration_count += 1
jbudorickd4f77982015-09-28 21:09:181041 results.append(iteration_results)
alexandermonte2cbe022015-12-16 04:58:581042 for r in iteration_results.GetAll():
1043 result_counts[r.GetName()][r.GetType()] += 1
jbudorickeb7ea71c2015-09-28 16:40:201044 report_results.LogFull(
1045 results=iteration_results,
1046 test_type=test.TestType(),
1047 test_package=test_run.TestPackage(),
1048 annotation=getattr(args, 'annotations', None),
1049 flakiness_server=getattr(args, 'flakiness_dashboard_server',
1050 None))
alexandermonta3f03bf2015-12-02 18:56:451051 if args.break_on_failure and not iteration_results.DidRunPass():
1052 break
jbudorick66dc3722014-11-06 21:33:511053
alexandermonte2cbe022015-12-16 04:58:581054 if iteration_count > 1:
1055 # display summary results
1056 # only display results for a test if at least one test did not pass
1057 all_pass = 0
1058 tot_tests = 0
1059 for test_name in result_counts:
1060 tot_tests += 1
1061 if any(result_counts[test_name][x] for x in (
1062 base_test_result.ResultType.FAIL,
1063 base_test_result.ResultType.CRASH,
1064 base_test_result.ResultType.TIMEOUT,
1065 base_test_result.ResultType.UNKNOWN)):
1066 logging.critical(
1067 '%s: %s',
1068 test_name,
1069 ', '.join('%s %s' % (str(result_counts[test_name][i]), i)
1070 for i in base_test_result.ResultType.GetTypes()))
1071 else:
1072 all_pass += 1
1073
1074 logging.critical('%s of %s tests passed in all %s runs',
1075 str(all_pass),
1076 str(tot_tests),
1077 str(iteration_count))
1078
jama47ca85c2014-12-03 18:38:071079 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:541080 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:071081 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:541082
jbudorickeb7ea71c2015-09-28 16:40:201083 return (0 if all(r.DidRunPass() for r in results)
1084 else constants.ERROR_EXIT_CODE)
jbudorick66dc3722014-11-06 21:33:511085
1086
jama47ca85c2014-12-03 18:38:071087CommandConfigTuple = collections.namedtuple(
1088 'CommandConfigTuple',
1089 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:261090VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:071091 'gtest': CommandConfigTuple(
1092 AddGTestOptions,
1093 'googletest-based C++ tests'),
1094 'instrumentation': CommandConfigTuple(
1095 AddInstrumentationTestOptions,
1096 'InstrumentationTestCase-based Java tests'),
jama47ca85c2014-12-03 18:38:071097 'junit': CommandConfigTuple(
1098 AddJUnitTestOptions,
1099 'JUnit4-based Java tests'),
1100 'monkey': CommandConfigTuple(
1101 AddMonkeyTestOptions,
1102 "Tests based on Android's monkey"),
1103 'perf': CommandConfigTuple(
1104 AddPerfTestOptions,
1105 'Performance tests'),
1106 'python': CommandConfigTuple(
1107 AddPythonTestOptions,
1108 'Python tests based on unittest.TestCase'),
1109 'linker': CommandConfigTuple(
1110 AddLinkerTestOptions,
1111 'Linker tests'),
rnephew5c499782014-12-12 19:08:551112 'uirobot': CommandConfigTuple(
1113 AddUirobotTestOptions,
1114 'Uirobot test'),
jama47ca85c2014-12-03 18:38:071115}
[email protected]fbe29322013-07-09 09:03:261116
1117
[email protected]7c53a602014-03-24 16:21:441118def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:241119 for thread in threading.enumerate():
1120 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:211121
1122
[email protected]7c53a602014-03-24 16:21:441123def main():
[email protected]83bb8152013-11-19 15:02:211124 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:071125
1126 parser = argparse.ArgumentParser()
1127 command_parsers = parser.add_subparsers(title='test types',
1128 dest='command')
1129
1130 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
1131 key=lambda x: x[0]):
1132 subparser = command_parsers.add_parser(
1133 test_type, usage='%(prog)s [options]', help=config.help_txt)
1134 config.add_options_func(subparser)
1135
1136 args = parser.parse_args()
mikecasee74051022015-02-26 23:08:221137
1138 try:
agrieve63d20782016-02-04 17:47:261139 return RunTestsCommand(args)
mikecasee74051022015-02-26 23:08:221140 except base_error.BaseError as e:
1141 logging.exception('Error occurred.')
1142 if e.is_infra_error:
1143 return constants.INFRA_EXIT_CODE
mswecce6732015-06-06 00:31:331144 return constants.ERROR_EXIT_CODE
mikecasee74051022015-02-26 23:08:221145 except: # pylint: disable=W0702
1146 logging.exception('Unrecognized error occurred.')
1147 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:261148
[email protected]fbe29322013-07-09 09:03:261149
1150if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:441151 sys.exit(main())