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