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 test_finders import test_info 27from test_runners import test_runner_base as tr_base 28 29FAKE_TR_NAME_A = 'FakeTestRunnerA' 30FAKE_TR_NAME_B = 'FakeTestRunnerB' 31MISSING_TR_NAME = 'MissingTestRunner' 32FAKE_TR_A_REQS = {'fake_tr_A_req1', 'fake_tr_A_req2'} 33FAKE_TR_B_REQS = {'fake_tr_B_req1', 'fake_tr_B_req2'} 34MODULE_NAME_A = 'ModuleNameA' 35MODULE_NAME_A_AGAIN = 'ModuleNameA_AGAIN' 36MODULE_NAME_B = 'ModuleNameB' 37MODULE_NAME_B_AGAIN = 'ModuleNameB_AGAIN' 38MODULE_INFO_A = test_info.TestInfo(MODULE_NAME_A, FAKE_TR_NAME_A, set()) 39MODULE_INFO_A_AGAIN = test_info.TestInfo(MODULE_NAME_A_AGAIN, FAKE_TR_NAME_A, 40 set()) 41MODULE_INFO_B = test_info.TestInfo(MODULE_NAME_B, FAKE_TR_NAME_B, set()) 42MODULE_INFO_B_AGAIN = test_info.TestInfo(MODULE_NAME_B_AGAIN, FAKE_TR_NAME_B, 43 set()) 44BAD_TESTINFO = test_info.TestInfo('bad_name', MISSING_TR_NAME, set()) 45 46class FakeTestRunnerA(tr_base.TestRunnerBase): 47 """Fake test runner A.""" 48 49 NAME = FAKE_TR_NAME_A 50 EXECUTABLE = 'echo' 51 52 def run_tests(self, test_infos, extra_args): 53 pass 54 55 def host_env_check(self): 56 pass 57 58 def get_test_runner_build_reqs(self): 59 return FAKE_TR_A_REQS 60 61 62class FakeTestRunnerB(FakeTestRunnerA): 63 """Fake test runner B.""" 64 65 NAME = FAKE_TR_NAME_B 66 67 def get_test_runner_build_reqs(self): 68 return FAKE_TR_B_REQS 69 70 71class TestRunnerHandlerUnittests(unittest.TestCase): 72 """Unit tests for test_runner_handler.py""" 73 74 _TEST_RUNNERS = { 75 FakeTestRunnerA.NAME: FakeTestRunnerA, 76 FakeTestRunnerB.NAME: FakeTestRunnerB, 77 } 78 79 def setUp(self): 80 mock.patch('test_runner_handler._get_test_runners', 81 return_value=self._TEST_RUNNERS).start() 82 83 def tearDown(self): 84 mock.patch.stopall() 85 86 def test_group_tests_by_test_runners(self): 87 """Test that we properly group tests by test runners.""" 88 # Happy path testing. 89 test_infos = [MODULE_INFO_A, MODULE_INFO_A_AGAIN, MODULE_INFO_B, 90 MODULE_INFO_B_AGAIN] 91 want_list = [(FakeTestRunnerA, [MODULE_INFO_A, MODULE_INFO_A_AGAIN]), 92 (FakeTestRunnerB, [MODULE_INFO_B, MODULE_INFO_B_AGAIN])] 93 self.assertEqual( 94 want_list, 95 test_runner_handler._group_tests_by_test_runners(test_infos)) 96 97 # Let's make sure we fail as expected. 98 self.assertRaises( 99 atest_error.UnknownTestRunnerError, 100 test_runner_handler._group_tests_by_test_runners, [BAD_TESTINFO]) 101 102 def test_get_test_runner_reqs(self): 103 """Test that we get all the reqs from the test runners.""" 104 test_infos = [MODULE_INFO_A, MODULE_INFO_B] 105 want_set = FAKE_TR_A_REQS | FAKE_TR_B_REQS 106 empty_module_info = None 107 self.assertEqual( 108 want_set, 109 test_runner_handler.get_test_runner_reqs(empty_module_info, 110 test_infos)) 111 112 113if __name__ == '__main__': 114 unittest.main() 115