| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Generates test runner factory and tests for GTests.""" | 5 """Generates test runner factory and tests for GTests.""" |
| 6 | 6 |
| 7 import fnmatch | 7 import fnmatch |
| 8 import glob | 8 import glob |
| 9 import logging | 9 import logging |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import sys | 12 import sys |
| 13 | 13 |
| 14 from pylib import android_commands | 14 from pylib import android_commands |
| 15 from pylib import cmd_helper | 15 from pylib import cmd_helper |
| 16 from pylib import constants | 16 from pylib import constants |
| 17 from pylib import ports | 17 from pylib import ports |
| 18 from pylib.base import base_test_result | 18 from pylib.base import base_test_result |
| 19 | 19 |
| 20 import gtest_config | 20 import gtest_config |
| 21 import test_package_apk |
| 22 import test_package_exe |
| 21 import test_runner | 23 import test_runner |
| 22 | 24 |
| 23 sys.path.insert(0, | 25 sys.path.insert(0, |
| 24 os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib')) | 26 os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib')) |
| 25 from common import unittest_util | 27 from common import unittest_util |
| 26 | 28 |
| 27 | 29 |
| 28 _ISOLATE_FILE_PATHS = { | 30 _ISOLATE_FILE_PATHS = { |
| 29 'base_unittests': 'base/base_unittests.isolate', | 31 'base_unittests': 'base/base_unittests.isolate', |
| 30 'breakpad_unittests': 'breakpad/breakpad_unittests.isolate', | 32 'breakpad_unittests': 'breakpad/breakpad_unittests.isolate', |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 143 |
| 142 # Move everything in PRODUCT_DIR to top level. | 144 # Move everything in PRODUCT_DIR to top level. |
| 143 deps_product_dir = os.path.join(constants.ISOLATE_DEPS_DIR, 'out', build_type) | 145 deps_product_dir = os.path.join(constants.ISOLATE_DEPS_DIR, 'out', build_type) |
| 144 if os.path.isdir(deps_product_dir): | 146 if os.path.isdir(deps_product_dir): |
| 145 for p in os.listdir(deps_product_dir): | 147 for p in os.listdir(deps_product_dir): |
| 146 shutil.move(os.path.join(deps_product_dir, p), constants.ISOLATE_DEPS_DIR) | 148 shutil.move(os.path.join(deps_product_dir, p), constants.ISOLATE_DEPS_DIR) |
| 147 os.rmdir(deps_product_dir) | 149 os.rmdir(deps_product_dir) |
| 148 os.rmdir(os.path.join(constants.ISOLATE_DEPS_DIR, 'out')) | 150 os.rmdir(os.path.join(constants.ISOLATE_DEPS_DIR, 'out')) |
| 149 | 151 |
| 150 | 152 |
| 151 def _GetSuitePath(use_exe_test_runner, suite_name, build_type): | |
| 152 """Get the absolute path to the test suite. | |
| 153 | |
| 154 Args: | |
| 155 use_exe_test_runner: If True, use the executable-based test runner. | |
| 156 suite_name: The suite name specified on the command line. | |
| 157 build_type: 'Release' or 'Debug'. | |
| 158 | |
| 159 Returns: | |
| 160 The absolute path of the given suite. | |
| 161 Ex. '/tmp/chrome/src/out/Debug/content_unittests_apk/' | |
| 162 'content_unittests-debug.apk' | |
| 163 | |
| 164 Raises: | |
| 165 Exception: If test suite not found. | |
| 166 """ | |
| 167 if use_exe_test_runner: | |
| 168 relpath = suite_name | |
| 169 else: | |
| 170 relpath = os.path.join(suite_name + '_apk', suite_name + '-debug.apk') | |
| 171 suite_path = os.path.join(cmd_helper.OutDirectory.get(), build_type, relpath) | |
| 172 | |
| 173 if not os.path.exists(suite_path): | |
| 174 raise Exception('Test suite %s not found in %s.\n' | |
| 175 'Supported test suites:\n %s\n' | |
| 176 'Ensure it has been built.\n' % | |
| 177 (suite_name, suite_path, | |
| 178 [s.name for s in gtest_config.STABLE_TEST_SUITES])) | |
| 179 | |
| 180 return suite_path | |
| 181 | |
| 182 | |
| 183 def _GetDisabledTestsFilterFromFile(suite_name): | 153 def _GetDisabledTestsFilterFromFile(suite_name): |
| 184 """Returns a gtest filter based on the *_disabled file. | 154 """Returns a gtest filter based on the *_disabled file. |
| 185 | 155 |
| 186 Args: | 156 Args: |
| 187 suite_name: Name of the test suite (e.g. base_unittests). | 157 suite_name: Name of the test suite (e.g. base_unittests). |
| 188 | 158 |
| 189 Returns: | 159 Returns: |
| 190 A gtest filter which excludes disabled tests. | 160 A gtest filter which excludes disabled tests. |
| 191 Example: '*-StackTrace.*:StringPrintfTest.StringPrintfMisc' | 161 Example: '*-StackTrace.*:StringPrintfTest.StringPrintfMisc' |
| 192 """ | 162 """ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 213 runner_factory: Callable that takes device and shard_index and returns | 183 runner_factory: Callable that takes device and shard_index and returns |
| 214 a TestRunner. | 184 a TestRunner. |
| 215 devices: A list of device ids. | 185 devices: A list of device ids. |
| 216 | 186 |
| 217 Returns: | 187 Returns: |
| 218 All the tests in the test suite. | 188 All the tests in the test suite. |
| 219 """ | 189 """ |
| 220 for device in devices: | 190 for device in devices: |
| 221 try: | 191 try: |
| 222 logging.info('Obtaining tests from %s', device) | 192 logging.info('Obtaining tests from %s', device) |
| 223 runner = runner_factory(device, 0) | 193 return runner_factory(device, 0).GetAllTests() |
| 224 runner.test_package.Install() | |
| 225 return runner.test_package.GetAllTests() | |
| 226 except (android_commands.errors.WaitForResponseTimedOutError, | 194 except (android_commands.errors.WaitForResponseTimedOutError, |
| 227 android_commands.errors.DeviceUnresponsiveError), e: | 195 android_commands.errors.DeviceUnresponsiveError), e: |
| 228 logging.warning('Failed obtaining tests from %s with exception: %s', | 196 logging.warning('Failed obtaining tests from %s with exception: %s', |
| 229 device, e) | 197 device, e) |
| 230 raise Exception('No device available to get the list of tests.') | 198 raise Exception('No device available to get the list of tests.') |
| 231 | 199 |
| 232 | 200 |
| 233 def _FilterTestsUsingPrefixes(all_tests, pre=False, manual=False): | 201 def _FilterTestsUsingPrefixes(all_tests, pre=False, manual=False): |
| 234 """Removes tests with disabled prefixes. | 202 """Removes tests with disabled prefixes. |
| 235 | 203 |
| 236 Args: | 204 Args: |
| 237 all_tests: List of tests to filter. | 205 all_tests: List of tests to filter. |
| 238 pre: If True, include tests with _PRE prefix. | 206 pre: If True, include tests with PRE_ prefix. |
| 239 manual: If True, include tests with _MANUAL prefix. | 207 manual: If True, include tests with MANUAL_ prefix. |
| 240 | 208 |
| 241 Returns: | 209 Returns: |
| 242 List of tests remaining. | 210 List of tests remaining. |
| 243 """ | 211 """ |
| 244 filtered_tests = [] | 212 filtered_tests = [] |
| 245 filter_prefixes = ['DISABLED_', 'FLAKY_', 'FAILS_'] | 213 filter_prefixes = ['DISABLED_', 'FLAKY_', 'FAILS_'] |
| 246 | 214 |
| 247 if not pre: | 215 if not pre: |
| 248 filter_prefixes.append('PRE_') | 216 filter_prefixes.append('PRE_') |
| 249 | 217 |
| 250 if not manual: | 218 if not manual: |
| 251 filter_prefixes.append('MANUAL_') | 219 filter_prefixes.append('MANUAL_') |
| 252 | 220 |
| 253 for t in all_tests: | 221 for t in all_tests: |
| 254 test_case, test = t.split('.', 1) | 222 test_case, test = t.split('.', 1) |
| 255 if not any([test_case.startswith(prefix) or test.startswith(prefix) for | 223 if not any([test_case.startswith(prefix) or test.startswith(prefix) for |
| 256 prefix in filter_prefixes]): | 224 prefix in filter_prefixes]): |
| 257 filtered_tests.append(t) | 225 filtered_tests.append(t) |
| 258 return filtered_tests | 226 return filtered_tests |
| 259 | 227 |
| 260 | 228 |
| 261 def GetTestsFiltered(suite_name, gtest_filter, runner_factory, devices): | 229 def _GetTestsFiltered(suite_name, gtest_filter, runner_factory, devices): |
| 262 """Get all tests in the suite and filter them. | 230 """Get all tests in the suite and filter them. |
| 263 | 231 |
| 264 Obtains a list of tests from the test package on the device, and | 232 Obtains a list of tests from the test package on the device, and |
| 265 applies the following filters in order: | 233 applies the following filters in order: |
| 266 1. Remove tests with disabled prefixes. | 234 1. Remove tests with disabled prefixes. |
| 267 2. Remove tests specified in the *_disabled files in the 'filter' dir | 235 2. Remove tests specified in the *_disabled files in the 'filter' dir |
| 268 3. Applies |gtest_filter|. | 236 3. Applies |gtest_filter|. |
| 269 | 237 |
| 270 Args: | 238 Args: |
| 271 suite_name: Name of the test suite (e.g. base_unittests). | 239 suite_name: Name of the test suite (e.g. base_unittests). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 282 tests, bool(gtest_filter), bool(gtest_filter)) | 250 tests, bool(gtest_filter), bool(gtest_filter)) |
| 283 tests = unittest_util.FilterTestNames( | 251 tests = unittest_util.FilterTestNames( |
| 284 tests, _GetDisabledTestsFilterFromFile(suite_name)) | 252 tests, _GetDisabledTestsFilterFromFile(suite_name)) |
| 285 | 253 |
| 286 if gtest_filter: | 254 if gtest_filter: |
| 287 tests = unittest_util.FilterTestNames(tests, gtest_filter) | 255 tests = unittest_util.FilterTestNames(tests, gtest_filter) |
| 288 | 256 |
| 289 return tests | 257 return tests |
| 290 | 258 |
| 291 | 259 |
| 292 def Setup(use_exe_test_runner, suite_name, test_arguments, timeout, | 260 def Setup(suite_name, test_arguments, timeout, |
| 293 cleanup_test_files, tool, build_type, push_deps, | 261 cleanup_test_files, tool, build_type, push_deps, |
| 294 gtest_filter): | 262 gtest_filter): |
| 295 """Create the test runner factory and tests. | 263 """Create the test runner factory and tests. |
| 296 | 264 |
| 297 Args: | 265 Args: |
| 298 use_exe_test_runner: If True, use the executable-based test runner. | |
| 299 suite_name: The suite name specified on the command line. | 266 suite_name: The suite name specified on the command line. |
| 300 test_arguments: Additional arguments to pass to the test binary. | 267 test_arguments: Additional arguments to pass to the test binary. |
| 301 timeout: Timeout for each test. | 268 timeout: Timeout for each test. |
| 302 cleanup_test_files: Whether or not to cleanup test files on device. | 269 cleanup_test_files: Whether or not to cleanup test files on device. |
| 303 tool: Name of the Valgrind tool. | 270 tool: Name of the Valgrind tool. |
| 304 build_type: 'Release' or 'Debug'. | 271 build_type: 'Release' or 'Debug'. |
| 305 push_deps: If True, push all dependencies to the device. | 272 push_deps: If True, push all dependencies to the device. |
| 306 gtest_filter: Filter for tests. | 273 gtest_filter: Filter for tests. |
| 307 | 274 |
| 308 Returns: | 275 Returns: |
| 309 A tuple of (TestRunnerFactory, tests). | 276 A tuple of (TestRunnerFactory, tests). |
| 310 """ | 277 """ |
| 311 | 278 |
| 312 if not ports.ResetTestServerPortAllocation(): | 279 if not ports.ResetTestServerPortAllocation(): |
| 313 raise Exception('Failed to reset test server port.') | 280 raise Exception('Failed to reset test server port.') |
| 314 | 281 |
| 315 suite_path = _GetSuitePath(use_exe_test_runner, suite_name, build_type) | 282 test_package = test_package_apk.TestPackageApk(suite_name, build_type) |
| 283 if not os.path.exists(test_package.suite_path): |
| 284 test_package = test_package_exe.TestPackageExecutable( |
| 285 suite_name, build_type) |
| 286 if not os.path.exists(test_package.suite_path): |
| 287 raise Exception( |
| 288 'Did not find %s target. Ensure it has been built.' % suite_name) |
| 289 logging.warning('Found target %s', test_package.suite_path) |
| 316 | 290 |
| 317 # TODO(gkanwar): This breaks the abstraction of having test_dispatcher.py deal | 291 _GenerateDepsDirUsingIsolate(suite_name, build_type) |
| 318 # entirely with the devices. Can we do this another way? | |
| 319 attached_devices = android_commands.GetAttachedDevices() | |
| 320 | 292 |
| 321 deps_dir = _GenerateDepsDirUsingIsolate(suite_name, build_type) | |
| 322 # Constructs a new TestRunner with the current options. | 293 # Constructs a new TestRunner with the current options. |
| 323 def TestRunnerFactory(device, shard_index): | 294 def TestRunnerFactory(device, shard_index): |
| 324 return test_runner.TestRunner( | 295 return test_runner.TestRunner( |
| 325 device, | 296 device, |
| 326 suite_path, | 297 test_package, |
| 327 test_arguments, | 298 test_arguments, |
| 328 timeout, | 299 timeout, |
| 329 cleanup_test_files, | 300 cleanup_test_files, |
| 330 tool, | 301 tool, |
| 331 build_type, | 302 build_type, |
| 332 push_deps, | 303 push_deps) |
| 333 constants.GTEST_TEST_PACKAGE_NAME, | |
| 334 constants.GTEST_TEST_ACTIVITY_NAME, | |
| 335 constants.GTEST_COMMAND_LINE_FILE) | |
| 336 | 304 |
| 337 # Get tests and split them up based on the number of devices. | 305 attached_devices = android_commands.GetAttachedDevices() |
| 338 tests = GetTestsFiltered(suite_name, gtest_filter, | 306 tests = _GetTestsFiltered(suite_name, gtest_filter, |
| 339 TestRunnerFactory, attached_devices) | 307 TestRunnerFactory, attached_devices) |
| 340 num_devices = len(attached_devices) | 308 # Coalesce unit tests into a single test per device |
| 341 tests = [':'.join(tests[i::num_devices]) for i in xrange(num_devices)] | 309 if suite_name != 'content_browsertests': |
| 342 tests = [t for t in tests if t] | 310 num_devices = len(attached_devices) |
| 311 tests = [':'.join(tests[i::num_devices]) for i in xrange(num_devices)] |
| 312 tests = [t for t in tests if t] |
| 343 | 313 |
| 344 return (TestRunnerFactory, tests) | 314 return (TestRunnerFactory, tests) |
| OLD | NEW |