blob: 92c97981a7edc8048c20e50a0a73a482ae8a3249 [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
jbudorick9a6b7b332014-09-20 00:01:0731from pylib.junit import setup as junit_setup
32from pylib.junit import test_dispatcher as junit_dispatcher
[email protected]3dbdfa42013-08-08 01:08:1433from pylib.monkey import setup as monkey_setup
34from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4735from pylib.perf import setup as perf_setup
36from pylib.perf import test_options as perf_test_options
37from pylib.perf import test_runner as perf_test_runner
[email protected]6bc1bda22013-07-19 22:08:3738from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2239from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]2eea4872014-07-28 23:06:1740from pylib.utils import apk_helper
[email protected]803f65a72013-08-20 19:11:3041from pylib.utils import command_option_parser
[email protected]6bc1bda22013-07-19 22:08:3742from pylib.utils import report_results
[email protected]71aec4b2013-11-20 00:35:2443from pylib.utils import reraiser_thread
[email protected]6bc1bda22013-07-19 22:08:3744from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2645
46
[email protected]fbe29322013-07-09 09:03:2647def AddCommonOptions(option_parser):
48 """Adds all common options to |option_parser|."""
49
[email protected]dfffbcbc2013-09-17 22:06:0150 group = optparse.OptionGroup(option_parser, 'Common Options')
51 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
52 group.add_option('--debug', action='store_const', const='Debug',
53 dest='build_type', default=default_build_type,
54 help=('If set, run test suites under out/Debug. '
55 'Default is env var BUILDTYPE or Debug.'))
56 group.add_option('--release', action='store_const',
57 const='Release', dest='build_type',
58 help=('If set, run test suites under out/Release.'
59 ' Default is env var BUILDTYPE or Debug.'))
r.kasibhatla3d57cba2014-10-09 10:55:5760 group.add_option('--build-directory', dest='build_directory',
61 help=('Path to the directory in which build files are'
62 ' located (should not include build type)'))
[email protected]dfffbcbc2013-09-17 22:06:0163 group.add_option('-c', dest='cleanup_test_files',
64 help='Cleanup test files on the device after run',
65 action='store_true')
66 group.add_option('--num_retries', dest='num_retries', type='int',
67 default=2,
68 help=('Number of retries for a test before '
69 'giving up.'))
70 group.add_option('-v',
71 '--verbose',
72 dest='verbose_count',
73 default=0,
74 action='count',
75 help='Verbose level (multiple times for more)')
76 group.add_option('--tool',
77 dest='tool',
78 help=('Run the test under a tool '
79 '(use --tool help to list them)'))
80 group.add_option('--flakiness-dashboard-server',
81 dest='flakiness_dashboard_server',
82 help=('Address of the server that is hosting the '
83 'Chrome for Android flakiness dashboard.'))
84 group.add_option('--skip-deps-push', dest='push_deps',
85 action='store_false', default=True,
86 help=('Do not push dependencies to the device. '
87 'Use this at own risk for speeding up test '
88 'execution on local machine.'))
89 group.add_option('-d', '--device', dest='test_device',
90 help=('Target device for the test suite '
91 'to run on.'))
92 option_parser.add_option_group(group)
[email protected]fbe29322013-07-09 09:03:2693
94
95def ProcessCommonOptions(options):
96 """Processes and handles all common options."""
[email protected]fbe29322013-07-09 09:03:2697 run_tests_helper.SetLogLevel(options.verbose_count)
[email protected]14b3b1202013-08-15 22:25:2898 constants.SetBuildType(options.build_type)
r.kasibhatla3d57cba2014-10-09 10:55:5799 if options.build_directory:
100 constants.SetBuildDirectory(options.build_directory)
[email protected]fbe29322013-07-09 09:03:26101
102
[email protected]fbe29322013-07-09 09:03:26103def AddGTestOptions(option_parser):
104 """Adds gtest options to |option_parser|."""
105
106 option_parser.usage = '%prog gtest [options]'
[email protected]dfffbcbc2013-09-17 22:06:01107 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26108 option_parser.example = '%prog gtest -s base_unittests'
109
[email protected]6bc1bda22013-07-19 22:08:37110 # TODO(gkanwar): Make this option required
111 option_parser.add_option('-s', '--suite', dest='suite_name',
[email protected]fbe29322013-07-09 09:03:26112 help=('Executable name of the test suite to run '
113 '(use -s help to list them).'))
[email protected]c53dc4332013-11-20 04:38:03114 option_parser.add_option('-f', '--gtest_filter', '--gtest-filter',
115 dest='test_filter',
[email protected]9e689252013-07-30 20:14:36116 help='googletest-style filter string.')
[email protected]c53dc4332013-11-20 04:38:03117 option_parser.add_option('--gtest_also_run_disabled_tests',
118 '--gtest-also-run-disabled-tests',
[email protected]dfffbcbc2013-09-17 22:06:01119 dest='run_disabled', action='store_true',
120 help='Also run disabled tests if applicable.')
121 option_parser.add_option('-a', '--test-arguments', dest='test_arguments',
122 default='',
[email protected]9e689252013-07-30 20:14:36123 help='Additional arguments to pass to the test.')
124 option_parser.add_option('-t', dest='timeout',
125 help='Timeout to wait for each test',
126 type='int',
127 default=60)
[email protected]5b8b8742014-05-22 08:18:50128 option_parser.add_option('--isolate_file_path',
129 '--isolate-file-path',
130 dest='isolate_file_path',
131 help='.isolate file path to override the default '
132 'path')
[email protected]fbe29322013-07-09 09:03:26133 # TODO(gkanwar): Move these to Common Options once we have the plumbing
134 # in our other test types to handle these commands
[email protected]fbe29322013-07-09 09:03:26135 AddCommonOptions(option_parser)
136
137
[email protected]6b6abac6d2013-10-03 11:56:38138def AddLinkerTestOptions(option_parser):
139 option_parser.usage = '%prog linker'
140 option_parser.commands_dict = {}
141 option_parser.example = '%prog linker'
142
[email protected]98c4feef2013-10-08 01:19:05143 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
144 help='googletest-style filter string.')
[email protected]6b6abac6d2013-10-03 11:56:38145 AddCommonOptions(option_parser)
146
147
[email protected]6bc1bda22013-07-19 22:08:37148def ProcessGTestOptions(options):
149 """Intercept test suite help to list test suites.
150
151 Args:
152 options: Command line options.
[email protected]6bc1bda22013-07-19 22:08:37153 """
154 if options.suite_name == 'help':
155 print 'Available test suites are:'
[email protected]9e689252013-07-30 20:14:36156 for test_suite in (gtest_config.STABLE_TEST_SUITES +
157 gtest_config.EXPERIMENTAL_TEST_SUITES):
158 print test_suite
[email protected]2a684222013-08-01 16:59:22159 sys.exit(0)
[email protected]6bc1bda22013-07-19 22:08:37160
161 # Convert to a list, assuming all test suites if nothing was specified.
162 # TODO(gkanwar): Require having a test suite
163 if options.suite_name:
164 options.suite_name = [options.suite_name]
165 else:
[email protected]9e689252013-07-30 20:14:36166 options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES]
[email protected]6bc1bda22013-07-19 22:08:37167
168
[email protected]fbe29322013-07-09 09:03:26169def AddJavaTestOptions(option_parser):
170 """Adds the Java test options to |option_parser|."""
171
[email protected]dfffbcbc2013-09-17 22:06:01172 option_parser.add_option('-f', '--test-filter', dest='test_filter',
[email protected]fbe29322013-07-09 09:03:26173 help=('Test filter (if not fully qualified, '
174 'will run all matches).'))
175 option_parser.add_option(
176 '-A', '--annotation', dest='annotation_str',
177 help=('Comma-separated list of annotations. Run only tests with any of '
178 'the given annotations. An annotation can be either a key or a '
179 'key-values pair. A test that has no annotation is considered '
180 '"SmallTest".'))
181 option_parser.add_option(
182 '-E', '--exclude-annotation', dest='exclude_annotation_str',
183 help=('Comma-separated list of annotations. Exclude tests with these '
184 'annotations.'))
jbudorickcbcc115d2014-09-18 17:50:59185 option_parser.add_option(
186 '--screenshot', dest='screenshot_failures', action='store_true',
187 help='Capture screenshots of test failures')
188 option_parser.add_option(
189 '--save-perf-json', action='store_true',
190 help='Saves the JSON file for each UI Perf test.')
191 option_parser.add_option(
192 '--official-build', action='store_true', help='Run official build tests.')
193 option_parser.add_option(
194 '--test_data', '--test-data', action='append', default=[],
195 help=('Each instance defines a directory of test data that should be '
196 'copied to the target(s) before running the tests. The argument '
197 'should be of the form <target>:<source>, <target> is relative to '
198 'the device data directory, and <source> is relative to the '
199 'chromium build directory.'))
[email protected]fbe29322013-07-09 09:03:26200
201
[email protected]7c53a602014-03-24 16:21:44202def ProcessJavaTestOptions(options):
[email protected]fbe29322013-07-09 09:03:26203 """Processes options/arguments and populates |options| with defaults."""
204
[email protected]fbe29322013-07-09 09:03:26205 if options.annotation_str:
206 options.annotations = options.annotation_str.split(',')
207 elif options.test_filter:
208 options.annotations = []
209 else:
[email protected]6bc1bda22013-07-19 22:08:37210 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
[email protected]4f777ca2014-08-08 01:45:59211 'EnormousTest', 'IntegrationTest']
[email protected]fbe29322013-07-09 09:03:26212
213 if options.exclude_annotation_str:
214 options.exclude_annotations = options.exclude_annotation_str.split(',')
215 else:
216 options.exclude_annotations = []
217
[email protected]fbe29322013-07-09 09:03:26218
219def AddInstrumentationTestOptions(option_parser):
220 """Adds Instrumentation test options to |option_parser|."""
221
222 option_parser.usage = '%prog instrumentation [options]'
[email protected]dfffbcbc2013-09-17 22:06:01223 option_parser.commands_dict = {}
[email protected]fb7ab5e82013-07-26 18:31:20224 option_parser.example = ('%prog instrumentation '
[email protected]efeb59e2014-03-12 01:31:26225 '--test-apk=ChromeShellTest')
[email protected]fbe29322013-07-09 09:03:26226
227 AddJavaTestOptions(option_parser)
228 AddCommonOptions(option_parser)
229
[email protected]dfffbcbc2013-09-17 22:06:01230 option_parser.add_option('-j', '--java-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13231 default=False, help='Run only the Java tests.')
[email protected]dfffbcbc2013-09-17 22:06:01232 option_parser.add_option('-p', '--python-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13233 default=False,
234 help='Run only the host-driven tests.')
[email protected]a69e85bc2013-08-16 18:07:26235 option_parser.add_option('--host-driven-root',
[email protected]37ee0c792013-08-06 19:10:13236 help='Root of the host-driven tests.')
[email protected]fbe29322013-07-09 09:03:26237 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
238 action='store_true',
239 help='Wait for debugger.')
[email protected]fbe29322013-07-09 09:03:26240 option_parser.add_option(
241 '--test-apk', dest='test_apk',
242 help=('The name of the apk containing the tests '
[email protected]ae68d4a2013-09-24 21:57:15243 '(without the .apk extension; e.g. "ContentShellTest").'))
[email protected]803f65a72013-08-20 19:11:30244 option_parser.add_option('--coverage-dir',
245 help=('Directory in which to place all generated '
246 'EMMA coverage files.'))
[email protected]4f777ca2014-08-08 01:45:59247 option_parser.add_option('--device-flags', dest='device_flags', default='',
248 help='The relative filepath to a file containing '
249 'command-line flags to set on the device')
[email protected]fbe29322013-07-09 09:03:26250
251
252def ProcessInstrumentationOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22253 """Processes options/arguments and populate |options| with defaults.
254
255 Args:
256 options: optparse.Options object.
257 error_func: Function to call with the error message in case of an error.
258
259 Returns:
260 An InstrumentationOptions named tuple which contains all options relevant to
261 instrumentation tests.
262 """
[email protected]fbe29322013-07-09 09:03:26263
[email protected]7c53a602014-03-24 16:21:44264 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26265
[email protected]37ee0c792013-08-06 19:10:13266 if options.java_only and options.python_only:
267 error_func('Options java_only (-j) and python_only (-p) '
268 'are mutually exclusive.')
269 options.run_java_tests = True
270 options.run_python_tests = True
271 if options.java_only:
272 options.run_python_tests = False
273 elif options.python_only:
274 options.run_java_tests = False
275
[email protected]67954f822013-08-14 18:09:08276 if not options.host_driven_root:
[email protected]37ee0c792013-08-06 19:10:13277 options.run_python_tests = False
278
[email protected]fbe29322013-07-09 09:03:26279 if not options.test_apk:
280 error_func('--test-apk must be specified.')
281
[email protected]ae68d4a2013-09-24 21:57:15282
[email protected]2eea4872014-07-28 23:06:17283 options.test_apk_path = os.path.join(
284 constants.GetOutDirectory(),
285 constants.SDK_BUILD_APKS_DIR,
286 '%s.apk' % options.test_apk)
[email protected]ae68d4a2013-09-24 21:57:15287 options.test_apk_jar_path = os.path.join(
288 constants.GetOutDirectory(),
289 constants.SDK_BUILD_TEST_JAVALIB_DIR,
290 '%s.jar' % options.test_apk)
[email protected]5e2f3f62014-06-23 12:31:46291 options.test_support_apk_path = '%sSupport%s' % (
[email protected]2eea4872014-07-28 23:06:17292 os.path.splitext(options.test_apk_path))
[email protected]5e2f3f62014-06-23 12:31:46293
[email protected]2eea4872014-07-28 23:06:17294 options.test_runner = apk_helper.GetInstrumentationName(options.test_apk_path)
[email protected]5e2f3f62014-06-23 12:31:46295
[email protected]2a684222013-08-01 16:59:22296 return instrumentation_test_options.InstrumentationOptions(
[email protected]2a684222013-08-01 16:59:22297 options.tool,
298 options.cleanup_test_files,
299 options.push_deps,
300 options.annotations,
301 options.exclude_annotations,
302 options.test_filter,
303 options.test_data,
304 options.save_perf_json,
305 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22306 options.wait_for_debugger,
[email protected]803f65a72013-08-20 19:11:30307 options.coverage_dir,
[email protected]2a684222013-08-01 16:59:22308 options.test_apk,
309 options.test_apk_path,
[email protected]5e2f3f62014-06-23 12:31:46310 options.test_apk_jar_path,
[email protected]65bd8fb2014-08-02 17:02:02311 options.test_runner,
[email protected]4f777ca2014-08-08 01:45:59312 options.test_support_apk_path,
313 options.device_flags
[email protected]5e2f3f62014-06-23 12:31:46314 )
[email protected]2a684222013-08-01 16:59:22315
[email protected]fbe29322013-07-09 09:03:26316
317def AddUIAutomatorTestOptions(option_parser):
318 """Adds UI Automator test options to |option_parser|."""
319
320 option_parser.usage = '%prog uiautomator [options]'
[email protected]dfffbcbc2013-09-17 22:06:01321 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26322 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26323 '%prog uiautomator --test-jar=chrome_shell_uiautomator_tests'
324 ' --package=chrome_shell')
[email protected]fbe29322013-07-09 09:03:26325 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30326 '--package',
327 help=('Package under test. Possible values: %s' %
328 constants.PACKAGE_INFO.keys()))
[email protected]fbe29322013-07-09 09:03:26329 option_parser.add_option(
330 '--test-jar', dest='test_jar',
331 help=('The name of the dexed jar containing the tests (without the '
332 '.dex.jar extension). Alternatively, this can be a full path '
333 'to the jar.'))
334
335 AddJavaTestOptions(option_parser)
336 AddCommonOptions(option_parser)
337
338
339def ProcessUIAutomatorOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22340 """Processes UIAutomator options/arguments.
341
342 Args:
343 options: optparse.Options object.
344 error_func: Function to call with the error message in case of an error.
345
346 Returns:
347 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14348 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22349 """
[email protected]fbe29322013-07-09 09:03:26350
[email protected]7c53a602014-03-24 16:21:44351 ProcessJavaTestOptions(options)
[email protected]fbe29322013-07-09 09:03:26352
[email protected]a8886c8a92013-10-08 17:29:30353 if not options.package:
354 error_func('--package is required.')
355
356 if options.package not in constants.PACKAGE_INFO:
357 error_func('Invalid package.')
[email protected]fbe29322013-07-09 09:03:26358
359 if not options.test_jar:
360 error_func('--test-jar must be specified.')
361
362 if os.path.exists(options.test_jar):
363 # The dexed JAR is fully qualified, assume the info JAR lives along side.
364 options.uiautomator_jar = options.test_jar
365 else:
366 options.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15367 constants.GetOutDirectory(),
368 constants.SDK_BUILD_JAVALIB_DIR,
[email protected]fbe29322013-07-09 09:03:26369 '%s.dex.jar' % options.test_jar)
370 options.uiautomator_info_jar = (
371 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
372 '_java.jar')
373
[email protected]2a684222013-08-01 16:59:22374 return uiautomator_test_options.UIAutomatorOptions(
[email protected]2a684222013-08-01 16:59:22375 options.tool,
376 options.cleanup_test_files,
377 options.push_deps,
378 options.annotations,
379 options.exclude_annotations,
380 options.test_filter,
381 options.test_data,
382 options.save_perf_json,
383 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22384 options.uiautomator_jar,
385 options.uiautomator_info_jar,
[email protected]a8886c8a92013-10-08 17:29:30386 options.package)
[email protected]2a684222013-08-01 16:59:22387
[email protected]fbe29322013-07-09 09:03:26388
jbudorick9a6b7b332014-09-20 00:01:07389def AddJUnitTestOptions(option_parser):
390 """Adds junit test options to |option_parser|."""
391 option_parser.usage = '%prog junit -s [test suite name]'
392 option_parser.commands_dict = {}
393
394 option_parser.add_option(
395 '-s', '--test-suite', dest='test_suite',
396 help=('JUnit test suite to run.'))
397 option_parser.add_option(
398 '-f', '--test-filter', dest='test_filter',
399 help='Filters tests googletest-style.')
400 option_parser.add_option(
401 '--package-filter', dest='package_filter',
402 help='Filters tests by package.')
403 option_parser.add_option(
404 '--runner-filter', dest='runner_filter',
405 help='Filters tests by runner class. Must be fully qualified.')
406 option_parser.add_option(
407 '--sdk-version', dest='sdk_version', type="int",
408 help='The Android SDK version.')
409 AddCommonOptions(option_parser)
410
411
412def ProcessJUnitTestOptions(options, error_func):
413 """Processes all JUnit test options."""
414 if not options.test_suite:
415 error_func('No test suite specified.')
416 return options
417
418
[email protected]3dbdfa42013-08-08 01:08:14419def AddMonkeyTestOptions(option_parser):
420 """Adds monkey test options to |option_parser|."""
[email protected]fb81b982013-08-09 00:07:12421
422 option_parser.usage = '%prog monkey [options]'
[email protected]dfffbcbc2013-09-17 22:06:01423 option_parser.commands_dict = {}
[email protected]fb81b982013-08-09 00:07:12424 option_parser.example = (
[email protected]efeb59e2014-03-12 01:31:26425 '%prog monkey --package=chrome_shell')
[email protected]fb81b982013-08-09 00:07:12426
[email protected]3dbdfa42013-08-08 01:08:14427 option_parser.add_option(
[email protected]a8886c8a92013-10-08 17:29:30428 '--package',
429 help=('Package under test. Possible values: %s' %
430 constants.PACKAGE_INFO.keys()))
[email protected]3dbdfa42013-08-08 01:08:14431 option_parser.add_option(
432 '--event-count', default=10000, type='int',
433 help='Number of events to generate [default: %default].')
434 option_parser.add_option(
435 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12436 help='A list of allowed categories.')
[email protected]3dbdfa42013-08-08 01:08:14437 option_parser.add_option(
438 '--throttle', default=100, type='int',
439 help='Delay between events (ms) [default: %default]. ')
440 option_parser.add_option(
441 '--seed', type='int',
442 help=('Seed value for pseudo-random generator. Same seed value generates '
443 'the same sequence of events. Seed is randomized by default.'))
444 option_parser.add_option(
445 '--extra-args', default='',
446 help=('String of other args to pass to the command verbatim '
447 '[default: "%default"].'))
448
449 AddCommonOptions(option_parser)
450
451
452def ProcessMonkeyTestOptions(options, error_func):
453 """Processes all monkey test options.
454
455 Args:
456 options: optparse.Options object.
457 error_func: Function to call with the error message in case of an error.
458
459 Returns:
460 A MonkeyOptions named tuple which contains all options relevant to
461 monkey tests.
462 """
[email protected]a8886c8a92013-10-08 17:29:30463 if not options.package:
464 error_func('--package is required.')
465
466 if options.package not in constants.PACKAGE_INFO:
467 error_func('Invalid package.')
[email protected]3dbdfa42013-08-08 01:08:14468
469 category = options.category
470 if category:
471 category = options.category.split(',')
472
473 return monkey_test_options.MonkeyOptions(
[email protected]3dbdfa42013-08-08 01:08:14474 options.verbose_count,
[email protected]a8886c8a92013-10-08 17:29:30475 options.package,
[email protected]3dbdfa42013-08-08 01:08:14476 options.event_count,
477 category,
478 options.throttle,
479 options.seed,
480 options.extra_args)
481
482
[email protected]ec3170b2013-08-14 14:39:47483def AddPerfTestOptions(option_parser):
484 """Adds perf test options to |option_parser|."""
485
486 option_parser.usage = '%prog perf [options]'
[email protected]dfffbcbc2013-09-17 22:06:01487 option_parser.commands_dict = {}
[email protected]def4bce2013-11-12 12:59:52488 option_parser.example = ('%prog perf '
[email protected]ad32f312013-11-13 04:03:29489 '[--single-step -- command args] or '
[email protected]def4bce2013-11-12 12:59:52490 '[--steps perf_steps.json] or '
[email protected]ad32f312013-11-13 04:03:29491 '[--print-step step]')
[email protected]ec3170b2013-08-14 14:39:47492
[email protected]181a5c92013-09-06 17:11:46493 option_parser.add_option(
[email protected]def4bce2013-11-12 12:59:52494 '--single-step',
[email protected]ad32f312013-11-13 04:03:29495 action='store_true',
[email protected]def4bce2013-11-12 12:59:52496 help='Execute the given command with retries, but only print the result '
497 'for the "most successful" round.')
498 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46499 '--steps',
[email protected]def4bce2013-11-12 12:59:52500 help='JSON file containing the list of commands to run.')
[email protected]181a5c92013-09-06 17:11:46501 option_parser.add_option(
502 '--flaky-steps',
503 help=('A JSON file containing steps that are flaky '
504 'and will have its exit code ignored.'))
505 option_parser.add_option(
[email protected]61487ed2014-06-09 12:33:56506 '--output-json-list',
507 help='Write a simple list of names from --steps into the given file.')
508 option_parser.add_option(
[email protected]181a5c92013-09-06 17:11:46509 '--print-step',
510 help='The name of a previously executed perf step to print.')
511 option_parser.add_option(
512 '--no-timeout', action='store_true',
513 help=('Do not impose a timeout. Each perf step is responsible for '
514 'implementing the timeout logic.'))
[email protected]650487c2013-09-30 11:40:49515 option_parser.add_option(
516 '-f', '--test-filter',
517 help=('Test filter (will match against the names listed in --steps).'))
518 option_parser.add_option(
519 '--dry-run',
520 action='store_true',
521 help='Just print the steps without executing.')
[email protected]ec3170b2013-08-14 14:39:47522 AddCommonOptions(option_parser)
523
524
[email protected]ad32f312013-11-13 04:03:29525def ProcessPerfTestOptions(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47526 """Processes all perf test options.
527
528 Args:
529 options: optparse.Options object.
530 error_func: Function to call with the error message in case of an error.
531
532 Returns:
533 A PerfOptions named tuple which contains all options relevant to
534 perf tests.
535 """
[email protected]def4bce2013-11-12 12:59:52536 # Only one of steps, print_step or single_step must be provided.
537 count = len(filter(None,
538 [options.steps, options.print_step, options.single_step]))
539 if count != 1:
540 error_func('Please specify one of: --steps, --print-step, --single-step.')
[email protected]ad32f312013-11-13 04:03:29541 single_step = None
542 if options.single_step:
543 single_step = ' '.join(args[2:])
[email protected]ec3170b2013-08-14 14:39:47544 return perf_test_options.PerfOptions(
[email protected]61487ed2014-06-09 12:33:56545 options.steps, options.flaky_steps, options.output_json_list,
546 options.print_step, options.no_timeout, options.test_filter,
547 options.dry_run, single_step)
[email protected]ec3170b2013-08-14 14:39:47548
549
[email protected]7c53a602014-03-24 16:21:44550def _RunGTests(options, devices):
[email protected]6bc1bda22013-07-19 22:08:37551 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]2a684222013-08-01 16:59:22552 ProcessGTestOptions(options)
[email protected]6bc1bda22013-07-19 22:08:37553
554 exit_code = 0
555 for suite_name in options.suite_name:
[email protected]2a684222013-08-01 16:59:22556 # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for
557 # the gtest command.
558 gtest_options = gtest_test_options.GTestOptions(
[email protected]2a684222013-08-01 16:59:22559 options.tool,
560 options.cleanup_test_files,
561 options.push_deps,
562 options.test_filter,
[email protected]dfffbcbc2013-09-17 22:06:01563 options.run_disabled,
[email protected]2a684222013-08-01 16:59:22564 options.test_arguments,
565 options.timeout,
[email protected]5b8b8742014-05-22 08:18:50566 options.isolate_file_path,
[email protected]2a684222013-08-01 16:59:22567 suite_name)
[email protected]f7148dd42013-08-20 14:24:57568 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37569
570 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57571 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37572 num_retries=options.num_retries)
573
574 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
575 exit_code = test_exit_code
576
577 report_results.LogFull(
578 results=results,
579 test_type='Unit test',
580 test_package=suite_name,
[email protected]6bc1bda22013-07-19 22:08:37581 flakiness_server=options.flakiness_dashboard_server)
582
583 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
584 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
585
586 return exit_code
587
588
[email protected]7c53a602014-03-24 16:21:44589def _RunLinkerTests(options, devices):
[email protected]6b6abac6d2013-10-03 11:56:38590 """Subcommand of RunTestsCommands which runs linker tests."""
591 runner_factory, tests = linker_setup.Setup(options, devices)
592
593 results, exit_code = test_dispatcher.RunTests(
594 tests, runner_factory, devices, shard=True, test_timeout=60,
595 num_retries=options.num_retries)
596
597 report_results.LogFull(
598 results=results,
599 test_type='Linker test',
[email protected]93c9f9b2014-02-10 16:19:22600 test_package='ChromiumLinkerTest')
[email protected]6b6abac6d2013-10-03 11:56:38601
602 return exit_code
603
604
[email protected]f7148dd42013-08-20 14:24:57605def _RunInstrumentationTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37606 """Subcommand of RunTestsCommands which runs instrumentation tests."""
[email protected]2a684222013-08-01 16:59:22607 instrumentation_options = ProcessInstrumentationOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37608
[email protected]f7148dd42013-08-20 14:24:57609 if len(devices) > 1 and options.wait_for_debugger:
610 logging.warning('Debugger can not be sharded, using first available device')
611 devices = devices[:1]
612
[email protected]6bc1bda22013-07-19 22:08:37613 results = base_test_result.TestRunResults()
614 exit_code = 0
615
616 if options.run_java_tests:
[email protected]2a684222013-08-01 16:59:22617 runner_factory, tests = instrumentation_setup.Setup(instrumentation_options)
[email protected]6bc1bda22013-07-19 22:08:37618
619 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57620 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37621 num_retries=options.num_retries)
622
623 results.AddTestRunResults(test_results)
624
625 if options.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13626 runner_factory, tests = host_driven_setup.InstrumentationSetup(
[email protected]67954f822013-08-14 18:09:08627 options.host_driven_root, options.official_build,
[email protected]37ee0c792013-08-06 19:10:13628 instrumentation_options)
629
[email protected]34020022013-08-06 23:35:34630 if tests:
631 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57632 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]34020022013-08-06 23:35:34633 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37634
[email protected]34020022013-08-06 23:35:34635 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37636
[email protected]34020022013-08-06 23:35:34637 # Only allow exit code escalation
638 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
639 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37640
[email protected]4f777ca2014-08-08 01:45:59641 if options.device_flags:
642 options.device_flags = os.path.join(constants.DIR_SOURCE_ROOT,
643 options.device_flags)
644
[email protected]6bc1bda22013-07-19 22:08:37645 report_results.LogFull(
646 results=results,
647 test_type='Instrumentation',
648 test_package=os.path.basename(options.test_apk),
649 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37650 flakiness_server=options.flakiness_dashboard_server)
651
652 return exit_code
653
654
[email protected]f7148dd42013-08-20 14:24:57655def _RunUIAutomatorTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37656 """Subcommand of RunTestsCommands which runs uiautomator tests."""
[email protected]2a684222013-08-01 16:59:22657 uiautomator_options = ProcessUIAutomatorOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37658
[email protected]37ee0c792013-08-06 19:10:13659 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37660
[email protected]37ee0c792013-08-06 19:10:13661 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57662 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]37ee0c792013-08-06 19:10:13663 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37664
665 report_results.LogFull(
666 results=results,
667 test_type='UIAutomator',
668 test_package=os.path.basename(options.test_jar),
669 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37670 flakiness_server=options.flakiness_dashboard_server)
671
672 return exit_code
673
674
jbudorick9a6b7b332014-09-20 00:01:07675def _RunJUnitTests(options, error_func):
676 """Subcommand of RunTestsCommand which runs junit tests."""
677 junit_options = ProcessJUnitTestOptions(options, error_func)
678 runner_factory, tests = junit_setup.Setup(junit_options)
679 _, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
680
681 return exit_code
682
683
[email protected]f7148dd42013-08-20 14:24:57684def _RunMonkeyTests(options, error_func, devices):
[email protected]3dbdfa42013-08-08 01:08:14685 """Subcommand of RunTestsCommands which runs monkey tests."""
686 monkey_options = ProcessMonkeyTestOptions(options, error_func)
687
688 runner_factory, tests = monkey_setup.Setup(monkey_options)
689
690 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46691 tests, runner_factory, devices, shard=False, test_timeout=None,
692 num_retries=options.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14693
694 report_results.LogFull(
695 results=results,
696 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28697 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14698
699 return exit_code
700
701
[email protected]a72f0752014-06-03 23:52:34702def _RunPerfTests(options, args, error_func):
[email protected]ec3170b2013-08-14 14:39:47703 """Subcommand of RunTestsCommands which runs perf tests."""
[email protected]ad32f312013-11-13 04:03:29704 perf_options = ProcessPerfTestOptions(options, args, error_func)
[email protected]61487ed2014-06-09 12:33:56705
706 # Just save a simple json with a list of test names.
707 if perf_options.output_json_list:
708 return perf_test_runner.OutputJsonList(
709 perf_options.steps, perf_options.output_json_list)
710
[email protected]ad32f312013-11-13 04:03:29711 # Just print the results from a single previously executed step.
[email protected]ec3170b2013-08-14 14:39:47712 if perf_options.print_step:
713 return perf_test_runner.PrintTestOutput(perf_options.print_step)
714
[email protected]a72f0752014-06-03 23:52:34715 runner_factory, tests, devices = perf_setup.Setup(perf_options)
[email protected]ec3170b2013-08-14 14:39:47716
[email protected]a72f0752014-06-03 23:52:34717 # shard=False means that each device will get the full list of tests
718 # and then each one will decide their own affinity.
719 # shard=True means each device will pop the next test available from a queue,
720 # which increases throughput but have no affinity.
[email protected]86184c7b2013-08-15 15:06:57721 results, _ = test_dispatcher.RunTests(
[email protected]a72f0752014-06-03 23:52:34722 tests, runner_factory, devices, shard=False, test_timeout=None,
[email protected]181a5c92013-09-06 17:11:46723 num_retries=options.num_retries)
[email protected]ec3170b2013-08-14 14:39:47724
725 report_results.LogFull(
726 results=results,
727 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12728 test_package='Perf')
[email protected]def4bce2013-11-12 12:59:52729
730 if perf_options.single_step:
731 return perf_test_runner.PrintTestOutput('single_step')
732
[email protected]11ce8452014-02-17 10:55:03733 perf_test_runner.PrintSummary(tests)
734
[email protected]86184c7b2013-08-15 15:06:57735 # Always return 0 on the sharding stage. Individual tests exit_code
736 # will be returned on the print_step stage.
737 return 0
[email protected]ec3170b2013-08-14 14:39:47738
[email protected]3dbdfa42013-08-08 01:08:14739
[email protected]f7148dd42013-08-20 14:24:57740def _GetAttachedDevices(test_device=None):
741 """Get all attached devices.
742
743 Args:
744 test_device: Name of a specific device to use.
745
746 Returns:
747 A list of attached devices.
748 """
749 attached_devices = []
750
751 attached_devices = android_commands.GetAttachedDevices()
752 if test_device:
753 assert test_device in attached_devices, (
754 'Did not find device %s among attached device. Attached devices: %s'
755 % (test_device, ', '.join(attached_devices)))
756 attached_devices = [test_device]
757
758 assert attached_devices, 'No devices attached.'
759
760 return sorted(attached_devices)
761
762
[email protected]fbe29322013-07-09 09:03:26763def RunTestsCommand(command, options, args, option_parser):
764 """Checks test type and dispatches to the appropriate function.
765
766 Args:
767 command: String indicating the command that was received to trigger
768 this function.
769 options: optparse options dictionary.
770 args: List of extra args from optparse.
771 option_parser: optparse.OptionParser object.
772
773 Returns:
774 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10775
776 Raises:
777 Exception: Unknown command name passed in, or an exception from an
778 individual test runner.
[email protected]fbe29322013-07-09 09:03:26779 """
780
[email protected]d82f0252013-07-12 23:22:57781 # Check for extra arguments
[email protected]ad32f312013-11-13 04:03:29782 if len(args) > 2 and command != 'perf':
[email protected]d82f0252013-07-12 23:22:57783 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[2:])))
784 return constants.ERROR_EXIT_CODE
[email protected]ad32f312013-11-13 04:03:29785 if command == 'perf':
786 if ((options.single_step and len(args) <= 2) or
787 (not options.single_step and len(args) > 2)):
788 option_parser.error('Unrecognized arguments: %s' % (' '.join(args)))
789 return constants.ERROR_EXIT_CODE
[email protected]d82f0252013-07-12 23:22:57790
[email protected]fbe29322013-07-09 09:03:26791 ProcessCommonOptions(options)
792
[email protected]f7148dd42013-08-20 14:24:57793 devices = _GetAttachedDevices(options.test_device)
794
[email protected]c0662e092013-11-12 11:51:25795 forwarder.Forwarder.RemoveHostLog()
[email protected]6b11583b2013-11-21 16:18:40796 if not ports.ResetTestServerPortAllocation():
797 raise Exception('Failed to reset test server port.')
[email protected]c0662e092013-11-12 11:51:25798
[email protected]fbe29322013-07-09 09:03:26799 if command == 'gtest':
[email protected]7c53a602014-03-24 16:21:44800 return _RunGTests(options, devices)
[email protected]6b6abac6d2013-10-03 11:56:38801 elif command == 'linker':
[email protected]7c53a602014-03-24 16:21:44802 return _RunLinkerTests(options, devices)
[email protected]fbe29322013-07-09 09:03:26803 elif command == 'instrumentation':
[email protected]f7148dd42013-08-20 14:24:57804 return _RunInstrumentationTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26805 elif command == 'uiautomator':
[email protected]f7148dd42013-08-20 14:24:57806 return _RunUIAutomatorTests(options, option_parser.error, devices)
jbudorick9a6b7b332014-09-20 00:01:07807 elif command == 'junit':
808 return _RunJUnitTests(options, option_parser.error)
[email protected]3dbdfa42013-08-08 01:08:14809 elif command == 'monkey':
[email protected]f7148dd42013-08-20 14:24:57810 return _RunMonkeyTests(options, option_parser.error, devices)
[email protected]ec3170b2013-08-14 14:39:47811 elif command == 'perf':
[email protected]a72f0752014-06-03 23:52:34812 return _RunPerfTests(options, args, option_parser.error)
[email protected]fbe29322013-07-09 09:03:26813 else:
[email protected]6bc1bda22013-07-19 22:08:37814 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26815
[email protected]fbe29322013-07-09 09:03:26816
[email protected]7c53a602014-03-24 16:21:44817def HelpCommand(command, _options, args, option_parser):
[email protected]fbe29322013-07-09 09:03:26818 """Display help for a certain command, or overall help.
819
820 Args:
821 command: String indicating the command that was received to trigger
822 this function.
[email protected]7c53a602014-03-24 16:21:44823 options: optparse options dictionary. unused.
[email protected]fbe29322013-07-09 09:03:26824 args: List of extra args from optparse.
825 option_parser: optparse.OptionParser object.
826
827 Returns:
828 Integer indicated exit code.
829 """
830 # If we don't have any args, display overall help
831 if len(args) < 3:
832 option_parser.print_help()
833 return 0
[email protected]d82f0252013-07-12 23:22:57834 # If we have too many args, print an error
835 if len(args) > 3:
836 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:])))
837 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:26838
839 command = args[2]
840
841 if command not in VALID_COMMANDS:
842 option_parser.error('Unrecognized command.')
843
844 # Treat the help command as a special case. We don't care about showing a
845 # specific help page for itself.
846 if command == 'help':
847 option_parser.print_help()
848 return 0
849
850 VALID_COMMANDS[command].add_options_func(option_parser)
851 option_parser.usage = '%prog ' + command + ' [options]'
[email protected]dfffbcbc2013-09-17 22:06:01852 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26853 option_parser.print_help()
854
855 return 0
856
857
858# Define a named tuple for the values in the VALID_COMMANDS dictionary so the
859# syntax is a bit prettier. The tuple is two functions: (add options, run
860# command).
861CommandFunctionTuple = collections.namedtuple(
862 'CommandFunctionTuple', ['add_options_func', 'run_command_func'])
863VALID_COMMANDS = {
864 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26865 'instrumentation': CommandFunctionTuple(
866 AddInstrumentationTestOptions, RunTestsCommand),
867 'uiautomator': CommandFunctionTuple(
868 AddUIAutomatorTestOptions, RunTestsCommand),
jbudorick9a6b7b332014-09-20 00:01:07869 'junit': CommandFunctionTuple(
870 AddJUnitTestOptions, RunTestsCommand),
[email protected]3dbdfa42013-08-08 01:08:14871 'monkey': CommandFunctionTuple(
872 AddMonkeyTestOptions, RunTestsCommand),
[email protected]ec3170b2013-08-14 14:39:47873 'perf': CommandFunctionTuple(
874 AddPerfTestOptions, RunTestsCommand),
[email protected]6b6abac6d2013-10-03 11:56:38875 'linker': CommandFunctionTuple(
876 AddLinkerTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26877 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand)
878 }
879
880
[email protected]7c53a602014-03-24 16:21:44881def DumpThreadStacks(_signal, _frame):
[email protected]71aec4b2013-11-20 00:35:24882 for thread in threading.enumerate():
883 reraiser_thread.LogThreadStack(thread)
[email protected]83bb8152013-11-19 15:02:21884
885
[email protected]7c53a602014-03-24 16:21:44886def main():
[email protected]83bb8152013-11-19 15:02:21887 signal.signal(signal.SIGUSR1, DumpThreadStacks)
[email protected]803f65a72013-08-20 19:11:30888 option_parser = command_option_parser.CommandOptionParser(
889 commands_dict=VALID_COMMANDS)
890 return command_option_parser.ParseAndExecute(option_parser)
[email protected]fbe29322013-07-09 09:03:26891
[email protected]fbe29322013-07-09 09:03:26892
893if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:44894 sys.exit(main())