blob: a0c3b72fe03e55b58ccfb099956b06fba90569e0 [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
[email protected]fbe29322013-07-09 09:03:2617
[email protected]f7148dd42013-08-20 14:24:5718from pylib import android_commands
[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
[email protected]6bc1bda22013-07-19 22:08:3723from pylib.base import test_dispatcher
[email protected]6bc1bda22013-07-19 22:08:3724from pylib.gtest import gtest_config
[email protected]2a684222013-08-01 16:59:2225from pylib.gtest import setup as gtest_setup
26from pylib.gtest import test_options as gtest_test_options
[email protected]6b6abac6d2013-10-03 11:56:3827from pylib.linker import setup as linker_setup
[email protected]37ee0c792013-08-06 19:10:1328from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3729from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2230from pylib.instrumentation import test_options as instrumentation_test_options
[email protected]3dbdfa42013-08-08 01:08:1431from pylib.monkey import setup as monkey_setup
32from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4733from pylib.perf import setup as perf_setup
34from pylib.perf import test_options as perf_test_options
35from pylib.perf import test_runner as perf_test_runner
[email protected]6bc1bda22013-07-19 22:08:3736from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2237from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]803f65a72013-08-20 19:11:3038from pylib.utils import command_option_parser
[email protected]6bc1bda22013-07-19 22:08:3739from pylib.utils import report_results
[email protected]71aec4b2013-11-20 00:35:2440from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3741from 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]c53dc4332013-11-20 04:38:03106 option_parser.add_option('-f', '--gtest_filter', '--gtest-filter',
107 dest='test_filter',
[email protected]9e689252013-07-30 20:14:36108 help='googletest-style filter string.')
[email protected]c53dc4332013-11-20 04:38:03109 option_parser.add_option('--gtest_also_run_disabled_tests',
110 '--gtest-also-run-disabled-tests',
[email protected]dfffbcbc2013-09-17 22:06:01111 dest='run_disabled', action='store_true',
112 help='Also run disabled tests if applicable.')
113 option_parser.add_option('-a', '--test-arguments', dest='test_arguments',
114 default='',
[email protected]9e689252013-07-30 20:14:36115 help='Additional arguments to pass to the test.')
116 option_parser.add_option('-t', dest='timeout',
117 help='Timeout to wait for each test',
118 type='int',
119 default=60)
[email protected]5b8b8742014-05-22 08:18:50120 option_parser.add_option('--isolate_file_path',
121 '--isolate-file-path',
122 dest='isolate_file_path',
123 help='.isolate file path to override the default '
124 'path')
[email protected]fbe29322013-07-09 09:03:26125 # TODO(gkanwar): Move these to Common Options once we have the plumbing
126 # in our other test types to handle these commands
[email protected]fbe29322013-07-09 09:03:26127 AddCommonOptions(option_parser)
128
129
[email protected]6b6abac6d2013-10-03 11:56:38130def AddLinkerTestOptions(option_parser):
131 option_parser.usage = '%prog linker'
132 option_parser.commands_dict = {}
133 option_parser.example = '%prog linker'
134
[email protected]98c4feef2013-10-08 01:19:05135 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
136 help='googletest-style filter string.')
[email protected]6b6abac6d2013-10-03 11:56:38137 AddCommonOptions(option_parser)
138
139
[email protected]6bc1bda22013-07-19 22:08:37140def ProcessGTestOptions(options):
141 """Intercept test suite help to list test suites.
142
143 Args:
144 options: Command line options.
[email protected]6bc1bda22013-07-19 22:08:37145 """
146 if options.suite_name == 'help':
147 print 'Available test suites are:'
[email protected]9e689252013-07-30 20:14:36148 for test_suite in (gtest_config.STABLE_TEST_SUITES +
149 gtest_config.EXPERIMENTAL_TEST_SUITES):
150 print test_suite
[email protected]2a684222013-08-01 16:59:22151 sys.exit(0)
[email protected]6bc1bda22013-07-19 22:08:37152
153 # Convert to a list, assuming all test suites if nothing was specified.
154 # TODO(gkanwar): Require having a test suite
155 if options.suite_name:
156 options.suite_name = [options.suite_name]
157 else:
[email protected]9e689252013-07-30 20:14:36158 options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES]
[email protected]6bc1bda22013-07-19 22:08:37159
160
[email protected]fbe29322013-07-09 09:03:26161def AddJavaTestOptions(option_parser):
162 """Adds the Java test options to |option_parser|."""
163
[email protected]dfffbcbc2013-09-17 22:06:01164 option_parser.add_option('-f', '--test-filter', dest='test_filter',
[email protected]fbe29322013-07-09 09:03:26165 help=('Test filter (if not fully qualified, '
166 'will run all matches).'))
167 option_parser.add_option(
168 '-A', '--annotation', dest='annotation_str',
169 help=('Comma-separated list of annotations. Run only tests with any of '
170 'the given annotations. An annotation can be either a key or a '
171 'key-values pair. A test that has no annotation is considered '
172 '"SmallTest".'))
173 option_parser.add_option(
174 '-E', '--exclude-annotation', dest='exclude_annotation_str',
175 help=('Comma-separated list of annotations. Exclude tests with these '
176 'annotations.'))
[email protected]fbe29322013-07-09 09:03:26177 option_parser.add_option('--screenshot', dest='screenshot_failures',
178 action='store_true',
179 help='Capture screenshots of test failures')
180 option_parser.add_option('--save-perf-json', action='store_true',
181 help='Saves the JSON file for each UI Perf test.')
[email protected]37ee0c792013-08-06 19:10:13182 option_parser.add_option('--official-build', action='store_true',
183 help='Run official build tests.')
[email protected]fbe29322013-07-09 09:03:26184 option_parser.add_option('--test_data', action='append', default=[],
185 help=('Each instance defines a directory of test '
186 'data that should be copied to the target(s) '
187 'before running the tests. The argument '
188 'should be of the form <target>:<source>, '
189 '<target> is relative to the device data'
190 'directory, and <source> is relative to the '
191 'chromium build directory.'))
192
193
[email protected]7c53a602014-03-24 16:21:44194def ProcessJavaTestOptions(options):
[email protected]fbe29322013-07-09 09:03:26195 """Processes options/arguments and populates |options| with defaults."""
196
[email protected]fbe29322013-07-09 09:03:26197 if options.annotation_str:
198 options.annotations = options.annotation_str.split(',')
199 elif options.test_filter:
200 options.annotations = []
201 else:
[email protected]6bc1bda22013-07-19 22:08:37202 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
203 'EnormousTest']
[email protected]fbe29322013-07-09 09:03:26204
205 if options.exclude_annotation_str:
206 options.exclude_annotations = options.exclude_annotation_str.split(',')
207 else:
208 options.exclude_annotations = []
209
[email protected]fbe29322013-07-09 09:03:26210
211def AddInstrumentationTestOptions(option_parser):
212 """Adds Instrumentation test options to |option_parser|."""
213
214 option_parser.usage = '%prog instrumentation [options]'
[email protected]dfffbcbc2013-09-17 22:06:01215 option_parser.commands_dict = {}
[email protected]fb7ab5e82013-07-26 18:31:20216 option_parser.example = ('%prog instrumentation '
[email protected]efeb59e2014-03-12 01:31:26217 '--test-apk=ChromeShellTest')
[email protected]fbe29322013-07-09 09:03:26218
219 AddJavaTestOptions(option_parser)
220 AddCommonOptions(option_parser)
221
[email protected]dfffbcbc2013-09-17 22:06:01222 option_parser.add_option('-j', '--java-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13223 default=False, help='Run only the Java tests.')
[email protected]dfffbcbc2013-09-17 22:06:01224 option_parser.add_option('-p', '--python-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13225 default=False,
226 help='Run only the host-driven tests.')
[email protected]a69e85bc2013-08-16 18:07:26227 option_parser.add_option('--host-driven-root',
[email protected]37ee0c792013-08-06 19:10:13228 help='Root of the host-driven tests.')
[email protected]fbe29322013-07-09 09:03:26229 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
230 action='store_true',
231 help='Wait for debugger.')
[email protected]fbe29322013-07-09 09:03:26232 option_parser.add_option(
233 '--test-apk', dest='test_apk',
234 help=('The name of the apk containing the tests '
[email protected]ae68d4a2013-09-24 21:57:15235 '(without the .apk extension; e.g. "ContentShellTest").'))
[email protected]803f65a72013-08-20 19:11:30236 option_parser.add_option('--coverage-dir',
237 help=('Directory in which to place all generated '
238 'EMMA coverage files.'))
[email protected]fbe29322013-07-09 09:03:26239
240
241def ProcessInstrumentationOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22242 """Processes options/arguments and populate |options| with defaults.
243
244 Args:
245 options: optparse.Options object.
246 error_func: Function to call with the error message in case of an error.
247
248 Returns:
249 An InstrumentationOptions named tuple which contains all options relevant to
250 instrumentation tests.
251 """
[email protected]fbe29322013-07-09 09:03:26252
[email protected]7c53a602014-03-24 16:21:44253 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26254
[email protected]37ee0c792013-08-06 19:10:13255 if options.java_only and options.python_only:
256 error_func('Options java_only (-j) and python_only (-p) '
257 'are mutually exclusive.')
258 options.run_java_tests = True
259 options.run_python_tests = True
260 if options.java_only:
261 options.run_python_tests = False
262 elif options.python_only:
263 options.run_java_tests = False
264
[email protected]67954f822013-08-14 18:09:08265 if not options.host_driven_root:
[email protected]37ee0c792013-08-06 19:10:13266 options.run_python_tests = False
267
[email protected]fbe29322013-07-09 09:03:26268 if not options.test_apk:
269 error_func('--test-apk must be specified.')
270
[email protected]ae68d4a2013-09-24 21:57:15271
272 options.test_apk_path = os.path.join(constants.GetOutDirectory(),
273 constants.SDK_BUILD_APKS_DIR,
274 '%s.apk' % options.test_apk)
275 options.test_apk_jar_path = os.path.join(
276 constants.GetOutDirectory(),
277 constants.SDK_BUILD_TEST_JAVALIB_DIR,
278 '%s.jar' % options.test_apk)
[email protected]fbe29322013-07-09 09:03:26279
[email protected]2a684222013-08-01 16:59:22280 return instrumentation_test_options.InstrumentationOptions(
[email protected]2a684222013-08-01 16:59:22281 options.tool,
282 options.cleanup_test_files,
283 options.push_deps,
284 options.annotations,
285 options.exclude_annotations,
286 options.test_filter,
287 options.test_data,
288 options.save_perf_json,
289 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22290 options.wait_for_debugger,
[email protected]803f65a72013-08-20 19:11:30291 options.coverage_dir,
[email protected]2a684222013-08-01 16:59:22292 options.test_apk,
293 options.test_apk_path,
294 options.test_apk_jar_path)
295
[email protected]fbe29322013-07-09 09:03:26296
297def AddUIAutomatorTestOptions(option_parser):
298 """Adds UI Automator test options to |option_parser|."""
299
300 option_parser.usage = '%prog uiautomator [options]'
[email protected]dfffbcbc2013-09-17 22:06:01301 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26302 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26303 '%prog uiautomator --test-jar=chrome_shell_uiautomator_tests'
304 ' --package=chrome_shell')
[email protected]fbe29322013-07-09 09:03:26305 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30306 '--package',
307 help=('Package under test. Possible values: %s' %
308 constants.PACKAGE_INFO.keys()))
[email protected]fbe29322013-07-09 09:03:26309 option_parser.add_option(
310 '--test-jar', dest='test_jar',
311 help=('The name of the dexed jar containing the tests (without the '
312 '.dex.jar extension). Alternatively, this can be a full path '
313 'to the jar.'))
314
315 AddJavaTestOptions(option_parser)
316 AddCommonOptions(option_parser)
317
318
319def ProcessUIAutomatorOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22320 """Processes UIAutomator options/arguments.
321
322 Args:
323 options: optparse.Options object.
324 error_func: Function to call with the error message in case of an error.
325
326 Returns:
327 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14328 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22329 """
[email protected]fbe29322013-07-09 09:03:26330
[email protected]7c53a602014-03-24 16:21:44331 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26332
[email protected]a8886c8a92013-10-08 17:29:30333 if not options.package:
334 error_func('--package is required.')
335
336 if options.package not in constants.PACKAGE_INFO:
337 error_func('Invalid package.')
[email protected]fbe29322013-07-09 09:03:26338
339 if not options.test_jar:
340 error_func('--test-jar must be specified.')
341
342 if os.path.exists(options.test_jar):
343 # The dexed JAR is fully qualified, assume the info JAR lives along side.
344 options.uiautomator_jar = options.test_jar
345 else:
346 options.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15347 constants.GetOutDirectory(),
348 constants.SDK_BUILD_JAVALIB_DIR,
[email protected]fbe29322013-07-09 09:03:26349 '%s.dex.jar' % options.test_jar)
350 options.uiautomator_info_jar = (
351 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
352 '_java.jar')
353
[email protected]2a684222013-08-01 16:59:22354 return uiautomator_test_options.UIAutomatorOptions(
[email protected]2a684222013-08-01 16:59:22355 options.tool,
356 options.cleanup_test_files,
357 options.push_deps,
358 options.annotations,
359 options.exclude_annotations,
360 options.test_filter,
361 options.test_data,
362 options.save_perf_json,
363 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22364 options.uiautomator_jar,
365 options.uiautomator_info_jar,
[email protected]a8886c8a92013-10-08 17:29:30366 options.package)
[email protected]2a684222013-08-01 16:59:22367
[email protected]fbe29322013-07-09 09:03:26368
[email protected]3dbdfa42013-08-08 01:08:14369def AddMonkeyTestOptions(option_parser):
370 """Adds monkey test options to |option_parser|."""
[email protected]fb81b982013-08-09 00:07:12371
372 option_parser.usage = '%prog monkey [options]'
[email protected]dfffbcbc2013-09-17 22:06:01373 option_parser.commands_dict = {}
[email protected]fb81b982013-08-09 00:07:12374 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26375 '%prog monkey --package=chrome_shell')
[email protected]fb81b982013-08-09 00:07:12376
[email protected]3dbdfa42013-08-08 01:08:14377 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30378 '--package',
379 help=('Package under test. Possible values: %s' %
380 constants.PACKAGE_INFO.keys()))
[email protected]3dbdfa42013-08-08 01:08:14381 option_parser.add_option(
382 '--event-count', default=10000, type='int',
383 help='Number of events to generate [default: %default].')
384 option_parser.add_option(
385 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12386 help='A list of allowed categories.')
[email protected]3dbdfa42013-08-08 01:08:14387 option_parser.add_option(
388 '--throttle', default=100, type='int',
389 help='Delay between events (ms) [default: %default]. ')
390 option_parser.add_option(
391 '--seed', type='int',
392 help=('Seed value for pseudo-random generator. Same seed value generates '
393 'the same sequence of events. Seed is randomized by default.'))
394 option_parser.add_option(
395 '--extra-args', default='',
396 help=('String of other args to pass to the command verbatim '
397 '[default: "%default"].'))
398
399 AddCommonOptions(option_parser)
400
401
402def ProcessMonkeyTestOptions(options, error_func):
403 """Processes all monkey test options.
404
405 Args:
406 options: optparse.Options object.
407 error_func: Function to call with the error message in case of an error.
408
409 Returns:
410 A MonkeyOptions named tuple which contains all options relevant to
411 monkey tests.
412 """
[email protected]a8886c8a92013-10-08 17:29:30413 if not options.package:
414 error_func('--package is required.')
415
416 if options.package not in constants.PACKAGE_INFO:
417 error_func('Invalid package.')
[email protected]3dbdfa42013-08-08 01:08:14418
419 category = options.category
420 if category:
421 category = options.category.split(',')
422
423 return monkey_test_options.MonkeyOptions(
[email protected]3dbdfa42013-08-08 01:08:14424 options.verbose_count,
[email protected]a8886c8a92013-10-08 17:29:30425 options.package,
[email protected]3dbdfa42013-08-08 01:08:14426 options.event_count,
427 category,
428 options.throttle,
429 options.seed,
430 options.extra_args)
431
432
[email protected]ec3170b2013-08-14 14:39:47433def AddPerfTestOptions(option_parser):
434 """Adds perf test options to |option_parser|."""
435
436 option_parser.usage = '%prog perf [options]'
[email protected]dfffbcbc2013-09-17 22:06:01437 option_parser.commands_dict = {}
[email protected]def4bce2013-11-12 12:59:52438 option_parser.example = ('%prog perf '
[email protected]ad32f312013-11-13 04:03:29439 '[--single-step -- command args] or '
[email protected]def4bce2013-11-12 12:59:52440 '[--steps perf_steps.json] or '
[email protected]ad32f312013-11-13 04:03:29441 '[--print-step step]')
[email protected]ec3170b2013-08-14 14:39:47442
[email protected]181a5c92013-09-06 17:11:46443 option_parser.add_option(
[email protected]def4bce2013-11-12 12:59:52444 '--single-step',
[email protected]ad32f312013-11-13 04:03:29445 action='store_true',
[email protected]def4bce2013-11-12 12:59:52446 help='Execute the given command with retries, but only print the result '
447 'for the "most successful" round.')
448 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46449 '--steps',
[email protected]def4bce2013-11-12 12:59:52450 help='JSON file containing the list of commands to run.')
[email protected]181a5c92013-09-06 17:11:46451 option_parser.add_option(
452 '--flaky-steps',
453 help=('A JSON file containing steps that are flaky '
454 'and will have its exit code ignored.'))
455 option_parser.add_option(
456 '--print-step',
457 help='The name of a previously executed perf step to print.')
458 option_parser.add_option(
459 '--no-timeout', action='store_true',
460 help=('Do not impose a timeout. Each perf step is responsible for '
461 'implementing the timeout logic.'))
[email protected]650487c2013-09-30 11:40:49462 option_parser.add_option(
463 '-f', '--test-filter',
464 help=('Test filter (will match against the names listed in --steps).'))
465 option_parser.add_option(
466 '--dry-run',
467 action='store_true',
468 help='Just print the steps without executing.')
[email protected]ec3170b2013-08-14 14:39:47469 AddCommonOptions(option_parser)
470
471
[email protected]ad32f312013-11-13 04:03:29472def ProcessPerfTestOptions(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47473 """Processes all perf test options.
474
475 Args:
476 options: optparse.Options object.
477 error_func: Function to call with the error message in case of an error.
478
479 Returns:
480 A PerfOptions named tuple which contains all options relevant to
481 perf tests.
482 """
[email protected]def4bce2013-11-12 12:59:52483 # Only one of steps, print_step or single_step must be provided.
484 count = len(filter(None,
485 [options.steps, options.print_step, options.single_step]))
486 if count != 1:
487 error_func('Please specify one of: --steps, --print-step, --single-step.')
[email protected]ad32f312013-11-13 04:03:29488 single_step = None
489 if options.single_step:
490 single_step = ' '.join(args[2:])
[email protected]ec3170b2013-08-14 14:39:47491 return perf_test_options.PerfOptions(
[email protected]181a5c92013-09-06 17:11:46492 options.steps, options.flaky_steps, options.print_step,
[email protected]def4bce2013-11-12 12:59:52493 options.no_timeout, options.test_filter, options.dry_run,
[email protected]ad32f312013-11-13 04:03:29494 single_step)
[email protected]ec3170b2013-08-14 14:39:47495
496
[email protected]7c53a602014-03-24 16:21:44497def _RunGTests(options, devices):
[email protected]6bc1bda22013-07-19 22:08:37498 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]2a684222013-08-01 16:59:22499 ProcessGTestOptions(options)
[email protected]6bc1bda22013-07-19 22:08:37500
501 exit_code = 0
502 for suite_name in options.suite_name:
[email protected]2a684222013-08-01 16:59:22503 # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for
504 # the gtest command.
505 gtest_options = gtest_test_options.GTestOptions(
[email protected]2a684222013-08-01 16:59:22506 options.tool,
507 options.cleanup_test_files,
508 options.push_deps,
509 options.test_filter,
[email protected]dfffbcbc2013-09-17 22:06:01510 options.run_disabled,
[email protected]2a684222013-08-01 16:59:22511 options.test_arguments,
512 options.timeout,
[email protected]5b8b8742014-05-22 08:18:50513 options.isolate_file_path,
[email protected]2a684222013-08-01 16:59:22514 suite_name)
[email protected]f7148dd42013-08-20 14:24:57515 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37516
517 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57518 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37519 num_retries=options.num_retries)
520
521 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
522 exit_code = test_exit_code
523
524 report_results.LogFull(
525 results=results,
526 test_type='Unit test',
527 test_package=suite_name,
[email protected]6bc1bda22013-07-19 22:08:37528 flakiness_server=options.flakiness_dashboard_server)
529
530 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
531 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
532
533 return exit_code
534
535
[email protected]7c53a602014-03-24 16:21:44536def _RunLinkerTests(options, devices):
[email protected]6b6abac6d2013-10-03 11:56:38537 """Subcommand of RunTestsCommands which runs linker tests."""
538 runner_factory, tests = linker_setup.Setup(options, devices)
539
540 results, exit_code = test_dispatcher.RunTests(
541 tests, runner_factory, devices, shard=True, test_timeout=60,
542 num_retries=options.num_retries)
543
544 report_results.LogFull(
545 results=results,
546 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22547 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38548
549 return exit_code
550
551
[email protected]f7148dd42013-08-20 14:24:57552def _RunInstrumentationTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37553 """Subcommand of RunTestsCommands which runs instrumentation tests."""
[email protected]2a684222013-08-01 16:59:22554 instrumentation_options = ProcessInstrumentationOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37555
[email protected]f7148dd42013-08-20 14:24:57556 if len(devices) > 1 and options.wait_for_debugger:
557 logging.warning('Debugger can not be sharded, using first available device')
558 devices = devices[:1]
559
[email protected]6bc1bda22013-07-19 22:08:37560 results = base_test_result.TestRunResults()
561 exit_code = 0
562
563 if options.run_java_tests:
[email protected]2a684222013-08-01 16:59:22564 runner_factory, tests = instrumentation_setup.Setup(instrumentation_options)
[email protected]6bc1bda22013-07-19 22:08:37565
566 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57567 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37568 num_retries=options.num_retries)
569
570 results.AddTestRunResults(test_results)
571
572 if options.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13573 runner_factory, tests = host_driven_setup.InstrumentationSetup(
[email protected]67954f822013-08-14 18:09:08574 options.host_driven_root, options.official_build,
[email protected]37ee0c792013-08-06 19:10:13575 instrumentation_options)
576
[email protected]34020022013-08-06 23:35:34577 if tests:
578 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57579 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]34020022013-08-06 23:35:34580 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37581
[email protected]34020022013-08-06 23:35:34582 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37583
[email protected]34020022013-08-06 23:35:34584 # Only allow exit code escalation
585 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
586 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37587
588 report_results.LogFull(
589 results=results,
590 test_type='Instrumentation',
591 test_package=os.path.basename(options.test_apk),
592 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37593 flakiness_server=options.flakiness_dashboard_server)
594
595 return exit_code
596
597
[email protected]f7148dd42013-08-20 14:24:57598def _RunUIAutomatorTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37599 """Subcommand of RunTestsCommands which runs uiautomator tests."""
[email protected]2a684222013-08-01 16:59:22600 uiautomator_options = ProcessUIAutomatorOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37601
[email protected]37ee0c792013-08-06 19:10:13602 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37603
[email protected]37ee0c792013-08-06 19:10:13604 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57605 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]37ee0c792013-08-06 19:10:13606 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37607
608 report_results.LogFull(
609 results=results,
610 test_type='UIAutomator',
611 test_package=os.path.basename(options.test_jar),
612 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37613 flakiness_server=options.flakiness_dashboard_server)
614
615 return exit_code
616
617
[email protected]f7148dd42013-08-20 14:24:57618def _RunMonkeyTests(options, error_func, devices):
[email protected]3dbdfa42013-08-08 01:08:14619 """Subcommand of RunTestsCommands which runs monkey tests."""
620 monkey_options = ProcessMonkeyTestOptions(options, error_func)
621
622 runner_factory, tests = monkey_setup.Setup(monkey_options)
623
624 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46625 tests, runner_factory, devices, shard=False, test_timeout=None,
626 num_retries=options.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14627
628 report_results.LogFull(
629 results=results,
630 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28631 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14632
633 return exit_code
634
635
[email protected]ad32f312013-11-13 04:03:29636def _RunPerfTests(options, args, error_func, devices):
[email protected]ec3170b2013-08-14 14:39:47637 """Subcommand of RunTestsCommands which runs perf tests."""
[email protected]ad32f312013-11-13 04:03:29638 perf_options = ProcessPerfTestOptions(options, args, error_func)
639 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47640 if perf_options.print_step:
641 return perf_test_runner.PrintTestOutput(perf_options.print_step)
642
643 runner_factory, tests = perf_setup.Setup(perf_options)
644
[email protected]86184c7b2013-08-15 15:06:57645 results, _ = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46646 tests, runner_factory, devices, shard=True, test_timeout=None,
647 num_retries=options.num_retries)
[email protected]ec3170b2013-08-14 14:39:47648
649 report_results.LogFull(
650 results=results,
651 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12652 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52653
654 if perf_options.single_step:
655 return perf_test_runner.PrintTestOutput('single_step')
656
[email protected]11ce8452014-02-17 10:55:03657 perf_test_runner.PrintSummary(tests)
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()
[email protected]6b11583b2013-11-21 16:18:40720 if not ports.ResetTestServerPortAllocation():
721 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25722
[email protected]fbe29322013-07-09 09:03:26723 if command == 'gtest':
[email protected]7c53a602014-03-24 16:21:44724 return _RunGTests(options, devices)
[email protected]6b6abac6d2013-10-03 11:56:38725 elif command == 'linker':
[email protected]7c53a602014-03-24 16:21:44726 return _RunLinkerTests(options, devices)
[email protected]fbe29322013-07-09 09:03:26727 elif command == 'instrumentation':
[email protected]f7148dd42013-08-20 14:24:57728 return _RunInstrumentationTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26729 elif command == 'uiautomator':
[email protected]f7148dd42013-08-20 14:24:57730 return _RunUIAutomatorTests(options, option_parser.error, devices)
[email protected]3dbdfa42013-08-08 01:08:14731 elif command == 'monkey':
[email protected]f7148dd42013-08-20 14:24:57732 return _RunMonkeyTests(options, option_parser.error, devices)
[email protected]ec3170b2013-08-14 14:39:47733 elif command == 'perf':
[email protected]ad32f312013-11-13 04:03:29734 return _RunPerfTests(options, args, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26735 else:
[email protected]6bc1bda22013-07-19 22:08:37736 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26737
[email protected]fbe29322013-07-09 09:03:26738
[email protected]7c53a602014-03-24 16:21:44739def HelpCommand(command, _options, args, option_parser):
[email protected]fbe29322013-07-09 09:03:26740 """Display help for a certain command, or overall help.
741
742 Args:
743 command: String indicating the command that was received to trigger
744 this function.
[email protected]7c53a602014-03-24 16:21:44745 options: optparse options dictionary. unused.
[email protected]fbe29322013-07-09 09:03:26746 args: List of extra args from optparse.
747 option_parser: optparse.OptionParser object.
748
749 Returns:
750 Integer indicated exit code.
751 """
752 # If we don't have any args, display overall help
753 if len(args) < 3:
754 option_parser.print_help()
755 return 0
[email protected]d82f0252013-07-12 23:22:57756 # If we have too many args, print an error
757 if len(args) > 3:
758 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:])))
759 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:26760
761 command = args[2]
762
763 if command not in VALID_COMMANDS:
764 option_parser.error('Unrecognized command.')
765
766 # Treat the help command as a special case. We don't care about showing a
767 # specific help page for itself.
768 if command == 'help':
769 option_parser.print_help()
770 return 0
771
772 VALID_COMMANDS[command].add_options_func(option_parser)
773 option_parser.usage = '%prog ' + command + ' [options]'
[email protected]dfffbcbc2013-09-17 22:06:01774 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26775 option_parser.print_help()
776
777 return 0
778
779
780# Define a named tuple for the values in the VALID_COMMANDS dictionary so the
781# syntax is a bit prettier. The tuple is two functions: (add options, run
782# command).
783CommandFunctionTuple = collections.namedtuple(
784 'CommandFunctionTuple', ['add_options_func', 'run_command_func'])
785VALID_COMMANDS = {
786 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26787 'instrumentation': CommandFunctionTuple(
788 AddInstrumentationTestOptions, RunTestsCommand),
789 'uiautomator': CommandFunctionTuple(
790 AddUIAutomatorTestOptions, RunTestsCommand),
[email protected]3dbdfa42013-08-08 01:08:14791 'monkey': CommandFunctionTuple(
792 AddMonkeyTestOptions, RunTestsCommand),
[email protected]ec3170b2013-08-14 14:39:47793 'perf': CommandFunctionTuple(
794 AddPerfTestOptions, RunTestsCommand),
[email protected]6b6abac6d2013-10-03 11:56:38795 'linker': CommandFunctionTuple(
796 AddLinkerTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26797 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand)
798 }
799
800
[email protected]7c53a602014-03-24 16:21:44801def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:24802 for thread in threading.enumerate():
803 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:21804
805
[email protected]7c53a602014-03-24 16:21:44806def main():
[email protected]83bb8152013-11-19 15:02:21807 signal.signal(signal.SIGUSR1, DumpThreadStacks)
[email protected]803f65a72013-08-20 19:11:30808 option_parser = command_option_parser.CommandOptionParser(
809 commands_dict=VALID_COMMANDS)
810 return command_option_parser.ParseAndExecute(option_parser)
[email protected]fbe29322013-07-09 09:03:26811
[email protected]fbe29322013-07-09 09:03:26812
813if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:44814 sys.exit(main())