blob: b9d2a3f5c67379e6132f0ceef7c0b664b1a5deba [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
[email protected]2a684222013-08-01 16:59:2230from pylib.gtest import setup as gtest_setup
31from pylib.gtest import test_options as gtest_test_options
[email protected]6b6abac6d2013-10-03 11:56:3832from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1333from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3734from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2235from pylib.instrumentation import test_options as instrumentation_test_options
jbudorick9a6b7b332014-09-20 00:01:0736from pylib.junit import setup as junit_setup
37from pylib.junit import test_dispatcher as junit_dispatcher
[email protected]3dbdfa42013-08-08 01:08:1438from pylib.monkey import setup as monkey_setup
39from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4740from pylib.perf import setup as perf_setup
41from pylib.perf import test_options as perf_test_options
42from pylib.perf import test_runner as perf_test_runner
jbudorickb8c42072014-12-01 18:07:5443from pylib.results import json_results
44from pylib.results import report_results
[email protected]6bc1bda22013-07-19 22:08:3745from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2246from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]2eea4872014-07-28 23:06:1747from pylib.utils import apk_helper
mikecasee74051022015-02-26 23:08:2248from pylib.utils import base_error
[email protected]71aec4b2013-11-20 00:35:2449from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3750from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2651
52
jama47ca85c2014-12-03 18:38:0753def AddCommonOptions(parser):
54 """Adds all common options to |parser|."""
[email protected]fbe29322013-07-09 09:03:2655
jama47ca85c2014-12-03 18:38:0756 group = parser.add_argument_group('Common Options')
57
[email protected]dfffbcbc2013-09-17 22:06:0158 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
jama47ca85c2014-12-03 18:38:0759
60 debug_or_release_group = group.add_mutually_exclusive_group()
61 debug_or_release_group.add_argument(
62 '--debug', action='store_const', const='Debug', dest='build_type',
63 default=default_build_type,
64 help=('If set, run test suites under out/Debug. '
65 'Default is env var BUILDTYPE or Debug.'))
66 debug_or_release_group.add_argument(
67 '--release', action='store_const', const='Release', dest='build_type',
68 help=('If set, run test suites under out/Release. '
69 'Default is env var BUILDTYPE or Debug.'))
70
71 group.add_argument('--build-directory', dest='build_directory',
72 help=('Path to the directory in which build files are'
73 ' located (should not include build type)'))
74 group.add_argument('--output-directory', dest='output_directory',
75 help=('Path to the directory in which build files are'
76 ' located (must include build type). This will take'
77 ' precedence over --debug, --release and'
78 ' --build-directory'))
79 group.add_argument('--num_retries', dest='num_retries', type=int, default=2,
80 help=('Number of retries for a test before '
81 'giving up (default: %(default)s).'))
82 group.add_argument('-v',
83 '--verbose',
84 dest='verbose_count',
85 default=0,
86 action='count',
87 help='Verbose level (multiple times for more)')
88 group.add_argument('--flakiness-dashboard-server',
89 dest='flakiness_dashboard_server',
90 help=('Address of the server that is hosting the '
91 'Chrome for Android flakiness dashboard.'))
92 group.add_argument('--enable-platform-mode', action='store_true',
93 help=('Run the test scripts in platform mode, which '
94 'conceptually separates the test runner from the '
95 '"device" (local or remote, real or emulated) on '
96 'which the tests are running. [experimental]'))
97 group.add_argument('-e', '--environment', default='local',
98 choices=constants.VALID_ENVIRONMENTS,
99 help='Test environment to run in (default: %(default)s).')
100 group.add_argument('--adb-path',
101 help=('Specify the absolute path of the adb binary that '
102 'should be used.'))
103 group.add_argument('--json-results-file', dest='json_results_file',
104 help='If set, will dump results in JSON form '
105 'to specified file.')
[email protected]fbe29322013-07-09 09:03:26106
jama47ca85c2014-12-03 18:38:07107def ProcessCommonOptions(args):
[email protected]fbe29322013-07-09 09:03:26108 """Processes and handles all common options."""
jama47ca85c2014-12-03 18:38:07109 run_tests_helper.SetLogLevel(args.verbose_count)
110 constants.SetBuildType(args.build_type)
111 if args.build_directory:
112 constants.SetBuildDirectory(args.build_directory)
113 if args.output_directory:
mikecase0aea9c52015-04-30 00:12:33114 constants.SetOutputDirectory(args.output_directory)
jama47ca85c2014-12-03 18:38:07115 if args.adb_path:
116 constants.SetAdbPath(args.adb_path)
mikecase48e16bf2014-11-19 22:46:45117 # Some things such as Forwarder require ADB to be in the environment path.
118 adb_dir = os.path.dirname(constants.GetAdbPath())
119 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep):
120 os.environ['PATH'] = adb_dir + os.pathsep + os.environ['PATH']
[email protected]fbe29322013-07-09 09:03:26121
122
rnephew5c499782014-12-12 19:08:55123def AddRemoteDeviceOptions(parser):
124 group = parser.add_argument_group('Remote Device Options')
125
rnephewefe44b42015-02-04 04:45:15126 group.add_argument('--trigger',
jbudoricke6c560152015-01-13 23:49:28127 help=('Only triggers the test if set. Stores test_run_id '
128 'in given file path. '))
rnephewefe44b42015-02-04 04:45:15129 group.add_argument('--collect',
jbudoricke6c560152015-01-13 23:49:28130 help=('Only collects the test results if set. '
131 'Gets test_run_id from given file path.'))
rnephewefe44b42015-02-04 04:45:15132 group.add_argument('--remote-device', action='append',
jbudoricke6c560152015-01-13 23:49:28133 help='Device type to run test on.')
rnephewefe44b42015-02-04 04:45:15134 group.add_argument('--results-path',
jbudoricke6c560152015-01-13 23:49:28135 help='File path to download results to.')
rnephew7f1e2052014-12-12 23:00:11136 group.add_argument('--api-protocol',
jbudoricke6c560152015-01-13 23:49:28137 help='HTTP protocol to use. (http or https)')
rnephewefe44b42015-02-04 04:45:15138 group.add_argument('--api-address',
139 help='Address to send HTTP requests.')
140 group.add_argument('--api-port',
141 help='Port to send HTTP requests to.')
142 group.add_argument('--runner-type',
jbudoricke6c560152015-01-13 23:49:28143 help='Type of test to run as.')
rnephewefe44b42015-02-04 04:45:15144 group.add_argument('--runner-package',
145 help='Package name of test.')
146 group.add_argument('--device-type',
rnephewa46fc562015-01-23 16:00:14147 choices=constants.VALID_DEVICE_TYPES,
148 help=('Type of device to run on. iOS or android'))
rnephewefe44b42015-02-04 04:45:15149 group.add_argument('--device-oem', action='append',
150 help='Device OEM to run on.')
151 group.add_argument('--remote-device-file',
152 help=('File with JSON to select remote device. '
153 'Overrides all other flags.'))
rnephewc9ae8f52015-02-13 03:02:55154 group.add_argument('--remote-device-timeout', type=int,
155 help='Times to retry finding remote device')
mikecase520cbbb52015-04-21 18:51:18156 group.add_argument('--network-config', type=int,
157 help='Integer that specifies the network environment '
158 'that the tests will be run in.')
rnephewefe44b42015-02-04 04:45:15159
160 device_os_group = group.add_mutually_exclusive_group()
161 device_os_group.add_argument('--remote-device-minimum-os',
162 help='Minimum OS on device.')
163 device_os_group.add_argument('--remote-device-os', action='append',
164 help='OS to have on the device.')
rnephew5c499782014-12-12 19:08:55165
166 api_secret_group = group.add_mutually_exclusive_group()
167 api_secret_group.add_argument('--api-secret', default='',
jbudoricke6c560152015-01-13 23:49:28168 help='API secret for remote devices.')
rnephew5c499782014-12-12 19:08:55169 api_secret_group.add_argument('--api-secret-file', default='',
jbudoricke6c560152015-01-13 23:49:28170 help='Path to file that contains API secret.')
rnephew5c499782014-12-12 19:08:55171
172 api_key_group = group.add_mutually_exclusive_group()
173 api_key_group.add_argument('--api-key', default='',
jbudoricke6c560152015-01-13 23:49:28174 help='API key for remote devices.')
rnephew5c499782014-12-12 19:08:55175 api_key_group.add_argument('--api-key-file', default='',
jbudoricke6c560152015-01-13 23:49:28176 help='Path to file that contains API key.')
rnephew5c499782014-12-12 19:08:55177
178
jama47ca85c2014-12-03 18:38:07179def AddDeviceOptions(parser):
180 """Adds device options to |parser|."""
181 group = parser.add_argument_group(title='Device Options')
jama47ca85c2014-12-03 18:38:07182 group.add_argument('--tool',
183 dest='tool',
184 help=('Run the test under a tool '
185 '(use --tool help to list them)'))
186 group.add_argument('-d', '--device', dest='test_device',
187 help=('Target device for the test suite '
188 'to run on.'))
jbudorick256fd532014-10-24 01:50:13189
190
jama47ca85c2014-12-03 18:38:07191def AddGTestOptions(parser):
192 """Adds gtest options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26193
jama47ca85c2014-12-03 18:38:07194 gtest_suites = list(gtest_config.STABLE_TEST_SUITES
195 + gtest_config.EXPERIMENTAL_TEST_SUITES)
[email protected]fbe29322013-07-09 09:03:26196
jama47ca85c2014-12-03 18:38:07197 group = parser.add_argument_group('GTest Options')
jbudorick15cdcd52014-12-03 19:58:49198 group.add_argument('-s', '--suite', dest='suite_name',
jama47ca85c2014-12-03 18:38:07199 nargs='+', metavar='SUITE_NAME', required=True,
jbudorick15cdcd52014-12-03 19:58:49200 help=('Executable name of the test suite to run. '
201 'Available suites include (but are not limited to): '
202 '%s' % ', '.join('"%s"' % s for s in gtest_suites)))
jama47ca85c2014-12-03 18:38:07203 group.add_argument('--gtest_also_run_disabled_tests',
204 '--gtest-also-run-disabled-tests',
205 dest='run_disabled', action='store_true',
206 help='Also run disabled tests if applicable.')
207 group.add_argument('-a', '--test-arguments', dest='test_arguments',
208 default='',
209 help='Additional arguments to pass to the test.')
210 group.add_argument('-t', dest='timeout', type=int, default=60,
211 help='Timeout to wait for each test '
212 '(default: %(default)s).')
213 group.add_argument('--isolate_file_path',
214 '--isolate-file-path',
215 dest='isolate_file_path',
216 help='.isolate file path to override the default '
217 'path')
jbudorick442a6932015-02-03 03:01:15218
219 filter_group = group.add_mutually_exclusive_group()
220 filter_group.add_argument('-f', '--gtest_filter', '--gtest-filter',
221 dest='test_filter',
222 help='googletest-style filter string.')
223 filter_group.add_argument('--gtest-filter-file', dest='test_filter_file',
224 help='Path to file that contains googletest-style '
225 'filter strings. (Lines will be joined with '
226 '":" to create a single filter string.)')
227
jama47ca85c2014-12-03 18:38:07228 AddDeviceOptions(parser)
229 AddCommonOptions(parser)
rnephew5c499782014-12-12 19:08:55230 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26231
232
jama47ca85c2014-12-03 18:38:07233def AddLinkerTestOptions(parser):
234 group = parser.add_argument_group('Linker Test Options')
235 group.add_argument('-f', '--gtest-filter', dest='test_filter',
236 help='googletest-style filter string.')
237 AddCommonOptions(parser)
238 AddDeviceOptions(parser)
[email protected]6b6abac6d2013-10-03 11:56:38239
240
jama47ca85c2014-12-03 18:38:07241def AddJavaTestOptions(argument_group):
[email protected]fbe29322013-07-09 09:03:26242 """Adds the Java test options to |option_parser|."""
243
jama47ca85c2014-12-03 18:38:07244 argument_group.add_argument(
245 '-f', '--test-filter', dest='test_filter',
246 help=('Test filter (if not fully qualified, will run all matches).'))
247 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26248 '-A', '--annotation', dest='annotation_str',
249 help=('Comma-separated list of annotations. Run only tests with any of '
250 'the given annotations. An annotation can be either a key or a '
251 'key-values pair. A test that has no annotation is considered '
252 '"SmallTest".'))
jama47ca85c2014-12-03 18:38:07253 argument_group.add_argument(
[email protected]fbe29322013-07-09 09:03:26254 '-E', '--exclude-annotation', dest='exclude_annotation_str',
255 help=('Comma-separated list of annotations. Exclude tests with these '
256 'annotations.'))
jama47ca85c2014-12-03 18:38:07257 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59258 '--screenshot', dest='screenshot_failures', action='store_true',
259 help='Capture screenshots of test failures')
jama47ca85c2014-12-03 18:38:07260 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59261 '--save-perf-json', action='store_true',
262 help='Saves the JSON file for each UI Perf test.')
jama47ca85c2014-12-03 18:38:07263 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59264 '--official-build', action='store_true', help='Run official build tests.')
jama47ca85c2014-12-03 18:38:07265 argument_group.add_argument(
jbudorickcbcc115d2014-09-18 17:50:59266 '--test_data', '--test-data', action='append', default=[],
267 help=('Each instance defines a directory of test data that should be '
268 'copied to the target(s) before running the tests. The argument '
269 'should be of the form <target>:<source>, <target> is relative to '
270 'the device data directory, and <source> is relative to the '
271 'chromium build directory.'))
davileen98efad12015-01-05 19:48:21272 argument_group.add_argument(
273 '--disable-dalvik-asserts', dest='set_asserts', action='store_false',
274 default=True, help='Removes the dalvik.vm.enableassertions property')
275
[email protected]fbe29322013-07-09 09:03:26276
277
jama47ca85c2014-12-03 18:38:07278def ProcessJavaTestOptions(args):
[email protected]fbe29322013-07-09 09:03:26279 """Processes options/arguments and populates |options| with defaults."""
280
jama47ca85c2014-12-03 18:38:07281 # TODO(jbudorick): Handle most of this function in argparse.
282 if args.annotation_str:
283 args.annotations = args.annotation_str.split(',')
284 elif args.test_filter:
285 args.annotations = []
[email protected]fbe29322013-07-09 09:03:26286 else:
jama47ca85c2014-12-03 18:38:07287 args.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
288 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26289
jama47ca85c2014-12-03 18:38:07290 if args.exclude_annotation_str:
291 args.exclude_annotations = args.exclude_annotation_str.split(',')
[email protected]fbe29322013-07-09 09:03:26292 else:
jama47ca85c2014-12-03 18:38:07293 args.exclude_annotations = []
[email protected]fbe29322013-07-09 09:03:26294
[email protected]fbe29322013-07-09 09:03:26295
jama47ca85c2014-12-03 18:38:07296def AddInstrumentationTestOptions(parser):
297 """Adds Instrumentation test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26298
jama47ca85c2014-12-03 18:38:07299 parser.usage = '%(prog)s [options]'
[email protected]fbe29322013-07-09 09:03:26300
jama47ca85c2014-12-03 18:38:07301 group = parser.add_argument_group('Instrumentation Test Options')
302 AddJavaTestOptions(group)
[email protected]fbe29322013-07-09 09:03:26303
jama47ca85c2014-12-03 18:38:07304 java_or_python_group = group.add_mutually_exclusive_group()
305 java_or_python_group.add_argument(
306 '-j', '--java-only', action='store_false',
307 dest='run_python_tests', default=True, help='Run only the Java tests.')
308 java_or_python_group.add_argument(
309 '-p', '--python-only', action='store_false',
310 dest='run_java_tests', default=True,
311 help='Run only the host-driven tests.')
312
313 group.add_argument('--host-driven-root',
314 help='Root of the host-driven tests.')
315 group.add_argument('-w', '--wait_debugger', dest='wait_for_debugger',
316 action='store_true',
317 help='Wait for debugger.')
jbudorick911be58d2015-01-13 02:51:06318 group.add_argument('--apk-under-test', dest='apk_under_test',
319 help=('the name of the apk under test.'))
jama47ca85c2014-12-03 18:38:07320 group.add_argument('--test-apk', dest='test_apk', required=True,
321 help=('The name of the apk containing the tests '
322 '(without the .apk extension; '
323 'e.g. "ContentShellTest").'))
324 group.add_argument('--coverage-dir',
325 help=('Directory in which to place all generated '
326 'EMMA coverage files.'))
327 group.add_argument('--device-flags', dest='device_flags', default='',
328 help='The relative filepath to a file containing '
329 'command-line flags to set on the device')
jbudorick911be58d2015-01-13 02:51:06330 group.add_argument('--device-flags-file', default='',
331 help='The relative filepath to a file containing '
332 'command-line flags to set on the device')
jama47ca85c2014-12-03 18:38:07333 group.add_argument('--isolate_file_path',
334 '--isolate-file-path',
335 dest='isolate_file_path',
336 help='.isolate file path to override the default '
337 'path')
338
339 AddCommonOptions(parser)
340 AddDeviceOptions(parser)
rnephewe416dff2015-01-21 21:26:37341 AddRemoteDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26342
343
jama47ca85c2014-12-03 18:38:07344def ProcessInstrumentationOptions(args):
[email protected]2a684222013-08-01 16:59:22345 """Processes options/arguments and populate |options| with defaults.
346
347 Args:
jama47ca85c2014-12-03 18:38:07348 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22349
350 Returns:
351 An InstrumentationOptions named tuple which contains all options relevant to
352 instrumentation tests.
353 """
[email protected]fbe29322013-07-09 09:03:26354
jama47ca85c2014-12-03 18:38:07355 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26356
jama47ca85c2014-12-03 18:38:07357 if not args.host_driven_root:
358 args.run_python_tests = False
[email protected]37ee0c792013-08-06 19:10:13359
jama47ca85c2014-12-03 18:38:07360 args.test_apk_path = os.path.join(
[email protected]2eea4872014-07-28 23:06:17361 constants.GetOutDirectory(),
362 constants.SDK_BUILD_APKS_DIR,
jama47ca85c2014-12-03 18:38:07363 '%s.apk' % args.test_apk)
364 args.test_apk_jar_path = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15365 constants.GetOutDirectory(),
366 constants.SDK_BUILD_TEST_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07367 '%s.jar' % args.test_apk)
368 args.test_support_apk_path = '%sSupport%s' % (
369 os.path.splitext(args.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46370
jama47ca85c2014-12-03 18:38:07371 args.test_runner = apk_helper.GetInstrumentationName(args.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46372
jama47ca85c2014-12-03 18:38:07373 # TODO(jbudorick): Get rid of InstrumentationOptions.
[email protected]2a684222013-08-01 16:59:22374 return instrumentation_test_options.InstrumentationOptions(
jama47ca85c2014-12-03 18:38:07375 args.tool,
jama47ca85c2014-12-03 18:38:07376 args.annotations,
377 args.exclude_annotations,
378 args.test_filter,
379 args.test_data,
380 args.save_perf_json,
381 args.screenshot_failures,
382 args.wait_for_debugger,
383 args.coverage_dir,
384 args.test_apk,
385 args.test_apk_path,
386 args.test_apk_jar_path,
387 args.test_runner,
388 args.test_support_apk_path,
389 args.device_flags,
davileen98efad12015-01-05 19:48:21390 args.isolate_file_path,
391 args.set_asserts
[email protected]5e2f3f62014-06-23 12:31:46392 )
[email protected]2a684222013-08-01 16:59:22393
[email protected]fbe29322013-07-09 09:03:26394
jama47ca85c2014-12-03 18:38:07395def AddUIAutomatorTestOptions(parser):
396 """Adds UI Automator test options to |parser|."""
[email protected]fbe29322013-07-09 09:03:26397
jama47ca85c2014-12-03 18:38:07398 group = parser.add_argument_group('UIAutomator Test Options')
399 AddJavaTestOptions(group)
400 group.add_argument(
401 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
402 metavar='PACKAGE', help='Package under test.')
403 group.add_argument(
404 '--test-jar', dest='test_jar', required=True,
[email protected]fbe29322013-07-09 09:03:26405 help=('The name of the dexed jar containing the tests (without the '
406 '.dex.jar extension). Alternatively, this can be a full path '
407 'to the jar.'))
408
jama47ca85c2014-12-03 18:38:07409 AddCommonOptions(parser)
410 AddDeviceOptions(parser)
[email protected]fbe29322013-07-09 09:03:26411
412
jama47ca85c2014-12-03 18:38:07413def ProcessUIAutomatorOptions(args):
[email protected]2a684222013-08-01 16:59:22414 """Processes UIAutomator options/arguments.
415
416 Args:
jama47ca85c2014-12-03 18:38:07417 args: argparse.Namespace object.
[email protected]2a684222013-08-01 16:59:22418
419 Returns:
420 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14421 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22422 """
[email protected]fbe29322013-07-09 09:03:26423
jama47ca85c2014-12-03 18:38:07424 ProcessJavaTestOptions(args)
[email protected]fbe29322013-07-09 09:03:26425
jama47ca85c2014-12-03 18:38:07426 if os.path.exists(args.test_jar):
[email protected]fbe29322013-07-09 09:03:26427 # The dexed JAR is fully qualified, assume the info JAR lives along side.
jama47ca85c2014-12-03 18:38:07428 args.uiautomator_jar = args.test_jar
[email protected]fbe29322013-07-09 09:03:26429 else:
jama47ca85c2014-12-03 18:38:07430 args.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15431 constants.GetOutDirectory(),
432 constants.SDK_BUILD_JAVALIB_DIR,
jama47ca85c2014-12-03 18:38:07433 '%s.dex.jar' % args.test_jar)
434 args.uiautomator_info_jar = (
435 args.uiautomator_jar[:args.uiautomator_jar.find('.dex.jar')] +
[email protected]fbe29322013-07-09 09:03:26436 '_java.jar')
437
[email protected]2a684222013-08-01 16:59:22438 return uiautomator_test_options.UIAutomatorOptions(
jama47ca85c2014-12-03 18:38:07439 args.tool,
jama47ca85c2014-12-03 18:38:07440 args.annotations,
441 args.exclude_annotations,
442 args.test_filter,
443 args.test_data,
444 args.save_perf_json,
445 args.screenshot_failures,
446 args.uiautomator_jar,
447 args.uiautomator_info_jar,
davileen98efad12015-01-05 19:48:21448 args.package,
449 args.set_asserts)
[email protected]2a684222013-08-01 16:59:22450
[email protected]fbe29322013-07-09 09:03:26451
jama47ca85c2014-12-03 18:38:07452def AddJUnitTestOptions(parser):
453 """Adds junit test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07454
jama47ca85c2014-12-03 18:38:07455 group = parser.add_argument_group('JUnit Test Options')
456 group.add_argument(
457 '-s', '--test-suite', dest='test_suite', required=True,
jbudorick9a6b7b332014-09-20 00:01:07458 help=('JUnit test suite to run.'))
jama47ca85c2014-12-03 18:38:07459 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07460 '-f', '--test-filter', dest='test_filter',
461 help='Filters tests googletest-style.')
jama47ca85c2014-12-03 18:38:07462 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07463 '--package-filter', dest='package_filter',
464 help='Filters tests by package.')
jama47ca85c2014-12-03 18:38:07465 group.add_argument(
jbudorick9a6b7b332014-09-20 00:01:07466 '--runner-filter', dest='runner_filter',
467 help='Filters tests by runner class. Must be fully qualified.')
jama47ca85c2014-12-03 18:38:07468 group.add_argument(
469 '--sdk-version', dest='sdk_version', type=int,
jbudorick9a6b7b332014-09-20 00:01:07470 help='The Android SDK version.')
jama47ca85c2014-12-03 18:38:07471 AddCommonOptions(parser)
jbudorick9a6b7b332014-09-20 00:01:07472
473
jama47ca85c2014-12-03 18:38:07474def AddMonkeyTestOptions(parser):
475 """Adds monkey test options to |parser|."""
jbudorick9a6b7b332014-09-20 00:01:07476
jama47ca85c2014-12-03 18:38:07477 group = parser.add_argument_group('Monkey Test Options')
478 group.add_argument(
479 '--package', required=True, choices=constants.PACKAGE_INFO.keys(),
480 metavar='PACKAGE', help='Package under test.')
481 group.add_argument(
482 '--event-count', default=10000, type=int,
483 help='Number of events to generate (default: %(default)s).')
484 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14485 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12486 help='A list of allowed categories.')
jama47ca85c2014-12-03 18:38:07487 group.add_argument(
488 '--throttle', default=100, type=int,
489 help='Delay between events (ms) (default: %(default)s). ')
490 group.add_argument(
491 '--seed', type=int,
[email protected]3dbdfa42013-08-08 01:08:14492 help=('Seed value for pseudo-random generator. Same seed value generates '
493 'the same sequence of events. Seed is randomized by default.'))
jama47ca85c2014-12-03 18:38:07494 group.add_argument(
[email protected]3dbdfa42013-08-08 01:08:14495 '--extra-args', default='',
jama47ca85c2014-12-03 18:38:07496 help=('String of other args to pass to the command verbatim.'))
[email protected]3dbdfa42013-08-08 01:08:14497
jama47ca85c2014-12-03 18:38:07498 AddCommonOptions(parser)
499 AddDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14500
jama47ca85c2014-12-03 18:38:07501def ProcessMonkeyTestOptions(args):
[email protected]3dbdfa42013-08-08 01:08:14502 """Processes all monkey test options.
503
504 Args:
jama47ca85c2014-12-03 18:38:07505 args: argparse.Namespace object.
[email protected]3dbdfa42013-08-08 01:08:14506
507 Returns:
508 A MonkeyOptions named tuple which contains all options relevant to
509 monkey tests.
510 """
jama47ca85c2014-12-03 18:38:07511 # TODO(jbudorick): Handle this directly in argparse with nargs='+'
512 category = args.category
[email protected]3dbdfa42013-08-08 01:08:14513 if category:
jama47ca85c2014-12-03 18:38:07514 category = args.category.split(',')
[email protected]3dbdfa42013-08-08 01:08:14515
jama47ca85c2014-12-03 18:38:07516 # TODO(jbudorick): Get rid of MonkeyOptions.
[email protected]3dbdfa42013-08-08 01:08:14517 return monkey_test_options.MonkeyOptions(
jama47ca85c2014-12-03 18:38:07518 args.verbose_count,
519 args.package,
520 args.event_count,
[email protected]3dbdfa42013-08-08 01:08:14521 category,
jama47ca85c2014-12-03 18:38:07522 args.throttle,
523 args.seed,
524 args.extra_args)
[email protected]3dbdfa42013-08-08 01:08:14525
rnephew5c499782014-12-12 19:08:55526def AddUirobotTestOptions(parser):
527 """Adds uirobot test options to |option_parser|."""
528 group = parser.add_argument_group('Uirobot Test Options')
529
rnephewefe44b42015-02-04 04:45:15530 group.add_argument('--app-under-test', required=True,
531 help='APK to run tests on.')
rnephew5c499782014-12-12 19:08:55532 group.add_argument(
533 '--minutes', default=5, type=int,
jbudorick676b1202015-02-06 22:02:27534 help='Number of minutes to run uirobot test [default: %(default)s].')
rnephew5c499782014-12-12 19:08:55535
536 AddCommonOptions(parser)
537 AddDeviceOptions(parser)
538 AddRemoteDeviceOptions(parser)
[email protected]3dbdfa42013-08-08 01:08:14539
jama47ca85c2014-12-03 18:38:07540def AddPerfTestOptions(parser):
541 """Adds perf test options to |parser|."""
[email protected]ec3170b2013-08-14 14:39:47542
jama47ca85c2014-12-03 18:38:07543 group = parser.add_argument_group('Perf Test Options')
[email protected]ec3170b2013-08-14 14:39:47544
jama47ca85c2014-12-03 18:38:07545 class SingleStepAction(argparse.Action):
546 def __call__(self, parser, namespace, values, option_string=None):
547 if values and not namespace.single_step:
548 parser.error('single step command provided, '
549 'but --single-step not specified.')
550 elif namespace.single_step and not values:
551 parser.error('--single-step specified, '
552 'but no single step command provided.')
553 setattr(namespace, self.dest, values)
554
555 step_group = group.add_mutually_exclusive_group(required=True)
556 # TODO(jbudorick): Revise --single-step to use argparse.REMAINDER.
557 # This requires removing "--" from client calls.
558 step_group.add_argument(
559 '--single-step', action='store_true',
[email protected]def4bce2013-11-12 12:59:52560 help='Execute the given command with retries, but only print the result '
561 'for the "most successful" round.')
jama47ca85c2014-12-03 18:38:07562 step_group.add_argument(
[email protected]181a5c92013-09-06 17:11:46563 '--steps',
[email protected]def4bce2013-11-12 12:59:52564 help='JSON file containing the list of commands to run.')
jama47ca85c2014-12-03 18:38:07565 step_group.add_argument(
566 '--print-step',
567 help='The name of a previously executed perf step to print.')
568
569 group.add_argument(
peterbd4e73d2014-12-03 15:47:36570 '--output-json-list',
571 help='Write a simple list of names from --steps into the given file.')
jama47ca85c2014-12-03 18:38:07572 group.add_argument(
peterbd4e73d2014-12-03 15:47:36573 '--collect-chartjson-data',
574 action='store_true',
575 help='Cache the chartjson output from each step for later use.')
jama47ca85c2014-12-03 18:38:07576 group.add_argument(
peterbd4e73d2014-12-03 15:47:36577 '--output-chartjson-data',
578 default='',
579 help='Write out chartjson into the given file.')
jama47ca85c2014-12-03 18:38:07580 group.add_argument(
581 '--flaky-steps',
582 help=('A JSON file containing steps that are flaky '
583 'and will have its exit code ignored.'))
584 group.add_argument(
[email protected]181a5c92013-09-06 17:11:46585 '--no-timeout', action='store_true',
586 help=('Do not impose a timeout. Each perf step is responsible for '
587 'implementing the timeout logic.'))
jama47ca85c2014-12-03 18:38:07588 group.add_argument(
[email protected]650487c2013-09-30 11:40:49589 '-f', '--test-filter',
590 help=('Test filter (will match against the names listed in --steps).'))
jama47ca85c2014-12-03 18:38:07591 group.add_argument(
592 '--dry-run', action='store_true',
[email protected]650487c2013-09-30 11:40:49593 help='Just print the steps without executing.')
jama47ca85c2014-12-03 18:38:07594 group.add_argument('single_step_command', nargs='*', action=SingleStepAction,
595 help='If --single-step is specified, the command to run.')
596 AddCommonOptions(parser)
597 AddDeviceOptions(parser)
[email protected]ec3170b2013-08-14 14:39:47598
599
jama47ca85c2014-12-03 18:38:07600def ProcessPerfTestOptions(args):
[email protected]ec3170b2013-08-14 14:39:47601 """Processes all perf test options.
602
603 Args:
jama47ca85c2014-12-03 18:38:07604 args: argparse.Namespace object.
[email protected]ec3170b2013-08-14 14:39:47605
606 Returns:
607 A PerfOptions named tuple which contains all options relevant to
608 perf tests.
609 """
jama47ca85c2014-12-03 18:38:07610 # TODO(jbudorick): Move single_step handling down into the perf tests.
611 if args.single_step:
612 args.single_step = ' '.join(args.single_step_command)
613 # TODO(jbudorick): Get rid of PerfOptions.
[email protected]ec3170b2013-08-14 14:39:47614 return perf_test_options.PerfOptions(
jama47ca85c2014-12-03 18:38:07615 args.steps, args.flaky_steps, args.output_json_list,
616 args.print_step, args.no_timeout, args.test_filter,
617 args.dry_run, args.single_step, args.collect_chartjson_data,
618 args.output_chartjson_data)
[email protected]ec3170b2013-08-14 14:39:47619
620
jama47ca85c2014-12-03 18:38:07621def AddPythonTestOptions(parser):
622 group = parser.add_argument_group('Python Test Options')
623 group.add_argument(
624 '-s', '--suite', dest='suite_name', metavar='SUITE_NAME',
625 choices=constants.PYTHON_UNIT_TEST_SUITES.keys(),
626 help='Name of the test suite to run.')
627 AddCommonOptions(parser)
jbudorick256fd532014-10-24 01:50:13628
629
jama47ca85c2014-12-03 18:38:07630def _RunGTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37631 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]6bc1bda22013-07-19 22:08:37632 exit_code = 0
jama47ca85c2014-12-03 18:38:07633 for suite_name in args.suite_name:
634 # TODO(jbudorick): Either deprecate multi-suite or move its handling down
635 # into the gtest code.
[email protected]2a684222013-08-01 16:59:22636 gtest_options = gtest_test_options.GTestOptions(
jama47ca85c2014-12-03 18:38:07637 args.tool,
jama47ca85c2014-12-03 18:38:07638 args.test_filter,
639 args.run_disabled,
640 args.test_arguments,
641 args.timeout,
642 args.isolate_file_path,
[email protected]2a684222013-08-01 16:59:22643 suite_name)
[email protected]f7148dd42013-08-20 14:24:57644 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37645
646 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57647 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07648 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37649
650 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
651 exit_code = test_exit_code
652
653 report_results.LogFull(
654 results=results,
655 test_type='Unit test',
656 test_package=suite_name,
jama47ca85c2014-12-03 18:38:07657 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37658
jama47ca85c2014-12-03 18:38:07659 if args.json_results_file:
660 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54661
[email protected]6bc1bda22013-07-19 22:08:37662 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
663 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
664
665 return exit_code
666
667
jama47ca85c2014-12-03 18:38:07668def _RunLinkerTests(args, devices):
[email protected]6b6abac6d2013-10-03 11:56:38669 """Subcommand of RunTestsCommands which runs linker tests."""
jama47ca85c2014-12-03 18:38:07670 runner_factory, tests = linker_setup.Setup(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38671
672 results, exit_code = test_dispatcher.RunTests(
673 tests, runner_factory, devices, shard=True, test_timeout=60,
jama47ca85c2014-12-03 18:38:07674 num_retries=args.num_retries)
[email protected]6b6abac6d2013-10-03 11:56:38675
676 report_results.LogFull(
677 results=results,
678 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22679 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38680
jama47ca85c2014-12-03 18:38:07681 if args.json_results_file:
682 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54683
[email protected]6b6abac6d2013-10-03 11:56:38684 return exit_code
685
686
jama47ca85c2014-12-03 18:38:07687def _RunInstrumentationTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37688 """Subcommand of RunTestsCommands which runs instrumentation tests."""
jama47ca85c2014-12-03 18:38:07689 logging.info('_RunInstrumentationTests(%s, %s)' % (str(args), str(devices)))
[email protected]6bc1bda22013-07-19 22:08:37690
jama47ca85c2014-12-03 18:38:07691 instrumentation_options = ProcessInstrumentationOptions(args)
692
693 if len(devices) > 1 and args.wait_for_debugger:
[email protected]f7148dd42013-08-20 14:24:57694 logging.warning('Debugger can not be sharded, using first available device')
695 devices = devices[:1]
696
[email protected]6bc1bda22013-07-19 22:08:37697 results = base_test_result.TestRunResults()
698 exit_code = 0
699
jama47ca85c2014-12-03 18:38:07700 if args.run_java_tests:
mikecase526d68e2014-11-19 20:02:05701 runner_factory, tests = instrumentation_setup.Setup(
702 instrumentation_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37703
704 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57705 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07706 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37707
708 results.AddTestRunResults(test_results)
709
jama47ca85c2014-12-03 18:38:07710 if args.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13711 runner_factory, tests = host_driven_setup.InstrumentationSetup(
jama47ca85c2014-12-03 18:38:07712 args.host_driven_root, args.official_build,
[email protected]37ee0c792013-08-06 19:10:13713 instrumentation_options)
714
[email protected]34020022013-08-06 23:35:34715 if tests:
716 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57717 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07718 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37719
[email protected]34020022013-08-06 23:35:34720 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37721
[email protected]34020022013-08-06 23:35:34722 # Only allow exit code escalation
723 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
724 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37725
jama47ca85c2014-12-03 18:38:07726 if args.device_flags:
727 args.device_flags = os.path.join(constants.DIR_SOURCE_ROOT,
728 args.device_flags)
[email protected]4f777ca2014-08-08 01:45:59729
[email protected]6bc1bda22013-07-19 22:08:37730 report_results.LogFull(
731 results=results,
732 test_type='Instrumentation',
jama47ca85c2014-12-03 18:38:07733 test_package=os.path.basename(args.test_apk),
734 annotation=args.annotations,
735 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37736
jama47ca85c2014-12-03 18:38:07737 if args.json_results_file:
738 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54739
[email protected]6bc1bda22013-07-19 22:08:37740 return exit_code
741
742
jama47ca85c2014-12-03 18:38:07743def _RunUIAutomatorTests(args, devices):
[email protected]6bc1bda22013-07-19 22:08:37744 """Subcommand of RunTestsCommands which runs uiautomator tests."""
jama47ca85c2014-12-03 18:38:07745 uiautomator_options = ProcessUIAutomatorOptions(args)
[email protected]6bc1bda22013-07-19 22:08:37746
[email protected]37ee0c792013-08-06 19:10:13747 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37748
[email protected]37ee0c792013-08-06 19:10:13749 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57750 tests, runner_factory, devices, shard=True, test_timeout=None,
jama47ca85c2014-12-03 18:38:07751 num_retries=args.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37752
753 report_results.LogFull(
754 results=results,
755 test_type='UIAutomator',
jama47ca85c2014-12-03 18:38:07756 test_package=os.path.basename(args.test_jar),
757 annotation=args.annotations,
758 flakiness_server=args.flakiness_dashboard_server)
[email protected]6bc1bda22013-07-19 22:08:37759
jama47ca85c2014-12-03 18:38:07760 if args.json_results_file:
761 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54762
[email protected]6bc1bda22013-07-19 22:08:37763 return exit_code
764
765
jama47ca85c2014-12-03 18:38:07766def _RunJUnitTests(args):
jbudorick9a6b7b332014-09-20 00:01:07767 """Subcommand of RunTestsCommand which runs junit tests."""
jama47ca85c2014-12-03 18:38:07768 runner_factory, tests = junit_setup.Setup(args)
mikecasec638a072015-04-01 16:35:35769 results, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
770
771 report_results.LogFull(
772 results=results,
773 test_type='JUnit',
774 test_package=args.test_suite)
775
mikecase572401b2015-04-09 02:28:57776 if args.json_results_file:
777 json_results.GenerateJsonResultsFile(results, args.json_results_file)
778
jbudorick9a6b7b332014-09-20 00:01:07779 return exit_code
780
781
jama47ca85c2014-12-03 18:38:07782def _RunMonkeyTests(args, devices):
[email protected]3dbdfa42013-08-08 01:08:14783 """Subcommand of RunTestsCommands which runs monkey tests."""
jama47ca85c2014-12-03 18:38:07784 monkey_options = ProcessMonkeyTestOptions(args)
[email protected]3dbdfa42013-08-08 01:08:14785
786 runner_factory, tests = monkey_setup.Setup(monkey_options)
787
788 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46789 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07790 num_retries=args.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14791
792 report_results.LogFull(
793 results=results,
794 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28795 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14796
jama47ca85c2014-12-03 18:38:07797 if args.json_results_file:
798 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54799
[email protected]3dbdfa42013-08-08 01:08:14800 return exit_code
801
802
jama47ca85c2014-12-03 18:38:07803def _RunPerfTests(args):
[email protected]ec3170b2013-08-14 14:39:47804 """Subcommand of RunTestsCommands which runs perf tests."""
jama47ca85c2014-12-03 18:38:07805 perf_options = ProcessPerfTestOptions(args)
[email protected]61487ed2014-06-09 12:33:56806
807 # Just save a simple json with a list of test names.
808 if perf_options.output_json_list:
809 return perf_test_runner.OutputJsonList(
810 perf_options.steps, perf_options.output_json_list)
811
[email protected]ad32f312013-11-13 04:03:29812 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47813 if perf_options.print_step:
simonhatch9b9256d2015-01-07 18:03:42814 return perf_test_runner.PrintTestOutput(
815 perf_options.print_step, perf_options.output_chartjson_data)
[email protected]ec3170b2013-08-14 14:39:47816
[email protected]a72f0752014-06-03 23:52:34817 runner_factory, tests, devices = perf_setup.Setup(perf_options)
[email protected]ec3170b2013-08-14 14:39:47818
[email protected]a72f0752014-06-03 23:52:34819 # shard=False means that each device will get the full list of tests
820 # and then each one will decide their own affinity.
821 # shard=True means each device will pop the next test available from a queue,
822 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57823 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34824 tests, runner_factory, devices, shard=False, test_timeout=None,
jama47ca85c2014-12-03 18:38:07825 num_retries=args.num_retries)
[email protected]ec3170b2013-08-14 14:39:47826
827 report_results.LogFull(
828 results=results,
829 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12830 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52831
jama47ca85c2014-12-03 18:38:07832 if args.json_results_file:
833 json_results.GenerateJsonResultsFile(results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54834
[email protected]def4bce2013-11-12 12:59:52835 if perf_options.single_step:
836 return perf_test_runner.PrintTestOutput('single_step')
837
[email protected]11ce8452014-02-17 10:55:03838 perf_test_runner.PrintSummary(tests)
839
[email protected]86184c7b2013-08-15 15:06:57840 # Always return 0 on the sharding stage. Individual tests exit_code
841 # will be returned on the print_step stage.
842 return 0
[email protected]ec3170b2013-08-14 14:39:47843
[email protected]3dbdfa42013-08-08 01:08:14844
jama47ca85c2014-12-03 18:38:07845def _RunPythonTests(args):
jbudorick256fd532014-10-24 01:50:13846 """Subcommand of RunTestsCommand which runs python unit tests."""
jama47ca85c2014-12-03 18:38:07847 suite_vars = constants.PYTHON_UNIT_TEST_SUITES[args.suite_name]
jbudorick256fd532014-10-24 01:50:13848 suite_path = suite_vars['path']
849 suite_test_modules = suite_vars['test_modules']
850
851 sys.path = [suite_path] + sys.path
852 try:
853 suite = unittest.TestSuite()
854 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(m)
855 for m in suite_test_modules)
jama47ca85c2014-12-03 18:38:07856 runner = unittest.TextTestRunner(verbosity=1+args.verbose_count)
jbudorick256fd532014-10-24 01:50:13857 return 0 if runner.run(suite).wasSuccessful() else 1
858 finally:
859 sys.path = sys.path[1:]
860
861
[email protected]f7148dd42013-08-20 14:24:57862def _GetAttachedDevices(test_device=None):
863 """Get all attached devices.
864
865 Args:
866 test_device: Name of a specific device to use.
867
868 Returns:
869 A list of attached devices.
870 """
jbudorick4551d0dc2015-04-29 16:07:06871 attached_devices = device_utils.DeviceUtils.HealthyDevices()
aberent6a02a6182015-04-29 11:07:55872 if test_device:
jbudorick4551d0dc2015-04-29 16:07:06873 test_device = [d for d in attached_devices if d == test_device]
874 if not test_device:
875 raise device_errors.DeviceUnreachableError(
876 'Did not find device %s among attached device. Attached devices: %s'
877 % (test_device, ', '.join(attached_devices)))
878 return test_device
aberent6a02a6182015-04-29 11:07:55879
jbudorick4551d0dc2015-04-29 16:07:06880 else:
881 if not attached_devices:
882 raise device_errors.NoDevicesError()
883 return sorted(attached_devices)
[email protected]f7148dd42013-08-20 14:24:57884
885
jama47ca85c2014-12-03 18:38:07886def RunTestsCommand(args, parser):
[email protected]fbe29322013-07-09 09:03:26887 """Checks test type and dispatches to the appropriate function.
888
889 Args:
jama47ca85c2014-12-03 18:38:07890 args: argparse.Namespace object.
891 parser: argparse.ArgumentParser object.
[email protected]fbe29322013-07-09 09:03:26892
893 Returns:
894 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10895
896 Raises:
897 Exception: Unknown command name passed in, or an exception from an
898 individual test runner.
[email protected]fbe29322013-07-09 09:03:26899 """
jama47ca85c2014-12-03 18:38:07900 command = args.command
[email protected]fbe29322013-07-09 09:03:26901
jama47ca85c2014-12-03 18:38:07902 ProcessCommonOptions(args)
[email protected]d82f0252013-07-12 23:22:57903
jama47ca85c2014-12-03 18:38:07904 if args.enable_platform_mode:
rnephew5c499782014-12-12 19:08:55905 return RunTestsInPlatformMode(args, parser)
jbudorick66dc3722014-11-06 21:33:51906
907 if command in constants.LOCAL_MACHINE_TESTS:
jbudorick256fd532014-10-24 01:50:13908 devices = []
909 else:
jama47ca85c2014-12-03 18:38:07910 devices = _GetAttachedDevices(args.test_device)
[email protected]f7148dd42013-08-20 14:24:57911
[email protected]c0662e092013-11-12 11:51:25912 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40913 if not ports.ResetTestServerPortAllocation():
914 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25915
[email protected]fbe29322013-07-09 09:03:26916 if command == 'gtest':
jama47ca85c2014-12-03 18:38:07917 return _RunGTests(args, devices)
[email protected]6b6abac6d2013-10-03 11:56:38918 elif command == 'linker':
jama47ca85c2014-12-03 18:38:07919 return _RunLinkerTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26920 elif command == 'instrumentation':
jama47ca85c2014-12-03 18:38:07921 return _RunInstrumentationTests(args, devices)
[email protected]fbe29322013-07-09 09:03:26922 elif command == 'uiautomator':
jama47ca85c2014-12-03 18:38:07923 return _RunUIAutomatorTests(args, devices)
jbudorick9a6b7b332014-09-20 00:01:07924 elif command == 'junit':
jama47ca85c2014-12-03 18:38:07925 return _RunJUnitTests(args)
[email protected]3dbdfa42013-08-08 01:08:14926 elif command == 'monkey':
jama47ca85c2014-12-03 18:38:07927 return _RunMonkeyTests(args, devices)
[email protected]ec3170b2013-08-14 14:39:47928 elif command == 'perf':
jama47ca85c2014-12-03 18:38:07929 return _RunPerfTests(args)
jbudorick256fd532014-10-24 01:50:13930 elif command == 'python':
jama47ca85c2014-12-03 18:38:07931 return _RunPythonTests(args)
[email protected]fbe29322013-07-09 09:03:26932 else:
[email protected]6bc1bda22013-07-19 22:08:37933 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26934
[email protected]fbe29322013-07-09 09:03:26935
jbudorick66dc3722014-11-06 21:33:51936_SUPPORTED_IN_PLATFORM_MODE = [
937 # TODO(jbudorick): Add support for more test types.
jbudorick911be58d2015-01-13 02:51:06938 'gtest',
939 'instrumentation',
940 'uirobot',
jbudorick66dc3722014-11-06 21:33:51941]
942
943
jama47ca85c2014-12-03 18:38:07944def RunTestsInPlatformMode(args, parser):
jbudorick66dc3722014-11-06 21:33:51945
jama47ca85c2014-12-03 18:38:07946 if args.command not in _SUPPORTED_IN_PLATFORM_MODE:
947 parser.error('%s is not yet supported in platform mode' % args.command)
jbudorick66dc3722014-11-06 21:33:51948
jama47ca85c2014-12-03 18:38:07949 with environment_factory.CreateEnvironment(args, parser.error) as env:
950 with test_instance_factory.CreateTestInstance(args, parser.error) as test:
jbudorick66dc3722014-11-06 21:33:51951 with test_run_factory.CreateTestRun(
jama47ca85c2014-12-03 18:38:07952 args, env, test, parser.error) as test_run:
jbudorick66dc3722014-11-06 21:33:51953 results = test_run.RunTests()
954
jbudorick911be58d2015-01-13 02:51:06955 if args.environment == 'remote_device' and args.trigger:
rnephew5c499782014-12-12 19:08:55956 return 0 # Not returning results, only triggering.
957
jbudorick66dc3722014-11-06 21:33:51958 report_results.LogFull(
959 results=results,
960 test_type=test.TestType(),
961 test_package=test_run.TestPackage(),
jbudorickf9543672014-12-08 22:36:33962 annotation=getattr(args, 'annotations', None),
963 flakiness_server=getattr(args, 'flakiness_dashboard_server', None))
jbudorick66dc3722014-11-06 21:33:51964
jama47ca85c2014-12-03 18:38:07965 if args.json_results_file:
jbudorickb8c42072014-12-01 18:07:54966 json_results.GenerateJsonResultsFile(
jama47ca85c2014-12-03 18:38:07967 results, args.json_results_file)
jbudorickb8c42072014-12-01 18:07:54968
mikecasee74051022015-02-26 23:08:22969 return 0 if results.DidRunPass() else constants.ERROR_EXIT_CODE
jbudorick66dc3722014-11-06 21:33:51970
971
jama47ca85c2014-12-03 18:38:07972CommandConfigTuple = collections.namedtuple(
973 'CommandConfigTuple',
974 ['add_options_func', 'help_txt'])
[email protected]fbe29322013-07-09 09:03:26975VALID_COMMANDS = {
jama47ca85c2014-12-03 18:38:07976 'gtest': CommandConfigTuple(
977 AddGTestOptions,
978 'googletest-based C++ tests'),
979 'instrumentation': CommandConfigTuple(
980 AddInstrumentationTestOptions,
981 'InstrumentationTestCase-based Java tests'),
982 'uiautomator': CommandConfigTuple(
983 AddUIAutomatorTestOptions,
984 "Tests that run via Android's uiautomator command"),
985 'junit': CommandConfigTuple(
986 AddJUnitTestOptions,
987 'JUnit4-based Java tests'),
988 'monkey': CommandConfigTuple(
989 AddMonkeyTestOptions,
990 "Tests based on Android's monkey"),
991 'perf': CommandConfigTuple(
992 AddPerfTestOptions,
993 'Performance tests'),
994 'python': CommandConfigTuple(
995 AddPythonTestOptions,
996 'Python tests based on unittest.TestCase'),
997 'linker': CommandConfigTuple(
998 AddLinkerTestOptions,
999 'Linker tests'),
rnephew5c499782014-12-12 19:08:551000 'uirobot': CommandConfigTuple(
1001 AddUirobotTestOptions,
1002 'Uirobot test'),
jama47ca85c2014-12-03 18:38:071003}
[email protected]fbe29322013-07-09 09:03:261004
1005
[email protected]7c53a602014-03-24 16:21:441006def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:241007 for thread in threading.enumerate():
1008 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:211009
1010
[email protected]7c53a602014-03-24 16:21:441011def main():
[email protected]83bb8152013-11-19 15:02:211012 signal.signal(signal.SIGUSR1, DumpThreadStacks)
jama47ca85c2014-12-03 18:38:071013
1014 parser = argparse.ArgumentParser()
1015 command_parsers = parser.add_subparsers(title='test types',
1016 dest='command')
1017
1018 for test_type, config in sorted(VALID_COMMANDS.iteritems(),
1019 key=lambda x: x[0]):
1020 subparser = command_parsers.add_parser(
1021 test_type, usage='%(prog)s [options]', help=config.help_txt)
1022 config.add_options_func(subparser)
1023
1024 args = parser.parse_args()
mikecasee74051022015-02-26 23:08:221025
1026 try:
1027 return RunTestsCommand(args, parser)
1028 except base_error.BaseError as e:
1029 logging.exception('Error occurred.')
1030 if e.is_infra_error:
1031 return constants.INFRA_EXIT_CODE
1032 else:
1033 return constants.ERROR_EXIT_CODE
1034 except: # pylint: disable=W0702
1035 logging.exception('Unrecognized error occurred.')
1036 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:261037
[email protected]fbe29322013-07-09 09:03:261038
1039if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:441040 sys.exit(main())