blob: e43e7219d96e28c4752a2ca27e6490c4c1de2bef [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]2eea4872014-07-28 23:06:1738from pylib.utils import apk_helper
[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
[email protected]71aec4b2013-11-20 00:35:2441from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3742from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2643
44
[email protected]fbe29322013-07-09 09:03:2645def AddCommonOptions(option_parser):
46 """Adds all common options to |option_parser|."""
47
[email protected]dfffbcbc2013-09-17 22:06:0148 group = optparse.OptionGroup(option_parser, 'Common Options')
49 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
50 group.add_option('--debug', action='store_const', const='Debug',
51 dest='build_type', default=default_build_type,
52 help=('If set, run test suites under out/Debug. '
53 'Default is env var BUILDTYPE or Debug.'))
54 group.add_option('--release', action='store_const',
55 const='Release', dest='build_type',
56 help=('If set, run test suites under out/Release.'
57 ' Default is env var BUILDTYPE or Debug.'))
58 group.add_option('-c', dest='cleanup_test_files',
59 help='Cleanup test files on the device after run',
60 action='store_true')
61 group.add_option('--num_retries', dest='num_retries', type='int',
62 default=2,
63 help=('Number of retries for a test before '
64 'giving up.'))
65 group.add_option('-v',
66 '--verbose',
67 dest='verbose_count',
68 default=0,
69 action='count',
70 help='Verbose level (multiple times for more)')
71 group.add_option('--tool',
72 dest='tool',
73 help=('Run the test under a tool '
74 '(use --tool help to list them)'))
75 group.add_option('--flakiness-dashboard-server',
76 dest='flakiness_dashboard_server',
77 help=('Address of the server that is hosting the '
78 'Chrome for Android flakiness dashboard.'))
79 group.add_option('--skip-deps-push', dest='push_deps',
80 action='store_false', default=True,
81 help=('Do not push dependencies to the device. '
82 'Use this at own risk for speeding up test '
83 'execution on local machine.'))
84 group.add_option('-d', '--device', dest='test_device',
85 help=('Target device for the test suite '
86 'to run on.'))
87 option_parser.add_option_group(group)
[email protected]fbe29322013-07-09 09:03:2688
89
90def ProcessCommonOptions(options):
91 """Processes and handles all common options."""
[email protected]fbe29322013-07-09 09:03:2692 run_tests_helper.SetLogLevel(options.verbose_count)
[email protected]14b3b1202013-08-15 22:25:2893 constants.SetBuildType(options.build_type)
[email protected]fbe29322013-07-09 09:03:2694
95
[email protected]fbe29322013-07-09 09:03:2696def AddGTestOptions(option_parser):
97 """Adds gtest options to |option_parser|."""
98
99 option_parser.usage = '%prog gtest [options]'
[email protected]dfffbcbc2013-09-17 22:06:01100 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26101 option_parser.example = '%prog gtest -s base_unittests'
102
[email protected]6bc1bda22013-07-19 22:08:37103 # TODO(gkanwar): Make this option required
104 option_parser.add_option('-s', '--suite', dest='suite_name',
[email protected]fbe29322013-07-09 09:03:26105 help=('Executable name of the test suite to run '
106 '(use -s help to list them).'))
[email protected]c53dc4332013-11-20 04:38:03107 option_parser.add_option('-f', '--gtest_filter', '--gtest-filter',
108 dest='test_filter',
[email protected]9e689252013-07-30 20:14:36109 help='googletest-style filter string.')
[email protected]c53dc4332013-11-20 04:38:03110 option_parser.add_option('--gtest_also_run_disabled_tests',
111 '--gtest-also-run-disabled-tests',
[email protected]dfffbcbc2013-09-17 22:06:01112 dest='run_disabled', action='store_true',
113 help='Also run disabled tests if applicable.')
114 option_parser.add_option('-a', '--test-arguments', dest='test_arguments',
115 default='',
[email protected]9e689252013-07-30 20:14:36116 help='Additional arguments to pass to the test.')
117 option_parser.add_option('-t', dest='timeout',
118 help='Timeout to wait for each test',
119 type='int',
120 default=60)
[email protected]5b8b8742014-05-22 08:18:50121 option_parser.add_option('--isolate_file_path',
122 '--isolate-file-path',
123 dest='isolate_file_path',
124 help='.isolate file path to override the default '
125 'path')
[email protected]fbe29322013-07-09 09:03:26126 # TODO(gkanwar): Move these to Common Options once we have the plumbing
127 # in our other test types to handle these commands
[email protected]fbe29322013-07-09 09:03:26128 AddCommonOptions(option_parser)
129
130
[email protected]6b6abac6d2013-10-03 11:56:38131def AddLinkerTestOptions(option_parser):
132 option_parser.usage = '%prog linker'
133 option_parser.commands_dict = {}
134 option_parser.example = '%prog linker'
135
[email protected]98c4feef2013-10-08 01:19:05136 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
137 help='googletest-style filter string.')
[email protected]6b6abac6d2013-10-03 11:56:38138 AddCommonOptions(option_parser)
139
140
[email protected]6bc1bda22013-07-19 22:08:37141def ProcessGTestOptions(options):
142 """Intercept test suite help to list test suites.
143
144 Args:
145 options: Command line options.
[email protected]6bc1bda22013-07-19 22:08:37146 """
147 if options.suite_name == 'help':
148 print 'Available test suites are:'
[email protected]9e689252013-07-30 20:14:36149 for test_suite in (gtest_config.STABLE_TEST_SUITES +
150 gtest_config.EXPERIMENTAL_TEST_SUITES):
151 print test_suite
[email protected]2a684222013-08-01 16:59:22152 sys.exit(0)
[email protected]6bc1bda22013-07-19 22:08:37153
154 # Convert to a list, assuming all test suites if nothing was specified.
155 # TODO(gkanwar): Require having a test suite
156 if options.suite_name:
157 options.suite_name = [options.suite_name]
158 else:
[email protected]9e689252013-07-30 20:14:36159 options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES]
[email protected]6bc1bda22013-07-19 22:08:37160
161
[email protected]fbe29322013-07-09 09:03:26162def AddJavaTestOptions(option_parser):
163 """Adds the Java test options to |option_parser|."""
164
[email protected]dfffbcbc2013-09-17 22:06:01165 option_parser.add_option('-f', '--test-filter', dest='test_filter',
[email protected]fbe29322013-07-09 09:03:26166 help=('Test filter (if not fully qualified, '
167 'will run all matches).'))
168 option_parser.add_option(
169 '-A', '--annotation', dest='annotation_str',
170 help=('Comma-separated list of annotations. Run only tests with any of '
171 'the given annotations. An annotation can be either a key or a '
172 'key-values pair. A test that has no annotation is considered '
173 '"SmallTest".'))
174 option_parser.add_option(
175 '-E', '--exclude-annotation', dest='exclude_annotation_str',
176 help=('Comma-separated list of annotations. Exclude tests with these '
177 'annotations.'))
jbudorickcbcc115d2014-09-18 17:50:59178 option_parser.add_option(
179 '--screenshot', dest='screenshot_failures', action='store_true',
180 help='Capture screenshots of test failures')
181 option_parser.add_option(
182 '--save-perf-json', action='store_true',
183 help='Saves the JSON file for each UI Perf test.')
184 option_parser.add_option(
185 '--official-build', action='store_true', help='Run official build tests.')
186 option_parser.add_option(
187 '--test_data', '--test-data', action='append', default=[],
188 help=('Each instance defines a directory of test data that should be '
189 'copied to the target(s) before running the tests. The argument '
190 'should be of the form <target>:<source>, <target> is relative to '
191 'the device data directory, and <source> is relative to the '
192 'chromium build directory.'))
[email protected]fbe29322013-07-09 09:03:26193
194
[email protected]7c53a602014-03-24 16:21:44195def ProcessJavaTestOptions(options):
[email protected]fbe29322013-07-09 09:03:26196 """Processes options/arguments and populates |options| with defaults."""
197
[email protected]fbe29322013-07-09 09:03:26198 if options.annotation_str:
199 options.annotations = options.annotation_str.split(',')
200 elif options.test_filter:
201 options.annotations = []
202 else:
[email protected]6bc1bda22013-07-19 22:08:37203 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
[email protected]4f777ca2014-08-08 01:45:59204 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26205
206 if options.exclude_annotation_str:
207 options.exclude_annotations = options.exclude_annotation_str.split(',')
208 else:
209 options.exclude_annotations = []
210
[email protected]fbe29322013-07-09 09:03:26211
212def AddInstrumentationTestOptions(option_parser):
213 """Adds Instrumentation test options to |option_parser|."""
214
215 option_parser.usage = '%prog instrumentation [options]'
[email protected]dfffbcbc2013-09-17 22:06:01216 option_parser.commands_dict = {}
[email protected]fb7ab5e82013-07-26 18:31:20217 option_parser.example = ('%prog instrumentation '
[email protected]efeb59e2014-03-12 01:31:26218 '--test-apk=ChromeShellTest')
[email protected]fbe29322013-07-09 09:03:26219
220 AddJavaTestOptions(option_parser)
221 AddCommonOptions(option_parser)
222
[email protected]dfffbcbc2013-09-17 22:06:01223 option_parser.add_option('-j', '--java-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13224 default=False, help='Run only the Java tests.')
[email protected]dfffbcbc2013-09-17 22:06:01225 option_parser.add_option('-p', '--python-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13226 default=False,
227 help='Run only the host-driven tests.')
[email protected]a69e85bc2013-08-16 18:07:26228 option_parser.add_option('--host-driven-root',
[email protected]37ee0c792013-08-06 19:10:13229 help='Root of the host-driven tests.')
[email protected]fbe29322013-07-09 09:03:26230 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
231 action='store_true',
232 help='Wait for debugger.')
[email protected]fbe29322013-07-09 09:03:26233 option_parser.add_option(
234 '--test-apk', dest='test_apk',
235 help=('The name of the apk containing the tests '
[email protected]ae68d4a2013-09-24 21:57:15236 '(without the .apk extension; e.g. "ContentShellTest").'))
[email protected]803f65a72013-08-20 19:11:30237 option_parser.add_option('--coverage-dir',
238 help=('Directory in which to place all generated '
239 'EMMA coverage files.'))
[email protected]4f777ca2014-08-08 01:45:59240 option_parser.add_option('--device-flags', dest='device_flags', default='',
241 help='The relative filepath to a file containing '
242 'command-line flags to set on the device')
[email protected]fbe29322013-07-09 09:03:26243
244
245def ProcessInstrumentationOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22246 """Processes options/arguments and populate |options| with defaults.
247
248 Args:
249 options: optparse.Options object.
250 error_func: Function to call with the error message in case of an error.
251
252 Returns:
253 An InstrumentationOptions named tuple which contains all options relevant to
254 instrumentation tests.
255 """
[email protected]fbe29322013-07-09 09:03:26256
[email protected]7c53a602014-03-24 16:21:44257 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26258
[email protected]37ee0c792013-08-06 19:10:13259 if options.java_only and options.python_only:
260 error_func('Options java_only (-j) and python_only (-p) '
261 'are mutually exclusive.')
262 options.run_java_tests = True
263 options.run_python_tests = True
264 if options.java_only:
265 options.run_python_tests = False
266 elif options.python_only:
267 options.run_java_tests = False
268
[email protected]67954f822013-08-14 18:09:08269 if not options.host_driven_root:
[email protected]37ee0c792013-08-06 19:10:13270 options.run_python_tests = False
271
[email protected]fbe29322013-07-09 09:03:26272 if not options.test_apk:
273 error_func('--test-apk must be specified.')
274
[email protected]ae68d4a2013-09-24 21:57:15275
[email protected]2eea4872014-07-28 23:06:17276 options.test_apk_path = os.path.join(
277 constants.GetOutDirectory(),
278 constants.SDK_BUILD_APKS_DIR,
279 '%s.apk' % options.test_apk)
[email protected]ae68d4a2013-09-24 21:57:15280 options.test_apk_jar_path = os.path.join(
281 constants.GetOutDirectory(),
282 constants.SDK_BUILD_TEST_JAVALIB_DIR,
283 '%s.jar' % options.test_apk)
[email protected]5e2f3f62014-06-23 12:31:46284 options.test_support_apk_path = '%sSupport%s' % (
[email protected]2eea4872014-07-28 23:06:17285 os.path.splitext(options.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46286
[email protected]2eea4872014-07-28 23:06:17287 options.test_runner = apk_helper.GetInstrumentationName(options.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46288
[email protected]2a684222013-08-01 16:59:22289 return instrumentation_test_options.InstrumentationOptions(
[email protected]2a684222013-08-01 16:59:22290 options.tool,
291 options.cleanup_test_files,
292 options.push_deps,
293 options.annotations,
294 options.exclude_annotations,
295 options.test_filter,
296 options.test_data,
297 options.save_perf_json,
298 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22299 options.wait_for_debugger,
[email protected]803f65a72013-08-20 19:11:30300 options.coverage_dir,
[email protected]2a684222013-08-01 16:59:22301 options.test_apk,
302 options.test_apk_path,
[email protected]5e2f3f62014-06-23 12:31:46303 options.test_apk_jar_path,
[email protected]65bd8fb2014-08-02 17:02:02304 options.test_runner,
[email protected]4f777ca2014-08-08 01:45:59305 options.test_support_apk_path,
306 options.device_flags
[email protected]5e2f3f62014-06-23 12:31:46307 )
[email protected]2a684222013-08-01 16:59:22308
[email protected]fbe29322013-07-09 09:03:26309
310def AddUIAutomatorTestOptions(option_parser):
311 """Adds UI Automator test options to |option_parser|."""
312
313 option_parser.usage = '%prog uiautomator [options]'
[email protected]dfffbcbc2013-09-17 22:06:01314 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26315 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26316 '%prog uiautomator --test-jar=chrome_shell_uiautomator_tests'
317 ' --package=chrome_shell')
[email protected]fbe29322013-07-09 09:03:26318 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30319 '--package',
320 help=('Package under test. Possible values: %s' %
321 constants.PACKAGE_INFO.keys()))
[email protected]fbe29322013-07-09 09:03:26322 option_parser.add_option(
323 '--test-jar', dest='test_jar',
324 help=('The name of the dexed jar containing the tests (without the '
325 '.dex.jar extension). Alternatively, this can be a full path '
326 'to the jar.'))
327
328 AddJavaTestOptions(option_parser)
329 AddCommonOptions(option_parser)
330
331
332def ProcessUIAutomatorOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22333 """Processes UIAutomator options/arguments.
334
335 Args:
336 options: optparse.Options object.
337 error_func: Function to call with the error message in case of an error.
338
339 Returns:
340 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14341 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22342 """
[email protected]fbe29322013-07-09 09:03:26343
[email protected]7c53a602014-03-24 16:21:44344 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26345
[email protected]a8886c8a92013-10-08 17:29:30346 if not options.package:
347 error_func('--package is required.')
348
349 if options.package not in constants.PACKAGE_INFO:
350 error_func('Invalid package.')
[email protected]fbe29322013-07-09 09:03:26351
352 if not options.test_jar:
353 error_func('--test-jar must be specified.')
354
355 if os.path.exists(options.test_jar):
356 # The dexed JAR is fully qualified, assume the info JAR lives along side.
357 options.uiautomator_jar = options.test_jar
358 else:
359 options.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15360 constants.GetOutDirectory(),
361 constants.SDK_BUILD_JAVALIB_DIR,
[email protected]fbe29322013-07-09 09:03:26362 '%s.dex.jar' % options.test_jar)
363 options.uiautomator_info_jar = (
364 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
365 '_java.jar')
366
[email protected]2a684222013-08-01 16:59:22367 return uiautomator_test_options.UIAutomatorOptions(
[email protected]2a684222013-08-01 16:59:22368 options.tool,
369 options.cleanup_test_files,
370 options.push_deps,
371 options.annotations,
372 options.exclude_annotations,
373 options.test_filter,
374 options.test_data,
375 options.save_perf_json,
376 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22377 options.uiautomator_jar,
378 options.uiautomator_info_jar,
[email protected]a8886c8a92013-10-08 17:29:30379 options.package)
[email protected]2a684222013-08-01 16:59:22380
[email protected]fbe29322013-07-09 09:03:26381
[email protected]3dbdfa42013-08-08 01:08:14382def AddMonkeyTestOptions(option_parser):
383 """Adds monkey test options to |option_parser|."""
[email protected]fb81b982013-08-09 00:07:12384
385 option_parser.usage = '%prog monkey [options]'
[email protected]dfffbcbc2013-09-17 22:06:01386 option_parser.commands_dict = {}
[email protected]fb81b982013-08-09 00:07:12387 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26388 '%prog monkey --package=chrome_shell')
[email protected]fb81b982013-08-09 00:07:12389
[email protected]3dbdfa42013-08-08 01:08:14390 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30391 '--package',
392 help=('Package under test. Possible values: %s' %
393 constants.PACKAGE_INFO.keys()))
[email protected]3dbdfa42013-08-08 01:08:14394 option_parser.add_option(
395 '--event-count', default=10000, type='int',
396 help='Number of events to generate [default: %default].')
397 option_parser.add_option(
398 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12399 help='A list of allowed categories.')
[email protected]3dbdfa42013-08-08 01:08:14400 option_parser.add_option(
401 '--throttle', default=100, type='int',
402 help='Delay between events (ms) [default: %default]. ')
403 option_parser.add_option(
404 '--seed', type='int',
405 help=('Seed value for pseudo-random generator. Same seed value generates '
406 'the same sequence of events. Seed is randomized by default.'))
407 option_parser.add_option(
408 '--extra-args', default='',
409 help=('String of other args to pass to the command verbatim '
410 '[default: "%default"].'))
411
412 AddCommonOptions(option_parser)
413
414
415def ProcessMonkeyTestOptions(options, error_func):
416 """Processes all monkey test options.
417
418 Args:
419 options: optparse.Options object.
420 error_func: Function to call with the error message in case of an error.
421
422 Returns:
423 A MonkeyOptions named tuple which contains all options relevant to
424 monkey tests.
425 """
[email protected]a8886c8a92013-10-08 17:29:30426 if not options.package:
427 error_func('--package is required.')
428
429 if options.package not in constants.PACKAGE_INFO:
430 error_func('Invalid package.')
[email protected]3dbdfa42013-08-08 01:08:14431
432 category = options.category
433 if category:
434 category = options.category.split(',')
435
436 return monkey_test_options.MonkeyOptions(
[email protected]3dbdfa42013-08-08 01:08:14437 options.verbose_count,
[email protected]a8886c8a92013-10-08 17:29:30438 options.package,
[email protected]3dbdfa42013-08-08 01:08:14439 options.event_count,
440 category,
441 options.throttle,
442 options.seed,
443 options.extra_args)
444
445
[email protected]ec3170b2013-08-14 14:39:47446def AddPerfTestOptions(option_parser):
447 """Adds perf test options to |option_parser|."""
448
449 option_parser.usage = '%prog perf [options]'
[email protected]dfffbcbc2013-09-17 22:06:01450 option_parser.commands_dict = {}
[email protected]def4bce2013-11-12 12:59:52451 option_parser.example = ('%prog perf '
[email protected]ad32f312013-11-13 04:03:29452 '[--single-step -- command args] or '
[email protected]def4bce2013-11-12 12:59:52453 '[--steps perf_steps.json] or '
[email protected]ad32f312013-11-13 04:03:29454 '[--print-step step]')
[email protected]ec3170b2013-08-14 14:39:47455
[email protected]181a5c92013-09-06 17:11:46456 option_parser.add_option(
[email protected]def4bce2013-11-12 12:59:52457 '--single-step',
[email protected]ad32f312013-11-13 04:03:29458 action='store_true',
[email protected]def4bce2013-11-12 12:59:52459 help='Execute the given command with retries, but only print the result '
460 'for the "most successful" round.')
461 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46462 '--steps',
[email protected]def4bce2013-11-12 12:59:52463 help='JSON file containing the list of commands to run.')
[email protected]181a5c92013-09-06 17:11:46464 option_parser.add_option(
465 '--flaky-steps',
466 help=('A JSON file containing steps that are flaky '
467 'and will have its exit code ignored.'))
468 option_parser.add_option(
[email protected]61487ed2014-06-09 12:33:56469 '--output-json-list',
470 help='Write a simple list of names from --steps into the given file.')
471 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46472 '--print-step',
473 help='The name of a previously executed perf step to print.')
474 option_parser.add_option(
475 '--no-timeout', action='store_true',
476 help=('Do not impose a timeout. Each perf step is responsible for '
477 'implementing the timeout logic.'))
[email protected]650487c2013-09-30 11:40:49478 option_parser.add_option(
479 '-f', '--test-filter',
480 help=('Test filter (will match against the names listed in --steps).'))
481 option_parser.add_option(
482 '--dry-run',
483 action='store_true',
484 help='Just print the steps without executing.')
[email protected]ec3170b2013-08-14 14:39:47485 AddCommonOptions(option_parser)
486
487
[email protected]ad32f312013-11-13 04:03:29488def ProcessPerfTestOptions(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47489 """Processes all perf test options.
490
491 Args:
492 options: optparse.Options object.
493 error_func: Function to call with the error message in case of an error.
494
495 Returns:
496 A PerfOptions named tuple which contains all options relevant to
497 perf tests.
498 """
[email protected]def4bce2013-11-12 12:59:52499 # Only one of steps, print_step or single_step must be provided.
500 count = len(filter(None,
501 [options.steps, options.print_step, options.single_step]))
502 if count != 1:
503 error_func('Please specify one of: --steps, --print-step, --single-step.')
[email protected]ad32f312013-11-13 04:03:29504 single_step = None
505 if options.single_step:
506 single_step = ' '.join(args[2:])
[email protected]ec3170b2013-08-14 14:39:47507 return perf_test_options.PerfOptions(
[email protected]61487ed2014-06-09 12:33:56508 options.steps, options.flaky_steps, options.output_json_list,
509 options.print_step, options.no_timeout, options.test_filter,
510 options.dry_run, single_step)
[email protected]ec3170b2013-08-14 14:39:47511
512
[email protected]7c53a602014-03-24 16:21:44513def _RunGTests(options, devices):
[email protected]6bc1bda22013-07-19 22:08:37514 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]2a684222013-08-01 16:59:22515 ProcessGTestOptions(options)
[email protected]6bc1bda22013-07-19 22:08:37516
517 exit_code = 0
518 for suite_name in options.suite_name:
[email protected]2a684222013-08-01 16:59:22519 # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for
520 # the gtest command.
521 gtest_options = gtest_test_options.GTestOptions(
[email protected]2a684222013-08-01 16:59:22522 options.tool,
523 options.cleanup_test_files,
524 options.push_deps,
525 options.test_filter,
[email protected]dfffbcbc2013-09-17 22:06:01526 options.run_disabled,
[email protected]2a684222013-08-01 16:59:22527 options.test_arguments,
528 options.timeout,
[email protected]5b8b8742014-05-22 08:18:50529 options.isolate_file_path,
[email protected]2a684222013-08-01 16:59:22530 suite_name)
[email protected]f7148dd42013-08-20 14:24:57531 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37532
533 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57534 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37535 num_retries=options.num_retries)
536
537 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
538 exit_code = test_exit_code
539
540 report_results.LogFull(
541 results=results,
542 test_type='Unit test',
543 test_package=suite_name,
[email protected]6bc1bda22013-07-19 22:08:37544 flakiness_server=options.flakiness_dashboard_server)
545
546 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
547 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
548
549 return exit_code
550
551
[email protected]7c53a602014-03-24 16:21:44552def _RunLinkerTests(options, devices):
[email protected]6b6abac6d2013-10-03 11:56:38553 """Subcommand of RunTestsCommands which runs linker tests."""
554 runner_factory, tests = linker_setup.Setup(options, devices)
555
556 results, exit_code = test_dispatcher.RunTests(
557 tests, runner_factory, devices, shard=True, test_timeout=60,
558 num_retries=options.num_retries)
559
560 report_results.LogFull(
561 results=results,
562 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22563 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38564
565 return exit_code
566
567
[email protected]f7148dd42013-08-20 14:24:57568def _RunInstrumentationTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37569 """Subcommand of RunTestsCommands which runs instrumentation tests."""
[email protected]2a684222013-08-01 16:59:22570 instrumentation_options = ProcessInstrumentationOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37571
[email protected]f7148dd42013-08-20 14:24:57572 if len(devices) > 1 and options.wait_for_debugger:
573 logging.warning('Debugger can not be sharded, using first available device')
574 devices = devices[:1]
575
[email protected]6bc1bda22013-07-19 22:08:37576 results = base_test_result.TestRunResults()
577 exit_code = 0
578
579 if options.run_java_tests:
[email protected]2a684222013-08-01 16:59:22580 runner_factory, tests = instrumentation_setup.Setup(instrumentation_options)
[email protected]6bc1bda22013-07-19 22:08:37581
582 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57583 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37584 num_retries=options.num_retries)
585
586 results.AddTestRunResults(test_results)
587
588 if options.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13589 runner_factory, tests = host_driven_setup.InstrumentationSetup(
[email protected]67954f822013-08-14 18:09:08590 options.host_driven_root, options.official_build,
[email protected]37ee0c792013-08-06 19:10:13591 instrumentation_options)
592
[email protected]34020022013-08-06 23:35:34593 if tests:
594 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57595 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]34020022013-08-06 23:35:34596 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37597
[email protected]34020022013-08-06 23:35:34598 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37599
[email protected]34020022013-08-06 23:35:34600 # Only allow exit code escalation
601 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
602 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37603
[email protected]4f777ca2014-08-08 01:45:59604 if options.device_flags:
605 options.device_flags = os.path.join(constants.DIR_SOURCE_ROOT,
606 options.device_flags)
607
[email protected]6bc1bda22013-07-19 22:08:37608 report_results.LogFull(
609 results=results,
610 test_type='Instrumentation',
611 test_package=os.path.basename(options.test_apk),
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 _RunUIAutomatorTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37619 """Subcommand of RunTestsCommands which runs uiautomator tests."""
[email protected]2a684222013-08-01 16:59:22620 uiautomator_options = ProcessUIAutomatorOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37621
[email protected]37ee0c792013-08-06 19:10:13622 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37623
[email protected]37ee0c792013-08-06 19:10:13624 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57625 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]37ee0c792013-08-06 19:10:13626 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37627
628 report_results.LogFull(
629 results=results,
630 test_type='UIAutomator',
631 test_package=os.path.basename(options.test_jar),
632 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37633 flakiness_server=options.flakiness_dashboard_server)
634
635 return exit_code
636
637
[email protected]f7148dd42013-08-20 14:24:57638def _RunMonkeyTests(options, error_func, devices):
[email protected]3dbdfa42013-08-08 01:08:14639 """Subcommand of RunTestsCommands which runs monkey tests."""
640 monkey_options = ProcessMonkeyTestOptions(options, error_func)
641
642 runner_factory, tests = monkey_setup.Setup(monkey_options)
643
644 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46645 tests, runner_factory, devices, shard=False, test_timeout=None,
646 num_retries=options.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14647
648 report_results.LogFull(
649 results=results,
650 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28651 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14652
653 return exit_code
654
655
[email protected]a72f0752014-06-03 23:52:34656def _RunPerfTests(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47657 """Subcommand of RunTestsCommands which runs perf tests."""
[email protected]ad32f312013-11-13 04:03:29658 perf_options = ProcessPerfTestOptions(options, args, error_func)
[email protected]61487ed2014-06-09 12:33:56659
660 # Just save a simple json with a list of test names.
661 if perf_options.output_json_list:
662 return perf_test_runner.OutputJsonList(
663 perf_options.steps, perf_options.output_json_list)
664
[email protected]ad32f312013-11-13 04:03:29665 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47666 if perf_options.print_step:
667 return perf_test_runner.PrintTestOutput(perf_options.print_step)
668
[email protected]a72f0752014-06-03 23:52:34669 runner_factory, tests, devices = perf_setup.Setup(perf_options)
[email protected]ec3170b2013-08-14 14:39:47670
[email protected]a72f0752014-06-03 23:52:34671 # shard=False means that each device will get the full list of tests
672 # and then each one will decide their own affinity.
673 # shard=True means each device will pop the next test available from a queue,
674 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57675 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34676 tests, runner_factory, devices, shard=False, test_timeout=None,
[email protected]181a5c92013-09-06 17:11:46677 num_retries=options.num_retries)
[email protected]ec3170b2013-08-14 14:39:47678
679 report_results.LogFull(
680 results=results,
681 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12682 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52683
684 if perf_options.single_step:
685 return perf_test_runner.PrintTestOutput('single_step')
686
[email protected]11ce8452014-02-17 10:55:03687 perf_test_runner.PrintSummary(tests)
688
[email protected]86184c7b2013-08-15 15:06:57689 # Always return 0 on the sharding stage. Individual tests exit_code
690 # will be returned on the print_step stage.
691 return 0
[email protected]ec3170b2013-08-14 14:39:47692
[email protected]3dbdfa42013-08-08 01:08:14693
[email protected]f7148dd42013-08-20 14:24:57694def _GetAttachedDevices(test_device=None):
695 """Get all attached devices.
696
697 Args:
698 test_device: Name of a specific device to use.
699
700 Returns:
701 A list of attached devices.
702 """
703 attached_devices = []
704
705 attached_devices = android_commands.GetAttachedDevices()
706 if test_device:
707 assert test_device in attached_devices, (
708 'Did not find device %s among attached device. Attached devices: %s'
709 % (test_device, ', '.join(attached_devices)))
710 attached_devices = [test_device]
711
712 assert attached_devices, 'No devices attached.'
713
714 return sorted(attached_devices)
715
716
[email protected]fbe29322013-07-09 09:03:26717def RunTestsCommand(command, options, args, option_parser):
718 """Checks test type and dispatches to the appropriate function.
719
720 Args:
721 command: String indicating the command that was received to trigger
722 this function.
723 options: optparse options dictionary.
724 args: List of extra args from optparse.
725 option_parser: optparse.OptionParser object.
726
727 Returns:
728 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10729
730 Raises:
731 Exception: Unknown command name passed in, or an exception from an
732 individual test runner.
[email protected]fbe29322013-07-09 09:03:26733 """
734
[email protected]d82f0252013-07-12 23:22:57735 # Check for extra arguments
[email protected]ad32f312013-11-13 04:03:29736 if len(args) > 2 and command != 'perf':
[email protected]d82f0252013-07-12 23:22:57737 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[2:])))
738 return constants.ERROR_EXIT_CODE
[email protected]ad32f312013-11-13 04:03:29739 if command == 'perf':
740 if ((options.single_step and len(args) <= 2) or
741 (not options.single_step and len(args) > 2)):
742 option_parser.error('Unrecognized arguments: %s' % (' '.join(args)))
743 return constants.ERROR_EXIT_CODE
[email protected]d82f0252013-07-12 23:22:57744
[email protected]fbe29322013-07-09 09:03:26745 ProcessCommonOptions(options)
746
[email protected]f7148dd42013-08-20 14:24:57747 devices = _GetAttachedDevices(options.test_device)
748
[email protected]c0662e092013-11-12 11:51:25749 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40750 if not ports.ResetTestServerPortAllocation():
751 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25752
[email protected]fbe29322013-07-09 09:03:26753 if command == 'gtest':
[email protected]7c53a602014-03-24 16:21:44754 return _RunGTests(options, devices)
[email protected]6b6abac6d2013-10-03 11:56:38755 elif command == 'linker':
[email protected]7c53a602014-03-24 16:21:44756 return _RunLinkerTests(options, devices)
[email protected]fbe29322013-07-09 09:03:26757 elif command == 'instrumentation':
[email protected]f7148dd42013-08-20 14:24:57758 return _RunInstrumentationTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26759 elif command == 'uiautomator':
[email protected]f7148dd42013-08-20 14:24:57760 return _RunUIAutomatorTests(options, option_parser.error, devices)
[email protected]3dbdfa42013-08-08 01:08:14761 elif command == 'monkey':
[email protected]f7148dd42013-08-20 14:24:57762 return _RunMonkeyTests(options, option_parser.error, devices)
[email protected]ec3170b2013-08-14 14:39:47763 elif command == 'perf':
[email protected]a72f0752014-06-03 23:52:34764 return _RunPerfTests(options, args, option_parser.error)
[email protected]fbe29322013-07-09 09:03:26765 else:
[email protected]6bc1bda22013-07-19 22:08:37766 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26767
[email protected]fbe29322013-07-09 09:03:26768
[email protected]7c53a602014-03-24 16:21:44769def HelpCommand(command, _options, args, option_parser):
[email protected]fbe29322013-07-09 09:03:26770 """Display help for a certain command, or overall help.
771
772 Args:
773 command: String indicating the command that was received to trigger
774 this function.
[email protected]7c53a602014-03-24 16:21:44775 options: optparse options dictionary. unused.
[email protected]fbe29322013-07-09 09:03:26776 args: List of extra args from optparse.
777 option_parser: optparse.OptionParser object.
778
779 Returns:
780 Integer indicated exit code.
781 """
782 # If we don't have any args, display overall help
783 if len(args) < 3:
784 option_parser.print_help()
785 return 0
[email protected]d82f0252013-07-12 23:22:57786 # If we have too many args, print an error
787 if len(args) > 3:
788 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:])))
789 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:26790
791 command = args[2]
792
793 if command not in VALID_COMMANDS:
794 option_parser.error('Unrecognized command.')
795
796 # Treat the help command as a special case. We don't care about showing a
797 # specific help page for itself.
798 if command == 'help':
799 option_parser.print_help()
800 return 0
801
802 VALID_COMMANDS[command].add_options_func(option_parser)
803 option_parser.usage = '%prog ' + command + ' [options]'
[email protected]dfffbcbc2013-09-17 22:06:01804 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26805 option_parser.print_help()
806
807 return 0
808
809
810# Define a named tuple for the values in the VALID_COMMANDS dictionary so the
811# syntax is a bit prettier. The tuple is two functions: (add options, run
812# command).
813CommandFunctionTuple = collections.namedtuple(
814 'CommandFunctionTuple', ['add_options_func', 'run_command_func'])
815VALID_COMMANDS = {
816 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26817 'instrumentation': CommandFunctionTuple(
818 AddInstrumentationTestOptions, RunTestsCommand),
819 'uiautomator': CommandFunctionTuple(
820 AddUIAutomatorTestOptions, RunTestsCommand),
[email protected]3dbdfa42013-08-08 01:08:14821 'monkey': CommandFunctionTuple(
822 AddMonkeyTestOptions, RunTestsCommand),
[email protected]ec3170b2013-08-14 14:39:47823 'perf': CommandFunctionTuple(
824 AddPerfTestOptions, RunTestsCommand),
[email protected]6b6abac6d2013-10-03 11:56:38825 'linker': CommandFunctionTuple(
826 AddLinkerTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26827 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand)
828 }
829
830
[email protected]7c53a602014-03-24 16:21:44831def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:24832 for thread in threading.enumerate():
833 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:21834
835
[email protected]7c53a602014-03-24 16:21:44836def main():
[email protected]83bb8152013-11-19 15:02:21837 signal.signal(signal.SIGUSR1, DumpThreadStacks)
[email protected]803f65a72013-08-20 19:11:30838 option_parser = command_option_parser.CommandOptionParser(
839 commands_dict=VALID_COMMANDS)
840 return command_option_parser.ParseAndExecute(option_parser)
[email protected]fbe29322013-07-09 09:03:26841
[email protected]fbe29322013-07-09 09:03:26842
843if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:44844 sys.exit(main())