Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 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 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 17 | import filecmp |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 18 | import math |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 19 | import os.path |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 20 | import random |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 21 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 22 | import common |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 23 | from build_image import ( |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 24 | AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom, |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 25 | SetUpInDirAndFsConfig) |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 26 | from test_utils import ReleaseToolsTestCase |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 27 | |
| 28 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 29 | class BuildImageTest(ReleaseToolsTestCase): |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 30 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 31 | # Available: 1000 blocks. |
| 32 | EXT4FS_OUTPUT = ( |
| 33 | "Created filesystem with 2777/129024 inodes and 515099/516099 blocks") |
| 34 | |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 35 | def setUp(self): |
| 36 | # To test AVBCalcMinPartitionSize(), by using 200MB to 2GB image size. |
| 37 | # - 51200 = 200MB * 1024 * 1024 / 4096 |
| 38 | # - 524288 = 2GB * 1024 * 1024 * 1024 / 4096 |
| 39 | self._image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset |
| 40 | for offset in range(BLOCK_SIZE)] |
| 41 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 42 | def test_CheckHeadroom_SizeUnderLimit(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 43 | # Required headroom: 1000 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 44 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 45 | 'fs_type' : 'ext4', |
| 46 | 'partition_headroom' : '4096000', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 47 | 'mount_point' : 'system', |
| 48 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 49 | CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 50 | |
| 51 | def test_CheckHeadroom_InsufficientHeadroom(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 52 | # Required headroom: 1001 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 53 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 54 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 55 | 'partition_headroom' : '4100096', |
| 56 | 'mount_point' : 'system', |
| 57 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 58 | self.assertRaises( |
| 59 | BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 60 | |
| 61 | def test_CheckHeadroom_WrongFsType(self): |
| 62 | prop_dict = { |
| 63 | 'fs_type' : 'f2fs', |
| 64 | 'partition_headroom' : '4100096', |
| 65 | 'mount_point' : 'system', |
| 66 | } |
| 67 | self.assertRaises( |
| 68 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
| 69 | |
| 70 | def test_CheckHeadroom_MissingProperties(self): |
| 71 | prop_dict = { |
| 72 | 'fs_type' : 'ext4', |
| 73 | 'partition_headroom' : '4100096', |
| 74 | } |
| 75 | self.assertRaises( |
| 76 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
| 77 | |
| 78 | prop_dict = { |
| 79 | 'fs_type' : 'ext4', |
| 80 | 'mount_point' : 'system', |
| 81 | } |
| 82 | self.assertRaises( |
| 83 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 84 | |
| 85 | def test_CheckHeadroom_WithMke2fsOutput(self): |
| 86 | """Tests the result parsing from actual call to mke2fs.""" |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 87 | input_dir = common.MakeTempDir() |
| 88 | output_image = common.MakeTempFile(suffix='.img') |
Tianjie Xu | 5733222 | 2018-08-15 16:16:21 -0700 | [diff] [blame] | 89 | command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 90 | '/system', '409600', '-j', '0'] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 91 | proc = common.Run(command) |
| 92 | ext4fs_output, _ = proc.communicate() |
| 93 | self.assertEqual(0, proc.returncode) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 94 | |
| 95 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 96 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 97 | 'partition_headroom' : '40960', |
| 98 | 'mount_point' : 'system', |
| 99 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 100 | CheckHeadroom(ext4fs_output, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 101 | |
| 102 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 103 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 104 | 'partition_headroom' : '413696', |
| 105 | 'mount_point' : 'system', |
| 106 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 107 | self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 108 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 109 | def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self): |
| 110 | prop_dict = { |
| 111 | 'fs_config': 'fs-config', |
| 112 | 'mount_point': 'vendor', |
| 113 | 'system_root_image': 'true', |
| 114 | } |
| 115 | in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict) |
| 116 | self.assertEqual('/path/to/in_dir', in_dir) |
| 117 | self.assertEqual('fs-config', fs_config) |
| 118 | self.assertEqual('vendor', prop_dict['mount_point']) |
| 119 | |
| 120 | @staticmethod |
| 121 | def _gen_fs_config(partition): |
| 122 | fs_config = common.MakeTempFile(suffix='.txt') |
| 123 | with open(fs_config, 'w') as fs_config_fp: |
| 124 | fs_config_fp.write('fs-config-{}\n'.format(partition)) |
| 125 | return fs_config |
| 126 | |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 127 | def test_SetUpInDirAndFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 128 | root_dir = common.MakeTempDir() |
| 129 | with open(os.path.join(root_dir, 'init'), 'w') as init_fp: |
| 130 | init_fp.write('init') |
| 131 | |
| 132 | origin_in = common.MakeTempDir() |
| 133 | with open(os.path.join(origin_in, 'file'), 'w') as in_fp: |
| 134 | in_fp.write('system-file') |
| 135 | os.symlink('../etc', os.path.join(origin_in, 'symlink')) |
| 136 | |
| 137 | fs_config_system = self._gen_fs_config('system') |
| 138 | |
| 139 | prop_dict = { |
| 140 | 'fs_config': fs_config_system, |
| 141 | 'mount_point': 'system', |
| 142 | 'root_dir': root_dir, |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 143 | } |
| 144 | in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict) |
| 145 | |
| 146 | self.assertTrue(filecmp.cmp( |
| 147 | os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init'))) |
| 148 | self.assertTrue(filecmp.cmp( |
| 149 | os.path.join(in_dir, 'system', 'file'), |
| 150 | os.path.join(origin_in, 'file'))) |
| 151 | self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink'))) |
| 152 | |
| 153 | self.assertTrue(filecmp.cmp(fs_config_system, fs_config)) |
| 154 | self.assertEqual('/', prop_dict['mount_point']) |
| 155 | |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 156 | def test_SetUpInDirAndFsConfig_WithRootFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 157 | root_dir = common.MakeTempDir() |
| 158 | with open(os.path.join(root_dir, 'init'), 'w') as init_fp: |
| 159 | init_fp.write('init') |
| 160 | |
| 161 | origin_in = common.MakeTempDir() |
| 162 | with open(os.path.join(origin_in, 'file'), 'w') as in_fp: |
| 163 | in_fp.write('system-file') |
| 164 | os.symlink('../etc', os.path.join(origin_in, 'symlink')) |
| 165 | |
| 166 | fs_config_system = self._gen_fs_config('system') |
| 167 | fs_config_root = self._gen_fs_config('root') |
| 168 | |
| 169 | prop_dict = { |
| 170 | 'fs_config': fs_config_system, |
| 171 | 'mount_point': 'system', |
| 172 | 'root_dir': root_dir, |
| 173 | 'root_fs_config': fs_config_root, |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 174 | } |
| 175 | in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict) |
| 176 | |
| 177 | self.assertTrue(filecmp.cmp( |
| 178 | os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init'))) |
| 179 | self.assertTrue(filecmp.cmp( |
| 180 | os.path.join(in_dir, 'system', 'file'), |
| 181 | os.path.join(origin_in, 'file'))) |
| 182 | self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink'))) |
| 183 | |
| 184 | with open(fs_config) as fs_config_fp: |
| 185 | fs_config_data = fs_config_fp.readlines() |
| 186 | self.assertIn('fs-config-system\n', fs_config_data) |
| 187 | self.assertIn('fs-config-root\n', fs_config_data) |
| 188 | self.assertEqual('/', prop_dict['mount_point']) |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 189 | |
| 190 | def test_AVBCalcMinPartitionSize_LinearFooterSize(self): |
| 191 | """Tests with footer size which is linear to partition size.""" |
| 192 | for image_size in self._image_sizes: |
| 193 | for ratio in 0.95, 0.56, 0.22: |
| 194 | expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio))) |
| 195 | self.assertEqual( |
| 196 | expected_size, |
| 197 | AVBCalcMinPartitionSize(image_size, lambda x: int(x * ratio))) |
| 198 | |
| 199 | def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self): |
| 200 | """Tests with footer size which grows slower than partition size.""" |
| 201 | |
| 202 | def _SizeCalculator(partition_size): |
| 203 | """Footer size is the power of 0.95 of partition size.""" |
| 204 | # Minus footer size to return max image size. |
| 205 | return partition_size - int(math.pow(partition_size, 0.95)) |
| 206 | |
| 207 | for image_size in self._image_sizes: |
| 208 | min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator) |
| 209 | # Checks min_partition_size can accommodate image_size. |
| 210 | self.assertGreaterEqual( |
| 211 | _SizeCalculator(min_partition_size), |
| 212 | image_size) |
| 213 | # Checks min_partition_size (round to BLOCK_SIZE) is the minimum. |
| 214 | self.assertLess( |
| 215 | _SizeCalculator(min_partition_size - BLOCK_SIZE), |
| 216 | image_size) |
| 217 | |
| 218 | def test_AVBCalcMinPartitionSize_FasterGrowthFooterSize(self): |
| 219 | """Tests with footer size which grows faster than partition size.""" |
| 220 | |
| 221 | def _SizeCalculator(partition_size): |
| 222 | """Max image size is the power of 0.95 of partition size.""" |
| 223 | # Max image size grows less than partition size, which means |
| 224 | # footer size grows faster than partition size. |
| 225 | return int(math.pow(partition_size, 0.95)) |
| 226 | |
| 227 | for image_size in self._image_sizes: |
| 228 | min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator) |
| 229 | # Checks min_partition_size can accommodate image_size. |
| 230 | self.assertGreaterEqual( |
| 231 | _SizeCalculator(min_partition_size), |
| 232 | image_size) |
| 233 | # Checks min_partition_size (round to BLOCK_SIZE) is the minimum. |
| 234 | self.assertLess( |
| 235 | _SizeCalculator(min_partition_size - BLOCK_SIZE), |
| 236 | image_size) |