1# 2# Copyright (C) 2017 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import filecmp 18import os.path 19 20import common 21import test_utils 22from build_image import ( 23 BuildImageError, CheckHeadroom, GetFilesystemCharacteristics, 24 SetUpInDirAndFsConfig) 25 26 27class BuildImageTest(test_utils.ReleaseToolsTestCase): 28 29 # Available: 1000 blocks. 30 EXT4FS_OUTPUT = ( 31 "Created filesystem with 2777/129024 inodes and 515099/516099 blocks") 32 33 def test_CheckHeadroom_SizeUnderLimit(self): 34 # Required headroom: 1000 blocks. 35 prop_dict = { 36 'fs_type' : 'ext4', 37 'partition_headroom' : '4096000', 38 'mount_point' : 'system', 39 } 40 CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict) 41 42 def test_CheckHeadroom_InsufficientHeadroom(self): 43 # Required headroom: 1001 blocks. 44 prop_dict = { 45 'fs_type' : 'ext4', 46 'partition_headroom' : '4100096', 47 'mount_point' : 'system', 48 } 49 self.assertRaises( 50 BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) 51 52 @test_utils.SkipIfExternalToolsUnavailable() 53 def test_CheckHeadroom_WrongFsType(self): 54 prop_dict = { 55 'fs_type' : 'f2fs', 56 'partition_headroom' : '4100096', 57 'mount_point' : 'system', 58 } 59 self.assertRaises( 60 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) 61 62 def test_CheckHeadroom_MissingProperties(self): 63 prop_dict = { 64 'fs_type' : 'ext4', 65 'partition_headroom' : '4100096', 66 } 67 self.assertRaises( 68 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) 69 70 prop_dict = { 71 'fs_type' : 'ext4', 72 'mount_point' : 'system', 73 } 74 self.assertRaises( 75 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) 76 77 @test_utils.SkipIfExternalToolsUnavailable() 78 def test_CheckHeadroom_WithMke2fsOutput(self): 79 """Tests the result parsing from actual call to mke2fs.""" 80 input_dir = common.MakeTempDir() 81 output_image = common.MakeTempFile(suffix='.img') 82 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4', 83 '/system', '409600', '-j', '0'] 84 proc = common.Run(command) 85 ext4fs_output, _ = proc.communicate() 86 self.assertEqual(0, proc.returncode) 87 88 prop_dict = { 89 'fs_type' : 'ext4', 90 'partition_headroom' : '40960', 91 'mount_point' : 'system', 92 } 93 CheckHeadroom(ext4fs_output, prop_dict) 94 95 prop_dict = { 96 'fs_type' : 'ext4', 97 'partition_headroom' : '413696', 98 'mount_point' : 'system', 99 } 100 self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict) 101 102 def test_SetUpInDirAndFsConfig_NonSystem(self): 103 prop_dict = { 104 'fs_config': 'fs-config', 105 'mount_point': 'vendor', 106 } 107 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict) 108 self.assertEqual('/path/to/in_dir', in_dir) 109 self.assertEqual('fs-config', fs_config) 110 self.assertEqual('vendor', prop_dict['mount_point']) 111 112 @staticmethod 113 def _gen_fs_config(partition): 114 fs_config = common.MakeTempFile(suffix='.txt') 115 with open(fs_config, 'w') as fs_config_fp: 116 fs_config_fp.write('fs-config-{}\n'.format(partition)) 117 return fs_config 118 119 def test_SetUpInDirAndFsConfig(self): 120 root_dir = common.MakeTempDir() 121 with open(os.path.join(root_dir, 'init'), 'w') as init_fp: 122 init_fp.write('init') 123 124 origin_in = common.MakeTempDir() 125 with open(os.path.join(origin_in, 'file'), 'w') as in_fp: 126 in_fp.write('system-file') 127 os.symlink('../etc', os.path.join(origin_in, 'symlink')) 128 129 fs_config_system = self._gen_fs_config('system') 130 131 prop_dict = { 132 'fs_config': fs_config_system, 133 'mount_point': 'system', 134 'root_dir': root_dir, 135 } 136 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict) 137 138 self.assertTrue(filecmp.cmp( 139 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init'))) 140 self.assertTrue(filecmp.cmp( 141 os.path.join(in_dir, 'system', 'file'), 142 os.path.join(origin_in, 'file'))) 143 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink'))) 144 145 self.assertTrue(filecmp.cmp(fs_config_system, fs_config)) 146 self.assertEqual('/', prop_dict['mount_point']) 147 148 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self): 149 root_dir = common.MakeTempDir() 150 with open(os.path.join(root_dir, 'init'), 'w') as init_fp: 151 init_fp.write('init') 152 153 origin_in = common.MakeTempDir() 154 with open(os.path.join(origin_in, 'file'), 'w') as in_fp: 155 in_fp.write('system-file') 156 os.symlink('../etc', os.path.join(origin_in, 'symlink')) 157 158 fs_config_system = self._gen_fs_config('system') 159 fs_config_root = self._gen_fs_config('root') 160 161 prop_dict = { 162 'fs_config': fs_config_system, 163 'mount_point': 'system', 164 'root_dir': root_dir, 165 'root_fs_config': fs_config_root, 166 } 167 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict) 168 169 self.assertTrue(filecmp.cmp( 170 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init'))) 171 self.assertTrue(filecmp.cmp( 172 os.path.join(in_dir, 'system', 'file'), 173 os.path.join(origin_in, 'file'))) 174 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink'))) 175 176 with open(fs_config) as fs_config_fp: 177 fs_config_data = fs_config_fp.readlines() 178 self.assertIn('fs-config-system\n', fs_config_data) 179 self.assertIn('fs-config-root\n', fs_config_data) 180 self.assertEqual('/', prop_dict['mount_point']) 181 182 @test_utils.SkipIfExternalToolsUnavailable() 183 def test_GetFilesystemCharacteristics(self): 184 input_dir = common.MakeTempDir() 185 output_image = common.MakeTempFile(suffix='.img') 186 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4', 187 '/system', '409600', '-j', '0'] 188 proc = common.Run(command) 189 proc.communicate() 190 self.assertEqual(0, proc.returncode) 191 192 output_file = common.MakeTempFile(suffix='.img') 193 cmd = ["img2simg", output_image, output_file] 194 p = common.Run(cmd) 195 p.communicate() 196 self.assertEqual(0, p.returncode) 197 198 fs_dict = GetFilesystemCharacteristics('ext4', output_file) 199 self.assertEqual(int(fs_dict['Block size']), 4096) 200 self.assertGreaterEqual(int(fs_dict['Free blocks']), 0) # expect ~88 201 self.assertGreater(int(fs_dict['Inode count']), 0) # expect ~64 202 self.assertGreaterEqual(int(fs_dict['Free inodes']), 0) # expect ~53 203 self.assertGreater(int(fs_dict['Inode count']), int(fs_dict['Free inodes'])) 204