blob: a2df27886af46336ffc8083a5b388b48a4b9bd27 [file] [log] [blame]
Tao Baod4349f22017-12-07 23:01:25 -08001#
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 Baoc2606eb2018-07-20 14:44:46 -070017import filecmp
Bowgo Tsai040410c2018-09-20 16:40:01 +080018import math
Tao Baoc2606eb2018-07-20 14:44:46 -070019import os.path
Bowgo Tsai040410c2018-09-20 16:40:01 +080020import random
Tao Baod4349f22017-12-07 23:01:25 -080021import unittest
22
Tao Baod8a953d2018-01-02 21:19:27 -080023import common
Bowgo Tsai040410c2018-09-20 16:40:01 +080024from build_image import (
Tao Baoc6bd70a2018-09-27 16:58:00 -070025 AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom,
Tao Bao986ee862018-10-04 15:46:16 -070026 SetUpInDirAndFsConfig)
Tao Baod4349f22017-12-07 23:01:25 -080027
28
29class BuildImageTest(unittest.TestCase):
30
Tao Baod8a953d2018-01-02 21:19:27 -080031 # Available: 1000 blocks.
32 EXT4FS_OUTPUT = (
33 "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
34
Bowgo Tsai040410c2018-09-20 16:40:01 +080035 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 Baoc2606eb2018-07-20 14:44:46 -070042 def tearDown(self):
43 common.Cleanup()
44
Tao Baod4349f22017-12-07 23:01:25 -080045 def test_CheckHeadroom_SizeUnderLimit(self):
Tao Baod8a953d2018-01-02 21:19:27 -080046 # Required headroom: 1000 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080047 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080048 'fs_type' : 'ext4',
49 'partition_headroom' : '4096000',
Tao Baod4349f22017-12-07 23:01:25 -080050 'mount_point' : 'system',
51 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070052 CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080053
54 def test_CheckHeadroom_InsufficientHeadroom(self):
Tao Baod8a953d2018-01-02 21:19:27 -080055 # Required headroom: 1001 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080056 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080057 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080058 'partition_headroom' : '4100096',
59 'mount_point' : 'system',
60 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070061 self.assertRaises(
62 BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod8a953d2018-01-02 21:19:27 -080063
64 def test_CheckHeadroom_WrongFsType(self):
65 prop_dict = {
66 'fs_type' : 'f2fs',
67 'partition_headroom' : '4100096',
68 'mount_point' : 'system',
69 }
70 self.assertRaises(
71 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
72
73 def test_CheckHeadroom_MissingProperties(self):
74 prop_dict = {
75 'fs_type' : 'ext4',
76 'partition_headroom' : '4100096',
77 }
78 self.assertRaises(
79 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
80
81 prop_dict = {
82 'fs_type' : 'ext4',
83 'mount_point' : 'system',
84 }
85 self.assertRaises(
86 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080087
88 def test_CheckHeadroom_WithMke2fsOutput(self):
89 """Tests the result parsing from actual call to mke2fs."""
Tao Baod8a953d2018-01-02 21:19:27 -080090 input_dir = common.MakeTempDir()
91 output_image = common.MakeTempFile(suffix='.img')
Tianjie Xu57332222018-08-15 16:16:21 -070092 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080093 '/system', '409600', '-j', '0']
Tao Bao986ee862018-10-04 15:46:16 -070094 proc = common.Run(command)
95 ext4fs_output, _ = proc.communicate()
96 self.assertEqual(0, proc.returncode)
Tao Baod4349f22017-12-07 23:01:25 -080097
98 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080099 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -0800100 'partition_headroom' : '40960',
101 'mount_point' : 'system',
102 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700103 CheckHeadroom(ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800104
105 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -0800106 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -0800107 'partition_headroom' : '413696',
108 'mount_point' : 'system',
109 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700110 self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800111
Tao Baoc2606eb2018-07-20 14:44:46 -0700112 def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self):
113 prop_dict = {
114 'fs_config': 'fs-config',
115 'mount_point': 'vendor',
116 'system_root_image': 'true',
117 }
118 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
119 self.assertEqual('/path/to/in_dir', in_dir)
120 self.assertEqual('fs-config', fs_config)
121 self.assertEqual('vendor', prop_dict['mount_point'])
122
123 @staticmethod
124 def _gen_fs_config(partition):
125 fs_config = common.MakeTempFile(suffix='.txt')
126 with open(fs_config, 'w') as fs_config_fp:
127 fs_config_fp.write('fs-config-{}\n'.format(partition))
128 return fs_config
129
Tom Cherryd14b8952018-08-09 14:26:00 -0700130 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700131 root_dir = common.MakeTempDir()
132 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
133 init_fp.write('init')
134
135 origin_in = common.MakeTempDir()
136 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
137 in_fp.write('system-file')
138 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
139
140 fs_config_system = self._gen_fs_config('system')
141
142 prop_dict = {
143 'fs_config': fs_config_system,
144 'mount_point': 'system',
145 'root_dir': root_dir,
Tao Baoc2606eb2018-07-20 14:44:46 -0700146 }
147 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
148
149 self.assertTrue(filecmp.cmp(
150 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
151 self.assertTrue(filecmp.cmp(
152 os.path.join(in_dir, 'system', 'file'),
153 os.path.join(origin_in, 'file')))
154 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
155
156 self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
157 self.assertEqual('/', prop_dict['mount_point'])
158
Tom Cherryd14b8952018-08-09 14:26:00 -0700159 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700160 root_dir = common.MakeTempDir()
161 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
162 init_fp.write('init')
163
164 origin_in = common.MakeTempDir()
165 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
166 in_fp.write('system-file')
167 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
168
169 fs_config_system = self._gen_fs_config('system')
170 fs_config_root = self._gen_fs_config('root')
171
172 prop_dict = {
173 'fs_config': fs_config_system,
174 'mount_point': 'system',
175 'root_dir': root_dir,
176 'root_fs_config': fs_config_root,
Tao Baoc2606eb2018-07-20 14:44:46 -0700177 }
178 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
179
180 self.assertTrue(filecmp.cmp(
181 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
182 self.assertTrue(filecmp.cmp(
183 os.path.join(in_dir, 'system', 'file'),
184 os.path.join(origin_in, 'file')))
185 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
186
187 with open(fs_config) as fs_config_fp:
188 fs_config_data = fs_config_fp.readlines()
189 self.assertIn('fs-config-system\n', fs_config_data)
190 self.assertIn('fs-config-root\n', fs_config_data)
191 self.assertEqual('/', prop_dict['mount_point'])
Bowgo Tsai040410c2018-09-20 16:40:01 +0800192
193 def test_AVBCalcMinPartitionSize_LinearFooterSize(self):
194 """Tests with footer size which is linear to partition size."""
195 for image_size in self._image_sizes:
196 for ratio in 0.95, 0.56, 0.22:
197 expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio)))
198 self.assertEqual(
199 expected_size,
200 AVBCalcMinPartitionSize(image_size, lambda x: int(x * ratio)))
201
202 def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self):
203 """Tests with footer size which grows slower than partition size."""
204
205 def _SizeCalculator(partition_size):
206 """Footer size is the power of 0.95 of partition size."""
207 # Minus footer size to return max image size.
208 return partition_size - int(math.pow(partition_size, 0.95))
209
210 for image_size in self._image_sizes:
211 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
212 # Checks min_partition_size can accommodate image_size.
213 self.assertGreaterEqual(
214 _SizeCalculator(min_partition_size),
215 image_size)
216 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
217 self.assertLess(
218 _SizeCalculator(min_partition_size - BLOCK_SIZE),
219 image_size)
220
221 def test_AVBCalcMinPartitionSize_FasterGrowthFooterSize(self):
222 """Tests with footer size which grows faster than partition size."""
223
224 def _SizeCalculator(partition_size):
225 """Max image size is the power of 0.95 of partition size."""
226 # Max image size grows less than partition size, which means
227 # footer size grows faster than partition size.
228 return int(math.pow(partition_size, 0.95))
229
230 for image_size in self._image_sizes:
231 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
232 # Checks min_partition_size can accommodate image_size.
233 self.assertGreaterEqual(
234 _SizeCalculator(min_partition_size),
235 image_size)
236 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
237 self.assertLess(
238 _SizeCalculator(min_partition_size - BLOCK_SIZE),
239 image_size)