xref: /aosp_15_r20/tools/asuite/atest/test_runner_handler_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2017, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for test_runner_handler."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerimport unittest
22*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
23*c2e18aaaSAndroid Build Coastguard Worker
24*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_error
25*c2e18aaaSAndroid Build Coastguard Workerfrom atest import test_runner_handler
26*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_info
27*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_runners import test_runner_base as tr_base
28*c2e18aaaSAndroid Build Coastguard Worker
29*c2e18aaaSAndroid Build Coastguard WorkerFAKE_TR_NAME_A = 'FakeTestRunnerA'
30*c2e18aaaSAndroid Build Coastguard WorkerFAKE_TR_NAME_B = 'FakeTestRunnerB'
31*c2e18aaaSAndroid Build Coastguard WorkerMISSING_TR_NAME = 'MissingTestRunner'
32*c2e18aaaSAndroid Build Coastguard WorkerFAKE_TR_A_REQS = {'fake_tr_A_req1', 'fake_tr_A_req2'}
33*c2e18aaaSAndroid Build Coastguard WorkerFAKE_TR_B_REQS = {'fake_tr_B_req1', 'fake_tr_B_req2'}
34*c2e18aaaSAndroid Build Coastguard WorkerMODULE_NAME_A = 'ModuleNameA'
35*c2e18aaaSAndroid Build Coastguard WorkerMODULE_NAME_A_AGAIN = 'ModuleNameA_AGAIN'
36*c2e18aaaSAndroid Build Coastguard WorkerMODULE_NAME_B = 'ModuleNameB'
37*c2e18aaaSAndroid Build Coastguard WorkerMODULE_NAME_B_AGAIN = 'ModuleNameB_AGAIN'
38*c2e18aaaSAndroid Build Coastguard WorkerMODULE_INFO_A = test_info.TestInfo(MODULE_NAME_A, FAKE_TR_NAME_A, set())
39*c2e18aaaSAndroid Build Coastguard WorkerMODULE_INFO_A_AGAIN = test_info.TestInfo(
40*c2e18aaaSAndroid Build Coastguard Worker    MODULE_NAME_A_AGAIN, FAKE_TR_NAME_A, set()
41*c2e18aaaSAndroid Build Coastguard Worker)
42*c2e18aaaSAndroid Build Coastguard WorkerMODULE_INFO_B = test_info.TestInfo(MODULE_NAME_B, FAKE_TR_NAME_B, set())
43*c2e18aaaSAndroid Build Coastguard WorkerMODULE_INFO_B_AGAIN = test_info.TestInfo(
44*c2e18aaaSAndroid Build Coastguard Worker    MODULE_NAME_B_AGAIN, FAKE_TR_NAME_B, set()
45*c2e18aaaSAndroid Build Coastguard Worker)
46*c2e18aaaSAndroid Build Coastguard WorkerBAD_TESTINFO = test_info.TestInfo('bad_name', MISSING_TR_NAME, set())
47*c2e18aaaSAndroid Build Coastguard Worker
48*c2e18aaaSAndroid Build Coastguard Worker
49*c2e18aaaSAndroid Build Coastguard Workerclass FakeTestRunnerA(tr_base.TestRunnerBase):
50*c2e18aaaSAndroid Build Coastguard Worker  """Fake test runner A."""
51*c2e18aaaSAndroid Build Coastguard Worker
52*c2e18aaaSAndroid Build Coastguard Worker  NAME = FAKE_TR_NAME_A
53*c2e18aaaSAndroid Build Coastguard Worker  EXECUTABLE = 'echo'
54*c2e18aaaSAndroid Build Coastguard Worker
55*c2e18aaaSAndroid Build Coastguard Worker  def run_tests(self, test_infos, extra_args, reporter):
56*c2e18aaaSAndroid Build Coastguard Worker    return 0
57*c2e18aaaSAndroid Build Coastguard Worker
58*c2e18aaaSAndroid Build Coastguard Worker  def host_env_check(self):
59*c2e18aaaSAndroid Build Coastguard Worker    pass
60*c2e18aaaSAndroid Build Coastguard Worker
61*c2e18aaaSAndroid Build Coastguard Worker  def get_test_runner_build_reqs(self, test_infos):
62*c2e18aaaSAndroid Build Coastguard Worker    return FAKE_TR_A_REQS
63*c2e18aaaSAndroid Build Coastguard Worker
64*c2e18aaaSAndroid Build Coastguard Worker  def generate_run_commands(self, test_infos, extra_args, port=None):
65*c2e18aaaSAndroid Build Coastguard Worker    return ['fake command']
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker
68*c2e18aaaSAndroid Build Coastguard Workerclass FakeTestRunnerB(FakeTestRunnerA):
69*c2e18aaaSAndroid Build Coastguard Worker  """Fake test runner B."""
70*c2e18aaaSAndroid Build Coastguard Worker
71*c2e18aaaSAndroid Build Coastguard Worker  NAME = FAKE_TR_NAME_B
72*c2e18aaaSAndroid Build Coastguard Worker
73*c2e18aaaSAndroid Build Coastguard Worker  def run_tests(self, test_infos, extra_args, reporter):
74*c2e18aaaSAndroid Build Coastguard Worker    return 1
75*c2e18aaaSAndroid Build Coastguard Worker
76*c2e18aaaSAndroid Build Coastguard Worker  def get_test_runner_build_reqs(self, test_infos):
77*c2e18aaaSAndroid Build Coastguard Worker    return FAKE_TR_B_REQS
78*c2e18aaaSAndroid Build Coastguard Worker
79*c2e18aaaSAndroid Build Coastguard Worker
80*c2e18aaaSAndroid Build Coastguard Workerclass TestRunnerHandlerUnittests(unittest.TestCase):
81*c2e18aaaSAndroid Build Coastguard Worker  """Unit tests for test_runner_handler.py"""
82*c2e18aaaSAndroid Build Coastguard Worker
83*c2e18aaaSAndroid Build Coastguard Worker  _TEST_RUNNERS = {
84*c2e18aaaSAndroid Build Coastguard Worker      FakeTestRunnerA.NAME: FakeTestRunnerA,
85*c2e18aaaSAndroid Build Coastguard Worker      FakeTestRunnerB.NAME: FakeTestRunnerB,
86*c2e18aaaSAndroid Build Coastguard Worker  }
87*c2e18aaaSAndroid Build Coastguard Worker
88*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
89*c2e18aaaSAndroid Build Coastguard Worker    mock.patch(
90*c2e18aaaSAndroid Build Coastguard Worker        'atest.test_runner_handler._get_test_runners',
91*c2e18aaaSAndroid Build Coastguard Worker        return_value=self._TEST_RUNNERS,
92*c2e18aaaSAndroid Build Coastguard Worker    ).start()
93*c2e18aaaSAndroid Build Coastguard Worker
94*c2e18aaaSAndroid Build Coastguard Worker  def tearDown(self):
95*c2e18aaaSAndroid Build Coastguard Worker    mock.patch.stopall()
96*c2e18aaaSAndroid Build Coastguard Worker
97*c2e18aaaSAndroid Build Coastguard Worker  def test_group_tests_by_test_runners(self):
98*c2e18aaaSAndroid Build Coastguard Worker    """Test that we properly group tests by test runners."""
99*c2e18aaaSAndroid Build Coastguard Worker    # Happy path testing.
100*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [
101*c2e18aaaSAndroid Build Coastguard Worker        MODULE_INFO_A,
102*c2e18aaaSAndroid Build Coastguard Worker        MODULE_INFO_A_AGAIN,
103*c2e18aaaSAndroid Build Coastguard Worker        MODULE_INFO_B,
104*c2e18aaaSAndroid Build Coastguard Worker        MODULE_INFO_B_AGAIN,
105*c2e18aaaSAndroid Build Coastguard Worker    ]
106*c2e18aaaSAndroid Build Coastguard Worker    want_list = [
107*c2e18aaaSAndroid Build Coastguard Worker        (FakeTestRunnerA, [MODULE_INFO_A, MODULE_INFO_A_AGAIN]),
108*c2e18aaaSAndroid Build Coastguard Worker        (FakeTestRunnerB, [MODULE_INFO_B, MODULE_INFO_B_AGAIN]),
109*c2e18aaaSAndroid Build Coastguard Worker    ]
110*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(
111*c2e18aaaSAndroid Build Coastguard Worker        want_list, test_runner_handler.group_tests_by_test_runners(test_infos)
112*c2e18aaaSAndroid Build Coastguard Worker    )
113*c2e18aaaSAndroid Build Coastguard Worker
114*c2e18aaaSAndroid Build Coastguard Worker    # Let's make sure we fail as expected.
115*c2e18aaaSAndroid Build Coastguard Worker    self.assertRaises(
116*c2e18aaaSAndroid Build Coastguard Worker        atest_error.UnknownTestRunnerError,
117*c2e18aaaSAndroid Build Coastguard Worker        test_runner_handler.group_tests_by_test_runners,
118*c2e18aaaSAndroid Build Coastguard Worker        [BAD_TESTINFO],
119*c2e18aaaSAndroid Build Coastguard Worker    )
120*c2e18aaaSAndroid Build Coastguard Worker
121*c2e18aaaSAndroid Build Coastguard Worker  def test_get_test_runner_reqs(self):
122*c2e18aaaSAndroid Build Coastguard Worker    """Test that we get all the reqs from the test runners."""
123*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [MODULE_INFO_A, MODULE_INFO_B]
124*c2e18aaaSAndroid Build Coastguard Worker    want_set = FAKE_TR_A_REQS | FAKE_TR_B_REQS
125*c2e18aaaSAndroid Build Coastguard Worker    empty_module_info = None
126*c2e18aaaSAndroid Build Coastguard Worker    invocations = test_runner_handler.create_test_runner_invocations(
127*c2e18aaaSAndroid Build Coastguard Worker        test_infos=test_infos,
128*c2e18aaaSAndroid Build Coastguard Worker        results_dir='',
129*c2e18aaaSAndroid Build Coastguard Worker        mod_info=empty_module_info,
130*c2e18aaaSAndroid Build Coastguard Worker        extra_args={},
131*c2e18aaaSAndroid Build Coastguard Worker        minimal_build=True,
132*c2e18aaaSAndroid Build Coastguard Worker    )
133*c2e18aaaSAndroid Build Coastguard Worker
134*c2e18aaaSAndroid Build Coastguard Worker    build_targets = set()
135*c2e18aaaSAndroid Build Coastguard Worker    for invocation in invocations:
136*c2e18aaaSAndroid Build Coastguard Worker      build_targets |= invocation.get_test_runner_reqs()
137*c2e18aaaSAndroid Build Coastguard Worker
138*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(want_set, build_targets)
139*c2e18aaaSAndroid Build Coastguard Worker
140*c2e18aaaSAndroid Build Coastguard Worker  def test_run_all_tests_succeed(self):
141*c2e18aaaSAndroid Build Coastguard Worker    """Test that the return value as we expected."""
142*c2e18aaaSAndroid Build Coastguard Worker    results_dir = ''
143*c2e18aaaSAndroid Build Coastguard Worker    extra_args = {}
144*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN]
145*c2e18aaaSAndroid Build Coastguard Worker    invocation = test_runner_handler.TestRunnerInvocation(
146*c2e18aaaSAndroid Build Coastguard Worker        test_infos=test_infos,
147*c2e18aaaSAndroid Build Coastguard Worker        test_runner=FakeTestRunnerA(results_dir),
148*c2e18aaaSAndroid Build Coastguard Worker        extra_args=extra_args,
149*c2e18aaaSAndroid Build Coastguard Worker    )
150*c2e18aaaSAndroid Build Coastguard Worker
151*c2e18aaaSAndroid Build Coastguard Worker    exit_code = invocation.run_all_tests(mock.MagicMock())
152*c2e18aaaSAndroid Build Coastguard Worker
153*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(0, exit_code)
154*c2e18aaaSAndroid Build Coastguard Worker
155*c2e18aaaSAndroid Build Coastguard Worker  def test_run_all_tests_failed(self):
156*c2e18aaaSAndroid Build Coastguard Worker    """Test that the return value as we expected."""
157*c2e18aaaSAndroid Build Coastguard Worker    results_dir = ''
158*c2e18aaaSAndroid Build Coastguard Worker    extra_args = {}
159*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [MODULE_INFO_B, MODULE_INFO_B_AGAIN]
160*c2e18aaaSAndroid Build Coastguard Worker    invocation = test_runner_handler.TestRunnerInvocation(
161*c2e18aaaSAndroid Build Coastguard Worker        test_infos=test_infos,
162*c2e18aaaSAndroid Build Coastguard Worker        test_runner=FakeTestRunnerB(results_dir),
163*c2e18aaaSAndroid Build Coastguard Worker        extra_args=extra_args,
164*c2e18aaaSAndroid Build Coastguard Worker    )
165*c2e18aaaSAndroid Build Coastguard Worker
166*c2e18aaaSAndroid Build Coastguard Worker    exit_code = invocation.run_all_tests(mock.MagicMock())
167*c2e18aaaSAndroid Build Coastguard Worker
168*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(1, exit_code)
169*c2e18aaaSAndroid Build Coastguard Worker
170*c2e18aaaSAndroid Build Coastguard Worker
171*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
172*c2e18aaaSAndroid Build Coastguard Worker  unittest.main()
173