1#!/usr/bin/env python 2# 3# Copyright 2018, 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"""Unittests for suite_plan_finder.""" 17 18import os 19import unittest 20import mock 21 22# pylint: disable=import-error 23import unittest_constants as uc 24import unittest_utils 25from test_finders import test_finder_utils 26from test_finders import test_info 27from test_finders import suite_plan_finder 28from test_runners import suite_plan_test_runner 29 30 31# pylint: disable=protected-access 32class SuitePlanFinderUnittests(unittest.TestCase): 33 """Unit tests for suite_plan_finder.py""" 34 35 def setUp(self): 36 """Set up stuff for testing.""" 37 self.suite_plan_finder = suite_plan_finder.SuitePlanFinder() 38 self.suite_plan_finder.suite_plan_dirs = [os.path.join(uc.ROOT, uc.CTS_INT_DIR)] 39 self.suite_plan_finder.root_dir = uc.ROOT 40 41 def test_get_test_info_from_path(self): 42 """Test _get_test_info_from_path. 43 Strategy: 44 If suite_path is to cts file --> 45 test_info: test_name=cts, 46 test_runner=TestSuiteTestRunner, 47 build_target=set(['cts'] 48 suite='cts') 49 If suite_path is to cts-common file --> 50 test_info: test_name=cts-common, 51 test_runner=TestSuiteTestRunner, 52 build_target=set(['cts'] 53 suite='cts') 54 If suite_path is to common file --> test_info: None 55 If suite_path is to non-existing file --> test_info: None 56 """ 57 suite_plan = 'cts' 58 path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_plan+'.xml') 59 want_info = test_info.TestInfo(test_name=suite_plan, 60 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 61 build_targets={suite_plan}, 62 suite=suite_plan) 63 unittest_utils.assert_equal_testinfos( 64 self, want_info, self.suite_plan_finder._get_test_info_from_path(path)) 65 66 suite_plan = 'cts-common' 67 path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_plan+'.xml') 68 want_info = test_info.TestInfo(test_name=suite_plan, 69 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 70 build_targets={'cts'}, 71 suite='cts') 72 unittest_utils.assert_equal_testinfos( 73 self, want_info, self.suite_plan_finder._get_test_info_from_path(path)) 74 75 suite_plan = 'common' 76 path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, 'cts-common.xml') 77 want_info = None 78 unittest_utils.assert_equal_testinfos( 79 self, want_info, self.suite_plan_finder._get_test_info_from_path(path, suite_plan)) 80 81 path = os.path.join(uc.ROOT, 'cts-common.xml') 82 want_info = None 83 unittest_utils.assert_equal_testinfos( 84 self, want_info, self.suite_plan_finder._get_test_info_from_path(path)) 85 86 @mock.patch.object(test_finder_utils, 'search_integration_dirs') 87 def test_find_test_by_suite_name(self, _search): 88 """Test find_test_by_suite_name. 89 Strategy: 90 suite_name: cts --> test_info: test_name=cts, 91 test_runner=TestSuiteTestRunner, 92 build_target=set(['cts'] 93 suite='cts') 94 suite_name: CTS --> test_info: None 95 suite_name: cts-common --> test_info: test_name=cts-common, 96 test_runner=TestSuiteTestRunner, 97 build_target=set(['cts'], 98 suite='cts') 99 """ 100 suite_name = 'cts' 101 t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name) 102 want_info = test_info.TestInfo(test_name=suite_name, 103 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 104 build_targets={suite_name}, 105 suite=suite_name) 106 unittest_utils.assert_equal_testinfos(self, t_info[0], want_info) 107 108 suite_name = 'CTS' 109 _search.return_value = None 110 t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name) 111 want_info = None 112 unittest_utils.assert_equal_testinfos(self, t_info, want_info) 113 114 suite_name = 'cts-common' 115 suite = 'cts' 116 _search.return_value = [os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_name + '.xml')] 117 t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name) 118 want_info = test_info.TestInfo(test_name=suite_name, 119 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 120 build_targets=set([suite]), 121 suite=suite) 122 unittest_utils.assert_equal_testinfos(self, t_info[0], want_info) 123 124 @mock.patch('os.path.realpath', 125 side_effect=unittest_utils.realpath_side_effect) 126 @mock.patch('os.path.isdir', return_value=True) 127 @mock.patch('os.path.isfile', return_value=True) 128 @mock.patch.object(test_finder_utils, 'get_int_dir_from_path') 129 @mock.patch('os.path.exists', return_value=True) 130 def test_find_suite_plan_test_by_suite_path(self, _exists, _find, _isfile, _isdir, _real): 131 """Test find_test_by_suite_name. 132 Strategy: 133 suite_name: cts.xml --> test_info: 134 test_name=cts, 135 test_runner=TestSuiteTestRunner, 136 build_target=set(['cts'] 137 suite='cts') 138 suite_name: cts-common.xml --> test_info: 139 test_name=cts-common, 140 test_runner=TestSuiteTestRunner, 141 build_target=set(['cts'], 142 suite='cts') 143 suite_name: cts-camera.xml --> test_info: 144 test_name=cts-camera, 145 test_runner=TestSuiteTestRunner, 146 build_target=set(['cts'], 147 suite='cts') 148 """ 149 suite_int_name = 'cts' 150 suite = 'cts' 151 path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml') 152 _find.return_value = uc.CTS_INT_DIR 153 t_info = self.suite_plan_finder.find_test_by_suite_path(path) 154 want_info = test_info.TestInfo(test_name=suite_int_name, 155 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 156 build_targets=set([suite]), 157 suite=suite) 158 unittest_utils.assert_equal_testinfos(self, t_info[0], want_info) 159 160 suite_int_name = 'cts-common' 161 suite = 'cts' 162 path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml') 163 _find.return_value = uc.CTS_INT_DIR 164 t_info = self.suite_plan_finder.find_test_by_suite_path(path) 165 want_info = test_info.TestInfo(test_name=suite_int_name, 166 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 167 build_targets=set([suite]), 168 suite=suite) 169 unittest_utils.assert_equal_testinfos(self, t_info[0], want_info) 170 171 suite_int_name = 'cts-camera' 172 suite = 'cts' 173 path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml') 174 _find.return_value = uc.CTS_INT_DIR 175 t_info = self.suite_plan_finder.find_test_by_suite_path(path) 176 want_info = test_info.TestInfo(test_name=suite_int_name, 177 test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME, 178 build_targets=set([suite]), 179 suite=suite) 180 unittest_utils.assert_equal_testinfos(self, t_info[0], want_info) 181 182 183if __name__ == '__main__': 184 unittest.main() 185