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 module_info.""" 18 19# pylint: disable=line-too-long 20 21import os 22import unittest 23 24from unittest import mock 25 26import constants 27import module_info 28import unittest_utils 29import unittest_constants as uc 30 31JSON_FILE_PATH = os.path.join(uc.TEST_DATA_DIR, uc.JSON_FILE) 32EXPECTED_MOD_TARGET = 'tradefed' 33EXPECTED_MOD_TARGET_PATH = ['tf/core'] 34UNEXPECTED_MOD_TARGET = 'this_should_not_be_in_module-info.json' 35MOD_NO_PATH = 'module-no-path' 36PATH_TO_MULT_MODULES = 'shared/path/to/be/used' 37MULT_MOODULES_WITH_SHARED_PATH = ['module2', 'module1'] 38PATH_TO_MULT_MODULES_WITH_MULTI_ARCH = 'shared/path/to/be/used2' 39TESTABLE_MODULES_WITH_SHARED_PATH = ['multiarch1', 'multiarch2', 'multiarch3', 'multiarch3_32'] 40 41ROBO_MOD_PATH = ['/shared/robo/path'] 42NON_RUN_ROBO_MOD_NAME = 'robo_mod' 43RUN_ROBO_MOD_NAME = 'run_robo_mod' 44NON_RUN_ROBO_MOD = {constants.MODULE_NAME: NON_RUN_ROBO_MOD_NAME, 45 constants.MODULE_PATH: ROBO_MOD_PATH, 46 constants.MODULE_CLASS: ['random_class']} 47RUN_ROBO_MOD = {constants.MODULE_NAME: RUN_ROBO_MOD_NAME, 48 constants.MODULE_PATH: ROBO_MOD_PATH, 49 constants.MODULE_CLASS: [constants.MODULE_CLASS_ROBOLECTRIC]} 50MOD_PATH_INFO_DICT = {ROBO_MOD_PATH[0]: [RUN_ROBO_MOD, NON_RUN_ROBO_MOD]} 51MOD_NAME_INFO_DICT = { 52 RUN_ROBO_MOD_NAME: RUN_ROBO_MOD, 53 NON_RUN_ROBO_MOD_NAME: NON_RUN_ROBO_MOD} 54MOD_NAME1 = 'mod1' 55MOD_NAME2 = 'mod2' 56MOD_NAME3 = 'mod3' 57MOD_NAME4 = 'mod4' 58MOD_INFO_DICT = {} 59MODULE_INFO = {constants.MODULE_NAME: 'random_name', 60 constants.MODULE_PATH: 'a/b/c/path', 61 constants.MODULE_CLASS: ['random_class']} 62NAME_TO_MODULE_INFO = {'random_name' : MODULE_INFO} 63 64#pylint: disable=protected-access 65class ModuleInfoUnittests(unittest.TestCase): 66 """Unit tests for module_info.py""" 67 68 @mock.patch('json.load', return_value={}) 69 @mock.patch('builtins.open', new_callable=mock.mock_open) 70 @mock.patch('os.path.isfile', return_value=True) 71 def test_load_mode_info_file_out_dir_handling(self, _isfile, _open, _json): 72 """Test _load_module_info_file out dir handling.""" 73 # Test out default out dir is used. 74 build_top = '/path/to/top' 75 default_out_dir = os.path.join(build_top, 'out/dir/here') 76 os_environ_mock = {'ANDROID_PRODUCT_OUT': default_out_dir, 77 constants.ANDROID_BUILD_TOP: build_top} 78 default_out_dir_mod_targ = 'out/dir/here/module-info.json' 79 # Make sure module_info_target is what we think it is. 80 with mock.patch.dict('os.environ', os_environ_mock, clear=True): 81 mod_info = module_info.ModuleInfo() 82 self.assertEqual(default_out_dir_mod_targ, 83 mod_info.module_info_target) 84 85 # Test out custom out dir is used (OUT_DIR=dir2). 86 custom_out_dir = os.path.join(build_top, 'out2/dir/here') 87 os_environ_mock = {'ANDROID_PRODUCT_OUT': custom_out_dir, 88 constants.ANDROID_BUILD_TOP: build_top} 89 custom_out_dir_mod_targ = 'out2/dir/here/module-info.json' 90 # Make sure module_info_target is what we think it is. 91 with mock.patch.dict('os.environ', os_environ_mock, clear=True): 92 mod_info = module_info.ModuleInfo() 93 self.assertEqual(custom_out_dir_mod_targ, 94 mod_info.module_info_target) 95 96 # Test out custom abs out dir is used (OUT_DIR=/tmp/out/dir2). 97 abs_custom_out_dir = '/tmp/out/dir' 98 os_environ_mock = {'ANDROID_PRODUCT_OUT': abs_custom_out_dir, 99 constants.ANDROID_BUILD_TOP: build_top} 100 custom_abs_out_dir_mod_targ = '/tmp/out/dir/module-info.json' 101 # Make sure module_info_target is what we think it is. 102 with mock.patch.dict('os.environ', os_environ_mock, clear=True): 103 mod_info = module_info.ModuleInfo() 104 self.assertEqual(custom_abs_out_dir_mod_targ, 105 mod_info.module_info_target) 106 107 @mock.patch.object(module_info.ModuleInfo, '_load_module_info_file',) 108 def test_get_path_to_module_info(self, mock_load_module): 109 """Test that we correctly create the path to module info dict.""" 110 mod_one = 'mod1' 111 mod_two = 'mod2' 112 mod_path_one = '/path/to/mod1' 113 mod_path_two = '/path/to/mod2' 114 mod_info_dict = {mod_one: {constants.MODULE_PATH: [mod_path_one], 115 constants.MODULE_NAME: mod_one}, 116 mod_two: {constants.MODULE_PATH: [mod_path_two], 117 constants.MODULE_NAME: mod_two}} 118 mock_load_module.return_value = ('mod_target', mod_info_dict) 119 path_to_mod_info = {mod_path_one: [{constants.MODULE_NAME: mod_one, 120 constants.MODULE_PATH: [mod_path_one]}], 121 mod_path_two: [{constants.MODULE_NAME: mod_two, 122 constants.MODULE_PATH: [mod_path_two]}]} 123 mod_info = module_info.ModuleInfo() 124 self.assertDictEqual(path_to_mod_info, 125 mod_info._get_path_to_module_info(mod_info_dict)) 126 127 def test_is_module(self): 128 """Test that we get the module when it's properly loaded.""" 129 # Load up the test json file and check that module is in it 130 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 131 self.assertTrue(mod_info.is_module(EXPECTED_MOD_TARGET)) 132 self.assertFalse(mod_info.is_module(UNEXPECTED_MOD_TARGET)) 133 134 def test_get_path(self): 135 """Test that we get the module path when it's properly loaded.""" 136 # Load up the test json file and check that module is in it 137 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 138 self.assertEqual(mod_info.get_paths(EXPECTED_MOD_TARGET), 139 EXPECTED_MOD_TARGET_PATH) 140 self.assertEqual(mod_info.get_paths(MOD_NO_PATH), []) 141 142 def test_get_module_names(self): 143 """test that we get the module name properly.""" 144 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 145 self.assertEqual(mod_info.get_module_names(EXPECTED_MOD_TARGET_PATH[0]), 146 [EXPECTED_MOD_TARGET]) 147 unittest_utils.assert_strict_equal( 148 self, mod_info.get_module_names(PATH_TO_MULT_MODULES), 149 MULT_MOODULES_WITH_SHARED_PATH) 150 151 def test_path_to_mod_info(self): 152 """test that we get the module name properly.""" 153 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 154 module_list = [] 155 for path_to_mod_info in mod_info.path_to_module_info[PATH_TO_MULT_MODULES_WITH_MULTI_ARCH]: 156 module_list.append(path_to_mod_info.get(constants.MODULE_NAME)) 157 module_list.sort() 158 TESTABLE_MODULES_WITH_SHARED_PATH.sort() 159 self.assertEqual(module_list, TESTABLE_MODULES_WITH_SHARED_PATH) 160 161 def test_is_suite_in_compatibility_suites(self): 162 """Test is_suite_in_compatibility_suites.""" 163 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 164 info = {'compatibility_suites': []} 165 self.assertFalse(mod_info.is_suite_in_compatibility_suites("cts", info)) 166 info2 = {'compatibility_suites': ["cts"]} 167 self.assertTrue(mod_info.is_suite_in_compatibility_suites("cts", info2)) 168 self.assertFalse(mod_info.is_suite_in_compatibility_suites("vts10", info2)) 169 info3 = {'compatibility_suites': ["cts", "vts10"]} 170 self.assertTrue(mod_info.is_suite_in_compatibility_suites("cts", info3)) 171 self.assertTrue(mod_info.is_suite_in_compatibility_suites("vts10", info3)) 172 self.assertFalse(mod_info.is_suite_in_compatibility_suites("ats", info3)) 173 174 @mock.patch.object(module_info.ModuleInfo, 'is_testable_module') 175 @mock.patch.object(module_info.ModuleInfo, 'is_suite_in_compatibility_suites') 176 def test_get_testable_modules(self, mock_is_suite_exist, mock_is_testable): 177 """Test get_testable_modules.""" 178 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 179 mock_is_testable.return_value = False 180 self.assertEqual(mod_info.get_testable_modules(), set()) 181 mod_info.name_to_module_info = NAME_TO_MODULE_INFO 182 mock_is_testable.return_value = True 183 mock_is_suite_exist.return_value = True 184 self.assertEqual(1, len(mod_info.get_testable_modules('test_suite'))) 185 mock_is_suite_exist.return_value = False 186 self.assertEqual(0, len(mod_info.get_testable_modules('test_suite'))) 187 self.assertEqual(1, len(mod_info.get_testable_modules())) 188 189 @mock.patch.object(module_info.ModuleInfo, 'has_test_config') 190 @mock.patch.object(module_info.ModuleInfo, 'is_robolectric_test') 191 def test_is_testable_module(self, mock_is_robo_test, mock_has_test_config): 192 """Test is_testable_module.""" 193 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 194 mock_is_robo_test.return_value = False 195 mock_has_test_config.return_value = True 196 installed_module_info = {constants.MODULE_INSTALLED: 197 uc.DEFAULT_INSTALL_PATH} 198 non_installed_module_info = {constants.MODULE_NAME: 'rand_name'} 199 # Empty mod_info or a non-installed module. 200 self.assertFalse(mod_info.is_testable_module(non_installed_module_info)) 201 self.assertFalse(mod_info.is_testable_module({})) 202 # Testable Module or is a robo module for non-installed module. 203 self.assertTrue(mod_info.is_testable_module(installed_module_info)) 204 mock_has_test_config.return_value = False 205 self.assertFalse(mod_info.is_testable_module(installed_module_info)) 206 mock_is_robo_test.return_value = True 207 self.assertTrue(mod_info.is_testable_module(non_installed_module_info)) 208 209 @mock.patch.dict('os.environ', {constants.ANDROID_BUILD_TOP:'/'}) 210 @mock.patch.object(module_info.ModuleInfo, 'is_auto_gen_test_config') 211 def test_has_test_config(self, mock_is_auto_gen): 212 """Test has_test_config.""" 213 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 214 info = {constants.MODULE_PATH:[uc.TEST_DATA_DIR]} 215 mock_is_auto_gen.return_value = True 216 # Validate we see the config when it's auto-generated. 217 self.assertTrue(mod_info.has_test_config(info)) 218 self.assertTrue(mod_info.has_test_config({})) 219 # Validate when actual config exists and there's no auto-generated config. 220 mock_is_auto_gen.return_value = False 221 self.assertTrue(mod_info.has_test_config(info)) 222 self.assertFalse(mod_info.has_test_config({})) 223 # Validate the case mod_info MODULE_TEST_CONFIG be set 224 info2 = {constants.MODULE_PATH:[uc.TEST_CONFIG_DATA_DIR], 225 constants.MODULE_TEST_CONFIG:[os.path.join(uc.TEST_CONFIG_DATA_DIR, "a.xml")]} 226 self.assertTrue(mod_info.has_test_config(info2)) 227 228 @mock.patch.object(module_info.ModuleInfo, 'get_module_names') 229 def test_get_robolectric_test_name(self, mock_get_module_names): 230 """Test get_robolectric_test_name.""" 231 # Happy path testing, make sure we get the run robo target. 232 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 233 mod_info.name_to_module_info = MOD_NAME_INFO_DICT 234 mod_info.path_to_module_info = MOD_PATH_INFO_DICT 235 mock_get_module_names.return_value = [RUN_ROBO_MOD_NAME, NON_RUN_ROBO_MOD_NAME] 236 self.assertEqual(mod_info.get_robolectric_test_name( 237 NON_RUN_ROBO_MOD_NAME), RUN_ROBO_MOD_NAME) 238 # Let's also make sure we don't return anything when we're not supposed 239 # to. 240 mock_get_module_names.return_value = [NON_RUN_ROBO_MOD_NAME] 241 self.assertEqual(mod_info.get_robolectric_test_name( 242 NON_RUN_ROBO_MOD_NAME), None) 243 244 @mock.patch.object(module_info.ModuleInfo, 'get_module_info') 245 @mock.patch.object(module_info.ModuleInfo, 'get_module_names') 246 def test_is_robolectric_test(self, mock_get_module_names, mock_get_module_info): 247 """Test is_robolectric_test.""" 248 # Happy path testing, make sure we get the run robo target. 249 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 250 mod_info.name_to_module_info = MOD_NAME_INFO_DICT 251 mod_info.path_to_module_info = MOD_PATH_INFO_DICT 252 mock_get_module_names.return_value = [RUN_ROBO_MOD_NAME, NON_RUN_ROBO_MOD_NAME] 253 mock_get_module_info.return_value = RUN_ROBO_MOD 254 # Test on a run robo module. 255 self.assertTrue(mod_info.is_robolectric_test(RUN_ROBO_MOD_NAME)) 256 # Test on a non-run robo module but shares with a run robo module. 257 self.assertTrue(mod_info.is_robolectric_test(NON_RUN_ROBO_MOD_NAME)) 258 # Make sure we don't find robo tests where they don't exist. 259 mock_get_module_info.return_value = None 260 self.assertFalse(mod_info.is_robolectric_test('rand_mod')) 261 262 @mock.patch.object(module_info.ModuleInfo, 'is_module') 263 def test_is_auto_gen_test_config(self, mock_is_module): 264 """Test is_auto_gen_test_config correctly detects the module.""" 265 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 266 mock_is_module.return_value = True 267 is_auto_test_config = {'auto_test_config': [True]} 268 is_not_auto_test_config = {'auto_test_config': [False]} 269 is_not_auto_test_config_again = {'auto_test_config': []} 270 MOD_INFO_DICT[MOD_NAME1] = is_auto_test_config 271 MOD_INFO_DICT[MOD_NAME2] = is_not_auto_test_config 272 MOD_INFO_DICT[MOD_NAME3] = is_not_auto_test_config_again 273 MOD_INFO_DICT[MOD_NAME4] = {} 274 mod_info.name_to_module_info = MOD_INFO_DICT 275 self.assertTrue(mod_info.is_auto_gen_test_config(MOD_NAME1)) 276 self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME2)) 277 self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME3)) 278 self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME4)) 279 280 def test_is_robolectric_module(self): 281 """Test is_robolectric_module correctly detects the module.""" 282 mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH) 283 is_robolectric_module = {'class': ['ROBOLECTRIC']} 284 is_not_robolectric_module = {'class': ['OTHERS']} 285 MOD_INFO_DICT[MOD_NAME1] = is_robolectric_module 286 MOD_INFO_DICT[MOD_NAME2] = is_not_robolectric_module 287 mod_info.name_to_module_info = MOD_INFO_DICT 288 self.assertTrue(mod_info.is_robolectric_module(MOD_INFO_DICT[MOD_NAME1])) 289 self.assertFalse(mod_info.is_robolectric_module(MOD_INFO_DICT[MOD_NAME2])) 290 291 292if __name__ == '__main__': 293 unittest.main() 294