The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2008, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 17 | """Command line utility for running Android tests |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 18 | |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 19 | runtest helps automate the instructions for building and running tests |
| 20 | - It builds the corresponding test package for the code you want to test |
| 21 | - It pushes the test package to your device or emulator |
| 22 | - It launches InstrumentationTestRunner (or similar) to run the tests you |
| 23 | specify. |
| 24 | |
| 25 | runtest supports running tests whose attributes have been pre-defined in |
| 26 | _TEST_FILE_NAME files, (runtest <testname>), or by specifying the file |
| 27 | system path to the test to run (runtest --path <path>). |
| 28 | |
| 29 | Do runtest --help to see full list of options. |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 30 | """ |
| 31 | |
| 32 | # Python imports |
| 33 | import glob |
| 34 | import optparse |
| 35 | import os |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 36 | import re |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 37 | from sets import Set |
| 38 | import sys |
Brett Chabot | cdfaae1 | 2011-06-07 10:10:38 -0700 | [diff] [blame] | 39 | import time |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 40 | |
| 41 | # local imports |
| 42 | import adb_interface |
| 43 | import android_build |
| 44 | import coverage |
| 45 | import errors |
| 46 | import logger |
| 47 | import run_command |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 48 | from test_defs import test_defs |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 49 | from test_defs import test_walker |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class TestRunner(object): |
| 53 | """Command line utility class for running pre-defined Android test(s).""" |
| 54 | |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame] | 55 | _TEST_FILE_NAME = "test_defs.xml" |
| 56 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 57 | # file path to android core platform tests, relative to android build root |
| 58 | # TODO move these test data files to another directory |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame] | 59 | _CORE_TEST_PATH = os.path.join("development", "testrunner", |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame] | 60 | _TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 61 | |
| 62 | # vendor glob file path patterns to tests, relative to android |
| 63 | # build root |
| 64 | _VENDOR_TEST_PATH = os.path.join("vendor", "*", "tests", "testinfo", |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame] | 65 | _TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 66 | |
| 67 | _RUNTEST_USAGE = ( |
| 68 | "usage: runtest.py [options] short-test-name[s]\n\n" |
| 69 | "The runtest script works in two ways. You can query it " |
| 70 | "for a list of tests, or you can launch one or more tests.") |
| 71 | |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 72 | # default value for make -jX |
Brett Chabot | 12db436 | 2012-01-17 16:03:49 -0800 | [diff] [blame] | 73 | _DEFAULT_JOBS = 16 |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 74 | |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 75 | _DALVIK_VERIFIER_OFF_PROP = "dalvik.vm.dexopt-flags = v=n" |
| 76 | |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 77 | # regular expression to match install: statements in make output |
| 78 | _RE_MAKE_INSTALL = re.compile(r'Install:\s(.+)') |
| 79 | |
| 80 | # regular expression to find remote device path from a file path relative |
| 81 | # to build root |
| 82 | _RE_MAKE_INSTALL_PATH = re.compile(r'out\/target\/product\/\w+\/(.+)$') |
| 83 | |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 84 | def __init__(self): |
| 85 | # disable logging of timestamp |
Niko Catania | 2e990b9 | 2009-04-02 16:52:26 -0700 | [diff] [blame] | 86 | self._root_path = android_build.GetTop() |
Nicolas Catania | 97b24c4 | 2009-04-22 11:08:32 -0700 | [diff] [blame] | 87 | logger.SetTimestampLogging(False) |
Brett Chabot | 3ae5f8a | 2009-06-28 12:00:47 -0700 | [diff] [blame] | 88 | self._adb = None |
| 89 | self._known_tests = None |
| 90 | self._options = None |
| 91 | self._test_args = None |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 92 | self._tests_to_run = None |
Brett Chabot | 72731f3 | 2009-03-31 11:14:05 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 94 | def _ProcessOptions(self): |
| 95 | """Processes command-line options.""" |
| 96 | # TODO error messages on once-only or mutually-exclusive options. |
| 97 | user_test_default = os.path.join(os.environ.get("HOME"), ".android", |
Brett Chabot | f61f43e | 2009-04-02 11:52:48 -0700 | [diff] [blame] | 98 | self._TEST_FILE_NAME) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 99 | |
| 100 | parser = optparse.OptionParser(usage=self._RUNTEST_USAGE) |
| 101 | |
| 102 | parser.add_option("-l", "--list-tests", dest="only_list_tests", |
| 103 | default=False, action="store_true", |
| 104 | help="To view the list of tests") |
| 105 | parser.add_option("-b", "--skip-build", dest="skip_build", default=False, |
| 106 | action="store_true", help="Skip build - just launch") |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 107 | parser.add_option("-j", "--jobs", dest="make_jobs", |
| 108 | metavar="X", default=self._DEFAULT_JOBS, |
| 109 | help="Number of make jobs to use when building") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 110 | parser.add_option("-n", "--skip_execute", dest="preview", default=False, |
| 111 | action="store_true", |
| 112 | help="Do not execute, just preview commands") |
| 113 | parser.add_option("-r", "--raw-mode", dest="raw_mode", default=False, |
| 114 | action="store_true", |
| 115 | help="Raw mode (for output to other tools)") |
| 116 | parser.add_option("-a", "--suite-assign", dest="suite_assign_mode", |
| 117 | default=False, action="store_true", |
| 118 | help="Suite assignment (for details & usage see " |
| 119 | "InstrumentationTestRunner)") |
| 120 | parser.add_option("-v", "--verbose", dest="verbose", default=False, |
| 121 | action="store_true", |
| 122 | help="Increase verbosity of %s" % sys.argv[0]) |
| 123 | parser.add_option("-w", "--wait-for-debugger", dest="wait_for_debugger", |
| 124 | default=False, action="store_true", |
| 125 | help="Wait for debugger before launching tests") |
| 126 | parser.add_option("-c", "--test-class", dest="test_class", |
| 127 | help="Restrict test to a specific class") |
| 128 | parser.add_option("-m", "--test-method", dest="test_method", |
| 129 | help="Restrict test to a specific method") |
Brett Chabot | 8a101cb | 2009-05-05 12:56:39 -0700 | [diff] [blame] | 130 | parser.add_option("-p", "--test-package", dest="test_package", |
| 131 | help="Restrict test to a specific java package") |
| 132 | parser.add_option("-z", "--size", dest="test_size", |
| 133 | help="Restrict test to a specific test size") |
Brett Chabot | c061154 | 2010-02-20 20:09:58 -0800 | [diff] [blame] | 134 | parser.add_option("--annotation", dest="test_annotation", |
| 135 | help="Include only those tests tagged with a specific" |
| 136 | " annotation") |
| 137 | parser.add_option("--not-annotation", dest="test_not_annotation", |
Brett Chabot | 2e16fbc | 2010-02-23 12:28:27 -0800 | [diff] [blame] | 138 | help="Exclude any tests tagged with a specific" |
Brett Chabot | c061154 | 2010-02-20 20:09:58 -0800 | [diff] [blame] | 139 | " annotation") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 140 | parser.add_option("-u", "--user-tests-file", dest="user_tests_file", |
| 141 | metavar="FILE", default=user_test_default, |
| 142 | help="Alternate source of user test definitions") |
| 143 | parser.add_option("-o", "--coverage", dest="coverage", |
| 144 | default=False, action="store_true", |
| 145 | help="Generate code coverage metrics for test(s)") |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 146 | parser.add_option("-x", "--path", dest="test_path", |
| 147 | help="Run test(s) at given file system path") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 148 | parser.add_option("-t", "--all-tests", dest="all_tests", |
| 149 | default=False, action="store_true", |
| 150 | help="Run all defined tests") |
| 151 | parser.add_option("--continuous", dest="continuous_tests", |
| 152 | default=False, action="store_true", |
| 153 | help="Run all tests defined as part of the continuous " |
| 154 | "test set") |
Wei-Ta Chen | 97752d4 | 2009-05-21 16:24:04 -0700 | [diff] [blame] | 155 | parser.add_option("--timeout", dest="timeout", |
| 156 | default=300, help="Set a timeout limit (in sec) for " |
| 157 | "running native tests on a device (default: 300 secs)") |
Brett Chabot | 4a5d9f1 | 2010-02-18 20:01:11 -0800 | [diff] [blame] | 158 | parser.add_option("--suite", dest="suite", |
Brett Chabot | 49b7711 | 2009-06-02 11:46:04 -0700 | [diff] [blame] | 159 | help="Run all tests defined as part of the " |
Brett Chabot | 4a5d9f1 | 2010-02-18 20:01:11 -0800 | [diff] [blame] | 160 | "the given test suite") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 161 | group = optparse.OptionGroup( |
| 162 | parser, "Targets", "Use these options to direct tests to a specific " |
| 163 | "Android target") |
| 164 | group.add_option("-e", "--emulator", dest="emulator", default=False, |
| 165 | action="store_true", help="use emulator") |
| 166 | group.add_option("-d", "--device", dest="device", default=False, |
| 167 | action="store_true", help="use device") |
| 168 | group.add_option("-s", "--serial", dest="serial", |
| 169 | help="use specific serial") |
| 170 | parser.add_option_group(group) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 171 | self._options, self._test_args = parser.parse_args() |
| 172 | |
Brett Chabot | 49b7711 | 2009-06-02 11:46:04 -0700 | [diff] [blame] | 173 | if (not self._options.only_list_tests |
| 174 | and not self._options.all_tests |
| 175 | and not self._options.continuous_tests |
Brett Chabot | 4a5d9f1 | 2010-02-18 20:01:11 -0800 | [diff] [blame] | 176 | and not self._options.suite |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 177 | and not self._options.test_path |
Brett Chabot | 49b7711 | 2009-06-02 11:46:04 -0700 | [diff] [blame] | 178 | and len(self._test_args) < 1): |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 179 | parser.print_help() |
| 180 | logger.SilentLog("at least one test name must be specified") |
| 181 | raise errors.AbortError |
| 182 | |
| 183 | self._adb = adb_interface.AdbInterface() |
| 184 | if self._options.emulator: |
| 185 | self._adb.SetEmulatorTarget() |
| 186 | elif self._options.device: |
| 187 | self._adb.SetDeviceTarget() |
| 188 | elif self._options.serial is not None: |
| 189 | self._adb.SetTargetSerial(self._options.serial) |
| 190 | |
| 191 | if self._options.verbose: |
| 192 | logger.SetVerbose(True) |
| 193 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 194 | self._known_tests = self._ReadTests() |
| 195 | |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 196 | self._options.host_lib_path = android_build.GetHostLibraryPath() |
| 197 | self._options.test_data_path = android_build.GetTestAppPath() |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 198 | |
| 199 | def _ReadTests(self): |
| 200 | """Parses the set of test definition data. |
| 201 | |
| 202 | Returns: |
| 203 | A TestDefinitions object that contains the set of parsed tests. |
| 204 | Raises: |
| 205 | AbortError: If a fatal error occurred when parsing the tests. |
| 206 | """ |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 207 | try: |
| 208 | known_tests = test_defs.TestDefinitions() |
Brett Chabot | 3c9cefc | 2011-06-06 20:53:56 -0700 | [diff] [blame] | 209 | # only read tests when not in path mode |
| 210 | if not self._options.test_path: |
| 211 | core_test_path = os.path.join(self._root_path, self._CORE_TEST_PATH) |
| 212 | if os.path.isfile(core_test_path): |
| 213 | known_tests.Parse(core_test_path) |
| 214 | # read all <android root>/vendor/*/tests/testinfo/test_defs.xml paths |
| 215 | vendor_tests_pattern = os.path.join(self._root_path, |
| 216 | self._VENDOR_TEST_PATH) |
| 217 | test_file_paths = glob.glob(vendor_tests_pattern) |
| 218 | for test_file_path in test_file_paths: |
| 219 | known_tests.Parse(test_file_path) |
| 220 | if os.path.isfile(self._options.user_tests_file): |
| 221 | known_tests.Parse(self._options.user_tests_file) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 222 | return known_tests |
| 223 | except errors.ParseError: |
| 224 | raise errors.AbortError |
| 225 | |
| 226 | def _DumpTests(self): |
| 227 | """Prints out set of defined tests.""" |
Brett Chabot | be659c0 | 2009-09-21 17:48:26 -0700 | [diff] [blame] | 228 | print "The following tests are currently defined:\n" |
| 229 | print "%-25s %-40s %s" % ("name", "build path", "description") |
| 230 | print "-" * 80 |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 231 | for test in self._known_tests: |
Brett Chabot | be659c0 | 2009-09-21 17:48:26 -0700 | [diff] [blame] | 232 | print "%-25s %-40s %s" % (test.GetName(), test.GetBuildPath(), |
| 233 | test.GetDescription()) |
| 234 | print "\nSee %s for more information" % self._TEST_FILE_NAME |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 235 | |
| 236 | def _DoBuild(self): |
| 237 | logger.SilentLog("Building tests...") |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 238 | |
| 239 | tests = self._GetTestsToRun() |
Brett Chabot | b1eb5d2 | 2010-06-15 11:18:42 -0700 | [diff] [blame] | 240 | # turn off dalvik verifier if necessary |
| 241 | self._TurnOffVerifier(tests) |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 242 | self._DoFullBuild(tests) |
| 243 | |
Brian Muramatsu | 95cbc9e | 2012-01-10 12:06:12 -0800 | [diff] [blame] | 244 | target_set = [] |
Brian Muramatsu | 95cbc9e | 2012-01-10 12:06:12 -0800 | [diff] [blame] | 245 | |
| 246 | extra_args_set = [] |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 247 | for test_suite in tests: |
Niko Catania | a6dc2ab | 2009-04-03 14:12:46 -0700 | [diff] [blame] | 248 | self._AddBuildTarget(test_suite, target_set, extra_args_set) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 249 | |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 250 | if not self._options.preview: |
| 251 | self._adb.EnableAdbRoot() |
| 252 | else: |
| 253 | logger.Log("adb root") |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 254 | if target_set: |
| 255 | if self._options.coverage: |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 256 | coverage.EnableCoverageBuild() |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 257 | target_set.append("external/emma/Android.mk") |
| 258 | # TODO: detect if external/emma exists |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 259 | |
Brian Muramatsu | 95cbc9e | 2012-01-10 12:06:12 -0800 | [diff] [blame] | 260 | target_build_string = " ".join(target_set) |
| 261 | extra_args_string = " ".join(extra_args_set) |
Brett Chabot | b1eb5d2 | 2010-06-15 11:18:42 -0700 | [diff] [blame] | 262 | |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 263 | # mmm cannot be used from python, so perform a similar operation using |
Brett Chabot | 2b6643b | 2009-04-07 18:35:27 -0700 | [diff] [blame] | 264 | # ONE_SHOT_MAKEFILE |
Brett Chabot | 546a328 | 2011-06-07 19:19:30 -0700 | [diff] [blame] | 265 | cmd = 'ONE_SHOT_MAKEFILE="%s" make -j%s -C "%s" all_modules %s' % ( |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 266 | target_build_string, self._options.make_jobs, self._root_path, |
| 267 | extra_args_string) |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 268 | logger.Log(cmd) |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 269 | if not self._options.preview: |
| 270 | output = run_command.RunCommand(cmd, return_output=True, timeout_time=600) |
| 271 | self._DoInstall(output) |
Niko Catania | a6dc2ab | 2009-04-03 14:12:46 -0700 | [diff] [blame] | 272 | |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 273 | def _DoInstall(self, make_output): |
| 274 | """Install artifacts from build onto device. |
| 275 | |
| 276 | Looks for 'install:' text from make output to find artifacts to install. |
| 277 | |
| 278 | Args: |
| 279 | make_output: stdout from make command |
| 280 | """ |
| 281 | for line in make_output.split("\n"): |
| 282 | m = self._RE_MAKE_INSTALL.match(line) |
| 283 | if m: |
| 284 | install_path = m.group(1) |
| 285 | if install_path.endswith(".apk"): |
| 286 | abs_install_path = os.path.join(self._root_path, install_path) |
| 287 | logger.Log("adb install -r %s" % abs_install_path) |
| 288 | logger.Log(self._adb.Install(abs_install_path)) |
| 289 | else: |
| 290 | self._PushInstallFileToDevice(install_path) |
| 291 | |
| 292 | def _PushInstallFileToDevice(self, install_path): |
| 293 | m = self._RE_MAKE_INSTALL_PATH.match(install_path) |
| 294 | if m: |
| 295 | remote_path = m.group(1) |
| 296 | abs_install_path = os.path.join(self._root_path, install_path) |
| 297 | logger.Log("adb push %s %s", abs_install_path, remote_path) |
| 298 | self._adb.Push(abs_install_path, remote_path) |
| 299 | else: |
| 300 | logger.Log("Error: Failed to recognize path of file to install %s" % install_path) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 301 | |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 302 | def _DoFullBuild(self, tests): |
| 303 | """If necessary, run a full 'make' command for the tests that need it.""" |
| 304 | extra_args_set = Set() |
| 305 | |
| 306 | # hack to build cts dependencies |
| 307 | # TODO: remove this when cts dependencies are removed |
| 308 | if self._IsCtsTests(tests): |
| 309 | # need to use make since these fail building with ONE_SHOT_MAKEFILE |
| 310 | extra_args_set.add('CtsTestStubs') |
| 311 | extra_args_set.add('android.core.tests.runner') |
| 312 | for test in tests: |
| 313 | if test.IsFullMake(): |
| 314 | if test.GetExtraBuildArgs(): |
| 315 | # extra args contains the args to pass to 'make' |
| 316 | extra_args_set.add(test.GetExtraBuildArgs()) |
| 317 | else: |
| 318 | logger.Log("Warning: test %s needs a full build but does not specify" |
| 319 | " extra_build_args" % test.GetName()) |
| 320 | |
| 321 | # check if there is actually any tests that required a full build |
| 322 | if extra_args_set: |
| 323 | cmd = ('make -j%s %s' % (self._options.make_jobs, |
| 324 | ' '.join(list(extra_args_set)))) |
| 325 | logger.Log(cmd) |
| 326 | if not self._options.preview: |
| 327 | old_dir = os.getcwd() |
| 328 | os.chdir(self._root_path) |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 329 | output = run_command.RunCommand(cmd, return_output=False) |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 330 | os.chdir(old_dir) |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 331 | self._DoInstall(output) |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 332 | |
Niko Catania | a6dc2ab | 2009-04-03 14:12:46 -0700 | [diff] [blame] | 333 | def _AddBuildTarget(self, test_suite, target_set, extra_args_set): |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 334 | if not test_suite.IsFullMake(): |
| 335 | build_dir = test_suite.GetBuildPath() |
| 336 | if self._AddBuildTargetPath(build_dir, target_set): |
Brian Muramatsu | 95cbc9e | 2012-01-10 12:06:12 -0800 | [diff] [blame] | 337 | extra_args_set.append(test_suite.GetExtraBuildArgs()) |
Brett Chabot | 8dc9eb8 | 2010-04-15 15:43:04 -0700 | [diff] [blame] | 338 | for path in test_suite.GetBuildDependencies(self._options): |
| 339 | self._AddBuildTargetPath(path, target_set) |
Brett Chabot | 2b6643b | 2009-04-07 18:35:27 -0700 | [diff] [blame] | 340 | |
| 341 | def _AddBuildTargetPath(self, build_dir, target_set): |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 342 | if build_dir is not None: |
| 343 | build_file_path = os.path.join(build_dir, "Android.mk") |
| 344 | if os.path.isfile(os.path.join(self._root_path, build_file_path)): |
Brian Muramatsu | 95cbc9e | 2012-01-10 12:06:12 -0800 | [diff] [blame] | 345 | target_set.append(build_file_path) |
Brett Chabot | 2b6643b | 2009-04-07 18:35:27 -0700 | [diff] [blame] | 346 | return True |
Brett Chabot | e00595b | 2009-10-21 20:01:31 -0700 | [diff] [blame] | 347 | else: |
| 348 | logger.Log("%s has no Android.mk, skipping" % build_dir) |
Brett Chabot | 2b6643b | 2009-04-07 18:35:27 -0700 | [diff] [blame] | 349 | return False |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 350 | |
| 351 | def _GetTestsToRun(self): |
| 352 | """Get a list of TestSuite objects to run, based on command line args.""" |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 353 | if self._tests_to_run: |
| 354 | return self._tests_to_run |
| 355 | |
| 356 | self._tests_to_run = [] |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 357 | if self._options.all_tests: |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 358 | self._tests_to_run = self._known_tests.GetTests() |
Brett Chabot | 49b7711 | 2009-06-02 11:46:04 -0700 | [diff] [blame] | 359 | elif self._options.continuous_tests: |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 360 | self._tests_to_run = self._known_tests.GetContinuousTests() |
Brett Chabot | 4a5d9f1 | 2010-02-18 20:01:11 -0800 | [diff] [blame] | 361 | elif self._options.suite: |
| 362 | self._tests_to_run = \ |
| 363 | self._known_tests.GetTestsInSuite(self._options.suite) |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 364 | elif self._options.test_path: |
| 365 | walker = test_walker.TestWalker() |
| 366 | self._tests_to_run = walker.FindTests(self._options.test_path) |
| 367 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 368 | for name in self._test_args: |
| 369 | test = self._known_tests.GetTest(name) |
| 370 | if test is None: |
| 371 | logger.Log("Error: Could not find test %s" % name) |
| 372 | self._DumpTests() |
| 373 | raise errors.AbortError |
Brett Chabot | 59b4778 | 2009-10-21 17:23:01 -0700 | [diff] [blame] | 374 | self._tests_to_run.append(test) |
| 375 | return self._tests_to_run |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 376 | |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 377 | def _IsCtsTests(self, test_list): |
| 378 | """Check if any cts tests are included in given list of tests to run.""" |
| 379 | for test in test_list: |
Brett Chabot | 4a5d9f1 | 2010-02-18 20:01:11 -0800 | [diff] [blame] | 380 | if test.GetSuite() == 'cts': |
Brett Chabot | 2477b38 | 2009-09-23 18:05:28 -0700 | [diff] [blame] | 381 | return True |
| 382 | return False |
| 383 | |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 384 | def _TurnOffVerifier(self, test_list): |
| 385 | """Turn off the dalvik verifier if needed by given tests. |
| 386 | |
| 387 | If one or more tests needs dalvik verifier off, and it is not already off, |
| 388 | turns off verifier and reboots device to allow change to take effect. |
| 389 | """ |
Brett Chabot | 7454171 | 2012-08-31 18:39:00 -0700 | [diff] [blame] | 390 | # hack to check if these are frameworks/base tests. If so, turn off verifier |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 391 | # to allow framework tests to access package-private framework api |
| 392 | framework_test = False |
| 393 | for test in test_list: |
| 394 | if os.path.commonprefix([test.GetBuildPath(), "frameworks/base"]): |
| 395 | framework_test = True |
| 396 | if framework_test: |
| 397 | # check if verifier is off already - to avoid the reboot if not |
| 398 | # necessary |
| 399 | output = self._adb.SendShellCommand("cat /data/local.prop") |
| 400 | if not self._DALVIK_VERIFIER_OFF_PROP in output: |
| 401 | if self._options.preview: |
| 402 | logger.Log("adb shell \"echo %s >> /data/local.prop\"" |
| 403 | % self._DALVIK_VERIFIER_OFF_PROP) |
Brett Chabot | 25dfd79 | 2012-02-16 15:51:43 -0800 | [diff] [blame] | 404 | logger.Log("adb shell chmod 644 /data/local.prop") |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 405 | logger.Log("adb reboot") |
| 406 | logger.Log("adb wait-for-device") |
| 407 | else: |
| 408 | logger.Log("Turning off dalvik verifier and rebooting") |
| 409 | self._adb.SendShellCommand("\"echo %s >> /data/local.prop\"" |
| 410 | % self._DALVIK_VERIFIER_OFF_PROP) |
Brett Chabot | 25dfd79 | 2012-02-16 15:51:43 -0800 | [diff] [blame] | 411 | |
| 412 | self._ChmodReboot() |
| 413 | elif not self._options.preview: |
| 414 | # check the permissions on the file |
| 415 | permout = self._adb.SendShellCommand("ls -l /data/local.prop") |
| 416 | if not "-rw-r--r--" in permout: |
| 417 | logger.Log("Fixing permissions on /data/local.prop and rebooting") |
| 418 | self._ChmodReboot() |
| 419 | |
| 420 | def _ChmodReboot(self): |
| 421 | """Perform a chmod of /data/local.prop and reboot. |
| 422 | """ |
| 423 | self._adb.SendShellCommand("chmod 644 /data/local.prop") |
| 424 | self._adb.SendCommand("reboot") |
| 425 | # wait for device to go offline |
| 426 | time.sleep(10) |
| 427 | self._adb.SendCommand("wait-for-device", timeout_time=60, |
| 428 | retry_count=3) |
| 429 | self._adb.EnableAdbRoot() |
| 430 | |
Brett Chabot | ccae47d | 2010-06-14 15:19:25 -0700 | [diff] [blame] | 431 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 432 | def RunTests(self): |
| 433 | """Main entry method - executes the tests according to command line args.""" |
| 434 | try: |
| 435 | run_command.SetAbortOnError() |
| 436 | self._ProcessOptions() |
| 437 | if self._options.only_list_tests: |
| 438 | self._DumpTests() |
| 439 | return |
| 440 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 441 | if not self._options.skip_build: |
| 442 | self._DoBuild() |
| 443 | |
| 444 | for test_suite in self._GetTestsToRun(): |
Brett Chabot | 920e9fe | 2010-01-21 17:30:47 -0800 | [diff] [blame] | 445 | try: |
| 446 | test_suite.Run(self._options, self._adb) |
| 447 | except errors.WaitForResponseTimedOutError: |
| 448 | logger.Log("Timed out waiting for response") |
Brett Chabot | 764d3fa | 2009-06-25 17:57:31 -0700 | [diff] [blame] | 449 | |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 450 | except KeyboardInterrupt: |
| 451 | logger.Log("Exiting...") |
Brett Chabot | 3ae5f8a | 2009-06-28 12:00:47 -0700 | [diff] [blame] | 452 | except errors.AbortError, error: |
| 453 | logger.Log(error.msg) |
The Android Open Source Project | 6ffae01 | 2009-03-18 17:39:43 -0700 | [diff] [blame] | 454 | logger.SilentLog("Exiting due to AbortError...") |
| 455 | except errors.WaitForResponseTimedOutError: |
| 456 | logger.Log("Timed out waiting for response") |
| 457 | |
| 458 | |
| 459 | def RunTests(): |
| 460 | runner = TestRunner() |
| 461 | runner.RunTests() |
| 462 | |
| 463 | if __name__ == "__main__": |
| 464 | RunTests() |