1#!/usr/bin/env python 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 21import unittest 22import mock 23 24import atest_error 25import test_runner_handler 26from metrics import metrics 27from test_finders import test_info 28from test_runners import test_runner_base as tr_base 29 30FAKE_TR_NAME_A = 'FakeTestRunnerA' 31FAKE_TR_NAME_B = 'FakeTestRunnerB' 32MISSING_TR_NAME = 'MissingTestRunner' 33FAKE_TR_A_REQS = {'fake_tr_A_req1', 'fake_tr_A_req2'} 34FAKE_TR_B_REQS = {'fake_tr_B_req1', 'fake_tr_B_req2'} 35MODULE_NAME_A = 'ModuleNameA' 36MODULE_NAME_A_AGAIN = 'ModuleNameA_AGAIN' 37MODULE_NAME_B = 'ModuleNameB' 38MODULE_NAME_B_AGAIN = 'ModuleNameB_AGAIN' 39MODULE_INFO_A = test_info.TestInfo(MODULE_NAME_A, FAKE_TR_NAME_A, set()) 40MODULE_INFO_A_AGAIN = test_info.TestInfo(MODULE_NAME_A_AGAIN, FAKE_TR_NAME_A, 41 set()) 42MODULE_INFO_B = test_info.TestInfo(MODULE_NAME_B, FAKE_TR_NAME_B, set()) 43MODULE_INFO_B_AGAIN = test_info.TestInfo(MODULE_NAME_B_AGAIN, FAKE_TR_NAME_B, 44 set()) 45BAD_TESTINFO = test_info.TestInfo('bad_name', MISSING_TR_NAME, set()) 46 47class FakeTestRunnerA(tr_base.TestRunnerBase): 48 """Fake test runner A.""" 49 50 NAME = FAKE_TR_NAME_A 51 EXECUTABLE = 'echo' 52 53 def run_tests(self, test_infos, extra_args, reporter): 54 return 0 55 56 def host_env_check(self): 57 pass 58 59 def get_test_runner_build_reqs(self): 60 return FAKE_TR_A_REQS 61 62 def generate_run_commands(self, test_infos, extra_args, port=None): 63 return ['fake command'] 64 65 66class FakeTestRunnerB(FakeTestRunnerA): 67 """Fake test runner B.""" 68 69 NAME = FAKE_TR_NAME_B 70 71 def run_tests(self, test_infos, extra_args, reporter): 72 return 1 73 74 def get_test_runner_build_reqs(self): 75 return FAKE_TR_B_REQS 76 77 78class TestRunnerHandlerUnittests(unittest.TestCase): 79 """Unit tests for test_runner_handler.py""" 80 81 _TEST_RUNNERS = { 82 FakeTestRunnerA.NAME: FakeTestRunnerA, 83 FakeTestRunnerB.NAME: FakeTestRunnerB, 84 } 85 86 def setUp(self): 87 mock.patch('test_runner_handler._get_test_runners', 88 return_value=self._TEST_RUNNERS).start() 89 90 def tearDown(self): 91 mock.patch.stopall() 92 93 def test_group_tests_by_test_runners(self): 94 """Test that we properly group tests by test runners.""" 95 # Happy path testing. 96 test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN, MODULE_INFO_B, 97 MODULE_INFO_B_AGAIN] 98 want_list = [(FakeTestRunnerA, [MODULE_INFO_A, MODULE_INFO_A_AGAIN]), 99 (FakeTestRunnerB, [MODULE_INFO_B, MODULE_INFO_B_AGAIN])] 100 self.assertEqual( 101 want_list, 102 test_runner_handler.group_tests_by_test_runners(test_infos)) 103 104 # Let's make sure we fail as expected. 105 self.assertRaises( 106 atest_error.UnknownTestRunnerError, 107 test_runner_handler.group_tests_by_test_runners, [BAD_TESTINFO]) 108 109 def test_get_test_runner_reqs(self): 110 """Test that we get all the reqs from the test runners.""" 111 test_infos = [MODULE_INFO_A, MODULE_INFO_B] 112 want_set = FAKE_TR_A_REQS | FAKE_TR_B_REQS 113 empty_module_info = None 114 self.assertEqual( 115 want_set, 116 test_runner_handler.get_test_runner_reqs(empty_module_info, 117 test_infos)) 118 119 @mock.patch.object(metrics, 'RunnerFinishEvent') 120 def test_run_all_tests(self, _mock_runner_finish): 121 """Test that the return value as we expected.""" 122 results_dir = "" 123 extra_args = [] 124 # Tests both run_tests return 0 125 test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN] 126 self.assertEqual( 127 0, 128 test_runner_handler.run_all_tests( 129 results_dir, test_infos, extra_args)[0]) 130 # Tests both run_tests return 1 131 test_infos = [MODULE_INFO_B, MODULE_INFO_B_AGAIN] 132 self.assertEqual( 133 1, 134 test_runner_handler.run_all_tests( 135 results_dir, test_infos, extra_args)[0]) 136 # Tests with on run_tests return 0, the other return 1 137 test_infos = [MODULE_INFO_A, MODULE_INFO_B] 138 self.assertEqual( 139 1, 140 test_runner_handler.run_all_tests( 141 results_dir, test_infos, extra_args)[0]) 142 143if __name__ == '__main__': 144 unittest.main() 145