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 17"""Unittests for tf_integration_finder.""" 18 19import os 20import unittest 21import mock 22 23# pylint: disable=import-error 24import constants 25import unittest_constants as uc 26import unittest_utils 27from test_finders import test_finder_utils 28from test_finders import test_info 29from test_finders import tf_integration_finder 30from 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={constants.TI_FILTER: frozenset([uc.CLASS_FILTER]), 41 constants.TI_REL_CONFIG: uc.INT_CONFIG}) 42INT_METHOD_INFO = test_info.TestInfo( 43 uc.INT_NAME, 44 atf_tr.AtestTradefedTestRunner.NAME, 45 set(), 46 data={constants.TI_FILTER: frozenset([uc.METHOD_FILTER]), 47 constants.TI_REL_CONFIG: uc.INT_CONFIG}) 48 49 50class TFIntegrationFinderUnittests(unittest.TestCase): 51 """Unit tests for tf_integration_finder.py""" 52 53 def setUp(self): 54 """Set up for testing.""" 55 self.tf_finder = tf_integration_finder.TFIntegrationFinder() 56 self.tf_finder.integration_dirs = [os.path.join(uc.ROOT, uc.INT_DIR), 57 os.path.join(uc.ROOT, uc.GTF_INT_DIR)] 58 self.tf_finder.root_dir = uc.ROOT 59 60 @mock.patch.object(tf_integration_finder.TFIntegrationFinder, 61 '_get_build_targets', return_value=set()) 62 @mock.patch.object(test_finder_utils, 'get_fully_qualified_class_name', 63 return_value=uc.FULL_CLASS_NAME) 64 @mock.patch('subprocess.check_output') 65 @mock.patch('os.path.exists', return_value=True) 66 @mock.patch('os.path.isfile', return_value=False) 67 @mock.patch('os.path.isdir', return_value=False) 68 #pylint: disable=unused-argument 69 def test_find_test_by_integration_name(self, _isdir, _isfile, _path, mock_find, 70 _fcqn, _build): 71 """Test find_test_by_integration_name. 72 73 Note that _isfile is always False since we don't index integration tests. 74 """ 75 mock_find.return_value = os.path.join(uc.ROOT, uc.INT_DIR, uc.INT_NAME + '.xml') 76 t_infos = self.tf_finder.find_test_by_integration_name(uc.INT_NAME) 77 self.assertEqual(len(t_infos), 0) 78 _isdir.return_value = True 79 t_infos = self.tf_finder.find_test_by_integration_name(uc.INT_NAME) 80 unittest_utils.assert_equal_testinfos(self, t_infos[0], uc.INT_INFO) 81 t_infos = self.tf_finder.find_test_by_integration_name(INT_NAME_CLASS) 82 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_CLASS_INFO) 83 t_infos = self.tf_finder.find_test_by_integration_name(INT_NAME_METHOD) 84 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_METHOD_INFO) 85 not_fully_qual = uc.INT_NAME + ':' + 'someClass' 86 t_infos = self.tf_finder.find_test_by_integration_name(not_fully_qual) 87 unittest_utils.assert_equal_testinfos(self, t_infos[0], INT_CLASS_INFO) 88 mock_find.return_value = os.path.join(uc.ROOT, uc.GTF_INT_DIR, 89 uc.GTF_INT_NAME + '.xml') 90 t_infos = self.tf_finder.find_test_by_integration_name(uc.GTF_INT_NAME) 91 unittest_utils.assert_equal_testinfos( 92 self, 93 t_infos[0], 94 uc.GTF_INT_INFO) 95 mock_find.return_value = '' 96 self.assertEqual( 97 self.tf_finder.find_test_by_integration_name('NotIntName'), []) 98 99 @mock.patch.object(tf_integration_finder.TFIntegrationFinder, 100 '_get_build_targets', return_value=set()) 101 @mock.patch('os.path.realpath', 102 side_effect=unittest_utils.realpath_side_effect) 103 @mock.patch('os.path.isdir', return_value=True) 104 @mock.patch('os.path.isfile', return_value=True) 105 @mock.patch.object(test_finder_utils, 'find_parent_module_dir') 106 @mock.patch('os.path.exists', return_value=True) 107 def test_find_int_test_by_path(self, _exists, _find, _isfile, _isdir, _real, 108 _build): 109 """Test find_int_test_by_path.""" 110 path = os.path.join(uc.INT_DIR, uc.INT_NAME + '.xml') 111 t_infos = self.tf_finder.find_int_test_by_path(path) 112 unittest_utils.assert_equal_testinfos( 113 self, uc.INT_INFO, t_infos[0]) 114 path = os.path.join(uc.GTF_INT_DIR, uc.GTF_INT_NAME + '.xml') 115 t_infos = self.tf_finder.find_int_test_by_path(path) 116 unittest_utils.assert_equal_testinfos( 117 self, uc.GTF_INT_INFO, t_infos[0]) 118 119 #pylint: disable=protected-access 120 @mock.patch.object(tf_integration_finder.TFIntegrationFinder, 121 '_search_integration_dirs') 122 def test_load_xml_file(self, search): 123 """Test _load_xml_file and _load_include_tags methods.""" 124 search.return_value = [os.path.join(uc.TEST_DATA_DIR, 125 'CtsUiDeviceTestCases.xml')] 126 xml_file = os.path.join(uc.TEST_DATA_DIR, constants.MODULE_CONFIG) 127 xml_root = self.tf_finder._load_xml_file(xml_file) 128 include_tags = xml_root.findall('.//include') 129 self.assertEqual(0, len(include_tags)) 130 option_tags = xml_root.findall('.//option') 131 included = False 132 for tag in option_tags: 133 if tag.attrib['value'].strip() == 'CtsUiDeviceTestCases.apk': 134 included = True 135 self.assertTrue(included) 136 137 138if __name__ == '__main__': 139 unittest.main() 140