• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21from atest import atest_utils
22from atest import constants
23
24from atest.test_finders import test_finder_base
25from atest.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 t_info.test_runner == 'BazelTestRunner':
82                return False
83            if not self._is_test_path_valid(t_info):
84                return False
85            if not self._is_test_build_target_valid(t_info):
86                return False
87            if not self._is_test_filter_valid(t_info):
88                return False
89        return True
90
91    def _is_test_path_valid(self, t_info):
92        """Check if test path is valid.
93
94        Args:
95            t_info: TestInfo that has been filled out by a find method.
96
97        Returns:
98            True if test path is valid. Otherwise, False.
99        """
100        # For RoboTest it won't have 'MODULES-IN-' as build target. Treat test
101        # path is valid if cached_test_paths is None.
102        cached_test_paths = t_info.get_test_paths()
103        if cached_test_paths is None:
104            return True
105        current_test_paths = self.module_info.get_paths(t_info.test_name)
106        if not current_test_paths:
107            return False
108        formatted_paths = [p.replace('/', '-') for p in current_test_paths]
109        if sorted(cached_test_paths) != sorted(formatted_paths):
110            logging.debug('Not a valid test path.')
111            return False
112        return True
113
114    def _is_test_build_target_valid(self, t_info):
115        """Check if test build targets are valid.
116
117        Args:
118            t_info: TestInfo that has been filled out by a find method.
119
120        Returns:
121            True if test's build target is valid. Otherwise, False.
122        """
123        # If the cached build target can be found in current module-info, then
124        # it is a valid build targets of the test.
125        for build_target in t_info.build_targets:
126            if str(build_target).startswith(constants.MODULES_IN):
127                continue
128            if not self.module_info.is_module(build_target):
129                logging.debug('%s is not a valid build target.', build_target)
130                return False
131        return True
132
133    def _is_test_filter_valid(self, t_info):
134        """Check if test filter is valid.
135
136        Args:
137            t_info: TestInfo that has been filled out by a find method.
138
139        Returns:
140            True if test filter is valid. Otherwise, False.
141        """
142        test_filters = t_info.data.get(constants.TI_FILTER, [])
143        if not test_filters:
144            return True
145        for test_filter in test_filters:
146            # Check if the class filter is under current module.
147            # TODO: (b/172260100) The test_name may not be inevitably equal to
148            #  the module_name.
149            if self._is_java_filter_in_module(t_info.test_name ,
150                                              test_filter.class_name):
151                return True
152            # TODO: (b/172260100) Also check for CC.
153        logging.debug('Not a valid test filter.')
154        return False
155
156    def _is_java_filter_in_module(self, module_name, filter_class):
157        """Check if input class is part of input module.
158
159        Args:
160            module_name: A string of the module name of the test.
161            filter_class: A string of the class name field of TI_FILTER.
162
163        Returns:
164            True if input filter_class is in the input module. Otherwise, False.
165        """
166        mod_info = self.module_info.get_module_info(module_name)
167        if not mod_info:
168            return False
169        module_srcs = mod_info.get(constants.MODULE_SRCS, [])
170        # If module didn't have src information treat the cached filter still
171        # valid. Remove this after all java srcs could be found in module-info.
172        if not module_srcs:
173            return True
174        ref_end = filter_class.rsplit('.', 1)[-1]
175        if '.' in filter_class:
176            file_path = str(filter_class).replace('.', '/')
177            # A Java class file always starts with a capital letter.
178            if ref_end[0].isupper():
179                file_path = file_path + '.'
180            for src_path in module_srcs:
181                # If java class, check if class file in module's src.
182                if src_path.find(file_path) >= 0:
183                    return True
184        return False
185