OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import os |
| 7 import re |
| 8 import shutil |
| 9 import sys |
| 10 import tempfile |
| 11 |
| 12 from pylib import constants |
| 13 from pylib.base import base_test_result |
| 14 from pylib.base import test_instance |
| 15 from pylib.utils import apk_helper |
| 16 |
| 17 sys.path.append(os.path.join( |
| 18 constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib', 'common')) |
| 19 import unittest_util |
| 20 |
| 21 |
| 22 BROWSER_TEST_SUITES = [ |
| 23 'components_browsertests', |
| 24 'content_browsertests', |
| 25 ] |
| 26 |
| 27 |
| 28 _DEFAULT_ISOLATE_FILE_PATHS = { |
| 29 'base_unittests': 'base/base_unittests.isolate', |
| 30 'blink_heap_unittests': |
| 31 'third_party/WebKit/Source/platform/heap/BlinkHeapUnitTests.isolate', |
| 32 'breakpad_unittests': 'breakpad/breakpad_unittests.isolate', |
| 33 'cc_perftests': 'cc/cc_perftests.isolate', |
| 34 'components_browsertests': 'components/components_browsertests.isolate', |
| 35 'components_unittests': 'components/components_unittests.isolate', |
| 36 'content_browsertests': 'content/content_browsertests.isolate', |
| 37 'content_unittests': 'content/content_unittests.isolate', |
| 38 'media_perftests': 'media/media_perftests.isolate', |
| 39 'media_unittests': 'media/media_unittests.isolate', |
| 40 'midi_unittests': 'media/midi/midi_unittests.isolate', |
| 41 'net_unittests': 'net/net_unittests.isolate', |
| 42 'sql_unittests': 'sql/sql_unittests.isolate', |
| 43 'sync_unit_tests': 'sync/sync_unit_tests.isolate', |
| 44 'ui_base_unittests': 'ui/base/ui_base_tests.isolate', |
| 45 'unit_tests': 'chrome/unit_tests.isolate', |
| 46 'webkit_unit_tests': |
| 47 'third_party/WebKit/Source/web/WebKitUnitTests.isolate', |
| 48 } |
| 49 |
| 50 |
| 51 # Used for filtering large data deps at a finer grain than what's allowed in |
| 52 # isolate files since pushing deps to devices is expensive. |
| 53 # Wildcards are allowed. |
| 54 _DEPS_EXCLUSION_LIST = [ |
| 55 'chrome/test/data/extensions/api_test', |
| 56 'chrome/test/data/extensions/secure_shell', |
| 57 'chrome/test/data/firefox*', |
| 58 'chrome/test/data/gpu', |
| 59 'chrome/test/data/image_decoding', |
| 60 'chrome/test/data/import', |
| 61 'chrome/test/data/page_cycler', |
| 62 'chrome/test/data/perf', |
| 63 'chrome/test/data/pyauto_private', |
| 64 'chrome/test/data/safari_import', |
| 65 'chrome/test/data/scroll', |
| 66 'chrome/test/data/third_party', |
| 67 'third_party/hunspell_dictionaries/*.dic', |
| 68 # crbug.com/258690 |
| 69 'webkit/data/bmp_decoder', |
| 70 'webkit/data/ico_decoder', |
| 71 ] |
| 72 |
| 73 |
| 74 _EXTRA_NATIVE_TEST_ACTIVITY = ( |
| 75 'org.chromium.native_test.NativeTestInstrumentationTestRunner.' |
| 76 'NativeTestActivity') |
| 77 _EXTRA_SHARD_SIZE_LIMIT =( |
| 78 'org.chromium.native_test.NativeTestInstrumentationTestRunner.' |
| 79 'ShardSizeLimit') |
| 80 |
| 81 # TODO(jbudorick): Remove these once we're no longer parsing stdout to generate |
| 82 # results. |
| 83 _RE_TEST_STATUS = re.compile( |
| 84 r'\[ +((?:RUN)|(?:FAILED)|(?:OK)) +\] ?([^ ]+)(?: \((\d+) ms\))?$') |
| 85 _RE_TEST_RUN_STATUS = re.compile( |
| 86 r'\[ +(PASSED|RUNNER_FAILED|CRASHED) \] ?[^ ]+') |
| 87 |
| 88 |
| 89 # TODO(jbudorick): Make this a class method of GtestTestInstance once |
| 90 # test_package_apk and test_package_exe are gone. |
| 91 def ParseGTestListTests(raw_list): |
| 92 """Parses a raw test list as provided by --gtest_list_tests. |
| 93 |
| 94 Args: |
| 95 raw_list: The raw test listing with the following format: |
| 96 |
| 97 IPCChannelTest. |
| 98 SendMessageInChannelConnected |
| 99 IPCSyncChannelTest. |
| 100 Simple |
| 101 DISABLED_SendWithTimeoutMixedOKAndTimeout |
| 102 |
| 103 Returns: |
| 104 A list of all tests. For the above raw listing: |
| 105 |
| 106 [IPCChannelTest.SendMessageInChannelConnected, IPCSyncChannelTest.Simple, |
| 107 IPCSyncChannelTest.DISABLED_SendWithTimeoutMixedOKAndTimeout] |
| 108 """ |
| 109 ret = [] |
| 110 current = '' |
| 111 for test in raw_list: |
| 112 if not test: |
| 113 continue |
| 114 if test[0] != ' ': |
| 115 test_case = test.split()[0] |
| 116 if test_case.endswith('.'): |
| 117 current = test_case |
| 118 elif not 'YOU HAVE' in test: |
| 119 test_name = test.split()[0] |
| 120 ret += [current + test_name] |
| 121 return ret |
| 122 |
| 123 |
| 124 class GtestTestInstance(test_instance.TestInstance): |
| 125 |
| 126 def __init__(self, args, isolate_delegate, error_func): |
| 127 super(GtestTestInstance, self).__init__() |
| 128 # TODO(jbudorick): Support multiple test suites. |
| 129 if len(args.suite_name) > 1: |
| 130 raise ValueError('Platform mode currently supports only 1 gtest suite') |
| 131 self._suite = args.suite_name[0] |
| 132 |
| 133 self._apk_path = os.path.join( |
| 134 constants.GetOutDirectory(), '%s_apk' % self._suite, |
| 135 '%s-debug.apk' % self._suite) |
| 136 self._exe_path = os.path.join(constants.GetOutDirectory(), |
| 137 self._suite) |
| 138 if not os.path.exists(self._apk_path): |
| 139 self._apk_path = None |
| 140 self._activity = None |
| 141 self._package = None |
| 142 self._runner = None |
| 143 else: |
| 144 helper = apk_helper.ApkHelper(self._apk_path) |
| 145 self._activity = helper.GetActivityName() |
| 146 self._package = helper.GetPackageName() |
| 147 self._runner = helper.GetInstrumentationName() |
| 148 self._extras = { |
| 149 _EXTRA_NATIVE_TEST_ACTIVITY: self._activity, |
| 150 } |
| 151 if self._suite in BROWSER_TEST_SUITES: |
| 152 self._extras[_EXTRA_SHARD_SIZE_LIMIT] = 1 |
| 153 |
| 154 if not os.path.exists(self._exe_path): |
| 155 self._exe_path = None |
| 156 if not self._apk_path and not self._exe_path: |
| 157 error_func('Could not find apk or executable for %s' % self._suite) |
| 158 |
| 159 self._data_deps = [] |
| 160 if args.test_filter: |
| 161 self._gtest_filter = args.test_filter |
| 162 elif args.test_filter_file: |
| 163 with open(args.test_filter_file, 'r') as f: |
| 164 self._gtest_filter = ':'.join(l.strip() for l in f) |
| 165 else: |
| 166 self._gtest_filter = None |
| 167 |
| 168 if not args.isolate_file_path: |
| 169 default_isolate_file_path = _DEFAULT_ISOLATE_FILE_PATHS.get(self._suite) |
| 170 if default_isolate_file_path: |
| 171 args.isolate_file_path = os.path.join( |
| 172 constants.DIR_SOURCE_ROOT, default_isolate_file_path) |
| 173 |
| 174 if args.isolate_file_path: |
| 175 self._isolate_abs_path = os.path.abspath(args.isolate_file_path) |
| 176 self._isolate_delegate = isolate_delegate |
| 177 self._isolated_abs_path = os.path.join( |
| 178 constants.GetOutDirectory(), '%s.isolated' % self._suite) |
| 179 else: |
| 180 logging.warning('No isolate file provided. No data deps will be pushed.'); |
| 181 self._isolate_delegate = None |
| 182 |
| 183 if args.app_data_files: |
| 184 self._app_data_files = args.app_data_files |
| 185 if args.app_data_file_dir: |
| 186 self._app_data_file_dir = args.app_data_file_dir |
| 187 else: |
| 188 self._app_data_file_dir = tempfile.mkdtemp() |
| 189 logging.critical('Saving app files to %s', self._app_data_file_dir) |
| 190 else: |
| 191 self._app_data_files = None |
| 192 self._app_data_file_dir = None |
| 193 |
| 194 #override |
| 195 def TestType(self): |
| 196 return 'gtest' |
| 197 |
| 198 #override |
| 199 def SetUp(self): |
| 200 """Map data dependencies via isolate.""" |
| 201 if self._isolate_delegate: |
| 202 self._isolate_delegate.Remap( |
| 203 self._isolate_abs_path, self._isolated_abs_path) |
| 204 self._isolate_delegate.PurgeExcluded(_DEPS_EXCLUSION_LIST) |
| 205 self._isolate_delegate.MoveOutputDeps() |
| 206 dest_dir = None |
| 207 if self._suite == 'breakpad_unittests': |
| 208 dest_dir = '/data/local/tmp/' |
| 209 self._data_deps.extend([(constants.ISOLATE_DEPS_DIR, dest_dir)]) |
| 210 |
| 211 |
| 212 def GetDataDependencies(self): |
| 213 """Returns the test suite's data dependencies. |
| 214 |
| 215 Returns: |
| 216 A list of (host_path, device_path) tuples to push. If device_path is |
| 217 None, the client is responsible for determining where to push the file. |
| 218 """ |
| 219 return self._data_deps |
| 220 |
| 221 def FilterTests(self, test_list, disabled_prefixes=None): |
| 222 """Filters |test_list| based on prefixes and, if present, a filter string. |
| 223 |
| 224 Args: |
| 225 test_list: The list of tests to filter. |
| 226 disabled_prefixes: A list of test prefixes to filter. Defaults to |
| 227 DISABLED_, FLAKY_, FAILS_, PRE_, and MANUAL_ |
| 228 Returns: |
| 229 A filtered list of tests to run. |
| 230 """ |
| 231 gtest_filter_strings = [ |
| 232 self._GenerateDisabledFilterString(disabled_prefixes)] |
| 233 if self._gtest_filter: |
| 234 gtest_filter_strings.append(self._gtest_filter) |
| 235 |
| 236 filtered_test_list = test_list |
| 237 for gtest_filter_string in gtest_filter_strings: |
| 238 logging.debug('Filtering tests using: %s', gtest_filter_string) |
| 239 filtered_test_list = unittest_util.FilterTestNames( |
| 240 filtered_test_list, gtest_filter_string) |
| 241 return filtered_test_list |
| 242 |
| 243 def _GenerateDisabledFilterString(self, disabled_prefixes): |
| 244 disabled_filter_items = [] |
| 245 |
| 246 if disabled_prefixes is None: |
| 247 disabled_prefixes = ['DISABLED_', 'FLAKY_', 'FAILS_', 'PRE_', 'MANUAL_'] |
| 248 disabled_filter_items += ['%s*' % dp for dp in disabled_prefixes] |
| 249 disabled_filter_items += ['*.%s*' % dp for dp in disabled_prefixes] |
| 250 |
| 251 disabled_tests_file_path = os.path.join( |
| 252 constants.DIR_SOURCE_ROOT, 'build', 'android', 'pylib', 'gtest', |
| 253 'filter', '%s_disabled' % self._suite) |
| 254 if disabled_tests_file_path and os.path.exists(disabled_tests_file_path): |
| 255 with open(disabled_tests_file_path) as disabled_tests_file: |
| 256 disabled_filter_items += [ |
| 257 '%s' % l for l in (line.strip() for line in disabled_tests_file) |
| 258 if l and not l.startswith('#')] |
| 259 |
| 260 return '*-%s' % ':'.join(disabled_filter_items) |
| 261 |
| 262 def ParseGTestOutput(self, output): |
| 263 """Parses raw gtest output and returns a list of results. |
| 264 |
| 265 Args: |
| 266 output: A list of output lines. |
| 267 Returns: |
| 268 A list of base_test_result.BaseTestResults. |
| 269 """ |
| 270 results = [] |
| 271 for l in output: |
| 272 matcher = _RE_TEST_STATUS.match(l) |
| 273 if matcher: |
| 274 result_type = None |
| 275 if matcher.group(1) == 'OK': |
| 276 result_type = base_test_result.ResultType.PASS |
| 277 elif matcher.group(1) == 'FAILED': |
| 278 result_type = base_test_result.ResultType.FAIL |
| 279 |
| 280 if result_type: |
| 281 test_name = matcher.group(2) |
| 282 duration = matcher.group(3) if matcher.group(3) else 0 |
| 283 results.append(base_test_result.BaseTestResult( |
| 284 test_name, result_type, duration)) |
| 285 logging.info(l) |
| 286 return results |
| 287 |
| 288 #override |
| 289 def TearDown(self): |
| 290 """Clear the mappings created by SetUp.""" |
| 291 if self._isolate_delegate: |
| 292 self._isolate_delegate.Clear() |
| 293 |
| 294 @property |
| 295 def activity(self): |
| 296 return self._activity |
| 297 |
| 298 @property |
| 299 def apk(self): |
| 300 return self._apk_path |
| 301 |
| 302 @property |
| 303 def app_file_dir(self): |
| 304 return self._app_data_file_dir |
| 305 |
| 306 @property |
| 307 def app_files(self): |
| 308 return self._app_data_files |
| 309 |
| 310 @property |
| 311 def exe(self): |
| 312 return self._exe_path |
| 313 |
| 314 @property |
| 315 def extras(self): |
| 316 return self._extras |
| 317 |
| 318 @property |
| 319 def package(self): |
| 320 return self._package |
| 321 |
| 322 @property |
| 323 def runner(self): |
| 324 return self._runner |
| 325 |
| 326 @property |
| 327 def suite(self): |
| 328 return self._suite |
| 329 |
OLD | NEW |