xref: /aosp_15_r20/tools/asuite/atest/module_info_unittest_base.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2024, 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"""Class that facilitates testing with ModuleInfo objects.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard WorkerContains methods to create module objects representing various types
18*c2e18aaaSAndroid Build Coastguard Workerof tests that can be run in Atest, as well as the corresponding ModuleInfo
19*c2e18aaaSAndroid Build Coastguard Workerobject, for use in unit tests.
20*c2e18aaaSAndroid Build Coastguard Worker"""
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Workerimport pathlib
23*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_utils
26*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants
27*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info
28*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_info
29*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders.test_info import TestInfo
30*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import atest_tf_test_runner
31*c2e18aaaSAndroid Build Coastguard Workerfrom pyfakefs import fake_filesystem_unittest
32*c2e18aaaSAndroid Build Coastguard Worker
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Workerclass ModuleInfoTest(fake_filesystem_unittest.TestCase):
35*c2e18aaaSAndroid Build Coastguard Worker  """Fixture for tests that require interacting with module-info."""
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
38*c2e18aaaSAndroid Build Coastguard Worker    self.setUpPyfakefs()
39*c2e18aaaSAndroid Build Coastguard Worker    self.product_out_path = pathlib.Path('/src/out/product')
40*c2e18aaaSAndroid Build Coastguard Worker    self.product_out_path.mkdir(parents=True)
41*c2e18aaaSAndroid Build Coastguard Worker
42*c2e18aaaSAndroid Build Coastguard Worker  def create_empty_module_info(self):
43*c2e18aaaSAndroid Build Coastguard Worker    """Creates an empty ModuleInfo object."""
44*c2e18aaaSAndroid Build Coastguard Worker    fake_temp_file = self.product_out_path.joinpath(
45*c2e18aaaSAndroid Build Coastguard Worker        next(tempfile._get_candidate_names())
46*c2e18aaaSAndroid Build Coastguard Worker    )
47*c2e18aaaSAndroid Build Coastguard Worker    self.fs.create_file(fake_temp_file, contents='{}')
48*c2e18aaaSAndroid Build Coastguard Worker    return module_info.load_from_file(module_file=fake_temp_file)
49*c2e18aaaSAndroid Build Coastguard Worker
50*c2e18aaaSAndroid Build Coastguard Worker  def create_module_info(self, modules: list[dict] = None):
51*c2e18aaaSAndroid Build Coastguard Worker    """Creates a ModuleInfo object from the given list of modules."""
52*c2e18aaaSAndroid Build Coastguard Worker    mod_info = self.create_empty_module_info()
53*c2e18aaaSAndroid Build Coastguard Worker    modules = modules or []
54*c2e18aaaSAndroid Build Coastguard Worker
55*c2e18aaaSAndroid Build Coastguard Worker    for m in modules:
56*c2e18aaaSAndroid Build Coastguard Worker      mod_info.name_to_module_info[m[constants.MODULE_INFO_ID]] = m
57*c2e18aaaSAndroid Build Coastguard Worker
58*c2e18aaaSAndroid Build Coastguard Worker    for m in modules:
59*c2e18aaaSAndroid Build Coastguard Worker      for path in m[constants.MODULE_PATH]:
60*c2e18aaaSAndroid Build Coastguard Worker        if not mod_info.path_to_module_info.get(path, []):
61*c2e18aaaSAndroid Build Coastguard Worker          mod_info.path_to_module_info[path] = [m]
62*c2e18aaaSAndroid Build Coastguard Worker        else:
63*c2e18aaaSAndroid Build Coastguard Worker          mod_info.path_to_module_info[path].append(m)
64*c2e18aaaSAndroid Build Coastguard Worker
65*c2e18aaaSAndroid Build Coastguard Worker    return mod_info
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker  def assertContainsSubset(self, expected_subset, actual_set):
68*c2e18aaaSAndroid Build Coastguard Worker    """Checks whether actual iterable is a superset of expected iterable."""
69*c2e18aaaSAndroid Build Coastguard Worker    missing = set(expected_subset) - set(actual_set)
70*c2e18aaaSAndroid Build Coastguard Worker    if not missing:
71*c2e18aaaSAndroid Build Coastguard Worker      return
72*c2e18aaaSAndroid Build Coastguard Worker
73*c2e18aaaSAndroid Build Coastguard Worker    self.fail(
74*c2e18aaaSAndroid Build Coastguard Worker        f'Missing elements {missing}\n'
75*c2e18aaaSAndroid Build Coastguard Worker        f'Expected: {expected_subset}\n'
76*c2e18aaaSAndroid Build Coastguard Worker        f'Actual: {actual_set}'
77*c2e18aaaSAndroid Build Coastguard Worker    )
78*c2e18aaaSAndroid Build Coastguard Worker
79*c2e18aaaSAndroid Build Coastguard Worker
80*c2e18aaaSAndroid Build Coastguard Workerdef host_jar_module(name, installed):
81*c2e18aaaSAndroid Build Coastguard Worker
82*c2e18aaaSAndroid Build Coastguard Worker  return module(
83*c2e18aaaSAndroid Build Coastguard Worker      name=name,
84*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['HOST'],
85*c2e18aaaSAndroid Build Coastguard Worker      installed=installed,
86*c2e18aaaSAndroid Build Coastguard Worker      auto_test_config=[],
87*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=[],
88*c2e18aaaSAndroid Build Coastguard Worker  )
89*c2e18aaaSAndroid Build Coastguard Worker
90*c2e18aaaSAndroid Build Coastguard Worker
91*c2e18aaaSAndroid Build Coastguard Workerdef device_driven_test_module(
92*c2e18aaaSAndroid Build Coastguard Worker    name,
93*c2e18aaaSAndroid Build Coastguard Worker    installed=None,
94*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
95*c2e18aaaSAndroid Build Coastguard Worker    host_deps=None,
96*c2e18aaaSAndroid Build Coastguard Worker    class_type=None,
97*c2e18aaaSAndroid Build Coastguard Worker    is_unit_test=None,
98*c2e18aaaSAndroid Build Coastguard Worker    module_path=None,
99*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
100*c2e18aaaSAndroid Build Coastguard Worker    test_configs=None,
101*c2e18aaaSAndroid Build Coastguard Worker):
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'hello_world_test'
104*c2e18aaaSAndroid Build Coastguard Worker  module_path = module_path or 'example_module/project'
105*c2e18aaaSAndroid Build Coastguard Worker
106*c2e18aaaSAndroid Build Coastguard Worker  return test_module(
107*c2e18aaaSAndroid Build Coastguard Worker      name=name,
108*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['DEVICE'],
109*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=compatibility_suites,
110*c2e18aaaSAndroid Build Coastguard Worker      installed=installed or [f'out/product/vsoc_x86/{name}/{name}.apk'],
111*c2e18aaaSAndroid Build Coastguard Worker      host_deps=host_deps,
112*c2e18aaaSAndroid Build Coastguard Worker      class_type=class_type or ['APP'],
113*c2e18aaaSAndroid Build Coastguard Worker      module_path=module_path,
114*c2e18aaaSAndroid Build Coastguard Worker      is_unit_test=is_unit_test,
115*c2e18aaaSAndroid Build Coastguard Worker      srcs=srcs,
116*c2e18aaaSAndroid Build Coastguard Worker      test_configs=test_configs,
117*c2e18aaaSAndroid Build Coastguard Worker  )
118*c2e18aaaSAndroid Build Coastguard Worker
119*c2e18aaaSAndroid Build Coastguard Worker
120*c2e18aaaSAndroid Build Coastguard Workerdef device_driven_multi_config_test_module(
121*c2e18aaaSAndroid Build Coastguard Worker    name,
122*c2e18aaaSAndroid Build Coastguard Worker    installed=None,
123*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
124*c2e18aaaSAndroid Build Coastguard Worker    host_deps=None,
125*c2e18aaaSAndroid Build Coastguard Worker    class_type=None,
126*c2e18aaaSAndroid Build Coastguard Worker    module_path=None,
127*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
128*c2e18aaaSAndroid Build Coastguard Worker):
129*c2e18aaaSAndroid Build Coastguard Worker
130*c2e18aaaSAndroid Build Coastguard Worker  module_path = module_path or 'example_module/project'
131*c2e18aaaSAndroid Build Coastguard Worker  return test_module(
132*c2e18aaaSAndroid Build Coastguard Worker      name=name,
133*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['DEVICE'],
134*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=compatibility_suites,
135*c2e18aaaSAndroid Build Coastguard Worker      installed=installed or [f'out/product/vsoc_x86/{name}/{name}.apk'],
136*c2e18aaaSAndroid Build Coastguard Worker      auto_test_config=[False],
137*c2e18aaaSAndroid Build Coastguard Worker      test_configs=[
138*c2e18aaaSAndroid Build Coastguard Worker          f'{module_path}/configs/Config1.xml',
139*c2e18aaaSAndroid Build Coastguard Worker          f'{module_path}/configs/Config2.xml',
140*c2e18aaaSAndroid Build Coastguard Worker      ],
141*c2e18aaaSAndroid Build Coastguard Worker      host_deps=host_deps,
142*c2e18aaaSAndroid Build Coastguard Worker      class_type=class_type or ['APP'],
143*c2e18aaaSAndroid Build Coastguard Worker      module_path=module_path,
144*c2e18aaaSAndroid Build Coastguard Worker      srcs=srcs,
145*c2e18aaaSAndroid Build Coastguard Worker  )
146*c2e18aaaSAndroid Build Coastguard Worker
147*c2e18aaaSAndroid Build Coastguard Worker
148*c2e18aaaSAndroid Build Coastguard Workerdef robolectric_test_module(name):
149*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'hello_world_test'
150*c2e18aaaSAndroid Build Coastguard Worker  return test_module(
151*c2e18aaaSAndroid Build Coastguard Worker      name=name,
152*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['DEVICE'],
153*c2e18aaaSAndroid Build Coastguard Worker      installed=[f'out/host/linux-x86/{name}/{name}.jar'],
154*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=['robolectric-tests'],
155*c2e18aaaSAndroid Build Coastguard Worker  )
156*c2e18aaaSAndroid Build Coastguard Worker
157*c2e18aaaSAndroid Build Coastguard Worker
158*c2e18aaaSAndroid Build Coastguard Workerdef host_driven_device_test_module(name, libs=None):
159*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'hello_world_test'
160*c2e18aaaSAndroid Build Coastguard Worker  return test_module(
161*c2e18aaaSAndroid Build Coastguard Worker      name=name,
162*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['HOST'],
163*c2e18aaaSAndroid Build Coastguard Worker      installed=[f'out/host/linux-x86/{name}/{name}.jar'],
164*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=['null-suite'],
165*c2e18aaaSAndroid Build Coastguard Worker      libs=libs,
166*c2e18aaaSAndroid Build Coastguard Worker  )
167*c2e18aaaSAndroid Build Coastguard Worker
168*c2e18aaaSAndroid Build Coastguard Worker
169*c2e18aaaSAndroid Build Coastguard Workerdef multi_variant_unit_test_module(name):
170*c2e18aaaSAndroid Build Coastguard Worker
171*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'hello_world_test'
172*c2e18aaaSAndroid Build Coastguard Worker
173*c2e18aaaSAndroid Build Coastguard Worker  return test_module(
174*c2e18aaaSAndroid Build Coastguard Worker      name=name,
175*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=['HOST', 'DEVICE'],
176*c2e18aaaSAndroid Build Coastguard Worker      installed=[
177*c2e18aaaSAndroid Build Coastguard Worker          f'out/host/linux-x86/{name}/{name}.cc',
178*c2e18aaaSAndroid Build Coastguard Worker          f'out/product/vsoc_x86/{name}/{name}.cc',
179*c2e18aaaSAndroid Build Coastguard Worker      ],
180*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=['host-unit-tests'],
181*c2e18aaaSAndroid Build Coastguard Worker      is_unit_test='true',
182*c2e18aaaSAndroid Build Coastguard Worker  )
183*c2e18aaaSAndroid Build Coastguard Worker
184*c2e18aaaSAndroid Build Coastguard Worker
185*c2e18aaaSAndroid Build Coastguard Workerdef test_module(
186*c2e18aaaSAndroid Build Coastguard Worker    name,
187*c2e18aaaSAndroid Build Coastguard Worker    supported_variants,
188*c2e18aaaSAndroid Build Coastguard Worker    installed,
189*c2e18aaaSAndroid Build Coastguard Worker    auto_test_config=[True],
190*c2e18aaaSAndroid Build Coastguard Worker    test_configs=[None],
191*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
192*c2e18aaaSAndroid Build Coastguard Worker    libs=None,
193*c2e18aaaSAndroid Build Coastguard Worker    host_deps=None,
194*c2e18aaaSAndroid Build Coastguard Worker    class_type=None,
195*c2e18aaaSAndroid Build Coastguard Worker    module_path=None,
196*c2e18aaaSAndroid Build Coastguard Worker    is_unit_test=None,
197*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
198*c2e18aaaSAndroid Build Coastguard Worker):
199*c2e18aaaSAndroid Build Coastguard Worker  """Creates a module object which with properties specific to a test module."""
200*c2e18aaaSAndroid Build Coastguard Worker  return module(
201*c2e18aaaSAndroid Build Coastguard Worker      name=name,
202*c2e18aaaSAndroid Build Coastguard Worker      supported_variants=supported_variants,
203*c2e18aaaSAndroid Build Coastguard Worker      installed=installed,
204*c2e18aaaSAndroid Build Coastguard Worker      auto_test_config=auto_test_config,
205*c2e18aaaSAndroid Build Coastguard Worker      test_configs=test_configs,
206*c2e18aaaSAndroid Build Coastguard Worker      compatibility_suites=compatibility_suites or ['null-suite'],
207*c2e18aaaSAndroid Build Coastguard Worker      libs=libs,
208*c2e18aaaSAndroid Build Coastguard Worker      host_deps=host_deps,
209*c2e18aaaSAndroid Build Coastguard Worker      class_type=class_type,
210*c2e18aaaSAndroid Build Coastguard Worker      module_path=[module_path],
211*c2e18aaaSAndroid Build Coastguard Worker      is_unit_test=is_unit_test,
212*c2e18aaaSAndroid Build Coastguard Worker      srcs=srcs,
213*c2e18aaaSAndroid Build Coastguard Worker  )
214*c2e18aaaSAndroid Build Coastguard Worker
215*c2e18aaaSAndroid Build Coastguard Worker
216*c2e18aaaSAndroid Build Coastguard Workerdef module(
217*c2e18aaaSAndroid Build Coastguard Worker    name,
218*c2e18aaaSAndroid Build Coastguard Worker    supported_variants,
219*c2e18aaaSAndroid Build Coastguard Worker    installed,
220*c2e18aaaSAndroid Build Coastguard Worker    auto_test_config=None,
221*c2e18aaaSAndroid Build Coastguard Worker    test_configs=None,
222*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
223*c2e18aaaSAndroid Build Coastguard Worker    libs=None,
224*c2e18aaaSAndroid Build Coastguard Worker    host_deps=None,
225*c2e18aaaSAndroid Build Coastguard Worker    class_type=None,
226*c2e18aaaSAndroid Build Coastguard Worker    module_path=None,
227*c2e18aaaSAndroid Build Coastguard Worker    is_unit_test=None,
228*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
229*c2e18aaaSAndroid Build Coastguard Worker):
230*c2e18aaaSAndroid Build Coastguard Worker  """Creates a ModuleInfo object.
231*c2e18aaaSAndroid Build Coastguard Worker
232*c2e18aaaSAndroid Build Coastguard Worker  This substitutes its creation from a module-info file for test purposes.
233*c2e18aaaSAndroid Build Coastguard Worker  """
234*c2e18aaaSAndroid Build Coastguard Worker
235*c2e18aaaSAndroid Build Coastguard Worker  m = {}
236*c2e18aaaSAndroid Build Coastguard Worker
237*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_INFO_ID] = name
238*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_NAME] = name
239*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_SUPPORTED_VARIANTS] = supported_variants
240*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_INSTALLED] = installed
241*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_AUTO_TEST_CONFIG] = auto_test_config or []
242*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_TEST_CONFIG] = test_configs or []
243*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_COMPATIBILITY_SUITES] = compatibility_suites or []
244*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_LIBS] = libs or []
245*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_HOST_DEPS] = host_deps or []
246*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_CLASS] = class_type or []
247*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_PATH] = module_path or []
248*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_IS_UNIT_TEST] = is_unit_test or 'false'
249*c2e18aaaSAndroid Build Coastguard Worker  m[constants.MODULE_SRCS] = srcs or []
250*c2e18aaaSAndroid Build Coastguard Worker
251*c2e18aaaSAndroid Build Coastguard Worker  return m
252