1#!/usr/bin/env python3 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 17"""Unittests for tf_integration_finder.""" 18 19import os 20import unittest 21from unittest import mock 22 23from atest import constants 24from atest import unittest_constants as uc 25from atest import unittest_utils 26from atest.test_finders import test_filter_utils 27from atest.test_finders import test_finder_utils 28from atest.test_finders import test_info 29from atest.test_finders import tf_integration_finder 30from atest.test_runners import atest_tf_test_runner as atf_tr 31 32 33INT_NAME_CLASS = uc.INT_NAME + ':' + uc.FULL_CLASS_NAME 34INT_NAME_METHOD = INT_NAME_CLASS + '#' + uc.METHOD_NAME 35GTF_INT_CONFIG = os.path.join(uc.GTF_INT_DIR, uc.GTF_INT_NAME + '.xml') 36INT_CLASS_INFO = test_info.TestInfo( 37 uc.INT_NAME, 38 atf_tr.AtestTradefedTestRunner.NAME, 39 set(), 40 data={ 41 constants.TI_FILTER: frozenset([uc.CLASS_FILTER]), 42 constants.TI_REL_CONFIG: uc.INT_CONFIG, 43 }, 44) 45INT_METHOD_INFO = test_info.TestInfo( 46 uc.INT_NAME, 47 atf_tr.AtestTradefedTestRunner.NAME, 48 set(), 49 data={ 50 constants.TI_FILTER: frozenset([uc.METHOD_FILTER]), 51 constants.TI_REL_CONFIG: uc.INT_CONFIG, 52 }, 53) 54 55 56class TFIntegrationFinderUnittests(unittest.TestCase): 57 """Unit tests for tf_integration_finder.py""" 58 59 def setUp(self): 60 """Set up for testing.""" 61 self.tf_finder = tf_integration_finder.TFIntegrationFinder() 62 self.tf_finder.integration_dirs = [ 63 os.path.join(uc.ROOT, uc.INT_DIR), 64 os.path.join(uc.ROOT, uc.GTF_INT_DIR), 65 ] 66 self.tf_finder.root_dir = uc.ROOT 67 68 @mock.patch.object( 69 tf_integration_finder.TFIntegrationFinder, 70 '_get_build_targets', 71 return_value=set(), 72 ) 73 @mock.patch.object( 74 test_filter_utils, 75 'get_fully_qualified_class_name', 76 return_value=uc.FULL_CLASS_NAME, 77 ) 78 @mock.patch('subprocess.check_output') 79 @mock.patch('os.path.exists', return_value=True) 80 @mock.patch('os.path.isfile', return_value=False) 81 @mock.patch('os.path.isdir', return_value=False) 82 # pylint: disable=unused-argument 83 def test_find_test_by_integration_name( 84 self, _isdir, _isfile, _path, mock_find, _fcqn, _build 85 ): 86 """Test find_test_by_integration_name. 87 88 Note that _isfile is always False since we don't index integration tests. 89 """ 90 mock_find.return_value = os.path.join( 91 uc.ROOT, uc.INT_DIR, uc.INT_NAME + '.xml' 92 ) 93 t_infos = self.tf_finder.find_test_by_integration_name(uc.INT_NAME) 94 self.assertEqual(len(t_infos), 0) 95 _isdir.return_value = True 96 t_infos = self.tf_finder.find_test_by_integration_name(uc.INT_NAME) 97 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.INT_INFO) 98 t_infos = self.tf_finder.find_test_by_integration_name(INT_NAME_CLASS) 99 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_CLASS_INFO) 100 t_infos = self.tf_finder.find_test_by_integration_name(INT_NAME_METHOD) 101 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_METHOD_INFO) 102 not_fully_qual = uc.INT_NAME + ':' + 'someClass' 103 t_infos = self.tf_finder.find_test_by_integration_name(not_fully_qual) 104 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_CLASS_INFO) 105 mock_find.return_value = os.path.join( 106 uc.ROOT, uc.GTF_INT_DIR, uc.GTF_INT_NAME + '.xml' 107 ) 108 t_infos = self.tf_finder.find_test_by_integration_name(uc.GTF_INT_NAME) 109 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.GTF_INT_INFO) 110 mock_find.return_value = '' 111 self.assertEqual( 112 self.tf_finder.find_test_by_integration_name('NotIntName'), [] 113 ) 114 115 @mock.patch.object( 116 tf_integration_finder.TFIntegrationFinder, 117 '_get_build_targets', 118 return_value=set(), 119 ) 120 @mock.patch( 121 'os.path.realpath', side_effect=unittest_utils.realpath_side_effect 122 ) 123 @mock.patch('os.path.isdir', return_value=True) 124 @mock.patch('os.path.isfile', return_value=True) 125 @mock.patch.object(test_finder_utils, 'find_parent_module_dir') 126 @mock.patch('os.path.exists', return_value=True) 127 def test_find_int_test_by_path( 128 self, _exists, _find, _isfile, _isdir, _real, _build 129 ): 130 """Test find_int_test_by_path.""" 131 path = os.path.join(uc.INT_DIR, uc.INT_NAME + '.xml') 132 t_infos = self.tf_finder.find_int_test_by_path(path) 133 unittest_utils.assert_equal_testinfos(self, uc.INT_INFO, t_infos[0]) 134 path = os.path.join(uc.GTF_INT_DIR, uc.GTF_INT_NAME + '.xml') 135 t_infos = self.tf_finder.find_int_test_by_path(path) 136 unittest_utils.assert_equal_testinfos(self, uc.GTF_INT_INFO, t_infos[0]) 137 138 # pylint: disable=protected-access 139 @mock.patch.object( 140 tf_integration_finder.TFIntegrationFinder, '_search_integration_dirs' 141 ) 142 def test_load_xml_file(self, search): 143 """Test _load_xml_file and _load_include_tags methods.""" 144 search.return_value = [ 145 os.path.join(uc.TEST_DATA_DIR, 'CtsUiDeviceTestCases.xml.data') 146 ] 147 xml_file = os.path.join(uc.TEST_DATA_DIR, constants.MODULE_CONFIG + '.data') 148 xml_root = self.tf_finder._load_xml_file(xml_file) 149 include_tags = xml_root.findall('.//include') 150 self.assertEqual(0, len(include_tags)) 151 option_tags = xml_root.findall('.//option') 152 included = False 153 for tag in option_tags: 154 if tag.attrib['value'].strip() == 'CtsUiDeviceTestCases.apk': 155 included = True 156 self.assertTrue(included) 157 158 @mock.patch.object( 159 tf_integration_finder.TFIntegrationFinder, '_get_prebuilt_jars' 160 ) 161 def test_search_prebuilt_jars(self, prebuilt_jars): 162 """Test _search_prebuilt_jars method.""" 163 test_plan = 'performance/inodeop-benchmark' 164 prebuilt_jars.return_value = [ 165 os.path.join( 166 uc.TEST_DATA_DIR, 167 'tradefed_prebuilt/prebuilts/filegroups/tradefed/tradefed-contrib.jar', 168 ) 169 ] 170 expect_path = [ 171 os.path.join( 172 self.tf_finder.temp_dir.name, 173 'tradefed-contrib.jar', 174 'config', 175 test_plan + '.xml', 176 ) 177 ] 178 self.assertEqual( 179 self.tf_finder._search_prebuilt_jars(test_plan), expect_path 180 ) 181 182 def test_get_prebuilt_jars(self): 183 """Test _get_prebuilt_jars method.""" 184 tf_int_finder = tf_integration_finder.TFIntegrationFinder() 185 tf_int_finder.tf_dirs = ['tradefed_prebuilt/prebuilts/test_harness'] 186 tf_int_finder.root_dir = uc.TEST_DATA_DIR 187 expect_prebuilt_jars = [ 188 os.path.join( 189 uc.TEST_DATA_DIR, 190 'tradefed_prebuilt/prebuilts/test_harness/..', 191 'filegroups/tradefed/tradefed-contrib.jar', 192 ) 193 ] 194 self.assertEqual(tf_int_finder._get_prebuilt_jars(), expect_prebuilt_jars) 195 196 197if __name__ == '__main__': 198 unittest.main() 199