1# Copyright 2018, 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"""TestInfo class.""" 16 17from collections import namedtuple 18from typing import Set 19 20from atest import constants 21 22TestFilterBase = namedtuple('TestFilter', ['class_name', 'methods']) 23MODULE_COMPATIBILITY_SUITES_RAVENWOOD_TESTS = 'ravenwood-tests' 24 25 26class TestInfo: 27 """Information needed to identify and run a test.""" 28 29 # pylint: disable=too-many-arguments 30 # TODO: remove all arguments but only test_name, test_runner, build_targets, 31 # data and compatibility_suites. 32 def __init__( 33 self, 34 test_name, 35 test_runner, 36 build_targets, 37 data=None, 38 suite=None, 39 module_class=None, 40 install_locations=None, 41 test_finder='', 42 compatibility_suites=None, 43 ): 44 """Init for TestInfo. 45 46 Args: 47 test_name: String of test name. 48 test_runner: String of test runner. 49 build_targets: Set of build targets. 50 data: Dict of data for test runners to use. 51 suite: Suite for test runners to use. 52 module_class: A list of test classes. It's a snippet of class in 53 module_info. e.g. ["EXECUTABLES", "NATIVE_TESTS"] 54 install_locations: Set of install locations. e.g. set(['host', 55 'device']) 56 test_finder: String of test finder. 57 compatibility_suites: A list of compatibility_suites. It's a snippet of 58 compatibility_suites in module_info. e.g. ["device-tests", "vts10"] 59 """ 60 self.test_name = test_name 61 self.raw_test_name = test_name 62 self.test_runner = test_runner 63 self.data = data if data else {} 64 self.suite = suite 65 self.module_class = module_class if module_class else [] 66 # robolectric test types: 67 # 0: Not robolectric test 68 # 1. Modern robolectric test(Tradefed Runner) 69 # 2: Legacy robolectric test(Robolectric Runner) 70 self.robo_type = 0 71 self.install_locations = install_locations if install_locations else set() 72 # True if the TestInfo is built from a test configured in TEST_MAPPING. 73 self.from_test_mapping = False 74 # True if the test should run on host and require no device. The 75 # attribute is only set through TEST_MAPPING file. 76 self.host = False 77 self.test_finder = test_finder 78 self.compatibility_suites = ( 79 compatibility_suites if compatibility_suites else [] 80 ) 81 # True if test need to generate aggregate metrics result. 82 self.aggregate_metrics_result = False 83 self.artifacts = set() 84 85 self._build_targets = set(build_targets) if build_targets else set() 86 self._mainline_modules = set() 87 88 def __str__(self): 89 host_info = ' - runs on host without device required.' if self.host else '' 90 return ( 91 f'test_name:{self.test_name} - ' 92 f'raw_test_name:{self.raw_test_name} - ' 93 f'test_runner:{self.test_runner} - ' 94 f'build_targets:{self._build_targets} - data:{self.data} - ' 95 f'suite:{self.suite} - module_class:{self.module_class} - ' 96 f'install_locations:{self.install_locations}{host_info} - ' 97 f'test_finder:{self.test_finder} - ' 98 f'compatibility_suites:{self.compatibility_suites} - ' 99 f'mainline_modules:{self._mainline_modules} - ' 100 f'aggregate_metrics_result:{self.aggregate_metrics_result} - ' 101 f'robo_type:{self.robo_type} - ' 102 f'artifacts:{self.artifacts}' 103 ) 104 105 @property 106 def build_targets(self) -> Set[str]: 107 """Gets all build targets of the test. 108 109 Gets all build targets of the test including mainline 110 modules build targets if it's a mainline test. 111 """ 112 return frozenset(self._build_targets) 113 114 def add_build_target(self, target: str): 115 """Sets build targets. 116 117 Args: 118 target: a string of build target name. 119 """ 120 self._build_targets.add(target) 121 122 @property 123 def mainline_modules(self) -> Set[str]: 124 """Gets mainline module build targets.""" 125 return frozenset(self._mainline_modules) 126 127 def add_mainline_module(self, module: str): 128 """Sets mainline modules. 129 130 Args: 131 module: the build module name of a mainline module. 132 """ 133 self._build_targets.add(module) 134 self._mainline_modules.add(module) 135 136 def get_supported_exec_mode(self): 137 """Get the supported execution mode of the test. 138 139 Determine which execution mode does the test support by strategy: 140 The compatibility_suites contains 'ravenwood-tests' --> 'host' 141 Modern Robolectric --> 'host' 142 Legacy Robolectric --> 'both' 143 JAVA_LIBRARIES --> 'both' 144 Not native tests or installed only in out/target --> 'device' 145 Installed only in out/host --> 'both' 146 Installed under host and target --> 'both' 147 148 Return: 149 String of execution mode. 150 """ 151 install_path = self.install_locations 152 if MODULE_COMPATIBILITY_SUITES_RAVENWOOD_TESTS in self.compatibility_suites: 153 return constants.DEVICELESS_TEST 154 if not self.module_class: 155 return constants.DEVICE_TEST 156 # Let Robolectric test support host/both accordingly. 157 if self.robo_type == constants.ROBOTYPE_MODERN: 158 return constants.DEVICELESS_TEST 159 if self.robo_type == constants.ROBOTYPE_LEGACY: 160 return constants.BOTH_TEST 161 if constants.MODULE_CLASS_JAVA_LIBRARIES in self.module_class: 162 return constants.BOTH_TEST 163 if not install_path: 164 return constants.DEVICE_TEST 165 # Non-Native test runs on device-only. 166 if constants.MODULE_CLASS_NATIVE_TESTS not in self.module_class: 167 return constants.DEVICE_TEST 168 # Native test with install path as host should be treated as both. 169 # Otherwise, return device test. 170 if install_path == {constants.DEVICE_TEST}: 171 return constants.DEVICE_TEST 172 return constants.BOTH_TEST 173 174 def get_test_paths(self): 175 """Get the relative path of test_info. 176 177 Search build target's MODULE-IN as the test path. 178 179 Return: 180 A list of string of the relative path for test(build target 181 formats, e.g., platform_testing-tests-example-native), 182 None if test path information not found. 183 """ 184 test_paths = [] 185 for build_target in self.build_targets: 186 if str(build_target).startswith(constants.MODULES_IN): 187 test_paths.append(str(build_target).replace(constants.MODULES_IN, '')) 188 return test_paths if test_paths else None 189 190 191class TestFilter(TestFilterBase): 192 """Information needed to filter a test in Tradefed""" 193 194 def to_list_of_tf_strings(self): 195 """Return TestFilter as set of strings in TradeFed filter format.""" 196 tf_strings = [] 197 if self.methods: 198 for method in self.methods: 199 tf_string = f'{self.class_name}#{method}' 200 if tf_string not in tf_strings: 201 tf_strings.append(tf_string) 202 else: 203 tf_strings = [self.class_name] 204 return tf_strings 205