OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 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 """Helper class for instrumenation test jar.""" |
| 6 # pylint: disable=W0702 |
| 7 |
| 8 import logging |
| 9 import os |
| 10 import pickle |
| 11 import re |
| 12 import sys |
| 13 |
| 14 from pylib import cmd_helper |
| 15 from pylib import constants |
| 16 from pylib.device import device_utils |
| 17 from pylib.utils import md5sum |
| 18 from pylib.utils import proguard |
| 19 |
| 20 sys.path.insert(0, |
| 21 os.path.join(constants.DIR_SOURCE_ROOT, |
| 22 'build', 'util', 'lib', 'common')) |
| 23 |
| 24 import unittest_util # pylint: disable=F0401 |
| 25 |
| 26 # If you change the cached output of proguard, increment this number |
| 27 PICKLE_FORMAT_VERSION = 4 |
| 28 |
| 29 |
| 30 class TestJar(object): |
| 31 _ANNOTATIONS = frozenset( |
| 32 ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', 'EnormousTest', |
| 33 'FlakyTest', 'DisabledTest', 'Manual', 'PerfTest', 'HostDrivenTest', |
| 34 'IntegrationTest']) |
| 35 _DEFAULT_ANNOTATION = 'SmallTest' |
| 36 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
| 37 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$') |
| 38 _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
| 39 _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
| 40 _PROGUARD_ANNOTATION_CONST_RE = ( |
| 41 re.compile(r'\s*?- Constant element value.*$')) |
| 42 _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
| 43 |
| 44 def __init__(self, jar_path): |
| 45 if not os.path.exists(jar_path): |
| 46 raise Exception('%s not found, please build it' % jar_path) |
| 47 |
| 48 self._PROGUARD_PATH = os.path.join(constants.ANDROID_SDK_ROOT, |
| 49 'tools/proguard/lib/proguard.jar') |
| 50 if not os.path.exists(self._PROGUARD_PATH): |
| 51 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
| 52 'external/proguard/lib/proguard.jar') |
| 53 self._jar_path = jar_path |
| 54 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' |
| 55 self._test_methods = {} |
| 56 if not self._GetCachedProguardData(): |
| 57 self._GetProguardData() |
| 58 |
| 59 def _GetCachedProguardData(self): |
| 60 if (os.path.exists(self._pickled_proguard_name) and |
| 61 (os.path.getmtime(self._pickled_proguard_name) > |
| 62 os.path.getmtime(self._jar_path))): |
| 63 logging.info('Loading cached proguard output from %s', |
| 64 self._pickled_proguard_name) |
| 65 try: |
| 66 with open(self._pickled_proguard_name, 'r') as r: |
| 67 d = pickle.loads(r.read()) |
| 68 jar_md5 = md5sum.CalculateHostMd5Sums( |
| 69 self._jar_path)[os.path.realpath(self._jar_path)] |
| 70 if (d['JAR_MD5SUM'] == jar_md5 and |
| 71 d['VERSION'] == PICKLE_FORMAT_VERSION): |
| 72 self._test_methods = d['TEST_METHODS'] |
| 73 return True |
| 74 except: |
| 75 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') |
| 76 return False |
| 77 |
| 78 def _GetProguardData(self): |
| 79 logging.info('Retrieving test methods via proguard.') |
| 80 |
| 81 p = proguard.Dump(self._jar_path) |
| 82 |
| 83 class_lookup = dict((c['class'], c) for c in p['classes']) |
| 84 def recursive_get_annotations(c): |
| 85 s = c['superclass'] |
| 86 if s in class_lookup: |
| 87 a = recursive_get_annotations(class_lookup[s]) |
| 88 else: |
| 89 a = {} |
| 90 a.update(c['annotations']) |
| 91 return a |
| 92 |
| 93 test_classes = (c for c in p['classes'] |
| 94 if c['class'].endswith('Test')) |
| 95 for c in test_classes: |
| 96 class_annotations = recursive_get_annotations(c) |
| 97 test_methods = (m for m in c['methods'] |
| 98 if m['method'].startswith('test')) |
| 99 for m in test_methods: |
| 100 qualified_method = '%s#%s' % (c['class'], m['method']) |
| 101 annotations = dict(class_annotations) |
| 102 annotations.update(m['annotations']) |
| 103 self._test_methods[qualified_method] = m |
| 104 self._test_methods[qualified_method]['annotations'] = annotations |
| 105 |
| 106 logging.info('Storing proguard output to %s', self._pickled_proguard_name) |
| 107 d = {'VERSION': PICKLE_FORMAT_VERSION, |
| 108 'TEST_METHODS': self._test_methods, |
| 109 'JAR_MD5SUM': |
| 110 md5sum.CalculateHostMd5Sums( |
| 111 self._jar_path)[os.path.realpath(self._jar_path)]} |
| 112 with open(self._pickled_proguard_name, 'w') as f: |
| 113 f.write(pickle.dumps(d)) |
| 114 |
| 115 @staticmethod |
| 116 def _IsTestMethod(test): |
| 117 class_name, method = test.split('#') |
| 118 return class_name.endswith('Test') and method.startswith('test') |
| 119 |
| 120 def GetTestAnnotations(self, test): |
| 121 """Returns a list of all annotations for the given |test|. May be empty.""" |
| 122 if not self._IsTestMethod(test) or not test in self._test_methods: |
| 123 return [] |
| 124 return self._test_methods[test]['annotations'] |
| 125 |
| 126 @staticmethod |
| 127 def _AnnotationsMatchFilters(annotation_filter_list, annotations): |
| 128 """Checks if annotations match any of the filters.""" |
| 129 if not annotation_filter_list: |
| 130 return True |
| 131 for annotation_filter in annotation_filter_list: |
| 132 filters = annotation_filter.split('=') |
| 133 if len(filters) == 2: |
| 134 key = filters[0] |
| 135 value_list = filters[1].split(',') |
| 136 for value in value_list: |
| 137 if key in annotations and value == annotations[key]: |
| 138 return True |
| 139 elif annotation_filter in annotations: |
| 140 return True |
| 141 return False |
| 142 |
| 143 def GetAnnotatedTests(self, annotation_filter_list): |
| 144 """Returns a list of all tests that match the given annotation filters.""" |
| 145 return [test for test in self.GetTestMethods() |
| 146 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
| 147 annotation_filter_list, self.GetTestAnnotations(test))] |
| 148 |
| 149 def GetTestMethods(self): |
| 150 """Returns a dict of all test methods and relevant attributes. |
| 151 |
| 152 Test methods are retrieved as Class#testMethod. |
| 153 """ |
| 154 return self._test_methods |
| 155 |
| 156 def _GetTestsMissingAnnotation(self): |
| 157 """Get a list of test methods with no known annotations.""" |
| 158 tests_missing_annotations = [] |
| 159 for test_method in self.GetTestMethods().iterkeys(): |
| 160 annotations_ = frozenset(self.GetTestAnnotations(test_method).iterkeys()) |
| 161 if (annotations_.isdisjoint(self._ANNOTATIONS) and |
| 162 not self.IsHostDrivenTest(test_method)): |
| 163 tests_missing_annotations.append(test_method) |
| 164 return sorted(tests_missing_annotations) |
| 165 |
| 166 def _IsTestValidForSdkRange(self, test_name, attached_min_sdk_level): |
| 167 required_min_sdk_level = int( |
| 168 self.GetTestAnnotations(test_name).get('MinAndroidSdkLevel', 0)) |
| 169 return (required_min_sdk_level is None or |
| 170 attached_min_sdk_level >= required_min_sdk_level) |
| 171 |
| 172 def GetAllMatchingTests(self, annotation_filter_list, |
| 173 exclude_annotation_list, test_filter): |
| 174 """Get a list of tests matching any of the annotations and the filter. |
| 175 |
| 176 Args: |
| 177 annotation_filter_list: List of test annotations. A test must have at |
| 178 least one of these annotations. A test without any annotations is |
| 179 considered to be SmallTest. |
| 180 exclude_annotation_list: List of test annotations. A test must not have |
| 181 any of these annotations. |
| 182 test_filter: Filter used for partial matching on the test method names. |
| 183 |
| 184 Returns: |
| 185 List of all matching tests. |
| 186 """ |
| 187 if annotation_filter_list: |
| 188 available_tests = self.GetAnnotatedTests(annotation_filter_list) |
| 189 # Include un-annotated tests in SmallTest. |
| 190 if annotation_filter_list.count(self._DEFAULT_ANNOTATION) > 0: |
| 191 for test in self._GetTestsMissingAnnotation(): |
| 192 logging.warning( |
| 193 '%s has no annotations. Assuming "%s".', test, |
| 194 self._DEFAULT_ANNOTATION) |
| 195 available_tests.append(test) |
| 196 else: |
| 197 available_tests = [m for m in self.GetTestMethods() |
| 198 if not self.IsHostDrivenTest(m)] |
| 199 |
| 200 if exclude_annotation_list: |
| 201 excluded_tests = self.GetAnnotatedTests(exclude_annotation_list) |
| 202 available_tests = list(set(available_tests) - set(excluded_tests)) |
| 203 |
| 204 tests = [] |
| 205 if test_filter: |
| 206 # |available_tests| are in adb instrument format: package.path.class#test. |
| 207 |
| 208 # Maps a 'class.test' name to each 'package.path.class#test' name. |
| 209 sanitized_test_names = dict([ |
| 210 (t.split('.')[-1].replace('#', '.'), t) for t in available_tests]) |
| 211 # Filters 'class.test' names and populates |tests| with the corresponding |
| 212 # 'package.path.class#test' names. |
| 213 tests = [ |
| 214 sanitized_test_names[t] for t in unittest_util.FilterTestNames( |
| 215 sanitized_test_names.keys(), test_filter.replace('#', '.'))] |
| 216 else: |
| 217 tests = available_tests |
| 218 |
| 219 # Filter out any tests with SDK level requirements that don't match the set |
| 220 # of attached devices. |
| 221 devices = device_utils.DeviceUtils.parallel() |
| 222 min_sdk_version = min(devices.build_version_sdk.pGet(None)) |
| 223 tests = [t for t in tests |
| 224 if self._IsTestValidForSdkRange(t, min_sdk_version)] |
| 225 |
| 226 return tests |
| 227 |
| 228 @staticmethod |
| 229 def IsHostDrivenTest(test): |
| 230 return 'pythonDrivenTests' in test |
OLD | NEW |