blob: 5456eafc4757702574a879dff24306f74ea97f59 [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]fbe29322013-07-09 09:03:2614import sys
15
[email protected]f7148dd42013-08-20 14:24:5716from pylib import android_commands
[email protected]fbe29322013-07-09 09:03:2617from pylib import constants
18from pylib import ports
19from pylib.base import base_test_result
[email protected]6bc1bda22013-07-19 22:08:3720from pylib.base import test_dispatcher
[email protected]6bc1bda22013-07-19 22:08:3721from pylib.gtest import gtest_config
[email protected]2a684222013-08-01 16:59:2222from pylib.gtest import setup as gtest_setup
23from pylib.gtest import test_options as gtest_test_options
[email protected]37ee0c792013-08-06 19:10:1324from pylib.host_driven import setup as host_driven_setup
[email protected]6bc1bda22013-07-19 22:08:3725from pylib.instrumentation import setup as instrumentation_setup
[email protected]2a684222013-08-01 16:59:2226from pylib.instrumentation import test_options as instrumentation_test_options
[email protected]3dbdfa42013-08-08 01:08:1427from pylib.monkey import setup as monkey_setup
28from pylib.monkey import test_options as monkey_test_options
[email protected]ec3170b2013-08-14 14:39:4729from pylib.perf import setup as perf_setup
30from pylib.perf import test_options as perf_test_options
31from pylib.perf import test_runner as perf_test_runner
[email protected]6bc1bda22013-07-19 22:08:3732from pylib.uiautomator import setup as uiautomator_setup
[email protected]2a684222013-08-01 16:59:2233from pylib.uiautomator import test_options as uiautomator_test_options
[email protected]803f65a72013-08-20 19:11:3034from pylib.utils import command_option_parser
[email protected]6bc1bda22013-07-19 22:08:3735from pylib.utils import report_results
36from pylib.utils import run_tests_helper
[email protected]fbe29322013-07-09 09:03:2637
38
[email protected]fbe29322013-07-09 09:03:2639def AddCommonOptions(option_parser):
40 """Adds all common options to |option_parser|."""
41
[email protected]dfffbcbc2013-09-17 22:06:0142 group = optparse.OptionGroup(option_parser, 'Common Options')
43 default_build_type = os.environ.get('BUILDTYPE', 'Debug')
44 group.add_option('--debug', action='store_const', const='Debug',
45 dest='build_type', default=default_build_type,
46 help=('If set, run test suites under out/Debug. '
47 'Default is env var BUILDTYPE or Debug.'))
48 group.add_option('--release', action='store_const',
49 const='Release', dest='build_type',
50 help=('If set, run test suites under out/Release.'
51 ' Default is env var BUILDTYPE or Debug.'))
52 group.add_option('-c', dest='cleanup_test_files',
53 help='Cleanup test files on the device after run',
54 action='store_true')
55 group.add_option('--num_retries', dest='num_retries', type='int',
56 default=2,
57 help=('Number of retries for a test before '
58 'giving up.'))
59 group.add_option('-v',
60 '--verbose',
61 dest='verbose_count',
62 default=0,
63 action='count',
64 help='Verbose level (multiple times for more)')
65 group.add_option('--tool',
66 dest='tool',
67 help=('Run the test under a tool '
68 '(use --tool help to list them)'))
69 group.add_option('--flakiness-dashboard-server',
70 dest='flakiness_dashboard_server',
71 help=('Address of the server that is hosting the '
72 'Chrome for Android flakiness dashboard.'))
73 group.add_option('--skip-deps-push', dest='push_deps',
74 action='store_false', default=True,
75 help=('Do not push dependencies to the device. '
76 'Use this at own risk for speeding up test '
77 'execution on local machine.'))
78 group.add_option('-d', '--device', dest='test_device',
79 help=('Target device for the test suite '
80 'to run on.'))
81 option_parser.add_option_group(group)
[email protected]fbe29322013-07-09 09:03:2682
83
84def ProcessCommonOptions(options):
85 """Processes and handles all common options."""
[email protected]fbe29322013-07-09 09:03:2686 run_tests_helper.SetLogLevel(options.verbose_count)
[email protected]14b3b1202013-08-15 22:25:2887 constants.SetBuildType(options.build_type)
[email protected]fbe29322013-07-09 09:03:2688
89
[email protected]fbe29322013-07-09 09:03:2690def AddGTestOptions(option_parser):
91 """Adds gtest options to |option_parser|."""
92
93 option_parser.usage = '%prog gtest [options]'
[email protected]dfffbcbc2013-09-17 22:06:0194 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:2695 option_parser.example = '%prog gtest -s base_unittests'
96
[email protected]6bc1bda22013-07-19 22:08:3797 # TODO(gkanwar): Make this option required
98 option_parser.add_option('-s', '--suite', dest='suite_name',
[email protected]fbe29322013-07-09 09:03:2699 help=('Executable name of the test suite to run '
100 '(use -s help to list them).'))
[email protected]dfffbcbc2013-09-17 22:06:01101 option_parser.add_option('-f', '--gtest-filter', dest='test_filter',
[email protected]9e689252013-07-30 20:14:36102 help='googletest-style filter string.')
[email protected]dfffbcbc2013-09-17 22:06:01103 option_parser.add_option('--gtest-also-run-disabled-tests',
104 dest='run_disabled', action='store_true',
105 help='Also run disabled tests if applicable.')
106 option_parser.add_option('-a', '--test-arguments', dest='test_arguments',
107 default='',
[email protected]9e689252013-07-30 20:14:36108 help='Additional arguments to pass to the test.')
109 option_parser.add_option('-t', dest='timeout',
110 help='Timeout to wait for each test',
111 type='int',
112 default=60)
[email protected]fbe29322013-07-09 09:03:26113 # TODO(gkanwar): Move these to Common Options once we have the plumbing
114 # in our other test types to handle these commands
[email protected]fbe29322013-07-09 09:03:26115 AddCommonOptions(option_parser)
116
117
[email protected]6bc1bda22013-07-19 22:08:37118def ProcessGTestOptions(options):
119 """Intercept test suite help to list test suites.
120
121 Args:
122 options: Command line options.
[email protected]6bc1bda22013-07-19 22:08:37123 """
124 if options.suite_name == 'help':
125 print 'Available test suites are:'
[email protected]9e689252013-07-30 20:14:36126 for test_suite in (gtest_config.STABLE_TEST_SUITES +
127 gtest_config.EXPERIMENTAL_TEST_SUITES):
128 print test_suite
[email protected]2a684222013-08-01 16:59:22129 sys.exit(0)
[email protected]6bc1bda22013-07-19 22:08:37130
131 # Convert to a list, assuming all test suites if nothing was specified.
132 # TODO(gkanwar): Require having a test suite
133 if options.suite_name:
134 options.suite_name = [options.suite_name]
135 else:
[email protected]9e689252013-07-30 20:14:36136 options.suite_name = [s for s in gtest_config.STABLE_TEST_SUITES]
[email protected]6bc1bda22013-07-19 22:08:37137
138
[email protected]fbe29322013-07-09 09:03:26139def AddJavaTestOptions(option_parser):
140 """Adds the Java test options to |option_parser|."""
141
[email protected]dfffbcbc2013-09-17 22:06:01142 option_parser.add_option('-f', '--test-filter', dest='test_filter',
[email protected]fbe29322013-07-09 09:03:26143 help=('Test filter (if not fully qualified, '
144 'will run all matches).'))
145 option_parser.add_option(
146 '-A', '--annotation', dest='annotation_str',
147 help=('Comma-separated list of annotations. Run only tests with any of '
148 'the given annotations. An annotation can be either a key or a '
149 'key-values pair. A test that has no annotation is considered '
150 '"SmallTest".'))
151 option_parser.add_option(
152 '-E', '--exclude-annotation', dest='exclude_annotation_str',
153 help=('Comma-separated list of annotations. Exclude tests with these '
154 'annotations.'))
[email protected]fbe29322013-07-09 09:03:26155 option_parser.add_option('--screenshot', dest='screenshot_failures',
156 action='store_true',
157 help='Capture screenshots of test failures')
158 option_parser.add_option('--save-perf-json', action='store_true',
159 help='Saves the JSON file for each UI Perf test.')
[email protected]37ee0c792013-08-06 19:10:13160 option_parser.add_option('--official-build', action='store_true',
161 help='Run official build tests.')
[email protected]fbe29322013-07-09 09:03:26162 option_parser.add_option('--keep_test_server_ports',
163 action='store_true',
164 help=('Indicates the test server ports must be '
165 'kept. When this is run via a sharder '
166 'the test server ports should be kept and '
167 'should not be reset.'))
[email protected]fbe29322013-07-09 09:03:26168 option_parser.add_option('--test_data', action='append', default=[],
169 help=('Each instance defines a directory of test '
170 'data that should be copied to the target(s) '
171 'before running the tests. The argument '
172 'should be of the form <target>:<source>, '
173 '<target> is relative to the device data'
174 'directory, and <source> is relative to the '
175 'chromium build directory.'))
176
177
178def ProcessJavaTestOptions(options, error_func):
179 """Processes options/arguments and populates |options| with defaults."""
180
[email protected]fbe29322013-07-09 09:03:26181 if options.annotation_str:
182 options.annotations = options.annotation_str.split(',')
183 elif options.test_filter:
184 options.annotations = []
185 else:
[email protected]6bc1bda22013-07-19 22:08:37186 options.annotations = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest',
187 'EnormousTest']
[email protected]fbe29322013-07-09 09:03:26188
189 if options.exclude_annotation_str:
190 options.exclude_annotations = options.exclude_annotation_str.split(',')
191 else:
192 options.exclude_annotations = []
193
194 if not options.keep_test_server_ports:
195 if not ports.ResetTestServerPortAllocation():
196 raise Exception('Failed to reset test server port.')
197
198
199def AddInstrumentationTestOptions(option_parser):
200 """Adds Instrumentation test options to |option_parser|."""
201
202 option_parser.usage = '%prog instrumentation [options]'
[email protected]dfffbcbc2013-09-17 22:06:01203 option_parser.commands_dict = {}
[email protected]fb7ab5e82013-07-26 18:31:20204 option_parser.example = ('%prog instrumentation '
[email protected]fbe29322013-07-09 09:03:26205 '--test-apk=ChromiumTestShellTest')
206
207 AddJavaTestOptions(option_parser)
208 AddCommonOptions(option_parser)
209
[email protected]dfffbcbc2013-09-17 22:06:01210 option_parser.add_option('-j', '--java-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13211 default=False, help='Run only the Java tests.')
[email protected]dfffbcbc2013-09-17 22:06:01212 option_parser.add_option('-p', '--python-only', action='store_true',
[email protected]37ee0c792013-08-06 19:10:13213 default=False,
214 help='Run only the host-driven tests.')
[email protected]a69e85bc2013-08-16 18:07:26215 option_parser.add_option('--host-driven-root',
[email protected]37ee0c792013-08-06 19:10:13216 help='Root of the host-driven tests.')
[email protected]fbe29322013-07-09 09:03:26217 option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
218 action='store_true',
219 help='Wait for debugger.')
[email protected]fbe29322013-07-09 09:03:26220 option_parser.add_option(
221 '--test-apk', dest='test_apk',
222 help=('The name of the apk containing the tests '
[email protected]ae68d4a2013-09-24 21:57:15223 '(without the .apk extension; e.g. "ContentShellTest").'))
[email protected]803f65a72013-08-20 19:11:30224 option_parser.add_option('--coverage-dir',
225 help=('Directory in which to place all generated '
226 'EMMA coverage files.'))
[email protected]fbe29322013-07-09 09:03:26227
228
229def ProcessInstrumentationOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22230 """Processes options/arguments and populate |options| with defaults.
231
232 Args:
233 options: optparse.Options object.
234 error_func: Function to call with the error message in case of an error.
235
236 Returns:
237 An InstrumentationOptions named tuple which contains all options relevant to
238 instrumentation tests.
239 """
[email protected]fbe29322013-07-09 09:03:26240
241 ProcessJavaTestOptions(options, error_func)
242
[email protected]37ee0c792013-08-06 19:10:13243 if options.java_only and options.python_only:
244 error_func('Options java_only (-j) and python_only (-p) '
245 'are mutually exclusive.')
246 options.run_java_tests = True
247 options.run_python_tests = True
248 if options.java_only:
249 options.run_python_tests = False
250 elif options.python_only:
251 options.run_java_tests = False
252
[email protected]67954f822013-08-14 18:09:08253 if not options.host_driven_root:
[email protected]37ee0c792013-08-06 19:10:13254 options.run_python_tests = False
255
[email protected]fbe29322013-07-09 09:03:26256 if not options.test_apk:
257 error_func('--test-apk must be specified.')
258
[email protected]ae68d4a2013-09-24 21:57:15259
260 options.test_apk_path = os.path.join(constants.GetOutDirectory(),
261 constants.SDK_BUILD_APKS_DIR,
262 '%s.apk' % options.test_apk)
263 options.test_apk_jar_path = os.path.join(
264 constants.GetOutDirectory(),
265 constants.SDK_BUILD_TEST_JAVALIB_DIR,
266 '%s.jar' % options.test_apk)
[email protected]fbe29322013-07-09 09:03:26267
[email protected]2a684222013-08-01 16:59:22268 return instrumentation_test_options.InstrumentationOptions(
[email protected]2a684222013-08-01 16:59:22269 options.tool,
270 options.cleanup_test_files,
271 options.push_deps,
272 options.annotations,
273 options.exclude_annotations,
274 options.test_filter,
275 options.test_data,
276 options.save_perf_json,
277 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22278 options.wait_for_debugger,
[email protected]803f65a72013-08-20 19:11:30279 options.coverage_dir,
[email protected]2a684222013-08-01 16:59:22280 options.test_apk,
281 options.test_apk_path,
282 options.test_apk_jar_path)
283
[email protected]fbe29322013-07-09 09:03:26284
285def AddUIAutomatorTestOptions(option_parser):
286 """Adds UI Automator test options to |option_parser|."""
287
288 option_parser.usage = '%prog uiautomator [options]'
[email protected]dfffbcbc2013-09-17 22:06:01289 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26290 option_parser.example = (
291 '%prog uiautomator --test-jar=chromium_testshell_uiautomator_tests'
292 ' --package-name=org.chromium.chrome.testshell')
293 option_parser.add_option(
294 '--package-name',
295 help='The package name used by the apk containing the application.')
296 option_parser.add_option(
297 '--test-jar', dest='test_jar',
298 help=('The name of the dexed jar containing the tests (without the '
299 '.dex.jar extension). Alternatively, this can be a full path '
300 'to the jar.'))
301
302 AddJavaTestOptions(option_parser)
303 AddCommonOptions(option_parser)
304
305
306def ProcessUIAutomatorOptions(options, error_func):
[email protected]2a684222013-08-01 16:59:22307 """Processes UIAutomator options/arguments.
308
309 Args:
310 options: optparse.Options object.
311 error_func: Function to call with the error message in case of an error.
312
313 Returns:
314 A UIAutomatorOptions named tuple which contains all options relevant to
[email protected]3dbdfa42013-08-08 01:08:14315 uiautomator tests.
[email protected]2a684222013-08-01 16:59:22316 """
[email protected]fbe29322013-07-09 09:03:26317
318 ProcessJavaTestOptions(options, error_func)
319
320 if not options.package_name:
321 error_func('--package-name must be specified.')
322
323 if not options.test_jar:
324 error_func('--test-jar must be specified.')
325
326 if os.path.exists(options.test_jar):
327 # The dexed JAR is fully qualified, assume the info JAR lives along side.
328 options.uiautomator_jar = options.test_jar
329 else:
330 options.uiautomator_jar = os.path.join(
[email protected]ae68d4a2013-09-24 21:57:15331 constants.GetOutDirectory(),
332 constants.SDK_BUILD_JAVALIB_DIR,
[email protected]fbe29322013-07-09 09:03:26333 '%s.dex.jar' % options.test_jar)
334 options.uiautomator_info_jar = (
335 options.uiautomator_jar[:options.uiautomator_jar.find('.dex.jar')] +
336 '_java.jar')
337
[email protected]2a684222013-08-01 16:59:22338 return uiautomator_test_options.UIAutomatorOptions(
[email protected]2a684222013-08-01 16:59:22339 options.tool,
340 options.cleanup_test_files,
341 options.push_deps,
342 options.annotations,
343 options.exclude_annotations,
344 options.test_filter,
345 options.test_data,
346 options.save_perf_json,
347 options.screenshot_failures,
[email protected]2a684222013-08-01 16:59:22348 options.uiautomator_jar,
349 options.uiautomator_info_jar,
350 options.package_name)
351
[email protected]fbe29322013-07-09 09:03:26352
[email protected]3dbdfa42013-08-08 01:08:14353def AddMonkeyTestOptions(option_parser):
354 """Adds monkey test options to |option_parser|."""
[email protected]fb81b982013-08-09 00:07:12355
356 option_parser.usage = '%prog monkey [options]'
[email protected]dfffbcbc2013-09-17 22:06:01357 option_parser.commands_dict = {}
[email protected]fb81b982013-08-09 00:07:12358 option_parser.example = (
359 '%prog monkey --package-name=org.chromium.content_shell_apk'
360 ' --activity-name=.ContentShellActivity')
361
[email protected]3dbdfa42013-08-08 01:08:14362 option_parser.add_option('--package-name', help='Allowed package.')
363 option_parser.add_option(
[email protected]fb81b982013-08-09 00:07:12364 '--activity-name', help='Name of the activity to start.')
[email protected]3dbdfa42013-08-08 01:08:14365 option_parser.add_option(
366 '--event-count', default=10000, type='int',
367 help='Number of events to generate [default: %default].')
368 option_parser.add_option(
369 '--category', default='',
[email protected]fb81b982013-08-09 00:07:12370 help='A list of allowed categories.')
[email protected]3dbdfa42013-08-08 01:08:14371 option_parser.add_option(
372 '--throttle', default=100, type='int',
373 help='Delay between events (ms) [default: %default]. ')
374 option_parser.add_option(
375 '--seed', type='int',
376 help=('Seed value for pseudo-random generator. Same seed value generates '
377 'the same sequence of events. Seed is randomized by default.'))
378 option_parser.add_option(
379 '--extra-args', default='',
380 help=('String of other args to pass to the command verbatim '
381 '[default: "%default"].'))
382
383 AddCommonOptions(option_parser)
384
385
386def ProcessMonkeyTestOptions(options, error_func):
387 """Processes all monkey test options.
388
389 Args:
390 options: optparse.Options object.
391 error_func: Function to call with the error message in case of an error.
392
393 Returns:
394 A MonkeyOptions named tuple which contains all options relevant to
395 monkey tests.
396 """
397 if not options.package_name:
398 error_func('Package name is required.')
399
400 category = options.category
401 if category:
402 category = options.category.split(',')
403
404 return monkey_test_options.MonkeyOptions(
[email protected]3dbdfa42013-08-08 01:08:14405 options.verbose_count,
406 options.package_name,
407 options.activity_name,
408 options.event_count,
409 category,
410 options.throttle,
411 options.seed,
412 options.extra_args)
413
414
[email protected]ec3170b2013-08-14 14:39:47415def AddPerfTestOptions(option_parser):
416 """Adds perf test options to |option_parser|."""
417
418 option_parser.usage = '%prog perf [options]'
[email protected]dfffbcbc2013-09-17 22:06:01419 option_parser.commands_dict = {}
[email protected]ec3170b2013-08-14 14:39:47420 option_parser.example = ('%prog perf --steps perf_steps.json')
421
[email protected]181a5c92013-09-06 17:11:46422 option_parser.add_option(
423 '--steps',
424 help='JSON file containing the list of perf steps to run.')
425 option_parser.add_option(
426 '--flaky-steps',
427 help=('A JSON file containing steps that are flaky '
428 'and will have its exit code ignored.'))
429 option_parser.add_option(
430 '--print-step',
431 help='The name of a previously executed perf step to print.')
432 option_parser.add_option(
433 '--no-timeout', action='store_true',
434 help=('Do not impose a timeout. Each perf step is responsible for '
435 'implementing the timeout logic.'))
[email protected]650487c2013-09-30 11:40:49436 option_parser.add_option(
437 '-f', '--test-filter',
438 help=('Test filter (will match against the names listed in --steps).'))
439 option_parser.add_option(
440 '--dry-run',
441 action='store_true',
442 help='Just print the steps without executing.')
[email protected]ec3170b2013-08-14 14:39:47443 AddCommonOptions(option_parser)
444
445
446def ProcessPerfTestOptions(options, error_func):
447 """Processes all perf test options.
448
449 Args:
450 options: optparse.Options object.
451 error_func: Function to call with the error message in case of an error.
452
453 Returns:
454 A PerfOptions named tuple which contains all options relevant to
455 perf tests.
456 """
457 if not options.steps and not options.print_step:
458 error_func('Please specify --steps or --print-step')
459 return perf_test_options.PerfOptions(
[email protected]181a5c92013-09-06 17:11:46460 options.steps, options.flaky_steps, options.print_step,
[email protected]650487c2013-09-30 11:40:49461 options.no_timeout, options.test_filter, options.dry_run)
[email protected]ec3170b2013-08-14 14:39:47462
463
[email protected]f7148dd42013-08-20 14:24:57464def _RunGTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37465 """Subcommand of RunTestsCommands which runs gtests."""
[email protected]2a684222013-08-01 16:59:22466 ProcessGTestOptions(options)
[email protected]6bc1bda22013-07-19 22:08:37467
468 exit_code = 0
469 for suite_name in options.suite_name:
[email protected]2a684222013-08-01 16:59:22470 # TODO(gkanwar): Move this into ProcessGTestOptions once we require -s for
471 # the gtest command.
472 gtest_options = gtest_test_options.GTestOptions(
[email protected]2a684222013-08-01 16:59:22473 options.tool,
474 options.cleanup_test_files,
475 options.push_deps,
476 options.test_filter,
[email protected]dfffbcbc2013-09-17 22:06:01477 options.run_disabled,
[email protected]2a684222013-08-01 16:59:22478 options.test_arguments,
479 options.timeout,
480 suite_name)
[email protected]f7148dd42013-08-20 14:24:57481 runner_factory, tests = gtest_setup.Setup(gtest_options, devices)
[email protected]6bc1bda22013-07-19 22:08:37482
483 results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57484 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37485 num_retries=options.num_retries)
486
487 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
488 exit_code = test_exit_code
489
490 report_results.LogFull(
491 results=results,
492 test_type='Unit test',
493 test_package=suite_name,
[email protected]6bc1bda22013-07-19 22:08:37494 flakiness_server=options.flakiness_dashboard_server)
495
496 if os.path.isdir(constants.ISOLATE_DEPS_DIR):
497 shutil.rmtree(constants.ISOLATE_DEPS_DIR)
498
499 return exit_code
500
501
[email protected]f7148dd42013-08-20 14:24:57502def _RunInstrumentationTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37503 """Subcommand of RunTestsCommands which runs instrumentation tests."""
[email protected]2a684222013-08-01 16:59:22504 instrumentation_options = ProcessInstrumentationOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37505
[email protected]f7148dd42013-08-20 14:24:57506 if len(devices) > 1 and options.wait_for_debugger:
507 logging.warning('Debugger can not be sharded, using first available device')
508 devices = devices[:1]
509
[email protected]6bc1bda22013-07-19 22:08:37510 results = base_test_result.TestRunResults()
511 exit_code = 0
512
513 if options.run_java_tests:
[email protected]2a684222013-08-01 16:59:22514 runner_factory, tests = instrumentation_setup.Setup(instrumentation_options)
[email protected]6bc1bda22013-07-19 22:08:37515
516 test_results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57517 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]6bc1bda22013-07-19 22:08:37518 num_retries=options.num_retries)
519
520 results.AddTestRunResults(test_results)
521
522 if options.run_python_tests:
[email protected]37ee0c792013-08-06 19:10:13523 runner_factory, tests = host_driven_setup.InstrumentationSetup(
[email protected]67954f822013-08-14 18:09:08524 options.host_driven_root, options.official_build,
[email protected]37ee0c792013-08-06 19:10:13525 instrumentation_options)
526
[email protected]34020022013-08-06 23:35:34527 if tests:
528 test_results, test_exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57529 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]34020022013-08-06 23:35:34530 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37531
[email protected]34020022013-08-06 23:35:34532 results.AddTestRunResults(test_results)
[email protected]6bc1bda22013-07-19 22:08:37533
[email protected]34020022013-08-06 23:35:34534 # Only allow exit code escalation
535 if test_exit_code and exit_code != constants.ERROR_EXIT_CODE:
536 exit_code = test_exit_code
[email protected]6bc1bda22013-07-19 22:08:37537
538 report_results.LogFull(
539 results=results,
540 test_type='Instrumentation',
541 test_package=os.path.basename(options.test_apk),
542 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37543 flakiness_server=options.flakiness_dashboard_server)
544
545 return exit_code
546
547
[email protected]f7148dd42013-08-20 14:24:57548def _RunUIAutomatorTests(options, error_func, devices):
[email protected]6bc1bda22013-07-19 22:08:37549 """Subcommand of RunTestsCommands which runs uiautomator tests."""
[email protected]2a684222013-08-01 16:59:22550 uiautomator_options = ProcessUIAutomatorOptions(options, error_func)
[email protected]6bc1bda22013-07-19 22:08:37551
[email protected]37ee0c792013-08-06 19:10:13552 runner_factory, tests = uiautomator_setup.Setup(uiautomator_options)
[email protected]6bc1bda22013-07-19 22:08:37553
[email protected]37ee0c792013-08-06 19:10:13554 results, exit_code = test_dispatcher.RunTests(
[email protected]f7148dd42013-08-20 14:24:57555 tests, runner_factory, devices, shard=True, test_timeout=None,
[email protected]37ee0c792013-08-06 19:10:13556 num_retries=options.num_retries)
[email protected]6bc1bda22013-07-19 22:08:37557
558 report_results.LogFull(
559 results=results,
560 test_type='UIAutomator',
561 test_package=os.path.basename(options.test_jar),
562 annotation=options.annotations,
[email protected]6bc1bda22013-07-19 22:08:37563 flakiness_server=options.flakiness_dashboard_server)
564
565 return exit_code
566
567
[email protected]f7148dd42013-08-20 14:24:57568def _RunMonkeyTests(options, error_func, devices):
[email protected]3dbdfa42013-08-08 01:08:14569 """Subcommand of RunTestsCommands which runs monkey tests."""
570 monkey_options = ProcessMonkeyTestOptions(options, error_func)
571
572 runner_factory, tests = monkey_setup.Setup(monkey_options)
573
574 results, exit_code = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46575 tests, runner_factory, devices, shard=False, test_timeout=None,
576 num_retries=options.num_retries)
[email protected]3dbdfa42013-08-08 01:08:14577
578 report_results.LogFull(
579 results=results,
580 test_type='Monkey',
[email protected]14b3b1202013-08-15 22:25:28581 test_package='Monkey')
[email protected]3dbdfa42013-08-08 01:08:14582
583 return exit_code
584
585
[email protected]f7148dd42013-08-20 14:24:57586def _RunPerfTests(options, error_func, devices):
[email protected]ec3170b2013-08-14 14:39:47587 """Subcommand of RunTestsCommands which runs perf tests."""
588 perf_options = ProcessPerfTestOptions(options, error_func)
589 # Just print the results from a single previously executed step.
590 if perf_options.print_step:
591 return perf_test_runner.PrintTestOutput(perf_options.print_step)
592
593 runner_factory, tests = perf_setup.Setup(perf_options)
594
[email protected]86184c7b2013-08-15 15:06:57595 results, _ = test_dispatcher.RunTests(
[email protected]181a5c92013-09-06 17:11:46596 tests, runner_factory, devices, shard=True, test_timeout=None,
597 num_retries=options.num_retries)
[email protected]ec3170b2013-08-14 14:39:47598
599 report_results.LogFull(
600 results=results,
601 test_type='Perf',
[email protected]865a47a2013-08-16 14:01:12602 test_package='Perf')
[email protected]86184c7b2013-08-15 15:06:57603 # Always return 0 on the sharding stage. Individual tests exit_code
604 # will be returned on the print_step stage.
605 return 0
[email protected]ec3170b2013-08-14 14:39:47606
[email protected]3dbdfa42013-08-08 01:08:14607
[email protected]f7148dd42013-08-20 14:24:57608def _GetAttachedDevices(test_device=None):
609 """Get all attached devices.
610
611 Args:
612 test_device: Name of a specific device to use.
613
614 Returns:
615 A list of attached devices.
616 """
617 attached_devices = []
618
619 attached_devices = android_commands.GetAttachedDevices()
620 if test_device:
621 assert test_device in attached_devices, (
622 'Did not find device %s among attached device. Attached devices: %s'
623 % (test_device, ', '.join(attached_devices)))
624 attached_devices = [test_device]
625
626 assert attached_devices, 'No devices attached.'
627
628 return sorted(attached_devices)
629
630
[email protected]fbe29322013-07-09 09:03:26631def RunTestsCommand(command, options, args, option_parser):
632 """Checks test type and dispatches to the appropriate function.
633
634 Args:
635 command: String indicating the command that was received to trigger
636 this function.
637 options: optparse options dictionary.
638 args: List of extra args from optparse.
639 option_parser: optparse.OptionParser object.
640
641 Returns:
642 Integer indicated exit code.
[email protected]b3873892013-07-10 04:57:10643
644 Raises:
645 Exception: Unknown command name passed in, or an exception from an
646 individual test runner.
[email protected]fbe29322013-07-09 09:03:26647 """
648
[email protected]d82f0252013-07-12 23:22:57649 # Check for extra arguments
650 if len(args) > 2:
651 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[2:])))
652 return constants.ERROR_EXIT_CODE
653
[email protected]fbe29322013-07-09 09:03:26654 ProcessCommonOptions(options)
655
[email protected]f7148dd42013-08-20 14:24:57656 devices = _GetAttachedDevices(options.test_device)
657
[email protected]fbe29322013-07-09 09:03:26658 if command == 'gtest':
[email protected]f7148dd42013-08-20 14:24:57659 return _RunGTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26660 elif command == 'instrumentation':
[email protected]f7148dd42013-08-20 14:24:57661 return _RunInstrumentationTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26662 elif command == 'uiautomator':
[email protected]f7148dd42013-08-20 14:24:57663 return _RunUIAutomatorTests(options, option_parser.error, devices)
[email protected]3dbdfa42013-08-08 01:08:14664 elif command == 'monkey':
[email protected]f7148dd42013-08-20 14:24:57665 return _RunMonkeyTests(options, option_parser.error, devices)
[email protected]ec3170b2013-08-14 14:39:47666 elif command == 'perf':
[email protected]f7148dd42013-08-20 14:24:57667 return _RunPerfTests(options, option_parser.error, devices)
[email protected]fbe29322013-07-09 09:03:26668 else:
[email protected]6bc1bda22013-07-19 22:08:37669 raise Exception('Unknown test type.')
[email protected]fbe29322013-07-09 09:03:26670
[email protected]fbe29322013-07-09 09:03:26671
672def HelpCommand(command, options, args, option_parser):
673 """Display help for a certain command, or overall help.
674
675 Args:
676 command: String indicating the command that was received to trigger
677 this function.
678 options: optparse options dictionary.
679 args: List of extra args from optparse.
680 option_parser: optparse.OptionParser object.
681
682 Returns:
683 Integer indicated exit code.
684 """
685 # If we don't have any args, display overall help
686 if len(args) < 3:
687 option_parser.print_help()
688 return 0
[email protected]d82f0252013-07-12 23:22:57689 # If we have too many args, print an error
690 if len(args) > 3:
691 option_parser.error('Unrecognized arguments: %s' % (' '.join(args[3:])))
692 return constants.ERROR_EXIT_CODE
[email protected]fbe29322013-07-09 09:03:26693
694 command = args[2]
695
696 if command not in VALID_COMMANDS:
697 option_parser.error('Unrecognized command.')
698
699 # Treat the help command as a special case. We don't care about showing a
700 # specific help page for itself.
701 if command == 'help':
702 option_parser.print_help()
703 return 0
704
705 VALID_COMMANDS[command].add_options_func(option_parser)
706 option_parser.usage = '%prog ' + command + ' [options]'
[email protected]dfffbcbc2013-09-17 22:06:01707 option_parser.commands_dict = {}
[email protected]fbe29322013-07-09 09:03:26708 option_parser.print_help()
709
710 return 0
711
712
713# Define a named tuple for the values in the VALID_COMMANDS dictionary so the
714# syntax is a bit prettier. The tuple is two functions: (add options, run
715# command).
716CommandFunctionTuple = collections.namedtuple(
717 'CommandFunctionTuple', ['add_options_func', 'run_command_func'])
718VALID_COMMANDS = {
719 'gtest': CommandFunctionTuple(AddGTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26720 'instrumentation': CommandFunctionTuple(
721 AddInstrumentationTestOptions, RunTestsCommand),
722 'uiautomator': CommandFunctionTuple(
723 AddUIAutomatorTestOptions, RunTestsCommand),
[email protected]3dbdfa42013-08-08 01:08:14724 'monkey': CommandFunctionTuple(
725 AddMonkeyTestOptions, RunTestsCommand),
[email protected]ec3170b2013-08-14 14:39:47726 'perf': CommandFunctionTuple(
727 AddPerfTestOptions, RunTestsCommand),
[email protected]fbe29322013-07-09 09:03:26728 'help': CommandFunctionTuple(lambda option_parser: None, HelpCommand)
729 }
730
731
[email protected]fbe29322013-07-09 09:03:26732def main(argv):
[email protected]803f65a72013-08-20 19:11:30733 option_parser = command_option_parser.CommandOptionParser(
734 commands_dict=VALID_COMMANDS)
735 return command_option_parser.ParseAndExecute(option_parser)
[email protected]fbe29322013-07-09 09:03:26736
[email protected]fbe29322013-07-09 09:03:26737
738if __name__ == '__main__':
739 sys.exit(main(sys.argv))