1# Copyright 2019, The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15""" 16Cache Finder class. 17""" 18 19import logging 20 21import atest_utils 22import constants 23 24from test_finders import test_finder_base 25from test_finders import test_info 26 27class CacheFinder(test_finder_base.TestFinderBase): 28 """Cache Finder class.""" 29 NAME = 'CACHE' 30 31 def __init__(self, module_info=None): 32 super().__init__() 33 self.module_info = module_info 34 35 def _is_latest_testinfos(self, test_infos): 36 """Check whether test_infos are up-to-date. 37 38 Args: 39 test_infos: A list of TestInfo. 40 41 Returns: 42 True if all keys in test_infos and TestInfo object are equal. 43 Otherwise, False. 44 """ 45 sorted_base_ti = sorted( 46 vars(test_info.TestInfo(None, None, None)).keys()) 47 for cached_test_info in test_infos: 48 sorted_cache_ti = sorted(vars(cached_test_info).keys()) 49 if not sorted_cache_ti == sorted_base_ti: 50 logging.debug('test_info is not up-to-date.') 51 return False 52 return True 53 54 def find_test_by_cache(self, test_reference): 55 """Find the matched test_infos in saved caches. 56 57 Args: 58 test_reference: A string of the path to the test's file or dir. 59 60 Returns: 61 A list of TestInfo namedtuple if cache found and is in latest 62 TestInfo format, else None. 63 """ 64 test_infos = atest_utils.load_test_info_cache(test_reference) 65 if test_infos and self._is_test_infos_valid(test_infos): 66 return test_infos 67 return None 68 69 def _is_test_infos_valid(self, test_infos): 70 """Check if the given test_infos are valid. 71 72 Args: 73 test_infos: A list of TestInfo. 74 75 Returns: 76 True if test_infos are all valid. Otherwise, False. 77 """ 78 if not self._is_latest_testinfos(test_infos): 79 return False 80 for t_info in test_infos: 81 if not self._is_test_path_valid(t_info): 82 return False 83 if not self._is_test_build_target_valid(t_info): 84 return False 85 if not self._is_test_filter_valid(t_info): 86 return False 87 return True 88 89 def _is_test_path_valid(self, t_info): 90 """Check if test path is valid. 91 92 Args: 93 t_info: TestInfo that has been filled out by a find method. 94 95 Returns: 96 True if test path is valid. Otherwise, False. 97 """ 98 # For RoboTest it won't have 'MODULES-IN-' as build target. Treat test 99 # path is valid if cached_test_paths is None. 100 cached_test_paths = t_info.get_test_paths() 101 if cached_test_paths is None: 102 return True 103 current_test_paths = self.module_info.get_paths(t_info.test_name) 104 if not current_test_paths: 105 return False 106 if sorted(cached_test_paths) != sorted(current_test_paths): 107 logging.debug('Not a valid test path.') 108 return False 109 return True 110 111 def _is_test_build_target_valid(self, t_info): 112 """Check if test build targets are valid. 113 114 Args: 115 t_info: TestInfo that has been filled out by a find method. 116 117 Returns: 118 True if test's build target is valid. Otherwise, False. 119 """ 120 # If the cached build target can be found in current module-info, then 121 # it is a valid build targets of the test. 122 for build_target in t_info.build_targets: 123 if str(build_target).startswith(constants.MODULES_IN): 124 continue 125 if not self.module_info.is_module(build_target): 126 logging.debug('%s is not a valid build target.', build_target) 127 return False 128 return True 129 130 def _is_test_filter_valid(self, t_info): 131 """Check if test filter is valid. 132 133 Args: 134 t_info: TestInfo that has been filled out by a find method. 135 136 Returns: 137 True if test filter is valid. Otherwise, False. 138 """ 139 test_filters = t_info.data.get(constants.TI_FILTER, []) 140 if not test_filters: 141 return True 142 for test_filter in test_filters: 143 # Check if the class filter is under current module. 144 # TODO: (b/172260100) The test_name may not be inevitably equal to 145 # the module_name. 146 if self._is_java_filter_in_module(t_info.test_name , 147 test_filter.class_name): 148 return True 149 # TODO: (b/172260100) Also check for CC. 150 logging.debug('Not a valid test filter.') 151 return False 152 153 def _is_java_filter_in_module(self, module_name, filter_class): 154 """Check if input class is part of input module. 155 156 Args: 157 module_name: A string of the module name of the test. 158 filter_class: A string of the class name field of TI_FILTER. 159 160 Returns: 161 True if input filter_class is in the input module. Otherwise, False. 162 """ 163 mod_info = self.module_info.get_module_info(module_name) 164 if not mod_info: 165 return False 166 module_srcs = mod_info.get(constants.MODULE_SRCS, []) 167 # If module didn't have src information treat the cached filter still 168 # valid. Remove this after all java srcs could be found in module-info. 169 if not module_srcs: 170 return True 171 ref_end = filter_class.rsplit('.', 1)[-1] 172 if '.' in filter_class: 173 file_path = str(filter_class).replace('.', '/') 174 # A Java class file always starts with a capital letter. 175 if ref_end[0].isupper(): 176 file_path = file_path + '.' 177 for src_path in module_srcs: 178 # If java class, check if class file in module's src. 179 if src_path.find(file_path) >= 0: 180 return True 181 return False 182