blob: d508ef8d7c446aee8950c41c7f5b0fab20ebe393 [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
[email protected]f7148dd42013-08-20 14:24:5711import logging
[email protected]fbe29322013-07-09 09:03:2612import os
[email protected]6bc1bda22013-07-19 22:08:3713import shutil
[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
[email protected]fbe29322013-07-09 09:03:2619from pylib import constants
[email protected]c0662e092013-11-12 11:51:2520from pylib import forwarder
[email protected]fbe29322013-07-09 09:03:2621from pylib import ports
22from pylib.base import base_test_result
jbudorick66dc3722014-11-06 21:33:5123from pylib.base import environment_factory
[email protected]6bc1bda22013-07-19 22:08:3724from pylib.base import test_dispatcher
jbudorick66dc3722014-11-06 21:33:5125from pylib.base import test_instance_factory
26from pylib.base import test_run_factory
jbudorick4551d0dc2015-04-29 16:07:0627from pylib.device import device_errors
28from pylib.device import device_utils
[email protected]6bc1bda22013-07-19 22:08:3729from pylib.gtest import gtest_config
jbudorick590a3d72015-06-12 23:53:3130# TODO(jbudorick): Remove this once we stop selectively enabling platform mode.
31from pylib.gtest import gtest_test_instance
[email protected]2a684222013-08-01 16:59:2232from pylib.gtest import setup as gtest_setup
33from pylib.gtest import test_options as gtest_test_options
[email protected]6b6abac6d2013-10-03 11:56:3834from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1335from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3736from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2237from pylib.instrumentation import test_options as instrumentation_test_options
jbudorick9a6b7b332014-09-20 00:01:0738from pylib.junit import setup as junit_setup
39from pylib.junit import test_dispatcher as junit_dispatcher
[email protected]3dbdfa42013-08-08 01:08:1440from pylib.monkey import setup as monkey_setup
41from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4742from pylib.perf import setup as perf_setup
43from pylib.perf import test_options as perf_test_options
44from pylib.perf import test_runner as perf_test_runner
jbudorickb8c42072014-12-01 18:07:5445from pylib.results import json_results
46from pylib.results import report_results
[email protected]6bc1bda22013-07-19 22:08:3747from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2248from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]2eea4872014-07-28 23:06:1749from pylib.utils import apk_helper
mikecasee74051022015-02-26 23:08:2250from pylib.utils import base_error
[email protected]71aec4b2013-11-20 00:35:2451from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3752from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2653
54
jama47ca85c2014-12-03 18:38:0755def AddCommonOptions(parser):
56 """Adds all common options to |parser|."""
[email protected]fbe29322013-07-09 09:03:2657
jama47ca85c2014-12-03 18:38:0758 group = parser.add_argument_group('Common Options')
59
[email protected]dfffbcbc2013-09-17 22:06:0160 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
jama47ca85c2014-12-03 18:38:0761
62 debug_or_release_group = group.add_mutually_exclusive_group()
63 debug_or_release_group.add_argument(
64 '--debug', action='store_const', const='Debug', dest='build_type',
65 default=default_build_type,
66 help=('If set, run test suites under out/Debug. '
67 'Default is env var BUILDTYPE or Debug.'))
68 debug_or_release_group.add_argument(
69 '--release', action='store_const', const='Release', dest='build_type',
70 help=('If set, run test suites under out/Release. '
71 'Default is env var BUILDTYPE or Debug.'))
72
73 group.add_argument('--build-directory', dest='build_directory',
74 help=('Path to the directory in which build files are'
75 ' located (should not include build type)'))
76 group.add_argument('--output-directory', dest='output_directory',
77 help=('Path to the directory in which build files are'
78 ' located (must include build type). This will take'
79 ' precedence over --debug, --release and'
80 ' --build-directory'))
81 group.add_argument('--num_retries', dest='num_retries', type=int, default=2,
82 help=('Number of retries for a test before '
83 'giving up (default: %(default)s).'))
84 group.add_argument('-v',
85 '--verbose',
86 dest='verbose_count',
87 default=0,
88 action='count',
89 help='Verbose level (multiple times for more)')
90 group.add_argument('--flakiness-dashboard-server',
91 dest='flakiness_dashboard_server',
92 help=('Address of the server that is hosting the '
93 'Chrome for Android flakiness dashboard.'))
94 group.add_argument('--enable-platform-mode', action='store_true',
95 help=('Run the test scripts in platform mode, which '
96 'conceptually separates the test runner from the '
97 '"device" (local or remote, real or emulated) on '
98 'which the tests are running. [experimental]'))
99 group.add_argument('-e', '--environment', default='local',
100 choices=constants.VALID_ENVIRONMENTS,
101 help='Test environment to run in (default: %(default)s).')
102 group.add_argument('--adb-path',
103 help=('Specify the absolute path of the adb binary that '
104 'should be used.'))
105 group.add_argument('--json-results-file', dest='json_results_file',
106 help='If set, will dump results in JSON form '
107 'to specified file.')
[email protected]fbe29322013-07-09 09:03:26108
jama47ca85c2014-12-03 18:38:07109def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26110 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07111 run_tests_helper.SetLogLevel(args.verbose_count)
112 constants.SetBuildType(args.build_type)
113 if args.build_directory:
114 constants.SetBuildDirectory(args.build_directory)
115 if args.output_directory:
mikecase0aea9c52015-04-30 00:12:33116 constants.SetOutputDirectory(args.output_directory)
jama47ca85c2014-12-03 18:38:07117 if args.adb_path:
118 constants.SetAdbPath(args.adb_path)
mikecase48e16bf2014-11-19 22:46:45119 # Some things such as Forwarder require ADB to be in the environment path.
120 adb_dir = os.path.dirname(constants.GetAdbPath())
121 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
122 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26123
124
rnephew5c499782014-12-12 19:08:55125def AddRemoteDeviceOptions(parser):
126 group = parser.add_argument_group('Remote Device Options')
127
rnephewefe44b42015-02-04 04:45:15128 group.add_argument('--trigger',
jbudoricke6c560152015-01-13 23:49:28129 help=('Only triggers the test if set. Stores test_run_id '
130 'in given file path. '))
rnephewefe44b42015-02-04 04:45:15131 group.add_argument('--collect',
jbudoricke6c560152015-01-13 23:49:28132 help=('Only collects the test results if set. '
133 'Gets test_run_id from given file path.'))
rnephewefe44b42015-02-04 04:45:15134 group.add_argument('--remote-device', action='append',
jbudoricke6c560152015-01-13 23:49:28135 help='Device type to run test on.')
rnephewefe44b42015-02-04 04:45:15136 group.add_argument('--results-path',
jbudoricke6c560152015-01-13 23:49:28137 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11138 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28139 help='HTTP protocol to use. (http or https)')
rnephewefe44b42015-02-04 04:45:15140 group.add_argument('--api-address',
141 help='Address to send HTTP requests.')
142 group.add_argument('--api-port',
143 help='Port to send HTTP requests to.')
144 group.add_argument('--runner-type',
jbudoricke6c560152015-01-13 23:49:28145 help='Type of test to run as.')
rnephewefe44b42015-02-04 04:45:15146 group.add_argument('--runner-package',
147 help='Package name of test.')
148 group.add_argument('--device-type',
rnephewa46fc562015-01-23 16:00:14149 choices=constants.VALID_DEVICE_TYPES,
150 help=('Type of device to run on. iOS or android'))
rnephewefe44b42015-02-04 04:45:15151 group.add_argument('--device-oem', action='append',
152 help='Device OEM to run on.')
153 group.add_argument('--remote-device-file',
154 help=('File with JSON to select remote device. '
155 'Overrides all other flags.'))
rnephewc9ae8f52015-02-13 03:02:55156 group.add_argument('--remote-device-timeout', type=int,
157 help='Times to retry finding remote device')
mikecase520cbbb52015-04-21 18:51:18158 group.add_argument('--network-config', type=int,
159 help='Integer that specifies the network environment '
160 'that the tests will be run in.')
rnephewefe44b42015-02-04 04:45:15161
162 device_os_group = group.add_mutually_exclusive_group()
163 device_os_group.add_argument('--remote-device-minimum-os',
164 help='Minimum OS on device.')
165 device_os_group.add_argument('--remote-device-os', action='append',
166 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55167
168 api_secret_group = group.add_mutually_exclusive_group()
169 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28170 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55171 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28172 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55173
174 api_key_group = group.add_mutually_exclusive_group()
175 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28176 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55177 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28178 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55179
180
jama47ca85c2014-12-03 18:38:07181def AddDeviceOptions(parser):
182 """Adds device options to |parser|."""
183 group = parser.add_argument_group(title='Device Options')
jama47ca85c2014-12-03 18:38:07184 group.add_argument('--tool',
185 dest='tool',
186 help=('Run the test under a tool '
187 '(use --tool help to list them)'))
188 group.add_argument('-d', '--device', dest='test_device',
189 help=('Target device for the test suite '
190 'to run on.'))
jbudorick256fd532014-10-24 01:50:13191
192
jama47ca85c2014-12-03 18:38:07193def AddGTestOptions(parser):
194 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26195
jama47ca85c2014-12-03 18:38:07196 gtest_suites = list(gtest_config.STABLE_TEST_SUITES
197 + gtest_config.EXPERIMENTAL_TEST_SUITES)
[email protected]fbe29322013-07-09 09:03:26198
jama47ca85c2014-12-03 18:38:07199 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49200 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07201 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick15cdcd52014-12-03 19:58:49202 help=('Executable name of the test suite to run. '
203 'Available suites include (but are not limited to): '
204 '%s' % ', '.join('"%s"' % s for s in gtest_suites)))
jama47ca85c2014-12-03 18:38:07205 group.add_argument('--gtest_also_run_disabled_tests',
206 '--gtest-also-run-disabled-tests',
207 dest='run_disabled', action='store_true',
208 help='Also run disabled tests if applicable.')
209 group.add_argument('-a', '--test-arguments', dest='test_arguments',
210 default='',
211 help='Additional arguments to pass to the test.')
212 group.add_argument('-t', dest='timeout', type=int, default=60,
213 help='Timeout to wait for each test '
214 '(default: %(default)s).')
215 group.add_argument('--isolate_file_path',
216 '--isolate-file-path',
217 dest='isolate_file_path',
218 help='.isolate file path to override the default '
219 'path')
jbudorick5ee45892015-06-10 18:46:22220 group.add_argument('--app-data-file', action='append', dest='app_data_files',
221 help='A file path relative to the app data directory '
222 'that should be saved to the host.')
223 group.add_argument('--app-data-file-dir',
224 help='Host directory to which app data files will be'
225 ' saved. Used with --app-data-file.')
mlliud7f9fe92015-06-15 19:36:56226 group.add_argument('--delete-stale-data', dest='delete_stale_data',
227 action='store_true',
228 help='Delete stale test data on the device.')
jbudorick442a6932015-02-03 03:01:15229
230 filter_group = group.add_mutually_exclusive_group()
231 filter_group.add_argument('-f', '--gtest_filter', '--gtest-filter',
232 dest='test_filter',
233 help='googletest-style filter string.')
234 filter_group.add_argument('--gtest-filter-file', dest='test_filter_file',
235 help='Path to file that contains googletest-style '
236 'filter strings. (Lines will be joined with '
237 '":" to create a single filter string.)')
238
jama47ca85c2014-12-03 18:38:07239 AddDeviceOptions(parser)
240 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55241 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26242
243
jama47ca85c2014-12-03 18:38:07244def AddLinkerTestOptions(parser):
245 group = parser.add_argument_group('Linker Test Options')
246 group.add_argument('-f', '--gtest-filter', dest='test_filter',
247 help='googletest-style filter string.')
248 AddCommonOptions(parser)
249 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38250
251
jama47ca85c2014-12-03 18:38:07252def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26253 """Adds the Java test options to |option_parser|."""
254
jama47ca85c2014-12-03 18:38:07255 argument_group.add_argument(
256 '-f', '--test-filter', dest='test_filter',
257 help=('Test filter (if not fully qualified, will run all matches).'))
258 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26259 '-A', '--annotation', dest='annotation_str',
260 help=('Comma-separated list of annotations. Run only tests with any of '
261 'the given annotations. An annotation can be either a key or a '
262 'key-values pair. A test that has no annotation is considered '
263 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07264 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26265 '-E', '--exclude-annotation', dest='exclude_annotation_str',
266 help=('Comma-separated list of annotations. Exclude tests with these '
267 'annotations.'))
jama47ca85c2014-12-03 18:38:07268 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59269 '--screenshot', dest='screenshot_failures', action='store_true',
270 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07271 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59272 '--save-perf-json', action='store_true',
273 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07274 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59275 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07276 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59277 '--test_data', '--test-data', action='append', default=[],
278 help=('Each instance defines a directory of test data that should be '
279 'copied to the target(s) before running the tests. The argument '
280 'should be of the form <target>:<source>, <target> is relative to '
281 'the device data directory, and <source> is relative to the '
282 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21283 argument_group.add_argument(
284 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
285 default=True, help='Removes the dalvik.vm.enableassertions property')
286
[email protected]fbe29322013-07-09 09:03:26287
288
jama47ca85c2014-12-03 18:38:07289def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26290 """Processes options/arguments and populates |options| with defaults."""
291
jama47ca85c2014-12-03 18:38:07292 # TODO(jbudorick): Handle most of this function in argparse.
293 if args.annotation_str:
294 args.annotations = args.annotation_str.split(',')
295 elif args.test_filter:
296 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26297 else:
jama47ca85c2014-12-03 18:38:07298 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
299 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26300
jama47ca85c2014-12-03 18:38:07301 if args.exclude_annotation_str:
302 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26303 else:
jama47ca85c2014-12-03 18:38:07304 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26305
[email protected]fbe29322013-07-09 09:03:26306
jama47ca85c2014-12-03 18:38:07307def AddInstrumentationTestOptions(parser):
308 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26309
jama47ca85c2014-12-03 18:38:07310 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26311
jama47ca85c2014-12-03 18:38:07312 group = parser.add_argument_group('Instrumentation Test Options')
313 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26314
jama47ca85c2014-12-03 18:38:07315 java_or_python_group = group.add_mutually_exclusive_group()
316 java_or_python_group.add_argument(
317 '-j', '--java-only', action='store_false',
318 dest='run_python_tests', default=True, help='Run only the Java tests.')
319 java_or_python_group.add_argument(
320 '-p', '--python-only', action='store_false',
321 dest='run_java_tests', default=True,
322 help='Run only the host-driven tests.')
323
324 group.add_argument('--host-driven-root',
325 help='Root of the host-driven tests.')
326 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
327 action='store_true',
328 help='Wait for debugger.')
jbudorick911be58d2015-01-13 02:51:06329 group.add_argument('--apk-under-test', dest='apk_under_test',
330 help=('the name of the apk under test.'))
jama47ca85c2014-12-03 18:38:07331 group.add_argument('--test-apk', dest='test_apk', required=True,
332 help=('The name of the apk containing the tests '
333 '(without the .apk extension; '
334 'e.g. "ContentShellTest").'))
335 group.add_argument('--coverage-dir',
336 help=('Directory in which to place all generated '
337 'EMMA coverage files.'))
338 group.add_argument('--device-flags', dest='device_flags', default='',
339 help='The relative filepath to a file containing '
340 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06341 group.add_argument('--device-flags-file', default='',
342 help='The relative filepath to a file containing '
343 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07344 group.add_argument('--isolate_file_path',
345 '--isolate-file-path',
346 dest='isolate_file_path',
347 help='.isolate file path to override the default '
348 'path')
mlliud7f9fe92015-06-15 19:36:56349 group.add_argument('--delete-stale-data', dest='delete_stale_data',
350 action='store_true',
351 help='Delete stale test data on the device.')
jama47ca85c2014-12-03 18:38:07352
353 AddCommonOptions(parser)
354 AddDeviceOptions(parser)
rnephewe416dff2015-01-21 21:26:37355 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26356
357
jama47ca85c2014-12-03 18:38:07358def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22359 """Processes options/arguments and populate |options| with defaults.
360
361 Args:
jama47ca85c2014-12-03 18:38:07362 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22363
364 Returns:
365 An InstrumentationOptions named tuple which contains all options relevant to
366 instrumentation tests.
367 """
[email protected]fbe29322013-07-09 09:03:26368
jama47ca85c2014-12-03 18:38:07369 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26370
jama47ca85c2014-12-03 18:38:07371 if not args.host_driven_root:
372 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13373
jama47ca85c2014-12-03 18:38:07374 args.test_apk_path = os.path.join(
[email protected]2eea4872014-07-28 23:06:17375 constants.GetOutDirectory(),
376 constants.SDK_BUILD_APKS_DIR,
jama47ca85c2014-12-03 18:38:07377 '%s.apk' % args.test_apk)
378 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15379 constants.GetOutDirectory(),
380 constants.SDK_BUILD_TEST_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07381 '%s.jar' % args.test_apk)
382 args.test_support_apk_path = '%sSupport%s' % (
383 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46384
jama47ca85c2014-12-03 18:38:07385 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46386
jama47ca85c2014-12-03 18:38:07387 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22388 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07389 args.tool,
jama47ca85c2014-12-03 18:38:07390 args.annotations,
391 args.exclude_annotations,
392 args.test_filter,
393 args.test_data,
394 args.save_perf_json,
395 args.screenshot_failures,
396 args.wait_for_debugger,
397 args.coverage_dir,
398 args.test_apk,
399 args.test_apk_path,
400 args.test_apk_jar_path,
401 args.test_runner,
402 args.test_support_apk_path,
403 args.device_flags,
davileen98efad12015-01-05 19:48:21404 args.isolate_file_path,
mlliud7f9fe92015-06-15 19:36:56405 args.set_asserts,
406 args.delete_stale_data
[email protected]5e2f3f62014-06-23 12:31:46407 )
[email protected]2a684222013-08-01 16:59:22408
[email protected]fbe29322013-07-09 09:03:26409
jama47ca85c2014-12-03 18:38:07410def AddUIAutomatorTestOptions(parser):
411 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26412
jama47ca85c2014-12-03 18:38:07413 group = parser.add_argument_group('UIAutomator Test Options')
414 AddJavaTestOptions(group)
415 group.add_argument(
416 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
417 metavar='PACKAGE', help='Package under test.')
418 group.add_argument(
419 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26420 help=('The name of the dexed jar containing the tests (without the '
421 '.dex.jar extension). Alternatively, this can be a full path '
422 'to the jar.'))
423
jama47ca85c2014-12-03 18:38:07424 AddCommonOptions(parser)
425 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26426
427
jama47ca85c2014-12-03 18:38:07428def ProcessUIAutomatorOptions(args):
[email protected]2a684222013-08-01 16:59:22429 """Processes UIAutomator options/arguments.
430
431 Args:
jama47ca85c2014-12-03 18:38:07432 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22433
434 Returns:
435 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14436 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22437 """
[email protected]fbe29322013-07-09 09:03:26438
jama47ca85c2014-12-03 18:38:07439 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26440
jama47ca85c2014-12-03 18:38:07441 if os.path.exists(args.test_jar):
[email protected]fbe29322013-07-09 09:03:26442 # The dexed JAR is fully qualified, assume the info JAR lives along side.
jama47ca85c2014-12-03 18:38:07443 args.uiautomator_jar = args.test_jar
[email protected]fbe29322013-07-09 09:03:26444 else:
jama47ca85c2014-12-03 18:38:07445 args.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15446 constants.GetOutDirectory(),
447 constants.SDK_BUILD_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07448 '%s.dex.jar' % args.test_jar)
449 args.uiautomator_info_jar = (
450 args.uiautomator_jar[:args.uiautomator_jar.find('.dex.jar')] +
[email protected]fbe29322013-07-09 09:03:26451 '_java.jar')
452
[email protected]2a684222013-08-01 16:59:22453 return uiautomator_test_options.UIAutomatorOptions(
jama47ca85c2014-12-03 18:38:07454 args.tool,
jama47ca85c2014-12-03 18:38:07455 args.annotations,
456 args.exclude_annotations,
457 args.test_filter,
458 args.test_data,
459 args.save_perf_json,
460 args.screenshot_failures,
461 args.uiautomator_jar,
462 args.uiautomator_info_jar,
davileen98efad12015-01-05 19:48:21463 args.package,
464 args.set_asserts)
[email protected]2a684222013-08-01 16:59:22465
[email protected]fbe29322013-07-09 09:03:26466
jama47ca85c2014-12-03 18:38:07467def AddJUnitTestOptions(parser):
468 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07469
jama47ca85c2014-12-03 18:38:07470 group = parser.add_argument_group('JUnit Test Options')
471 group.add_argument(
472 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07473 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07474 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07475 '-f', '--test-filter', dest='test_filter',
476 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07477 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07478 '--package-filter', dest='package_filter',
479 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07480 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07481 '--runner-filter', dest='runner_filter',
482 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07483 group.add_argument(
484 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07485 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07486 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07487
488
jama47ca85c2014-12-03 18:38:07489def AddMonkeyTestOptions(parser):
490 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07491
jama47ca85c2014-12-03 18:38:07492 group = parser.add_argument_group('Monkey Test Options')
493 group.add_argument(
494 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
495 metavar='PACKAGE', help='Package under test.')
496 group.add_argument(
497 '--event-count', default=10000, type=int,
498 help='Number of events to generate (default: %(default)s).')
499 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14500 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12501 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07502 group.add_argument(
503 '--throttle', default=100, type=int,
504 help='Delay between events (ms) (default: %(default)s). ')
505 group.add_argument(
506 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14507 help=('Seed value for pseudo-random generator. Same seed value generates '
508 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07509 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14510 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07511 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14512
jama47ca85c2014-12-03 18:38:07513 AddCommonOptions(parser)
514 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14515
jama47ca85c2014-12-03 18:38:07516def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14517 """Processes all monkey test options.
518
519 Args:
jama47ca85c2014-12-03 18:38:07520 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14521
522 Returns:
523 A MonkeyOptions named tuple which contains all options relevant to
524 monkey tests.
525 """
jama47ca85c2014-12-03 18:38:07526 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
527 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14528 if category:
jama47ca85c2014-12-03 18:38:07529 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14530
jama47ca85c2014-12-03 18:38:07531 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14532 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07533 args.verbose_count,
534 args.package,
535 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14536 category,
jama47ca85c2014-12-03 18:38:07537 args.throttle,
538 args.seed,
539 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14540
rnephew5c499782014-12-12 19:08:55541def AddUirobotTestOptions(parser):
542 """Adds uirobot test options to |option_parser|."""
543 group = parser.add_argument_group('Uirobot Test Options')
544
rnephewefe44b42015-02-04 04:45:15545 group.add_argument('--app-under-test', required=True,
546 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55547 group.add_argument(
548 '--minutes', default=5, type=int,
jbudorick676b1202015-02-06 22:02:27549 help='Number of minutes to run uirobot test [default: %(default)s].')
rnephew5c499782014-12-12 19:08:55550
551 AddCommonOptions(parser)
552 AddDeviceOptions(parser)
553 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14554
jama47ca85c2014-12-03 18:38:07555def AddPerfTestOptions(parser):
556 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47557
jama47ca85c2014-12-03 18:38:07558 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47559
jama47ca85c2014-12-03 18:38:07560 class SingleStepAction(argparse.Action):
561 def __call__(self, parser, namespace, values, option_string=None):
562 if values and not namespace.single_step:
563 parser.error('single step command provided, '
564 'but --single-step not specified.')
565 elif namespace.single_step and not values:
566 parser.error('--single-step specified, '
567 'but no single step command provided.')
568 setattr(namespace, self.dest, values)
569
570 step_group = group.add_mutually_exclusive_group(required=True)
571 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
572 # This requires removing "--" from client calls.
573 step_group.add_argument(
574 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52575 help='Execute the given command with retries, but only print the result '
576 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07577 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46578 '--steps',
[email protected]def4bce2013-11-12 12:59:52579 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07580 step_group.add_argument(
581 '--print-step',
582 help='The name of a previously executed perf step to print.')
583
584 group.add_argument(
peterbd4e73d2014-12-03 15:47:36585 '--output-json-list',
586 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07587 group.add_argument(
peterbd4e73d2014-12-03 15:47:36588 '--collect-chartjson-data',
589 action='store_true',
590 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07591 group.add_argument(
peterbd4e73d2014-12-03 15:47:36592 '--output-chartjson-data',
593 default='',
594 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07595 group.add_argument(
596 '--flaky-steps',
597 help=('A JSON file containing steps that are flaky '
598 'and will have its exit code ignored.'))
599 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46600 '--no-timeout', action='store_true',
601 help=('Do not impose a timeout. Each perf step is responsible for '
602 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07603 group.add_argument(
[email protected]650487c2013-09-30 11:40:49604 '-f', '--test-filter',
605 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07606 group.add_argument(
607 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49608 help='Just print the steps without executing.')
jbudorick5cfff872015-07-01 18:46:13609 # Uses 0.1 degrees C because that's what Android does.
610 group.add_argument(
611 '--max-battery-temp', type=int,
612 help='Only start tests when the battery is at or below the given '
613 'temperature (0.1 C)')
jama47ca85c2014-12-03 18:38:07614 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
615 help='If --single-step is specified, the command to run.')
616 AddCommonOptions(parser)
617 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47618
619
jama47ca85c2014-12-03 18:38:07620def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47621 """Processes all perf test options.
622
623 Args:
jama47ca85c2014-12-03 18:38:07624 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47625
626 Returns:
627 A PerfOptions named tuple which contains all options relevant to
628 perf tests.
629 """
jama47ca85c2014-12-03 18:38:07630 # TODO(jbudorick): Move single_step handling down into the perf tests.
631 if args.single_step:
632 args.single_step = ' '.join(args.single_step_command)
633 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47634 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07635 args.steps, args.flaky_steps, args.output_json_list,
636 args.print_step, args.no_timeout, args.test_filter,
637 args.dry_run, args.single_step, args.collect_chartjson_data,
jbudorick5cfff872015-07-01 18:46:13638 args.output_chartjson_data, args.max_battery_temp)
[email protected]ec3170b2013-08-14 14:39:47639
640
jama47ca85c2014-12-03 18:38:07641def AddPythonTestOptions(parser):
642 group = parser.add_argument_group('Python Test Options')
643 group.add_argument(
644 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
645 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
646 help='Name of the test suite to run.')
647 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13648
649
jama47ca85c2014-12-03 18:38:07650def _RunGTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37651 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]6bc1bda22013-07-19 22:08:37652 exit_code = 0
jama47ca85c2014-12-03 18:38:07653 for suite_name in args.suite_name:
654 # TODO(jbudorick): Either deprecate multi-suite or move its handling down
655 # into the gtest code.
[email protected]2a684222013-08-01 16:59:22656 gtest_options = gtest_test_options.GTestOptions(
jama47ca85c2014-12-03 18:38:07657 args.tool,
jama47ca85c2014-12-03 18:38:07658 args.test_filter,
659 args.run_disabled,
660 args.test_arguments,
661 args.timeout,
662 args.isolate_file_path,
jbudorick5ee45892015-06-10 18:46:22663 suite_name,
664 args.app_data_files,
mlliud7f9fe92015-06-15 19:36:56665 args.app_data_file_dir,
666 args.delete_stale_data)
[email protected]f7148dd42013-08-20 14:24:57667 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37668
669 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57670 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07671 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37672
673 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
674 exit_code = test_exit_code
675
676 report_results.LogFull(
677 results=results,
678 test_type='Unit test',
679 test_package=suite_name,
jama47ca85c2014-12-03 18:38:07680 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37681
jama47ca85c2014-12-03 18:38:07682 if args.json_results_file:
683 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54684
[email protected]6bc1bda22013-07-19 22:08:37685 return exit_code
686
687
jama47ca85c2014-12-03 18:38:07688def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38689 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07690 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38691
692 results, exit_code = test_dispatcher.RunTests(
693 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07694 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38695
696 report_results.LogFull(
697 results=results,
698 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22699 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38700
jama47ca85c2014-12-03 18:38:07701 if args.json_results_file:
702 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54703
[email protected]6b6abac6d2013-10-03 11:56:38704 return exit_code
705
706
jama47ca85c2014-12-03 18:38:07707def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37708 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jama47ca85c2014-12-03 18:38:07709 logging.info('_RunInstrumentationTests(%s, %s)' % (str(args), str(devices)))
[email protected]6bc1bda22013-07-19 22:08:37710
jama47ca85c2014-12-03 18:38:07711 instrumentation_options = ProcessInstrumentationOptions(args)
712
713 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57714 logging.warning('Debugger can not be sharded, using first available device')
715 devices = devices[:1]
716
[email protected]6bc1bda22013-07-19 22:08:37717 results = base_test_result.TestRunResults()
718 exit_code = 0
719
jama47ca85c2014-12-03 18:38:07720 if args.run_java_tests:
mikecase526d68e2014-11-19 20:02:05721 runner_factory, tests = instrumentation_setup.Setup(
722 instrumentation_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37723
724 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57725 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07726 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37727
728 results.AddTestRunResults(test_results)
729
jama47ca85c2014-12-03 18:38:07730 if args.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13731 runner_factory, tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07732 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13733 instrumentation_options)
734
[email protected]34020022013-08-06 23:35:34735 if tests:
736 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57737 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07738 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37739
[email protected]34020022013-08-06 23:35:34740 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37741
[email protected]34020022013-08-06 23:35:34742 # Only allow exit code escalation
743 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
744 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37745
jama47ca85c2014-12-03 18:38:07746 if args.device_flags:
747 args.device_flags = os.path.join(constants.DIR_SOURCE_ROOT,
748 args.device_flags)
[email protected]4f777ca2014-08-08 01:45:59749
[email protected]6bc1bda22013-07-19 22:08:37750 report_results.LogFull(
751 results=results,
752 test_type='Instrumentation',
jama47ca85c2014-12-03 18:38:07753 test_package=os.path.basename(args.test_apk),
754 annotation=args.annotations,
755 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37756
jama47ca85c2014-12-03 18:38:07757 if args.json_results_file:
758 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54759
[email protected]6bc1bda22013-07-19 22:08:37760 return exit_code
761
762
jama47ca85c2014-12-03 18:38:07763def _RunUIAutomatorTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37764 """Subcommand of RunTestsCommands which runs uiautomator tests."""
jama47ca85c2014-12-03 18:38:07765 uiautomator_options = ProcessUIAutomatorOptions(args)
[email protected]6bc1bda22013-07-19 22:08:37766
[email protected]37ee0c792013-08-06 19:10:13767 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37768
[email protected]37ee0c792013-08-06 19:10:13769 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57770 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07771 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37772
773 report_results.LogFull(
774 results=results,
775 test_type='UIAutomator',
jama47ca85c2014-12-03 18:38:07776 test_package=os.path.basename(args.test_jar),
777 annotation=args.annotations,
778 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37779
jama47ca85c2014-12-03 18:38:07780 if args.json_results_file:
781 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54782
[email protected]6bc1bda22013-07-19 22:08:37783 return exit_code
784
785
jama47ca85c2014-12-03 18:38:07786def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07787 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07788 runner_factory, tests = junit_setup.Setup(args)
mikecasec638a072015-04-01 16:35:35789 results, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
790
791 report_results.LogFull(
792 results=results,
793 test_type='JUnit',
794 test_package=args.test_suite)
795
mikecase572401b2015-04-09 02:28:57796 if args.json_results_file:
797 json_results.GenerateJsonResultsFile(results, args.json_results_file)
798
jbudorick9a6b7b332014-09-20 00:01:07799 return exit_code
800
801
jama47ca85c2014-12-03 18:38:07802def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14803 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07804 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14805
806 runner_factory, tests = monkey_setup.Setup(monkey_options)
807
808 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46809 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07810 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14811
812 report_results.LogFull(
813 results=results,
814 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28815 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14816
jama47ca85c2014-12-03 18:38:07817 if args.json_results_file:
818 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54819
[email protected]3dbdfa42013-08-08 01:08:14820 return exit_code
821
822
jama47ca85c2014-12-03 18:38:07823def _RunPerfTests(args):
[email protected]ec3170b2013-08-14 14:39:47824 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07825 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56826
827 # Just save a simple json with a list of test names.
828 if perf_options.output_json_list:
829 return perf_test_runner.OutputJsonList(
830 perf_options.steps, perf_options.output_json_list)
831
[email protected]ad32f312013-11-13 04:03:29832 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47833 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42834 return perf_test_runner.PrintTestOutput(
835 perf_options.print_step, perf_options.output_chartjson_data)
[email protected]ec3170b2013-08-14 14:39:47836
[email protected]a72f0752014-06-03 23:52:34837 runner_factory, tests, devices = perf_setup.Setup(perf_options)
[email protected]ec3170b2013-08-14 14:39:47838
[email protected]a72f0752014-06-03 23:52:34839 # shard=False means that each device will get the full list of tests
840 # and then each one will decide their own affinity.
841 # shard=True means each device will pop the next test available from a queue,
842 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57843 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34844 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07845 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47846
847 report_results.LogFull(
848 results=results,
849 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12850 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52851
jama47ca85c2014-12-03 18:38:07852 if args.json_results_file:
853 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54854
[email protected]def4bce2013-11-12 12:59:52855 if perf_options.single_step:
856 return perf_test_runner.PrintTestOutput('single_step')
857
[email protected]11ce8452014-02-17 10:55:03858 perf_test_runner.PrintSummary(tests)
859
[email protected]86184c7b2013-08-15 15:06:57860 # Always return 0 on the sharding stage. Individual tests exit_code
861 # will be returned on the print_step stage.
862 return 0
[email protected]ec3170b2013-08-14 14:39:47863
[email protected]3dbdfa42013-08-08 01:08:14864
jama47ca85c2014-12-03 18:38:07865def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13866 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07867 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13868 suite_path = suite_vars['path']
869 suite_test_modules = suite_vars['test_modules']
870
871 sys.path = [suite_path] + sys.path
872 try:
873 suite = unittest.TestSuite()
874 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
875 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07876 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13877 return 0 if runner.run(suite).wasSuccessful() else 1
878 finally:
879 sys.path = sys.path[1:]
880
881
[email protected]f7148dd42013-08-20 14:24:57882def _GetAttachedDevices(test_device=None):
883 """Get all attached devices.
884
885 Args:
886 test_device: Name of a specific device to use.
887
888 Returns:
889 A list of attached devices.
890 """
jbudorick4551d0dc2015-04-29 16:07:06891 attached_devices = device_utils.DeviceUtils.HealthyDevices()
aberent6a02a6182015-04-29 11:07:55892 if test_device:
jbudorick4551d0dc2015-04-29 16:07:06893 test_device = [d for d in attached_devices if d == test_device]
894 if not test_device:
895 raise device_errors.DeviceUnreachableError(
896 'Did not find device %s among attached device. Attached devices: %s'
897 % (test_device, ', '.join(attached_devices)))
898 return test_device
aberent6a02a6182015-04-29 11:07:55899
jbudorick4551d0dc2015-04-29 16:07:06900 else:
901 if not attached_devices:
902 raise device_errors.NoDevicesError()
903 return sorted(attached_devices)
[email protected]f7148dd42013-08-20 14:24:57904
905
jama47ca85c2014-12-03 18:38:07906def RunTestsCommand(args, parser):
[email protected]fbe29322013-07-09 09:03:26907 """Checks test type and dispatches to the appropriate function.
908
909 Args:
jama47ca85c2014-12-03 18:38:07910 args: argparse.Namespace object.
911 parser: argparse.ArgumentParser object.
[email protected]fbe29322013-07-09 09:03:26912
913 Returns:
914 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10915
916 Raises:
917 Exception: Unknown command name passed in, or an exception from an
918 individual test runner.
[email protected]fbe29322013-07-09 09:03:26919 """
jama47ca85c2014-12-03 18:38:07920 command = args.command
[email protected]fbe29322013-07-09 09:03:26921
jama47ca85c2014-12-03 18:38:07922 ProcessCommonOptions(args)
[email protected]d82f0252013-07-12 23:22:57923
jama47ca85c2014-12-03 18:38:07924 if args.enable_platform_mode:
rnephew5c499782014-12-12 19:08:55925 return RunTestsInPlatformMode(args, parser)
jbudorick66dc3722014-11-06 21:33:51926
927 if command in constants.LOCAL_MACHINE_TESTS:
jbudorick256fd532014-10-24 01:50:13928 devices = []
929 else:
jama47ca85c2014-12-03 18:38:07930 devices = _GetAttachedDevices(args.test_device)
[email protected]f7148dd42013-08-20 14:24:57931
[email protected]c0662e092013-11-12 11:51:25932 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40933 if not ports.ResetTestServerPortAllocation():
934 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25935
[email protected]fbe29322013-07-09 09:03:26936 if command == 'gtest':
jbudorick590a3d72015-06-12 23:53:31937 if args.suite_name[0] in gtest_test_instance.BROWSER_TEST_SUITES:
938 return RunTestsInPlatformMode(args, parser)
jama47ca85c2014-12-03 18:38:07939 return _RunGTests(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38940 elif command == 'linker':
jama47ca85c2014-12-03 18:38:07941 return _RunLinkerTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26942 elif command == 'instrumentation':
jama47ca85c2014-12-03 18:38:07943 return _RunInstrumentationTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26944 elif command == 'uiautomator':
jama47ca85c2014-12-03 18:38:07945 return _RunUIAutomatorTests(args, devices)
jbudorick9a6b7b332014-09-20 00:01:07946 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07947 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:14948 elif command == 'monkey':
jama47ca85c2014-12-03 18:38:07949 return _RunMonkeyTests(args, devices)
[email protected]ec3170b2013-08-14 14:39:47950 elif command == 'perf':
jama47ca85c2014-12-03 18:38:07951 return _RunPerfTests(args)
jbudorick256fd532014-10-24 01:50:13952 elif command == 'python':
jama47ca85c2014-12-03 18:38:07953 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:26954 else:
[email protected]6bc1bda22013-07-19 22:08:37955 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26956
[email protected]fbe29322013-07-09 09:03:26957
jbudorick66dc3722014-11-06 21:33:51958_SUPPORTED_IN_PLATFORM_MODE = [
959 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:06960 'gtest',
961 'instrumentation',
962 'uirobot',
jbudorick66dc3722014-11-06 21:33:51963]
964
965
jama47ca85c2014-12-03 18:38:07966def RunTestsInPlatformMode(args, parser):
jbudorick66dc3722014-11-06 21:33:51967
jama47ca85c2014-12-03 18:38:07968 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
969 parser.error('%s is not yet supported in platform mode' % args.command)
jbudorick66dc3722014-11-06 21:33:51970
jama47ca85c2014-12-03 18:38:07971 with environment_factory.CreateEnvironment(args, parser.error) as env:
972 with test_instance_factory.CreateTestInstance(args, parser.error) as test:
jbudorick66dc3722014-11-06 21:33:51973 with test_run_factory.CreateTestRun(
jama47ca85c2014-12-03 18:38:07974 args, env, test, parser.error) as test_run:
jbudorick66dc3722014-11-06 21:33:51975 results = test_run.RunTests()
976
jbudorick911be58d2015-01-13 02:51:06977 if args.environment == 'remote_device' and args.trigger:
rnephew5c499782014-12-12 19:08:55978 return 0 # Not returning results, only triggering.
979
jbudorick66dc3722014-11-06 21:33:51980 report_results.LogFull(
981 results=results,
982 test_type=test.TestType(),
983 test_package=test_run.TestPackage(),
jbudorickf9543672014-12-08 22:36:33984 annotation=getattr(args, 'annotations', None),
985 flakiness_server=getattr(args, 'flakiness_dashboard_server', None))
jbudorick66dc3722014-11-06 21:33:51986
jama47ca85c2014-12-03 18:38:07987 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:54988 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:07989 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54990
mikecasee74051022015-02-26 23:08:22991 return 0 if results.DidRunPass() else constants.ERROR_EXIT_CODE
jbudorick66dc3722014-11-06 21:33:51992
993
jama47ca85c2014-12-03 18:38:07994CommandConfigTuple = collections.namedtuple(
995 'CommandConfigTuple',
996 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:26997VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:07998 'gtest': CommandConfigTuple(
999 AddGTestOptions,
1000 'googletest-based C++ tests'),
1001 'instrumentation': CommandConfigTuple(
1002 AddInstrumentationTestOptions,
1003 'InstrumentationTestCase-based Java tests'),
1004 'uiautomator': CommandConfigTuple(
1005 AddUIAutomatorTestOptions,
1006 "Tests that run via Android's uiautomator command"),
1007 'junit': CommandConfigTuple(
1008 AddJUnitTestOptions,
1009 'JUnit4-based Java tests'),
1010 'monkey': CommandConfigTuple(
1011 AddMonkeyTestOptions,
1012 "Tests based on Android's monkey"),
1013 'perf': CommandConfigTuple(
1014 AddPerfTestOptions,
1015 'Performance tests'),
1016 'python': CommandConfigTuple(
1017 AddPythonTestOptions,
1018 'Python tests based on unittest.TestCase'),
1019 'linker': CommandConfigTuple(
1020 AddLinkerTestOptions,
1021 'Linker tests'),
rnephew5c499782014-12-12 19:08:551022 'uirobot': CommandConfigTuple(
1023 AddUirobotTestOptions,
1024 'Uirobot test'),
jama47ca85c2014-12-03 18:38:071025}
[email protected]fbe29322013-07-09 09:03:261026
1027
[email protected]7c53a602014-03-24 16:21:441028def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:241029 for thread in threading.enumerate():
1030 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:211031
1032
[email protected]7c53a602014-03-24 16:21:441033def main():
[email protected]83bb8152013-11-19 15:02:211034 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:071035
1036 parser = argparse.ArgumentParser()
1037 command_parsers = parser.add_subparsers(title='test types',
1038 dest='command')
1039
1040 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
1041 key=lambda x: x[0]):
1042 subparser = command_parsers.add_parser(
1043 test_type, usage='%(prog)s [options]', help=config.help_txt)
1044 config.add_options_func(subparser)
1045
1046 args = parser.parse_args()
mikecasee74051022015-02-26 23:08:221047
1048 try:
1049 return RunTestsCommand(args, parser)
1050 except base_error.BaseError as e:
1051 logging.exception('Error occurred.')
1052 if e.is_infra_error:
1053 return constants.INFRA_EXIT_CODE
mswecce6732015-06-06 00:31:331054 return constants.ERROR_EXIT_CODE
mikecasee74051022015-02-26 23:08:221055 except: # pylint: disable=W0702
1056 logging.exception('Unrecognized error occurred.')
1057 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:261058
[email protected]fbe29322013-07-09 09:03:261059
1060if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:441061 sys.exit(main())