blob: ea6ef0917ba9958da2f46f297e0a3a9628700b78 [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
9import collections
[email protected]f7148dd42013-08-20 14:24:5710import logging
[email protected]fbe29322013-07-09 09:03:2611import optparse
12import 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
17import traceback
[email protected]fbe29322013-07-09 09:03:2618
[email protected]f7148dd42013-08-20 14:24:5719from pylib import android_commands
[email protected]fbe29322013-07-09 09:03:2620from pylib import constants
[email protected]c0662e092013-11-12 11:51:2521from pylib import forwarder
[email protected]fbe29322013-07-09 09:03:2622from pylib import ports
23from pylib.base import base_test_result
[email protected]6bc1bda22013-07-19 22:08:3724from pylib.base import test_dispatcher
[email protected]6bc1bda22013-07-19 22:08:3725from pylib.gtest import gtest_config
[email protected]2a684222013-08-01 16:59:2226from pylib.gtest import setup as gtest_setup
27from pylib.gtest import test_options as gtest_test_options
[email protected]6b6abac6d2013-10-03 11:56:3828from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1329from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3730from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2231from pylib.instrumentation import test_options as instrumentation_test_options
[email protected]3dbdfa42013-08-08 01:08:1432from pylib.monkey import setup as monkey_setup
33from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4734from pylib.perf import setup as perf_setup
35from pylib.perf import test_options as perf_test_options
36from pylib.perf import test_runner as perf_test_runner
[email protected]6bc1bda22013-07-19 22:08:3737from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2238from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]803f65a72013-08-20 19:11:3039from pylib.utils import command_option_parser
[email protected]6bc1bda22013-07-19 22:08:3740from pylib.utils import report_results
41from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2642
43
[email protected]fbe29322013-07-09 09:03:2644def AddCommonOptions(option_parser):
45 """Adds all common options to |option_parser|."""
46
[email protected]dfffbcbc2013-09-17 22:06:0147 group = optparse.OptionGroup(option_parser, 'Common Options')
48 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
49 group.add_option('--debug', action='store_const', const='Debug',
50 dest='build_type', default=default_build_type,
51 help=('If set, run test suites under out/Debug. '
52 'Default is env var BUILDTYPE or Debug.'))
53 group.add_option('--release', action='store_const',
54 const='Release', dest='build_type',
55 help=('If set, run test suites under out/Release.'
56 ' Default is env var BUILDTYPE or Debug.'))
57 group.add_option('-c', dest='cleanup_test_files',
58 help='Cleanup test files on the device after run',
59 action='store_true')
60 group.add_option('--num_retries', dest='num_retries', type='int',
61 default=2,
62 help=('Number of retries for a test before '
63 'giving up.'))
64 group.add_option('-v',
65 '--verbose',
66 dest='verbose_count',
67 default=0,
68 action='count',
69 help='Verbose level (multiple times for more)')
70 group.add_option('--tool',
71 dest='tool',
72 help=('Run the test under a tool '
73 '(use --tool help to list them)'))
74 group.add_option('--flakiness-dashboard-server',
75 dest='flakiness_dashboard_server',
76 help=('Address of the server that is hosting the '
77 'Chrome for Android flakiness dashboard.'))
78 group.add_option('--skip-deps-push', dest='push_deps',
79 action='store_false', default=True,
80 help=('Do not push dependencies to the device. '
81 'Use this at own risk for speeding up test '
82 'execution on local machine.'))
83 group.add_option('-d', '--device', dest='test_device',
84 help=('Target device for the test suite '
85 'to run on.'))
86 option_parser.add_option_group(group)
[email protected]fbe29322013-07-09 09:03:2687
88
89def ProcessCommonOptions(options):
90 """Processes and handles all common options."""
[email protected]fbe29322013-07-09 09:03:2691 run_tests_helper.SetLogLevel(options.verbose_count)
[email protected]14b3b1202013-08-15 22:25:2892 constants.SetBuildType(options.build_type)
[email protected]fbe29322013-07-09 09:03:2693
94
[email protected]fbe29322013-07-09 09:03:2695def AddGTestOptions(option_parser):
96 """Adds gtest options to |option_parser|."""
97
98 option_parser.usage = '%prog gtest [options]'
[email protected]dfffbcbc2013-09-17 22:06:0199 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26100 option_parser.example = '%prog gtest -s base_unittests'
101
[email protected]6bc1bda22013-07-19 22:08:37102 # TODO(gkanwar): Make this option required
103 option_parser.add_option('-s', '--suite', dest='suite_name',
[email protected]fbe29322013-07-09 09:03:26104 help=('Executable name of the test suite to run '
105 '(use -s help to list them).'))
[email protected]dfffbcbc2013-09-17 22:06:01106 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
[email protected]9e689252013-07-30 20:14:36107 help='googletest-style filter string.')
[email protected]dfffbcbc2013-09-17 22:06:01108 option_parser.add_option('--gtest-also-run-disabled-tests',
109 dest='run_disabled', action='store_true',
110 help='Also run disabled tests if applicable.')
111 option_parser.add_option('-a', '--test-arguments', dest='test_arguments',
112 default='',
[email protected]9e689252013-07-30 20:14:36113 help='Additional arguments to pass to the test.')
114 option_parser.add_option('-t', dest='timeout',
115 help='Timeout to wait for each test',
116 type='int',
117 default=60)
[email protected]fbe29322013-07-09 09:03:26118 # TODO(gkanwar): Move these to Common Options once we have the plumbing
119 # in our other test types to handle these commands
[email protected]fbe29322013-07-09 09:03:26120 AddCommonOptions(option_parser)
121
122
[email protected]6b6abac6d2013-10-03 11:56:38123def AddLinkerTestOptions(option_parser):
124 option_parser.usage = '%prog linker'
125 option_parser.commands_dict = {}
126 option_parser.example = '%prog linker'
127
[email protected]98c4feef2013-10-08 01:19:05128 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
129 help='googletest-style filter string.')
[email protected]6b6abac6d2013-10-03 11:56:38130 AddCommonOptions(option_parser)
131
132
[email protected]6bc1bda22013-07-19 22:08:37133def ProcessGTestOptions(options):
134 """Intercept test suite help to list test suites.
135
136 Args:
137 options: Command line options.
[email protected]6bc1bda22013-07-19 22:08:37138 """
139 if options.suite_name == 'help':
140 print 'Available test suites are:'
[email protected]9e689252013-07-30 20:14:36141 for test_suite in (gtest_config.STABLE_TEST_SUITES +
142 gtest_config.EXPERIMENTAL_TEST_SUITES):
143 print test_suite
[email protected]2a684222013-08-01 16:59:22144 sys.exit(0)
[email protected]6bc1bda22013-07-19 22:08:37145
146 # Convert to a list, assuming all test suites if nothing was specified.
147 # TODO(gkanwar): Require having a test suite
148 if options.suite_name:
149 options.suite_name = [options.suite_name]
150 else:
[email protected]9e689252013-07-30 20:14:36151 options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES]
[email protected]6bc1bda22013-07-19 22:08:37152
153
[email protected]fbe29322013-07-09 09:03:26154def AddJavaTestOptions(option_parser):
155 """Adds the Java test options to |option_parser|."""
156
[email protected]dfffbcbc2013-09-17 22:06:01157 option_parser.add_option('-f', '--test-filter', dest='test_filter',
[email protected]fbe29322013-07-09 09:03:26158 help=('Test filter (if not fully qualified, '
159 'will run all matches).'))
160 option_parser.add_option(
161 '-A', '--annotation', dest='annotation_str',
162 help=('Comma-separated list of annotations. Run only tests with any of '
163 'the given annotations. An annotation can be either a key or a '
164 'key-values pair. A test that has no annotation is considered '
165 '"SmallTest".'))
166 option_parser.add_option(
167 '-E', '--exclude-annotation', dest='exclude_annotation_str',
168 help=('Comma-separated list of annotations. Exclude tests with these '
169 'annotations.'))
[email protected]fbe29322013-07-09 09:03:26170 option_parser.add_option('--screenshot', dest='screenshot_failures',
171 action='store_true',
172 help='Capture screenshots of test failures')
173 option_parser.add_option('--save-perf-json', action='store_true',
174 help='Saves the JSON file for each UI Perf test.')
[email protected]37ee0c792013-08-06 19:10:13175 option_parser.add_option('--official-build', action='store_true',
176 help='Run official build tests.')
[email protected]fbe29322013-07-09 09:03:26177 option_parser.add_option('--keep_test_server_ports',
178 action='store_true',
179 help=('Indicates the test server ports must be '
180 'kept. When this is run via a sharder '
181 'the test server ports should be kept and '
182 'should not be reset.'))
[email protected]fbe29322013-07-09 09:03:26183 option_parser.add_option('--test_data', action='append', default=[],
184 help=('Each instance defines a directory of test '
185 'data that should be copied to the target(s) '
186 'before running the tests. The argument '
187 'should be of the form <target>:<source>, '
188 '<target> is relative to the device data'
189 'directory, and <source> is relative to the '
190 'chromium build directory.'))
191
192
193def ProcessJavaTestOptions(options, error_func):
194 """Processes options/arguments and populates |options| with defaults."""
195
[email protected]fbe29322013-07-09 09:03:26196 if options.annotation_str:
197 options.annotations = options.annotation_str.split(',')
198 elif options.test_filter:
199 options.annotations = []
200 else:
[email protected]6bc1bda22013-07-19 22:08:37201 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
202 'EnormousTest']
[email protected]fbe29322013-07-09 09:03:26203
204 if options.exclude_annotation_str:
205 options.exclude_annotations = options.exclude_annotation_str.split(',')
206 else:
207 options.exclude_annotations = []
208
209 if not options.keep_test_server_ports:
210 if not ports.ResetTestServerPortAllocation():
211 raise Exception('Failed to reset test server port.')
212
213
214def AddInstrumentationTestOptions(option_parser):
215 """Adds Instrumentation test options to |option_parser|."""
216
217 option_parser.usage = '%prog instrumentation [options]'
[email protected]dfffbcbc2013-09-17 22:06:01218 option_parser.commands_dict = {}
[email protected]fb7ab5e82013-07-26 18:31:20219 option_parser.example = ('%prog instrumentation '
[email protected]fbe29322013-07-09 09:03:26220 '--test-apk=ChromiumTestShellTest')
221
222 AddJavaTestOptions(option_parser)
223 AddCommonOptions(option_parser)
224
[email protected]dfffbcbc2013-09-17 22:06:01225 option_parser.add_option('-j', '--java-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13226 default=False, help='Run only the Java tests.')
[email protected]dfffbcbc2013-09-17 22:06:01227 option_parser.add_option('-p', '--python-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13228 default=False,
229 help='Run only the host-driven tests.')
[email protected]a69e85bc2013-08-16 18:07:26230 option_parser.add_option('--host-driven-root',
[email protected]37ee0c792013-08-06 19:10:13231 help='Root of the host-driven tests.')
[email protected]fbe29322013-07-09 09:03:26232 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
233 action='store_true',
234 help='Wait for debugger.')
[email protected]fbe29322013-07-09 09:03:26235 option_parser.add_option(
236 '--test-apk', dest='test_apk',
237 help=('The name of the apk containing the tests '
[email protected]ae68d4a2013-09-24 21:57:15238 '(without the .apk extension; e.g. "ContentShellTest").'))
[email protected]803f65a72013-08-20 19:11:30239 option_parser.add_option('--coverage-dir',
240 help=('Directory in which to place all generated '
241 'EMMA coverage files.'))
[email protected]fbe29322013-07-09 09:03:26242
243
244def ProcessInstrumentationOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22245 """Processes options/arguments and populate |options| with defaults.
246
247 Args:
248 options: optparse.Options object.
249 error_func: Function to call with the error message in case of an error.
250
251 Returns:
252 An InstrumentationOptions named tuple which contains all options relevant to
253 instrumentation tests.
254 """
[email protected]fbe29322013-07-09 09:03:26255
256 ProcessJavaTestOptions(options, error_func)
257
[email protected]37ee0c792013-08-06 19:10:13258 if options.java_only and options.python_only:
259 error_func('Options java_only (-j) and python_only (-p) '
260 'are mutually exclusive.')
261 options.run_java_tests = True
262 options.run_python_tests = True
263 if options.java_only:
264 options.run_python_tests = False
265 elif options.python_only:
266 options.run_java_tests = False
267
[email protected]67954f822013-08-14 18:09:08268 if not options.host_driven_root:
[email protected]37ee0c792013-08-06 19:10:13269 options.run_python_tests = False
270
[email protected]fbe29322013-07-09 09:03:26271 if not options.test_apk:
272 error_func('--test-apk must be specified.')
273
[email protected]ae68d4a2013-09-24 21:57:15274
275 options.test_apk_path = os.path.join(constants.GetOutDirectory(),
276 constants.SDK_BUILD_APKS_DIR,
277 '%s.apk' % options.test_apk)
278 options.test_apk_jar_path = os.path.join(
279 constants.GetOutDirectory(),
280 constants.SDK_BUILD_TEST_JAVALIB_DIR,
281 '%s.jar' % options.test_apk)
[email protected]fbe29322013-07-09 09:03:26282
[email protected]2a684222013-08-01 16:59:22283 return instrumentation_test_options.InstrumentationOptions(
[email protected]2a684222013-08-01 16:59:22284 options.tool,
285 options.cleanup_test_files,
286 options.push_deps,
287 options.annotations,
288 options.exclude_annotations,
289 options.test_filter,
290 options.test_data,
291 options.save_perf_json,
292 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22293 options.wait_for_debugger,
[email protected]803f65a72013-08-20 19:11:30294 options.coverage_dir,
[email protected]2a684222013-08-01 16:59:22295 options.test_apk,
296 options.test_apk_path,
297 options.test_apk_jar_path)
298
[email protected]fbe29322013-07-09 09:03:26299
300def AddUIAutomatorTestOptions(option_parser):
301 """Adds UI Automator test options to |option_parser|."""
302
303 option_parser.usage = '%prog uiautomator [options]'
[email protected]dfffbcbc2013-09-17 22:06:01304 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26305 option_parser.example = (
306 '%prog uiautomator --test-jar=chromium_testshell_uiautomator_tests'
[email protected]a8886c8a92013-10-08 17:29:30307 ' --package=chromium_test_shell')
[email protected]fbe29322013-07-09 09:03:26308 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30309 '--package',
310 help=('Package under test. Possible values: %s' %
311 constants.PACKAGE_INFO.keys()))
[email protected]fbe29322013-07-09 09:03:26312 option_parser.add_option(
313 '--test-jar', dest='test_jar',
314 help=('The name of the dexed jar containing the tests (without the '
315 '.dex.jar extension). Alternatively, this can be a full path '
316 'to the jar.'))
317
318 AddJavaTestOptions(option_parser)
319 AddCommonOptions(option_parser)
320
321
322def ProcessUIAutomatorOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22323 """Processes UIAutomator options/arguments.
324
325 Args:
326 options: optparse.Options object.
327 error_func: Function to call with the error message in case of an error.
328
329 Returns:
330 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14331 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22332 """
[email protected]fbe29322013-07-09 09:03:26333
334 ProcessJavaTestOptions(options, error_func)
335
[email protected]a8886c8a92013-10-08 17:29:30336 if not options.package:
337 error_func('--package is required.')
338
339 if options.package not in constants.PACKAGE_INFO:
340 error_func('Invalid package.')
[email protected]fbe29322013-07-09 09:03:26341
342 if not options.test_jar:
343 error_func('--test-jar must be specified.')
344
345 if os.path.exists(options.test_jar):
346 # The dexed JAR is fully qualified, assume the info JAR lives along side.
347 options.uiautomator_jar = options.test_jar
348 else:
349 options.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15350 constants.GetOutDirectory(),
351 constants.SDK_BUILD_JAVALIB_DIR,
[email protected]fbe29322013-07-09 09:03:26352 '%s.dex.jar' % options.test_jar)
353 options.uiautomator_info_jar = (
354 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
355 '_java.jar')
356
[email protected]2a684222013-08-01 16:59:22357 return uiautomator_test_options.UIAutomatorOptions(
[email protected]2a684222013-08-01 16:59:22358 options.tool,
359 options.cleanup_test_files,
360 options.push_deps,
361 options.annotations,
362 options.exclude_annotations,
363 options.test_filter,
364 options.test_data,
365 options.save_perf_json,
366 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22367 options.uiautomator_jar,
368 options.uiautomator_info_jar,
[email protected]a8886c8a92013-10-08 17:29:30369 options.package)
[email protected]2a684222013-08-01 16:59:22370
[email protected]fbe29322013-07-09 09:03:26371
[email protected]3dbdfa42013-08-08 01:08:14372def AddMonkeyTestOptions(option_parser):
373 """Adds monkey test options to |option_parser|."""
[email protected]fb81b982013-08-09 00:07:12374
375 option_parser.usage = '%prog monkey [options]'
[email protected]dfffbcbc2013-09-17 22:06:01376 option_parser.commands_dict = {}
[email protected]fb81b982013-08-09 00:07:12377 option_parser.example = (
[email protected]a8886c8a92013-10-08 17:29:30378 '%prog monkey --package=chromium_test_shell')
[email protected]fb81b982013-08-09 00:07:12379
[email protected]3dbdfa42013-08-08 01:08:14380 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30381 '--package',
382 help=('Package under test. Possible values: %s' %
383 constants.PACKAGE_INFO.keys()))
[email protected]3dbdfa42013-08-08 01:08:14384 option_parser.add_option(
385 '--event-count', default=10000, type='int',
386 help='Number of events to generate [default: %default].')
387 option_parser.add_option(
388 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12389 help='A list of allowed categories.')
[email protected]3dbdfa42013-08-08 01:08:14390 option_parser.add_option(
391 '--throttle', default=100, type='int',
392 help='Delay between events (ms) [default: %default]. ')
393 option_parser.add_option(
394 '--seed', type='int',
395 help=('Seed value for pseudo-random generator. Same seed value generates '
396 'the same sequence of events. Seed is randomized by default.'))
397 option_parser.add_option(
398 '--extra-args', default='',
399 help=('String of other args to pass to the command verbatim '
400 '[default: "%default"].'))
401
402 AddCommonOptions(option_parser)
403
404
405def ProcessMonkeyTestOptions(options, error_func):
406 """Processes all monkey test options.
407
408 Args:
409 options: optparse.Options object.
410 error_func: Function to call with the error message in case of an error.
411
412 Returns:
413 A MonkeyOptions named tuple which contains all options relevant to
414 monkey tests.
415 """
[email protected]a8886c8a92013-10-08 17:29:30416 if not options.package:
417 error_func('--package is required.')
418
419 if options.package not in constants.PACKAGE_INFO:
420 error_func('Invalid package.')
[email protected]3dbdfa42013-08-08 01:08:14421
422 category = options.category
423 if category:
424 category = options.category.split(',')
425
426 return monkey_test_options.MonkeyOptions(
[email protected]3dbdfa42013-08-08 01:08:14427 options.verbose_count,
[email protected]a8886c8a92013-10-08 17:29:30428 options.package,
[email protected]3dbdfa42013-08-08 01:08:14429 options.event_count,
430 category,
431 options.throttle,
432 options.seed,
433 options.extra_args)
434
435
[email protected]ec3170b2013-08-14 14:39:47436def AddPerfTestOptions(option_parser):
437 """Adds perf test options to |option_parser|."""
438
439 option_parser.usage = '%prog perf [options]'
[email protected]dfffbcbc2013-09-17 22:06:01440 option_parser.commands_dict = {}
[email protected]def4bce2013-11-12 12:59:52441 option_parser.example = ('%prog perf '
[email protected]ad32f312013-11-13 04:03:29442 '[--single-step -- command args] or '
[email protected]def4bce2013-11-12 12:59:52443 '[--steps perf_steps.json] or '
[email protected]ad32f312013-11-13 04:03:29444 '[--print-step step]')
[email protected]ec3170b2013-08-14 14:39:47445
[email protected]181a5c92013-09-06 17:11:46446 option_parser.add_option(
[email protected]def4bce2013-11-12 12:59:52447 '--single-step',
[email protected]ad32f312013-11-13 04:03:29448 action='store_true',
[email protected]def4bce2013-11-12 12:59:52449 help='Execute the given command with retries, but only print the result '
450 'for the "most successful" round.')
451 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46452 '--steps',
[email protected]def4bce2013-11-12 12:59:52453 help='JSON file containing the list of commands to run.')
[email protected]181a5c92013-09-06 17:11:46454 option_parser.add_option(
455 '--flaky-steps',
456 help=('A JSON file containing steps that are flaky '
457 'and will have its exit code ignored.'))
458 option_parser.add_option(
459 '--print-step',
460 help='The name of a previously executed perf step to print.')
461 option_parser.add_option(
462 '--no-timeout', action='store_true',
463 help=('Do not impose a timeout. Each perf step is responsible for '
464 'implementing the timeout logic.'))
[email protected]650487c2013-09-30 11:40:49465 option_parser.add_option(
466 '-f', '--test-filter',
467 help=('Test filter (will match against the names listed in --steps).'))
468 option_parser.add_option(
469 '--dry-run',
470 action='store_true',
471 help='Just print the steps without executing.')
[email protected]ec3170b2013-08-14 14:39:47472 AddCommonOptions(option_parser)
473
474
[email protected]ad32f312013-11-13 04:03:29475def ProcessPerfTestOptions(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47476 """Processes all perf test options.
477
478 Args:
479 options: optparse.Options object.
480 error_func: Function to call with the error message in case of an error.
481
482 Returns:
483 A PerfOptions named tuple which contains all options relevant to
484 perf tests.
485 """
[email protected]def4bce2013-11-12 12:59:52486 # Only one of steps, print_step or single_step must be provided.
487 count = len(filter(None,
488 [options.steps, options.print_step, options.single_step]))
489 if count != 1:
490 error_func('Please specify one of: --steps, --print-step, --single-step.')
[email protected]ad32f312013-11-13 04:03:29491 single_step = None
492 if options.single_step:
493 single_step = ' '.join(args[2:])
[email protected]ec3170b2013-08-14 14:39:47494 return perf_test_options.PerfOptions(
[email protected]181a5c92013-09-06 17:11:46495 options.steps, options.flaky_steps, options.print_step,
[email protected]def4bce2013-11-12 12:59:52496 options.no_timeout, options.test_filter, options.dry_run,
[email protected]ad32f312013-11-13 04:03:29497 single_step)
[email protected]ec3170b2013-08-14 14:39:47498
499
[email protected]f7148dd42013-08-20 14:24:57500def _RunGTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37501 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]2a684222013-08-01 16:59:22502 ProcessGTestOptions(options)
[email protected]6bc1bda22013-07-19 22:08:37503
504 exit_code = 0
505 for suite_name in options.suite_name:
[email protected]2a684222013-08-01 16:59:22506 # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for
507 # the gtest command.
508 gtest_options = gtest_test_options.GTestOptions(
[email protected]2a684222013-08-01 16:59:22509 options.tool,
510 options.cleanup_test_files,
511 options.push_deps,
512 options.test_filter,
[email protected]dfffbcbc2013-09-17 22:06:01513 options.run_disabled,
[email protected]2a684222013-08-01 16:59:22514 options.test_arguments,
515 options.timeout,
516 suite_name)
[email protected]f7148dd42013-08-20 14:24:57517 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37518
519 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57520 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37521 num_retries=options.num_retries)
522
523 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
524 exit_code = test_exit_code
525
526 report_results.LogFull(
527 results=results,
528 test_type='Unit test',
529 test_package=suite_name,
[email protected]6bc1bda22013-07-19 22:08:37530 flakiness_server=options.flakiness_dashboard_server)
531
532 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
533 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
534
535 return exit_code
536
537
[email protected]6b6abac6d2013-10-03 11:56:38538def _RunLinkerTests(options, error_func, devices):
539 """Subcommand of RunTestsCommands which runs linker tests."""
540 runner_factory, tests = linker_setup.Setup(options, devices)
541
542 results, exit_code = test_dispatcher.RunTests(
543 tests, runner_factory, devices, shard=True, test_timeout=60,
544 num_retries=options.num_retries)
545
546 report_results.LogFull(
547 results=results,
548 test_type='Linker test',
549 test_package='ContentLinkerTest')
550
551 return exit_code
552
553
[email protected]f7148dd42013-08-20 14:24:57554def _RunInstrumentationTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37555 """Subcommand of RunTestsCommands which runs instrumentation tests."""
[email protected]2a684222013-08-01 16:59:22556 instrumentation_options = ProcessInstrumentationOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37557
[email protected]f7148dd42013-08-20 14:24:57558 if len(devices) > 1 and options.wait_for_debugger:
559 logging.warning('Debugger can not be sharded, using first available device')
560 devices = devices[:1]
561
[email protected]6bc1bda22013-07-19 22:08:37562 results = base_test_result.TestRunResults()
563 exit_code = 0
564
565 if options.run_java_tests:
[email protected]2a684222013-08-01 16:59:22566 runner_factory, tests = instrumentation_setup.Setup(instrumentation_options)
[email protected]6bc1bda22013-07-19 22:08:37567
568 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57569 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37570 num_retries=options.num_retries)
571
572 results.AddTestRunResults(test_results)
573
574 if options.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13575 runner_factory, tests = host_driven_setup.InstrumentationSetup(
[email protected]67954f822013-08-14 18:09:08576 options.host_driven_root, options.official_build,
[email protected]37ee0c792013-08-06 19:10:13577 instrumentation_options)
578
[email protected]34020022013-08-06 23:35:34579 if tests:
580 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57581 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]34020022013-08-06 23:35:34582 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37583
[email protected]34020022013-08-06 23:35:34584 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37585
[email protected]34020022013-08-06 23:35:34586 # Only allow exit code escalation
587 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
588 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37589
590 report_results.LogFull(
591 results=results,
592 test_type='Instrumentation',
593 test_package=os.path.basename(options.test_apk),
594 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37595 flakiness_server=options.flakiness_dashboard_server)
596
597 return exit_code
598
599
[email protected]f7148dd42013-08-20 14:24:57600def _RunUIAutomatorTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37601 """Subcommand of RunTestsCommands which runs uiautomator tests."""
[email protected]2a684222013-08-01 16:59:22602 uiautomator_options = ProcessUIAutomatorOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37603
[email protected]37ee0c792013-08-06 19:10:13604 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37605
[email protected]37ee0c792013-08-06 19:10:13606 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57607 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]37ee0c792013-08-06 19:10:13608 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37609
610 report_results.LogFull(
611 results=results,
612 test_type='UIAutomator',
613 test_package=os.path.basename(options.test_jar),
614 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37615 flakiness_server=options.flakiness_dashboard_server)
616
617 return exit_code
618
619
[email protected]f7148dd42013-08-20 14:24:57620def _RunMonkeyTests(options, error_func, devices):
[email protected]3dbdfa42013-08-08 01:08:14621 """Subcommand of RunTestsCommands which runs monkey tests."""
622 monkey_options = ProcessMonkeyTestOptions(options, error_func)
623
624 runner_factory, tests = monkey_setup.Setup(monkey_options)
625
626 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46627 tests, runner_factory, devices, shard=False, test_timeout=None,
628 num_retries=options.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14629
630 report_results.LogFull(
631 results=results,
632 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28633 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14634
635 return exit_code
636
637
[email protected]ad32f312013-11-13 04:03:29638def _RunPerfTests(options, args, error_func, devices):
[email protected]ec3170b2013-08-14 14:39:47639 """Subcommand of RunTestsCommands which runs perf tests."""
[email protected]ad32f312013-11-13 04:03:29640 perf_options = ProcessPerfTestOptions(options, args, error_func)
641 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47642 if perf_options.print_step:
643 return perf_test_runner.PrintTestOutput(perf_options.print_step)
644
645 runner_factory, tests = perf_setup.Setup(perf_options)
646
[email protected]86184c7b2013-08-15 15:06:57647 results, _ = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46648 tests, runner_factory, devices, shard=True, test_timeout=None,
649 num_retries=options.num_retries)
[email protected]ec3170b2013-08-14 14:39:47650
651 report_results.LogFull(
652 results=results,
653 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12654 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52655
656 if perf_options.single_step:
657 return perf_test_runner.PrintTestOutput('single_step')
658
[email protected]86184c7b2013-08-15 15:06:57659 # Always return 0 on the sharding stage. Individual tests exit_code
660 # will be returned on the print_step stage.
661 return 0
[email protected]ec3170b2013-08-14 14:39:47662
[email protected]3dbdfa42013-08-08 01:08:14663
[email protected]f7148dd42013-08-20 14:24:57664def _GetAttachedDevices(test_device=None):
665 """Get all attached devices.
666
667 Args:
668 test_device: Name of a specific device to use.
669
670 Returns:
671 A list of attached devices.
672 """
673 attached_devices = []
674
675 attached_devices = android_commands.GetAttachedDevices()
676 if test_device:
677 assert test_device in attached_devices, (
678 'Did not find device %s among attached device. Attached devices: %s'
679 % (test_device, ', '.join(attached_devices)))
680 attached_devices = [test_device]
681
682 assert attached_devices, 'No devices attached.'
683
684 return sorted(attached_devices)
685
686
[email protected]fbe29322013-07-09 09:03:26687def RunTestsCommand(command, options, args, option_parser):
688 """Checks test type and dispatches to the appropriate function.
689
690 Args:
691 command: String indicating the command that was received to trigger
692 this function.
693 options: optparse options dictionary.
694 args: List of extra args from optparse.
695 option_parser: optparse.OptionParser object.
696
697 Returns:
698 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10699
700 Raises:
701 Exception: Unknown command name passed in, or an exception from an
702 individual test runner.
[email protected]fbe29322013-07-09 09:03:26703 """
704
[email protected]d82f0252013-07-12 23:22:57705 # Check for extra arguments
[email protected]ad32f312013-11-13 04:03:29706 if len(args) > 2 and command != 'perf':
[email protected]d82f0252013-07-12 23:22:57707 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[2:])))
708 return constants.ERROR_EXIT_CODE
[email protected]ad32f312013-11-13 04:03:29709 if command == 'perf':
710 if ((options.single_step and len(args) <= 2) or
711 (not options.single_step and len(args) > 2)):
712 option_parser.error('Unrecognized arguments: %s' % (' '.join(args)))
713 return constants.ERROR_EXIT_CODE
[email protected]d82f0252013-07-12 23:22:57714
[email protected]fbe29322013-07-09 09:03:26715 ProcessCommonOptions(options)
716
[email protected]f7148dd42013-08-20 14:24:57717 devices = _GetAttachedDevices(options.test_device)
718
[email protected]c0662e092013-11-12 11:51:25719 forwarder.Forwarder.RemoveHostLog()
720
[email protected]fbe29322013-07-09 09:03:26721 if command == 'gtest':
[email protected]f7148dd42013-08-20 14:24:57722 return _RunGTests(options, option_parser.error, devices)
[email protected]6b6abac6d2013-10-03 11:56:38723 elif command == 'linker':
724 return _RunLinkerTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26725 elif command == 'instrumentation':
[email protected]f7148dd42013-08-20 14:24:57726 return _RunInstrumentationTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26727 elif command == 'uiautomator':
[email protected]f7148dd42013-08-20 14:24:57728 return _RunUIAutomatorTests(options, option_parser.error, devices)
[email protected]3dbdfa42013-08-08 01:08:14729 elif command == 'monkey':
[email protected]f7148dd42013-08-20 14:24:57730 return _RunMonkeyTests(options, option_parser.error, devices)
[email protected]ec3170b2013-08-14 14:39:47731 elif command == 'perf':
[email protected]ad32f312013-11-13 04:03:29732 return _RunPerfTests(options, args, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26733 else:
[email protected]6bc1bda22013-07-19 22:08:37734 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26735
[email protected]fbe29322013-07-09 09:03:26736
737def HelpCommand(command, options, args, option_parser):
738 """Display help for a certain command, or overall help.
739
740 Args:
741 command: String indicating the command that was received to trigger
742 this function.
743 options: optparse options dictionary.
744 args: List of extra args from optparse.
745 option_parser: optparse.OptionParser object.
746
747 Returns:
748 Integer indicated exit code.
749 """
750 # If we don't have any args, display overall help
751 if len(args) < 3:
752 option_parser.print_help()
753 return 0
[email protected]d82f0252013-07-12 23:22:57754 # If we have too many args, print an error
755 if len(args) > 3:
756 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:])))
757 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:26758
759 command = args[2]
760
761 if command not in VALID_COMMANDS:
762 option_parser.error('Unrecognized command.')
763
764 # Treat the help command as a special case. We don't care about showing a
765 # specific help page for itself.
766 if command == 'help':
767 option_parser.print_help()
768 return 0
769
770 VALID_COMMANDS[command].add_options_func(option_parser)
771 option_parser.usage = '%prog ' + command + ' [options]'
[email protected]dfffbcbc2013-09-17 22:06:01772 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26773 option_parser.print_help()
774
775 return 0
776
777
778# Define a named tuple for the values in the VALID_COMMANDS dictionary so the
779# syntax is a bit prettier. The tuple is two functions: (add options, run
780# command).
781CommandFunctionTuple = collections.namedtuple(
782 'CommandFunctionTuple', ['add_options_func', 'run_command_func'])
783VALID_COMMANDS = {
784 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26785 'instrumentation': CommandFunctionTuple(
786 AddInstrumentationTestOptions, RunTestsCommand),
787 'uiautomator': CommandFunctionTuple(
788 AddUIAutomatorTestOptions, RunTestsCommand),
[email protected]3dbdfa42013-08-08 01:08:14789 'monkey': CommandFunctionTuple(
790 AddMonkeyTestOptions, RunTestsCommand),
[email protected]ec3170b2013-08-14 14:39:47791 'perf': CommandFunctionTuple(
792 AddPerfTestOptions, RunTestsCommand),
[email protected]6b6abac6d2013-10-03 11:56:38793 'linker': CommandFunctionTuple(
794 AddLinkerTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26795 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand)
796 }
797
798
[email protected]83bb8152013-11-19 15:02:21799def DumpThreadStacks(signal, frame):
800 thread_names_map = dict(
801 [(thread.ident, thread.name) for thread in threading.enumerate()])
802 lines = []
803 for thread_id, stack in sys._current_frames().items():
804 lines.append(
805 '\n# Thread: %s (%d)' % (
806 thread_names_map.get(thread_id, ''), thread_id))
807 for filename, lineno, name, line in traceback.extract_stack(stack):
808 lines.append('File: "%s", line %d, in %s' % (filename, lineno, name))
809 if line:
810 lines.append(' %s' % (line.strip()))
811 print '\n'.join(lines)
812
813
[email protected]fbe29322013-07-09 09:03:26814def main(argv):
[email protected]83bb8152013-11-19 15:02:21815 signal.signal(signal.SIGUSR1, DumpThreadStacks)
[email protected]803f65a72013-08-20 19:11:30816 option_parser = command_option_parser.CommandOptionParser(
817 commands_dict=VALID_COMMANDS)
818 return command_option_parser.ParseAndExecute(option_parser)
[email protected]fbe29322013-07-09 09:03:26819
[email protected]fbe29322013-07-09 09:03:26820
821if __name__ == '__main__':
822 sys.exit(main(sys.argv))