blob: 2de0a33d7907a4e840526463909cfbcece41413f [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.')
[email protected]fbe29322013-07-09 09:03:26112
jama47ca85c2014-12-03 18:38:07113def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26114 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07115 run_tests_helper.SetLogLevel(args.verbose_count)
116 constants.SetBuildType(args.build_type)
117 if args.build_directory:
118 constants.SetBuildDirectory(args.build_directory)
119 if args.output_directory:
mikecase0aea9c52015-04-30 00:12:33120 constants.SetOutputDirectory(args.output_directory)
jbudorick0c2a94a2015-12-04 14:27:43121
122 devil_custom_deps = None
jama47ca85c2014-12-03 18:38:07123 if args.adb_path:
jbudorick0c2a94a2015-12-04 14:27:43124 devil_custom_deps = {
125 'adb': {
jbudoricke29693be2015-12-07 15:53:23126 devil_env.GetPlatform(): [args.adb_path]
jbudorick0c2a94a2015-12-04 14:27:43127 }
128 }
129
130 devil_chromium.Initialize(
131 output_directory=constants.GetOutDirectory(),
132 custom_deps=devil_custom_deps)
133
mikecase48e16bf2014-11-19 22:46:45134 # Some things such as Forwarder require ADB to be in the environment path.
135 adb_dir = os.path.dirname(constants.GetAdbPath())
136 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
137 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26138
139
rnephew5c499782014-12-12 19:08:55140def AddRemoteDeviceOptions(parser):
141 group = parser.add_argument_group('Remote Device Options')
142
rnephewefe44b42015-02-04 04:45:15143 group.add_argument('--trigger',
jbudoricke6c560152015-01-13 23:49:28144 help=('Only triggers the test if set. Stores test_run_id '
145 'in given file path. '))
rnephewefe44b42015-02-04 04:45:15146 group.add_argument('--collect',
jbudoricke6c560152015-01-13 23:49:28147 help=('Only collects the test results if set. '
148 'Gets test_run_id from given file path.'))
rnephewefe44b42015-02-04 04:45:15149 group.add_argument('--remote-device', action='append',
jbudoricke6c560152015-01-13 23:49:28150 help='Device type to run test on.')
rnephewefe44b42015-02-04 04:45:15151 group.add_argument('--results-path',
jbudoricke6c560152015-01-13 23:49:28152 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11153 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28154 help='HTTP protocol to use. (http or https)')
rnephewefe44b42015-02-04 04:45:15155 group.add_argument('--api-address',
156 help='Address to send HTTP requests.')
157 group.add_argument('--api-port',
158 help='Port to send HTTP requests to.')
159 group.add_argument('--runner-type',
jbudoricke6c560152015-01-13 23:49:28160 help='Type of test to run as.')
rnephewefe44b42015-02-04 04:45:15161 group.add_argument('--runner-package',
162 help='Package name of test.')
163 group.add_argument('--device-type',
rnephewa46fc562015-01-23 16:00:14164 choices=constants.VALID_DEVICE_TYPES,
165 help=('Type of device to run on. iOS or android'))
rnephewefe44b42015-02-04 04:45:15166 group.add_argument('--device-oem', action='append',
167 help='Device OEM to run on.')
168 group.add_argument('--remote-device-file',
169 help=('File with JSON to select remote device. '
170 'Overrides all other flags.'))
rnephewc9ae8f52015-02-13 03:02:55171 group.add_argument('--remote-device-timeout', type=int,
172 help='Times to retry finding remote device')
mikecase520cbbb52015-04-21 18:51:18173 group.add_argument('--network-config', type=int,
174 help='Integer that specifies the network environment '
175 'that the tests will be run in.')
mikecaseddfa35d2015-10-28 01:14:27176 group.add_argument('--test-timeout', type=int,
177 help='Test run timeout in seconds.')
rnephewefe44b42015-02-04 04:45:15178
179 device_os_group = group.add_mutually_exclusive_group()
180 device_os_group.add_argument('--remote-device-minimum-os',
181 help='Minimum OS on device.')
182 device_os_group.add_argument('--remote-device-os', action='append',
183 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55184
185 api_secret_group = group.add_mutually_exclusive_group()
186 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28187 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55188 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28189 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55190
191 api_key_group = group.add_mutually_exclusive_group()
192 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28193 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55194 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28195 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55196
197
jama47ca85c2014-12-03 18:38:07198def AddDeviceOptions(parser):
199 """Adds device options to |parser|."""
200 group = parser.add_argument_group(title='Device Options')
jama47ca85c2014-12-03 18:38:07201 group.add_argument('--tool',
202 dest='tool',
203 help=('Run the test under a tool '
204 '(use --tool help to list them)'))
205 group.add_argument('-d', '--device', dest='test_device',
206 help=('Target device for the test suite '
207 'to run on.'))
jbudorickdde688fb2015-08-27 03:00:17208 group.add_argument('--blacklist-file', help='Device blacklist file.')
agrievea538a142015-10-09 15:45:56209 group.add_argument('--enable-device-cache', action='store_true',
210 help='Cache device state to disk between runs')
agrieve1a02e582015-10-15 21:35:39211 group.add_argument('--incremental-install', action='store_true',
212 help='Use an _incremental apk.')
agrieve8bcb52e2015-10-20 19:38:33213 group.add_argument('--enable-concurrent-adb', action='store_true',
214 help='Run multiple adb commands at the same time, even '
215 'for the same device.')
jbudorick256fd532014-10-24 01:50:13216
217
jama47ca85c2014-12-03 18:38:07218def AddGTestOptions(parser):
219 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26220
jama47ca85c2014-12-03 18:38:07221 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49222 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07223 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick277f2312015-09-24 16:37:43224 help='Executable name of the test suite to run.')
jama47ca85c2014-12-03 18:38:07225 group.add_argument('--gtest_also_run_disabled_tests',
226 '--gtest-also-run-disabled-tests',
227 dest='run_disabled', action='store_true',
228 help='Also run disabled tests if applicable.')
229 group.add_argument('-a', '--test-arguments', dest='test_arguments',
230 default='',
231 help='Additional arguments to pass to the test.')
jbudorick24616eb2015-10-06 02:40:57232 group.add_argument('-t', '--shard-timeout',
233 dest='shard_timeout', type=int, default=60,
jama47ca85c2014-12-03 18:38:07234 help='Timeout to wait for each test '
235 '(default: %(default)s).')
236 group.add_argument('--isolate_file_path',
237 '--isolate-file-path',
238 dest='isolate_file_path',
239 help='.isolate file path to override the default '
240 'path')
jbudorick5ee45892015-06-10 18:46:22241 group.add_argument('--app-data-file', action='append', dest='app_data_files',
242 help='A file path relative to the app data directory '
243 'that should be saved to the host.')
244 group.add_argument('--app-data-file-dir',
245 help='Host directory to which app data files will be'
246 ' saved. Used with --app-data-file.')
mlliud7f9fe92015-06-15 19:36:56247 group.add_argument('--delete-stale-data', dest='delete_stale_data',
248 action='store_true',
249 help='Delete stale test data on the device.')
jbudorickeb7ea71c2015-09-28 16:40:20250 group.add_argument('--repeat', '--gtest_repeat', '--gtest-repeat',
251 dest='repeat', type=int, default=0,
252 help='Number of times to repeat the specified set of '
253 'tests.')
alexandermonta3f03bf2015-12-02 18:56:45254 group.add_argument('--break-on-failure', '--break_on_failure',
255 dest='break_on_failure', action='store_true',
256 help='Whether to break on failure.')
jbudorick442a6932015-02-03 03:01:15257
258 filter_group = group.add_mutually_exclusive_group()
259 filter_group.add_argument('-f', '--gtest_filter', '--gtest-filter',
260 dest='test_filter',
261 help='googletest-style filter string.')
262 filter_group.add_argument('--gtest-filter-file', dest='test_filter_file',
263 help='Path to file that contains googletest-style '
264 'filter strings. (Lines will be joined with '
265 '":" to create a single filter string.)')
266
jama47ca85c2014-12-03 18:38:07267 AddDeviceOptions(parser)
268 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55269 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26270
271
jama47ca85c2014-12-03 18:38:07272def AddLinkerTestOptions(parser):
273 group = parser.add_argument_group('Linker Test Options')
274 group.add_argument('-f', '--gtest-filter', dest='test_filter',
275 help='googletest-style filter string.')
276 AddCommonOptions(parser)
277 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38278
279
jama47ca85c2014-12-03 18:38:07280def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26281 """Adds the Java test options to |option_parser|."""
282
jama47ca85c2014-12-03 18:38:07283 argument_group.add_argument(
284 '-f', '--test-filter', dest='test_filter',
285 help=('Test filter (if not fully qualified, will run all matches).'))
286 argument_group.add_argument(
jbudorickeb7ea71c2015-09-28 16:40:20287 '--repeat', dest='repeat', type=int, default=0,
288 help='Number of times to repeat the specified set of tests.')
289 argument_group.add_argument(
alexandermonta3f03bf2015-12-02 18:56:45290 '--break-on-failure', '--break_on_failure',
291 dest='break_on_failure', action='store_true',
292 help='Whether to break on failure.')
293 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26294 '-A', '--annotation', dest='annotation_str',
295 help=('Comma-separated list of annotations. Run only tests with any of '
296 'the given annotations. An annotation can be either a key or a '
297 'key-values pair. A test that has no annotation is considered '
298 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07299 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26300 '-E', '--exclude-annotation', dest='exclude_annotation_str',
301 help=('Comma-separated list of annotations. Exclude tests with these '
302 'annotations.'))
jama47ca85c2014-12-03 18:38:07303 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59304 '--screenshot', dest='screenshot_failures', action='store_true',
305 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07306 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59307 '--save-perf-json', action='store_true',
308 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07309 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59310 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07311 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59312 '--test_data', '--test-data', action='append', default=[],
313 help=('Each instance defines a directory of test data that should be '
314 'copied to the target(s) before running the tests. The argument '
315 'should be of the form <target>:<source>, <target> is relative to '
316 'the device data directory, and <source> is relative to the '
317 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21318 argument_group.add_argument(
319 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
320 default=True, help='Removes the dalvik.vm.enableassertions property')
321
[email protected]fbe29322013-07-09 09:03:26322
323
jama47ca85c2014-12-03 18:38:07324def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26325 """Processes options/arguments and populates |options| with defaults."""
326
jama47ca85c2014-12-03 18:38:07327 # TODO(jbudorick): Handle most of this function in argparse.
328 if args.annotation_str:
329 args.annotations = args.annotation_str.split(',')
330 elif args.test_filter:
331 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26332 else:
jama47ca85c2014-12-03 18:38:07333 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
334 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26335
jama47ca85c2014-12-03 18:38:07336 if args.exclude_annotation_str:
337 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26338 else:
jama47ca85c2014-12-03 18:38:07339 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26340
[email protected]fbe29322013-07-09 09:03:26341
jama47ca85c2014-12-03 18:38:07342def AddInstrumentationTestOptions(parser):
343 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26344
jama47ca85c2014-12-03 18:38:07345 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26346
jama47ca85c2014-12-03 18:38:07347 group = parser.add_argument_group('Instrumentation Test Options')
348 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26349
jama47ca85c2014-12-03 18:38:07350 java_or_python_group = group.add_mutually_exclusive_group()
351 java_or_python_group.add_argument(
352 '-j', '--java-only', action='store_false',
353 dest='run_python_tests', default=True, help='Run only the Java tests.')
354 java_or_python_group.add_argument(
355 '-p', '--python-only', action='store_false',
356 dest='run_java_tests', default=True,
357 help='Run only the host-driven tests.')
358
359 group.add_argument('--host-driven-root',
360 help='Root of the host-driven tests.')
361 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
362 action='store_true',
363 help='Wait for debugger.')
jbudorick911be58d2015-01-13 02:51:06364 group.add_argument('--apk-under-test', dest='apk_under_test',
365 help=('the name of the apk under test.'))
jama47ca85c2014-12-03 18:38:07366 group.add_argument('--test-apk', dest='test_apk', required=True,
367 help=('The name of the apk containing the tests '
368 '(without the .apk extension; '
369 'e.g. "ContentShellTest").'))
mikecasee7258622015-09-29 13:47:35370 group.add_argument('--additional-apk', action='append',
mikecase8c4ab302015-09-29 17:03:29371 dest='additional_apks', default=[],
mikecasee7258622015-09-29 13:47:35372 help='Additional apk that must be installed on '
373 'the device when the tests are run')
jama47ca85c2014-12-03 18:38:07374 group.add_argument('--coverage-dir',
375 help=('Directory in which to place all generated '
376 'EMMA coverage files.'))
377 group.add_argument('--device-flags', dest='device_flags', default='',
378 help='The relative filepath to a file containing '
379 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06380 group.add_argument('--device-flags-file', default='',
381 help='The relative filepath to a file containing '
382 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07383 group.add_argument('--isolate_file_path',
384 '--isolate-file-path',
385 dest='isolate_file_path',
386 help='.isolate file path to override the default '
387 'path')
mlliud7f9fe92015-06-15 19:36:56388 group.add_argument('--delete-stale-data', dest='delete_stale_data',
389 action='store_true',
390 help='Delete stale test data on the device.')
jbudorickede49722015-11-25 05:16:34391 group.add_argument('--timeout-scale', type=float,
392 help='Factor by which timeouts should be scaled.')
jama47ca85c2014-12-03 18:38:07393
394 AddCommonOptions(parser)
395 AddDeviceOptions(parser)
rnephewe416dff2015-01-21 21:26:37396 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26397
398
jama47ca85c2014-12-03 18:38:07399def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22400 """Processes options/arguments and populate |options| with defaults.
401
402 Args:
jama47ca85c2014-12-03 18:38:07403 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22404
405 Returns:
406 An InstrumentationOptions named tuple which contains all options relevant to
407 instrumentation tests.
408 """
[email protected]fbe29322013-07-09 09:03:26409
jama47ca85c2014-12-03 18:38:07410 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26411
jama47ca85c2014-12-03 18:38:07412 if not args.host_driven_root:
413 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13414
jbudorick9ef3f9552015-10-20 22:58:33415 if os.path.exists(args.test_apk):
416 args.test_apk_path = args.test_apk
417 args.test_apk, _ = os.path.splitext(os.path.basename(args.test_apk))
418 else:
419 args.test_apk_path = os.path.join(
420 constants.GetOutDirectory(),
421 constants.SDK_BUILD_APKS_DIR,
422 '%s.apk' % args.test_apk)
423
jama47ca85c2014-12-03 18:38:07424 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15425 constants.GetOutDirectory(),
426 constants.SDK_BUILD_TEST_JAVALIB_DIR,
jbudorick9ef3f9552015-10-20 22:58:33427 '%s.jar' % args.test_apk)
yusufo72c598c02015-07-16 23:40:20428 args.test_support_apk_path = '%sSupport%s' % (
429 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46430
jama47ca85c2014-12-03 18:38:07431 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46432
jama47ca85c2014-12-03 18:38:07433 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22434 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07435 args.tool,
jama47ca85c2014-12-03 18:38:07436 args.annotations,
437 args.exclude_annotations,
438 args.test_filter,
439 args.test_data,
440 args.save_perf_json,
441 args.screenshot_failures,
442 args.wait_for_debugger,
443 args.coverage_dir,
444 args.test_apk,
445 args.test_apk_path,
446 args.test_apk_jar_path,
447 args.test_runner,
448 args.test_support_apk_path,
449 args.device_flags,
davileen98efad12015-01-05 19:48:21450 args.isolate_file_path,
mlliud7f9fe92015-06-15 19:36:56451 args.set_asserts,
jbudorickede49722015-11-25 05:16:34452 args.delete_stale_data,
jbudorick248e31a2016-01-06 16:28:11453 args.timeout_scale,
454 args.apk_under_test,
455 args.additional_apks)
[email protected]2a684222013-08-01 16:59:22456
[email protected]fbe29322013-07-09 09:03:26457
jama47ca85c2014-12-03 18:38:07458def AddUIAutomatorTestOptions(parser):
459 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26460
jama47ca85c2014-12-03 18:38:07461 group = parser.add_argument_group('UIAutomator Test Options')
462 AddJavaTestOptions(group)
463 group.add_argument(
464 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
465 metavar='PACKAGE', help='Package under test.')
466 group.add_argument(
467 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26468 help=('The name of the dexed jar containing the tests (without the '
469 '.dex.jar extension). Alternatively, this can be a full path '
470 'to the jar.'))
471
jama47ca85c2014-12-03 18:38:07472 AddCommonOptions(parser)
473 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26474
475
jama47ca85c2014-12-03 18:38:07476def AddJUnitTestOptions(parser):
477 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07478
jama47ca85c2014-12-03 18:38:07479 group = parser.add_argument_group('JUnit Test Options')
480 group.add_argument(
481 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07482 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07483 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07484 '-f', '--test-filter', dest='test_filter',
485 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07486 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07487 '--package-filter', dest='package_filter',
488 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07489 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07490 '--runner-filter', dest='runner_filter',
491 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07492 group.add_argument(
493 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07494 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07495 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07496
497
jama47ca85c2014-12-03 18:38:07498def AddMonkeyTestOptions(parser):
499 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07500
jama47ca85c2014-12-03 18:38:07501 group = parser.add_argument_group('Monkey Test Options')
502 group.add_argument(
503 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
504 metavar='PACKAGE', help='Package under test.')
505 group.add_argument(
506 '--event-count', default=10000, type=int,
507 help='Number of events to generate (default: %(default)s).')
508 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14509 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12510 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07511 group.add_argument(
512 '--throttle', default=100, type=int,
513 help='Delay between events (ms) (default: %(default)s). ')
514 group.add_argument(
515 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14516 help=('Seed value for pseudo-random generator. Same seed value generates '
517 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07518 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14519 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07520 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14521
jama47ca85c2014-12-03 18:38:07522 AddCommonOptions(parser)
523 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14524
jama47ca85c2014-12-03 18:38:07525def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14526 """Processes all monkey test options.
527
528 Args:
jama47ca85c2014-12-03 18:38:07529 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14530
531 Returns:
532 A MonkeyOptions named tuple which contains all options relevant to
533 monkey tests.
534 """
jama47ca85c2014-12-03 18:38:07535 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
536 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14537 if category:
jama47ca85c2014-12-03 18:38:07538 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14539
jama47ca85c2014-12-03 18:38:07540 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14541 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07542 args.verbose_count,
543 args.package,
544 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14545 category,
jama47ca85c2014-12-03 18:38:07546 args.throttle,
547 args.seed,
548 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14549
rnephew5c499782014-12-12 19:08:55550def AddUirobotTestOptions(parser):
551 """Adds uirobot test options to |option_parser|."""
552 group = parser.add_argument_group('Uirobot Test Options')
553
rnephewefe44b42015-02-04 04:45:15554 group.add_argument('--app-under-test', required=True,
555 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55556 group.add_argument(
mikecaseafa43842015-10-19 23:04:12557 '--repeat', dest='repeat', type=int, default=0,
558 help='Number of times to repeat the uirobot test.')
559 group.add_argument(
rnephew5c499782014-12-12 19:08:55560 '--minutes', default=5, type=int,
jbudorick676b1202015-02-06 22:02:27561 help='Number of minutes to run uirobot test [default: %(default)s].')
rnephew5c499782014-12-12 19:08:55562
563 AddCommonOptions(parser)
564 AddDeviceOptions(parser)
565 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14566
jama47ca85c2014-12-03 18:38:07567def AddPerfTestOptions(parser):
568 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47569
jama47ca85c2014-12-03 18:38:07570 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47571
jama47ca85c2014-12-03 18:38:07572 class SingleStepAction(argparse.Action):
573 def __call__(self, parser, namespace, values, option_string=None):
574 if values and not namespace.single_step:
575 parser.error('single step command provided, '
576 'but --single-step not specified.')
577 elif namespace.single_step and not values:
578 parser.error('--single-step specified, '
579 'but no single step command provided.')
580 setattr(namespace, self.dest, values)
581
582 step_group = group.add_mutually_exclusive_group(required=True)
583 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
584 # This requires removing "--" from client calls.
585 step_group.add_argument(
586 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52587 help='Execute the given command with retries, but only print the result '
588 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07589 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46590 '--steps',
[email protected]def4bce2013-11-12 12:59:52591 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07592 step_group.add_argument(
593 '--print-step',
594 help='The name of a previously executed perf step to print.')
595
596 group.add_argument(
peterbd4e73d2014-12-03 15:47:36597 '--output-json-list',
598 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07599 group.add_argument(
peterbd4e73d2014-12-03 15:47:36600 '--collect-chartjson-data',
601 action='store_true',
602 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07603 group.add_argument(
peterbd4e73d2014-12-03 15:47:36604 '--output-chartjson-data',
605 default='',
606 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07607 group.add_argument(
perezju67cf7f12015-09-29 11:39:05608 '--get-output-dir-archive', metavar='FILENAME',
609 help='Write the chached output directory archived by a step into the'
610 ' given ZIP file.')
611 group.add_argument(
jama47ca85c2014-12-03 18:38:07612 '--flaky-steps',
613 help=('A JSON file containing steps that are flaky '
614 'and will have its exit code ignored.'))
615 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46616 '--no-timeout', action='store_true',
617 help=('Do not impose a timeout. Each perf step is responsible for '
618 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07619 group.add_argument(
[email protected]650487c2013-09-30 11:40:49620 '-f', '--test-filter',
621 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07622 group.add_argument(
623 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49624 help='Just print the steps without executing.')
jbudorick5cfff872015-07-01 18:46:13625 # Uses 0.1 degrees C because that's what Android does.
626 group.add_argument(
627 '--max-battery-temp', type=int,
628 help='Only start tests when the battery is at or below the given '
629 'temperature (0.1 C)')
jama47ca85c2014-12-03 18:38:07630 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
631 help='If --single-step is specified, the command to run.')
rnephewdde05da82015-07-09 20:31:01632 group.add_argument('--min-battery-level', type=int,
633 help='Only starts tests when the battery is charged above '
634 'given level.')
jama47ca85c2014-12-03 18:38:07635 AddCommonOptions(parser)
636 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47637
638
jama47ca85c2014-12-03 18:38:07639def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47640 """Processes all perf test options.
641
642 Args:
jama47ca85c2014-12-03 18:38:07643 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47644
645 Returns:
646 A PerfOptions named tuple which contains all options relevant to
647 perf tests.
648 """
jama47ca85c2014-12-03 18:38:07649 # TODO(jbudorick): Move single_step handling down into the perf tests.
650 if args.single_step:
651 args.single_step = ' '.join(args.single_step_command)
652 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47653 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07654 args.steps, args.flaky_steps, args.output_json_list,
655 args.print_step, args.no_timeout, args.test_filter,
656 args.dry_run, args.single_step, args.collect_chartjson_data,
perezju67cf7f12015-09-29 11:39:05657 args.output_chartjson_data, args.get_output_dir_archive,
658 args.max_battery_temp, args.min_battery_level)
[email protected]ec3170b2013-08-14 14:39:47659
660
jama47ca85c2014-12-03 18:38:07661def AddPythonTestOptions(parser):
662 group = parser.add_argument_group('Python Test Options')
663 group.add_argument(
664 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
665 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
666 help='Name of the test suite to run.')
667 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13668
669
jama47ca85c2014-12-03 18:38:07670def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38671 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07672 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38673
674 results, exit_code = test_dispatcher.RunTests(
675 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07676 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38677
678 report_results.LogFull(
679 results=results,
680 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22681 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38682
jama47ca85c2014-12-03 18:38:07683 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20684 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54685
[email protected]6b6abac6d2013-10-03 11:56:38686 return exit_code
687
688
jama47ca85c2014-12-03 18:38:07689def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37690 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jbudorick58b4d362015-09-08 16:44:59691 logging.info('_RunInstrumentationTests(%s, %s)', str(args), str(devices))
[email protected]6bc1bda22013-07-19 22:08:37692
jama47ca85c2014-12-03 18:38:07693 instrumentation_options = ProcessInstrumentationOptions(args)
694
695 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57696 logging.warning('Debugger can not be sharded, using first available device')
697 devices = devices[:1]
698
[email protected]6bc1bda22013-07-19 22:08:37699 results = base_test_result.TestRunResults()
700 exit_code = 0
701
jama47ca85c2014-12-03 18:38:07702 if args.run_java_tests:
jbudorickeb7ea71c2015-09-28 16:40:20703 java_runner_factory, java_tests = instrumentation_setup.Setup(
mikecase526d68e2014-11-19 20:02:05704 instrumentation_options, devices)
jbudorickeb7ea71c2015-09-28 16:40:20705 else:
706 java_runner_factory = None
707 java_tests = None
[email protected]6bc1bda22013-07-19 22:08:37708
jama47ca85c2014-12-03 18:38:07709 if args.run_python_tests:
jbudorickeb7ea71c2015-09-28 16:40:20710 py_runner_factory, py_tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07711 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13712 instrumentation_options)
jbudorickeb7ea71c2015-09-28 16:40:20713 else:
714 py_runner_factory = None
715 py_tests = None
[email protected]37ee0c792013-08-06 19:10:13716
jbudorickeb7ea71c2015-09-28 16:40:20717 results = []
718 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
719 else itertools.count())
alexandermonta3f03bf2015-12-02 18:56:45720
alexandermonte2cbe022015-12-16 04:58:58721 code_counts = {constants.INFRA_EXIT_CODE: 0,
722 constants.ERROR_EXIT_CODE: 0,
723 constants.WARNING_EXIT_CODE: 0,
724 0: 0}
725
alexandermonta3f03bf2015-12-02 18:56:45726 def _escalate_code(old, new):
727 for x in (constants.INFRA_EXIT_CODE,
728 constants.ERROR_EXIT_CODE,
729 constants.WARNING_EXIT_CODE):
730 if x in (old, new):
731 return x
732 return 0
733
jbudorickeb7ea71c2015-09-28 16:40:20734 for _ in repetitions:
735 iteration_results = base_test_result.TestRunResults()
736 if java_tests:
[email protected]34020022013-08-06 23:35:34737 test_results, test_exit_code = test_dispatcher.RunTests(
jbudorickeb7ea71c2015-09-28 16:40:20738 java_tests, java_runner_factory, devices, shard=True,
739 test_timeout=None, num_retries=args.num_retries)
740 iteration_results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37741
alexandermonte2cbe022015-12-16 04:58:58742 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45743 exit_code = _escalate_code(exit_code, test_exit_code)
[email protected]6bc1bda22013-07-19 22:08:37744
jbudorickeb7ea71c2015-09-28 16:40:20745 if py_tests:
746 test_results, test_exit_code = test_dispatcher.RunTests(
747 py_tests, py_runner_factory, devices, shard=True, test_timeout=None,
748 num_retries=args.num_retries)
749 iteration_results.AddTestRunResults(test_results)
[email protected]4f777ca2014-08-08 01:45:59750
alexandermonte2cbe022015-12-16 04:58:58751 code_counts[test_exit_code] += 1
alexandermonta3f03bf2015-12-02 18:56:45752 exit_code = _escalate_code(exit_code, test_exit_code)
jbudorickeb7ea71c2015-09-28 16:40:20753
754 results.append(iteration_results)
755 report_results.LogFull(
756 results=iteration_results,
757 test_type='Instrumentation',
758 test_package=os.path.basename(args.test_apk),
759 annotation=args.annotations,
760 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37761
alexandermonte2cbe022015-12-16 04:58:58762
alexandermonta3f03bf2015-12-02 18:56:45763 if args.break_on_failure and exit_code in (constants.ERROR_EXIT_CODE,
764 constants.INFRA_EXIT_CODE):
765 break
766
alexandermonte2cbe022015-12-16 04:58:58767 logging.critical('Instr tests: %s success, %s infra, %s errors, %s warnings',
768 str(code_counts[0]),
769 str(code_counts[constants.INFRA_EXIT_CODE]),
770 str(code_counts[constants.ERROR_EXIT_CODE]),
771 str(code_counts[constants.WARNING_EXIT_CODE]))
772
jama47ca85c2014-12-03 18:38:07773 if args.json_results_file:
774 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54775
[email protected]6bc1bda22013-07-19 22:08:37776 return exit_code
777
778
jama47ca85c2014-12-03 18:38:07779def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07780 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07781 runner_factory, tests = junit_setup.Setup(args)
mikecasec638a072015-04-01 16:35:35782 results, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
783
784 report_results.LogFull(
785 results=results,
786 test_type='JUnit',
787 test_package=args.test_suite)
788
mikecase572401b2015-04-09 02:28:57789 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20790 json_results.GenerateJsonResultsFile([results], args.json_results_file)
mikecase572401b2015-04-09 02:28:57791
jbudorick9a6b7b332014-09-20 00:01:07792 return exit_code
793
794
jama47ca85c2014-12-03 18:38:07795def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14796 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07797 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14798
799 runner_factory, tests = monkey_setup.Setup(monkey_options)
800
801 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46802 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07803 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14804
805 report_results.LogFull(
806 results=results,
807 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28808 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14809
jama47ca85c2014-12-03 18:38:07810 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20811 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54812
[email protected]3dbdfa42013-08-08 01:08:14813 return exit_code
814
815
jbudorickdde688fb2015-08-27 03:00:17816def _RunPerfTests(args, active_devices):
[email protected]ec3170b2013-08-14 14:39:47817 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07818 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56819
820 # Just save a simple json with a list of test names.
821 if perf_options.output_json_list:
822 return perf_test_runner.OutputJsonList(
823 perf_options.steps, perf_options.output_json_list)
824
[email protected]ad32f312013-11-13 04:03:29825 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47826 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42827 return perf_test_runner.PrintTestOutput(
perezju67cf7f12015-09-29 11:39:05828 perf_options.print_step, perf_options.output_chartjson_data,
829 perf_options.get_output_dir_archive)
[email protected]ec3170b2013-08-14 14:39:47830
jbudorickdde688fb2015-08-27 03:00:17831 runner_factory, tests, devices = perf_setup.Setup(
832 perf_options, active_devices)
[email protected]ec3170b2013-08-14 14:39:47833
[email protected]a72f0752014-06-03 23:52:34834 # shard=False means that each device will get the full list of tests
835 # and then each one will decide their own affinity.
836 # shard=True means each device will pop the next test available from a queue,
837 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57838 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34839 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07840 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47841
842 report_results.LogFull(
843 results=results,
844 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12845 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52846
jama47ca85c2014-12-03 18:38:07847 if args.json_results_file:
jbudorickeb7ea71c2015-09-28 16:40:20848 json_results.GenerateJsonResultsFile([results], args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54849
[email protected]def4bce2013-11-12 12:59:52850 if perf_options.single_step:
851 return perf_test_runner.PrintTestOutput('single_step')
852
[email protected]11ce8452014-02-17 10:55:03853 perf_test_runner.PrintSummary(tests)
854
[email protected]86184c7b2013-08-15 15:06:57855 # Always return 0 on the sharding stage. Individual tests exit_code
856 # will be returned on the print_step stage.
857 return 0
[email protected]ec3170b2013-08-14 14:39:47858
[email protected]3dbdfa42013-08-08 01:08:14859
jama47ca85c2014-12-03 18:38:07860def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13861 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07862 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13863 suite_path = suite_vars['path']
864 suite_test_modules = suite_vars['test_modules']
865
866 sys.path = [suite_path] + sys.path
867 try:
868 suite = unittest.TestSuite()
869 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
870 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07871 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13872 return 0 if runner.run(suite).wasSuccessful() else 1
873 finally:
874 sys.path = sys.path[1:]
875
876
agrievea538a142015-10-09 15:45:56877def _GetAttachedDevices(blacklist_file, test_device, enable_cache):
[email protected]f7148dd42013-08-20 14:24:57878 """Get all attached devices.
879
880 Args:
agrievea538a142015-10-09 15:45:56881 blacklist_file: Path to device blacklist.
[email protected]f7148dd42013-08-20 14:24:57882 test_device: Name of a specific device to use.
agrievea538a142015-10-09 15:45:56883 enable_cache: Whether to enable checksum caching.
[email protected]f7148dd42013-08-20 14:24:57884
885 Returns:
886 A list of attached devices.
887 """
jbudoricka583ba32015-09-11 17:23:19888 blacklist = (device_blacklist.Blacklist(blacklist_file)
889 if blacklist_file
890 else None)
jbudorickdde688fb2015-08-27 03:00:17891
agrievea538a142015-10-09 15:45:56892 attached_devices = device_utils.DeviceUtils.HealthyDevices(
893 blacklist, enable_device_files_cache=enable_cache)
aberent6a02a6182015-04-29 11:07:55894 if test_device:
jbudorick4551d0dc2015-04-29 16:07:06895 test_device = [d for d in attached_devices if d == test_device]
896 if not test_device:
897 raise device_errors.DeviceUnreachableError(
898 'Did not find device %s among attached device. Attached devices: %s'
899 % (test_device, ', '.join(attached_devices)))
900 return test_device
aberent6a02a6182015-04-29 11:07:55901
jbudorick4551d0dc2015-04-29 16:07:06902 else:
903 if not attached_devices:
904 raise device_errors.NoDevicesError()
905 return sorted(attached_devices)
[email protected]f7148dd42013-08-20 14:24:57906
907
jbudorick58b4d362015-09-08 16:44:59908def RunTestsCommand(args, parser): # pylint: disable=too-many-return-statements
[email protected]fbe29322013-07-09 09:03:26909 """Checks test type and dispatches to the appropriate function.
910
911 Args:
jama47ca85c2014-12-03 18:38:07912 args: argparse.Namespace object.
913 parser: argparse.ArgumentParser object.
[email protected]fbe29322013-07-09 09:03:26914
915 Returns:
916 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10917
918 Raises:
919 Exception: Unknown command name passed in, or an exception from an
920 individual test runner.
[email protected]fbe29322013-07-09 09:03:26921 """
jama47ca85c2014-12-03 18:38:07922 command = args.command
[email protected]fbe29322013-07-09 09:03:26923
jama47ca85c2014-12-03 18:38:07924 ProcessCommonOptions(args)
[email protected]d82f0252013-07-12 23:22:57925
jama47ca85c2014-12-03 18:38:07926 if args.enable_platform_mode:
rnephew5c499782014-12-12 19:08:55927 return RunTestsInPlatformMode(args, parser)
jbudorick66dc3722014-11-06 21:33:51928
[email protected]c0662e092013-11-12 11:51:25929 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40930 if not ports.ResetTestServerPortAllocation():
931 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25932
agrieve18930bd2015-10-09 17:41:42933 def get_devices():
934 return _GetAttachedDevices(args.blacklist_file, args.test_device,
935 args.enable_device_cache)
936
[email protected]fbe29322013-07-09 09:03:26937 if command == 'gtest':
jbudorick566592ab2015-09-21 15:32:47938 return RunTestsInPlatformMode(args, parser)
[email protected]6b6abac6d2013-10-03 11:56:38939 elif command == 'linker':
agrieve18930bd2015-10-09 17:41:42940 return _RunLinkerTests(args, get_devices())
[email protected]fbe29322013-07-09 09:03:26941 elif command == 'instrumentation':
agrieve18930bd2015-10-09 17:41:42942 return _RunInstrumentationTests(args, get_devices())
jbudorick9a6b7b332014-09-20 00:01:07943 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07944 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:14945 elif command == 'monkey':
agrieve18930bd2015-10-09 17:41:42946 return _RunMonkeyTests(args, get_devices())
[email protected]ec3170b2013-08-14 14:39:47947 elif command == 'perf':
agrieve18930bd2015-10-09 17:41:42948 return _RunPerfTests(args, get_devices())
jbudorick256fd532014-10-24 01:50:13949 elif command == 'python':
jama47ca85c2014-12-03 18:38:07950 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:26951 else:
[email protected]6bc1bda22013-07-19 22:08:37952 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26953
[email protected]fbe29322013-07-09 09:03:26954
jbudorick66dc3722014-11-06 21:33:51955_SUPPORTED_IN_PLATFORM_MODE = [
956 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:06957 'gtest',
958 'instrumentation',
959 'uirobot',
jbudorick66dc3722014-11-06 21:33:51960]
961
962
jama47ca85c2014-12-03 18:38:07963def RunTestsInPlatformMode(args, parser):
jbudorick66dc3722014-11-06 21:33:51964
jbudorick566592ab2015-09-21 15:32:47965 def infra_error(message):
966 parser.exit(status=constants.INFRA_EXIT_CODE, message=message)
jbudorickb9b0ada2015-09-17 22:52:58967
jbudorick566592ab2015-09-21 15:32:47968 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
969 infra_error('%s is not yet supported in platform mode' % args.command)
970
971 with environment_factory.CreateEnvironment(args, infra_error) as env:
972 with test_instance_factory.CreateTestInstance(args, infra_error) as test:
jbudorick66dc3722014-11-06 21:33:51973 with test_run_factory.CreateTestRun(
jbudorick566592ab2015-09-21 15:32:47974 args, env, test, infra_error) as test_run:
jbudorickeb7ea71c2015-09-28 16:40:20975 results = []
976 repetitions = (xrange(args.repeat + 1) if args.repeat >= 0
977 else itertools.count())
alexandermonte2cbe022015-12-16 04:58:58978 result_counts = collections.defaultdict(
979 lambda: collections.defaultdict(int))
980 iteration_count = 0
jbudorickeb7ea71c2015-09-28 16:40:20981 for _ in repetitions:
982 iteration_results = test_run.RunTests()
jbudorickeb7ea71c2015-09-28 16:40:20983 if iteration_results is not None:
alexandermonte2cbe022015-12-16 04:58:58984 iteration_count += 1
jbudorickd4f77982015-09-28 21:09:18985 results.append(iteration_results)
alexandermonte2cbe022015-12-16 04:58:58986 for r in iteration_results.GetAll():
987 result_counts[r.GetName()][r.GetType()] += 1
jbudorickeb7ea71c2015-09-28 16:40:20988 report_results.LogFull(
989 results=iteration_results,
990 test_type=test.TestType(),
991 test_package=test_run.TestPackage(),
992 annotation=getattr(args, 'annotations', None),
993 flakiness_server=getattr(args, 'flakiness_dashboard_server',
994 None))
alexandermonta3f03bf2015-12-02 18:56:45995 if args.break_on_failure and not iteration_results.DidRunPass():
996 break
jbudorick66dc3722014-11-06 21:33:51997
alexandermonte2cbe022015-12-16 04:58:58998 if iteration_count > 1:
999 # display summary results
1000 # only display results for a test if at least one test did not pass
1001 all_pass = 0
1002 tot_tests = 0
1003 for test_name in result_counts:
1004 tot_tests += 1
1005 if any(result_counts[test_name][x] for x in (
1006 base_test_result.ResultType.FAIL,
1007 base_test_result.ResultType.CRASH,
1008 base_test_result.ResultType.TIMEOUT,
1009 base_test_result.ResultType.UNKNOWN)):
1010 logging.critical(
1011 '%s: %s',
1012 test_name,
1013 ', '.join('%s %s' % (str(result_counts[test_name][i]), i)
1014 for i in base_test_result.ResultType.GetTypes()))
1015 else:
1016 all_pass += 1
1017
1018 logging.critical('%s of %s tests passed in all %s runs',
1019 str(all_pass),
1020 str(tot_tests),
1021 str(iteration_count))
1022
jama47ca85c2014-12-03 18:38:071023 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:541024 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:071025 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:541026
jbudorickeb7ea71c2015-09-28 16:40:201027 return (0 if all(r.DidRunPass() for r in results)
1028 else constants.ERROR_EXIT_CODE)
jbudorick66dc3722014-11-06 21:33:511029
1030
jama47ca85c2014-12-03 18:38:071031CommandConfigTuple = collections.namedtuple(
1032 'CommandConfigTuple',
1033 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:261034VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:071035 'gtest': CommandConfigTuple(
1036 AddGTestOptions,
1037 'googletest-based C++ tests'),
1038 'instrumentation': CommandConfigTuple(
1039 AddInstrumentationTestOptions,
1040 'InstrumentationTestCase-based Java tests'),
jama47ca85c2014-12-03 18:38:071041 'junit': CommandConfigTuple(
1042 AddJUnitTestOptions,
1043 'JUnit4-based Java tests'),
1044 'monkey': CommandConfigTuple(
1045 AddMonkeyTestOptions,
1046 "Tests based on Android's monkey"),
1047 'perf': CommandConfigTuple(
1048 AddPerfTestOptions,
1049 'Performance tests'),
1050 'python': CommandConfigTuple(
1051 AddPythonTestOptions,
1052 'Python tests based on unittest.TestCase'),
1053 'linker': CommandConfigTuple(
1054 AddLinkerTestOptions,
1055 'Linker tests'),
rnephew5c499782014-12-12 19:08:551056 'uirobot': CommandConfigTuple(
1057 AddUirobotTestOptions,
1058 'Uirobot test'),
jama47ca85c2014-12-03 18:38:071059}
[email protected]fbe29322013-07-09 09:03:261060
1061
[email protected]7c53a602014-03-24 16:21:441062def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:241063 for thread in threading.enumerate():
1064 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:211065
1066
[email protected]7c53a602014-03-24 16:21:441067def main():
[email protected]83bb8152013-11-19 15:02:211068 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:071069
1070 parser = argparse.ArgumentParser()
1071 command_parsers = parser.add_subparsers(title='test types',
1072 dest='command')
1073
1074 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
1075 key=lambda x: x[0]):
1076 subparser = command_parsers.add_parser(
1077 test_type, usage='%(prog)s [options]', help=config.help_txt)
1078 config.add_options_func(subparser)
1079
1080 args = parser.parse_args()
mikecasee74051022015-02-26 23:08:221081
1082 try:
1083 return RunTestsCommand(args, parser)
1084 except base_error.BaseError as e:
1085 logging.exception('Error occurred.')
1086 if e.is_infra_error:
1087 return constants.INFRA_EXIT_CODE
mswecce6732015-06-06 00:31:331088 return constants.ERROR_EXIT_CODE
mikecasee74051022015-02-26 23:08:221089 except: # pylint: disable=W0702
1090 logging.exception('Unrecognized error occurred.')
1091 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:261092
[email protected]fbe29322013-07-09 09:03:261093
1094if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:441095 sys.exit(main())