• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2017, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for test_runner_handler."""
18
19# pylint: disable=protected-access
20# pylint: disable=line-too-long
21
22import os
23import unittest
24
25from unittest import mock
26
27import atest_error
28import constants
29import module_info
30import test_runner_handler
31import unittest_constants as uc
32
33from metrics import metrics
34from test_finders import test_info
35from test_runners import test_runner_base as tr_base
36
37FAKE_TR_NAME_A = 'FakeTestRunnerA'
38FAKE_TR_NAME_B = 'FakeTestRunnerB'
39MISSING_TR_NAME = 'MissingTestRunner'
40FAKE_TR_A_REQS = {'fake_tr_A_req1', 'fake_tr_A_req2'}
41FAKE_TR_B_REQS = {'fake_tr_B_req1', 'fake_tr_B_req2'}
42MODULE_NAME_A = 'ModuleNameA'
43MODULE_NAME_A_AGAIN = 'ModuleNameA_AGAIN'
44MODULE_NAME_B = 'ModuleNameB'
45MODULE_NAME_B_AGAIN = 'ModuleNameB_AGAIN'
46MODULE_INFO_A = test_info.TestInfo(MODULE_NAME_A, FAKE_TR_NAME_A, set())
47MODULE_INFO_A_AGAIN = test_info.TestInfo(MODULE_NAME_A_AGAIN, FAKE_TR_NAME_A,
48                                         set())
49MODULE_INFO_B = test_info.TestInfo(MODULE_NAME_B, FAKE_TR_NAME_B, set())
50MODULE_INFO_B_AGAIN = test_info.TestInfo(MODULE_NAME_B_AGAIN, FAKE_TR_NAME_B,
51                                         set())
52BAD_TESTINFO = test_info.TestInfo('bad_name', MISSING_TR_NAME, set())
53
54class FakeTestRunnerA(tr_base.TestRunnerBase):
55    """Fake test runner A."""
56
57    NAME = FAKE_TR_NAME_A
58    EXECUTABLE = 'echo'
59
60    def run_tests(self, test_infos, extra_args, reporter):
61        return 0
62
63    def host_env_check(self):
64        pass
65
66    def get_test_runner_build_reqs(self):
67        return FAKE_TR_A_REQS
68
69    def generate_run_commands(self, test_infos, extra_args, port=None):
70        return ['fake command']
71
72
73class FakeTestRunnerB(FakeTestRunnerA):
74    """Fake test runner B."""
75
76    NAME = FAKE_TR_NAME_B
77
78    def run_tests(self, test_infos, extra_args, reporter):
79        return 1
80
81    def get_test_runner_build_reqs(self):
82        return FAKE_TR_B_REQS
83
84
85class TestRunnerHandlerUnittests(unittest.TestCase):
86    """Unit tests for test_runner_handler.py"""
87
88    _TEST_RUNNERS = {
89        FakeTestRunnerA.NAME: FakeTestRunnerA,
90        FakeTestRunnerB.NAME: FakeTestRunnerB,
91    }
92
93    def setUp(self):
94        mock.patch('test_runner_handler._get_test_runners',
95                   return_value=self._TEST_RUNNERS).start()
96
97    def tearDown(self):
98        mock.patch.stopall()
99
100    def test_group_tests_by_test_runners(self):
101        """Test that we properly group tests by test runners."""
102        # Happy path testing.
103        test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN, MODULE_INFO_B,
104                      MODULE_INFO_B_AGAIN]
105        want_list = [(FakeTestRunnerA, [MODULE_INFO_A, MODULE_INFO_A_AGAIN]),
106                     (FakeTestRunnerB, [MODULE_INFO_B, MODULE_INFO_B_AGAIN])]
107        self.assertEqual(
108            want_list,
109            test_runner_handler.group_tests_by_test_runners(test_infos))
110
111        # Let's make sure we fail as expected.
112        self.assertRaises(
113            atest_error.UnknownTestRunnerError,
114            test_runner_handler.group_tests_by_test_runners, [BAD_TESTINFO])
115
116    def test_get_test_runner_reqs(self):
117        """Test that we get all the reqs from the test runners."""
118        test_infos = [MODULE_INFO_A, MODULE_INFO_B]
119        want_set = FAKE_TR_A_REQS | FAKE_TR_B_REQS
120        empty_module_info = None
121        self.assertEqual(
122            want_set,
123            test_runner_handler.get_test_runner_reqs(empty_module_info,
124                                                     test_infos))
125
126    @mock.patch.dict('os.environ', {constants.ANDROID_BUILD_TOP:'/'})
127    @mock.patch.object(metrics, 'RunnerFinishEvent')
128    def test_run_all_tests(self, _mock_runner_finish):
129        """Test that the return value as we expected."""
130        results_dir = ""
131        extra_args = {}
132        mod_info = module_info.ModuleInfo(
133            module_file=os.path.join(uc.TEST_DATA_DIR, uc.JSON_FILE))
134        # Tests both run_tests return 0
135        test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN]
136        self.assertEqual(
137            0,
138            test_runner_handler.run_all_tests(
139                results_dir, test_infos, extra_args, mod_info)[0])
140        # Tests both run_tests return 1
141        test_infos = [MODULE_INFO_B, MODULE_INFO_B_AGAIN]
142        self.assertEqual(
143            1,
144            test_runner_handler.run_all_tests(
145                results_dir, test_infos, extra_args, mod_info)[0])
146        # Tests with on run_tests return 0, the other return 1
147        test_infos = [MODULE_INFO_A, MODULE_INFO_B]
148        self.assertEqual(
149            1,
150            test_runner_handler.run_all_tests(
151                results_dir, test_infos, extra_args, mod_info)[0])
152
153if __name__ == '__main__':
154    unittest.main()
155