xref: /aosp_15_r20/tools/asuite/atest/test_runner_handler.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2017, 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"""Aggregates test runners, groups tests by test runners and kicks off tests."""
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=import-outside-toplevel
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerfrom __future__ import annotations
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerimport itertools
22*c2e18aaaSAndroid Build Coastguard Workerfrom typing import Any, Dict, List
23*c2e18aaaSAndroid Build Coastguard Worker
24*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_error
25*c2e18aaaSAndroid Build Coastguard Workerfrom atest import bazel_mode
26*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info
27*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_info
28*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runner_invocation import TestRunnerInvocation
29*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import atest_tf_test_runner
30*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import mobly_test_runner
31*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import robolectric_test_runner
32*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import suite_plan_test_runner
33*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import test_runner_base
34*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import vts_tf_test_runner
35*c2e18aaaSAndroid Build Coastguard Worker
36*c2e18aaaSAndroid Build Coastguard Worker_TEST_RUNNERS = {
37*c2e18aaaSAndroid Build Coastguard Worker    atest_tf_test_runner.AtestTradefedTestRunner.NAME: (
38*c2e18aaaSAndroid Build Coastguard Worker        atest_tf_test_runner.AtestTradefedTestRunner
39*c2e18aaaSAndroid Build Coastguard Worker    ),
40*c2e18aaaSAndroid Build Coastguard Worker    mobly_test_runner.MoblyTestRunner.NAME: mobly_test_runner.MoblyTestRunner,
41*c2e18aaaSAndroid Build Coastguard Worker    robolectric_test_runner.RobolectricTestRunner.NAME: (
42*c2e18aaaSAndroid Build Coastguard Worker        robolectric_test_runner.RobolectricTestRunner
43*c2e18aaaSAndroid Build Coastguard Worker    ),
44*c2e18aaaSAndroid Build Coastguard Worker    suite_plan_test_runner.SuitePlanTestRunner.NAME: (
45*c2e18aaaSAndroid Build Coastguard Worker        suite_plan_test_runner.SuitePlanTestRunner
46*c2e18aaaSAndroid Build Coastguard Worker    ),
47*c2e18aaaSAndroid Build Coastguard Worker    vts_tf_test_runner.VtsTradefedTestRunner.NAME: (
48*c2e18aaaSAndroid Build Coastguard Worker        vts_tf_test_runner.VtsTradefedTestRunner
49*c2e18aaaSAndroid Build Coastguard Worker    ),
50*c2e18aaaSAndroid Build Coastguard Worker    bazel_mode.BazelTestRunner.NAME: bazel_mode.BazelTestRunner,
51*c2e18aaaSAndroid Build Coastguard Worker}
52*c2e18aaaSAndroid Build Coastguard Worker
53*c2e18aaaSAndroid Build Coastguard Worker
54*c2e18aaaSAndroid Build Coastguard Workerdef _get_test_runners():
55*c2e18aaaSAndroid Build Coastguard Worker  """Returns the test runners.
56*c2e18aaaSAndroid Build Coastguard Worker
57*c2e18aaaSAndroid Build Coastguard Worker  If external test runners are defined outside atest, they can be try-except
58*c2e18aaaSAndroid Build Coastguard Worker  imported into here.
59*c2e18aaaSAndroid Build Coastguard Worker
60*c2e18aaaSAndroid Build Coastguard Worker  Returns:
61*c2e18aaaSAndroid Build Coastguard Worker      Dict of test runner name to test runner class.
62*c2e18aaaSAndroid Build Coastguard Worker  """
63*c2e18aaaSAndroid Build Coastguard Worker  test_runners_dict = _TEST_RUNNERS
64*c2e18aaaSAndroid Build Coastguard Worker  # Example import of example test runner:
65*c2e18aaaSAndroid Build Coastguard Worker  try:
66*c2e18aaaSAndroid Build Coastguard Worker    from test_runners import example_test_runner
67*c2e18aaaSAndroid Build Coastguard Worker
68*c2e18aaaSAndroid Build Coastguard Worker    test_runners_dict[example_test_runner.ExampleTestRunner.NAME] = (
69*c2e18aaaSAndroid Build Coastguard Worker        example_test_runner.ExampleTestRunner
70*c2e18aaaSAndroid Build Coastguard Worker    )
71*c2e18aaaSAndroid Build Coastguard Worker  except ImportError:
72*c2e18aaaSAndroid Build Coastguard Worker    pass
73*c2e18aaaSAndroid Build Coastguard Worker  return test_runners_dict
74*c2e18aaaSAndroid Build Coastguard Worker
75*c2e18aaaSAndroid Build Coastguard Worker
76*c2e18aaaSAndroid Build Coastguard Workerdef group_tests_by_test_runners(test_infos):
77*c2e18aaaSAndroid Build Coastguard Worker  """Group the test_infos by test runners
78*c2e18aaaSAndroid Build Coastguard Worker
79*c2e18aaaSAndroid Build Coastguard Worker  Args:
80*c2e18aaaSAndroid Build Coastguard Worker      test_infos: List of TestInfo.
81*c2e18aaaSAndroid Build Coastguard Worker
82*c2e18aaaSAndroid Build Coastguard Worker  Returns:
83*c2e18aaaSAndroid Build Coastguard Worker      List of tuples (test runner, tests).
84*c2e18aaaSAndroid Build Coastguard Worker  """
85*c2e18aaaSAndroid Build Coastguard Worker  tests_by_test_runner = []
86*c2e18aaaSAndroid Build Coastguard Worker  test_runner_dict = _get_test_runners()
87*c2e18aaaSAndroid Build Coastguard Worker  key = lambda x: x.test_runner
88*c2e18aaaSAndroid Build Coastguard Worker  sorted_test_infos = sorted(list(test_infos), key=key)
89*c2e18aaaSAndroid Build Coastguard Worker  for test_runner, tests in itertools.groupby(sorted_test_infos, key):
90*c2e18aaaSAndroid Build Coastguard Worker    # groupby returns a grouper object, we want to operate on a list.
91*c2e18aaaSAndroid Build Coastguard Worker    tests = list(tests)
92*c2e18aaaSAndroid Build Coastguard Worker    test_runner_class = test_runner_dict.get(test_runner)
93*c2e18aaaSAndroid Build Coastguard Worker    if test_runner_class is None:
94*c2e18aaaSAndroid Build Coastguard Worker      raise atest_error.UnknownTestRunnerError(
95*c2e18aaaSAndroid Build Coastguard Worker          'Unknown Test Runner %s' % test_runner
96*c2e18aaaSAndroid Build Coastguard Worker      )
97*c2e18aaaSAndroid Build Coastguard Worker    tests_by_test_runner.append((test_runner_class, tests))
98*c2e18aaaSAndroid Build Coastguard Worker  return tests_by_test_runner
99*c2e18aaaSAndroid Build Coastguard Worker
100*c2e18aaaSAndroid Build Coastguard Worker
101*c2e18aaaSAndroid Build Coastguard Workerdef create_test_runner_invocations(
102*c2e18aaaSAndroid Build Coastguard Worker    *,
103*c2e18aaaSAndroid Build Coastguard Worker    test_infos: List[test_info.TestInfo],
104*c2e18aaaSAndroid Build Coastguard Worker    results_dir: str,
105*c2e18aaaSAndroid Build Coastguard Worker    mod_info: module_info.ModuleInfo,
106*c2e18aaaSAndroid Build Coastguard Worker    extra_args: Dict[str, Any],
107*c2e18aaaSAndroid Build Coastguard Worker    minimal_build: bool,
108*c2e18aaaSAndroid Build Coastguard Worker) -> List[TestRunnerInvocation]:
109*c2e18aaaSAndroid Build Coastguard Worker  """Creates TestRunnerInvocation instances.
110*c2e18aaaSAndroid Build Coastguard Worker
111*c2e18aaaSAndroid Build Coastguard Worker  Args:
112*c2e18aaaSAndroid Build Coastguard Worker      test_infos: A list of instances of TestInfo.
113*c2e18aaaSAndroid Build Coastguard Worker      results_dir: A directory which stores the ATest execution information.
114*c2e18aaaSAndroid Build Coastguard Worker      mod_info: An instance of ModuleInfo.
115*c2e18aaaSAndroid Build Coastguard Worker      extra_args: A dict of arguments for the test runner to utilize.
116*c2e18aaaSAndroid Build Coastguard Worker      minimal_build: A boolean setting whether or not this invocation will
117*c2e18aaaSAndroid Build Coastguard Worker        minimize the build target set.
118*c2e18aaaSAndroid Build Coastguard Worker
119*c2e18aaaSAndroid Build Coastguard Worker  Returns:
120*c2e18aaaSAndroid Build Coastguard Worker      A list of TestRunnerInvocation instances.
121*c2e18aaaSAndroid Build Coastguard Worker  """
122*c2e18aaaSAndroid Build Coastguard Worker
123*c2e18aaaSAndroid Build Coastguard Worker  test_runner_invocations = []
124*c2e18aaaSAndroid Build Coastguard Worker  for test_runner_class, tests in group_tests_by_test_runners(test_infos):
125*c2e18aaaSAndroid Build Coastguard Worker    test_runner = test_runner_class(
126*c2e18aaaSAndroid Build Coastguard Worker        results_dir,
127*c2e18aaaSAndroid Build Coastguard Worker        mod_info=mod_info,
128*c2e18aaaSAndroid Build Coastguard Worker        extra_args=extra_args,
129*c2e18aaaSAndroid Build Coastguard Worker        minimal_build=minimal_build,
130*c2e18aaaSAndroid Build Coastguard Worker    )
131*c2e18aaaSAndroid Build Coastguard Worker
132*c2e18aaaSAndroid Build Coastguard Worker    test_runner_invocations.extend(
133*c2e18aaaSAndroid Build Coastguard Worker        test_runner.create_invocations(extra_args=extra_args, test_infos=tests)
134*c2e18aaaSAndroid Build Coastguard Worker    )
135*c2e18aaaSAndroid Build Coastguard Worker
136*c2e18aaaSAndroid Build Coastguard Worker  return test_runner_invocations
137