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