blob: 94c31ee6db3e0f8a6bc8d1914056620c279852fc [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,
26 RunCommand, 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']
94 ext4fs_output, exit_code = RunCommand(command)
95 self.assertEqual(0, exit_code)
96
97 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080098 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080099 'partition_headroom' : '40960',
100 'mount_point' : 'system',
101 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700102 CheckHeadroom(ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800103
104 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -0800105 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -0800106 'partition_headroom' : '413696',
107 'mount_point' : 'system',
108 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700109 self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800110
Tao Baoc2606eb2018-07-20 14:44:46 -0700111 def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self):
112 prop_dict = {
113 'fs_config': 'fs-config',
114 'mount_point': 'vendor',
115 'system_root_image': 'true',
116 }
117 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
118 self.assertEqual('/path/to/in_dir', in_dir)
119 self.assertEqual('fs-config', fs_config)
120 self.assertEqual('vendor', prop_dict['mount_point'])
121
122 @staticmethod
123 def _gen_fs_config(partition):
124 fs_config = common.MakeTempFile(suffix='.txt')
125 with open(fs_config, 'w') as fs_config_fp:
126 fs_config_fp.write('fs-config-{}\n'.format(partition))
127 return fs_config
128
Tom Cherryd14b8952018-08-09 14:26:00 -0700129 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700130 root_dir = common.MakeTempDir()
131 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
132 init_fp.write('init')
133
134 origin_in = common.MakeTempDir()
135 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
136 in_fp.write('system-file')
137 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
138
139 fs_config_system = self._gen_fs_config('system')
140
141 prop_dict = {
142 'fs_config': fs_config_system,
143 'mount_point': 'system',
144 'root_dir': root_dir,
Tao Baoc2606eb2018-07-20 14:44:46 -0700145 }
146 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
147
148 self.assertTrue(filecmp.cmp(
149 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
150 self.assertTrue(filecmp.cmp(
151 os.path.join(in_dir, 'system', 'file'),
152 os.path.join(origin_in, 'file')))
153 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
154
155 self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
156 self.assertEqual('/', prop_dict['mount_point'])
157
Tom Cherryd14b8952018-08-09 14:26:00 -0700158 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700159 root_dir = common.MakeTempDir()
160 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
161 init_fp.write('init')
162
163 origin_in = common.MakeTempDir()
164 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
165 in_fp.write('system-file')
166 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
167
168 fs_config_system = self._gen_fs_config('system')
169 fs_config_root = self._gen_fs_config('root')
170
171 prop_dict = {
172 'fs_config': fs_config_system,
173 'mount_point': 'system',
174 'root_dir': root_dir,
175 'root_fs_config': fs_config_root,
Tao Baoc2606eb2018-07-20 14:44:46 -0700176 }
177 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
178
179 self.assertTrue(filecmp.cmp(
180 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
181 self.assertTrue(filecmp.cmp(
182 os.path.join(in_dir, 'system', 'file'),
183 os.path.join(origin_in, 'file')))
184 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
185
186 with open(fs_config) as fs_config_fp:
187 fs_config_data = fs_config_fp.readlines()
188 self.assertIn('fs-config-system\n', fs_config_data)
189 self.assertIn('fs-config-root\n', fs_config_data)
190 self.assertEqual('/', prop_dict['mount_point'])
Bowgo Tsai040410c2018-09-20 16:40:01 +0800191
192 def test_AVBCalcMinPartitionSize_LinearFooterSize(self):
193 """Tests with footer size which is linear to partition size."""
194 for image_size in self._image_sizes:
195 for ratio in 0.95, 0.56, 0.22:
196 expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio)))
197 self.assertEqual(
198 expected_size,
199 AVBCalcMinPartitionSize(image_size, lambda x: int(x * ratio)))
200
201 def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self):
202 """Tests with footer size which grows slower than partition size."""
203
204 def _SizeCalculator(partition_size):
205 """Footer size is the power of 0.95 of partition size."""
206 # Minus footer size to return max image size.
207 return partition_size - int(math.pow(partition_size, 0.95))
208
209 for image_size in self._image_sizes:
210 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
211 # Checks min_partition_size can accommodate image_size.
212 self.assertGreaterEqual(
213 _SizeCalculator(min_partition_size),
214 image_size)
215 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
216 self.assertLess(
217 _SizeCalculator(min_partition_size - BLOCK_SIZE),
218 image_size)
219
220 def test_AVBCalcMinPartitionSize_FasterGrowthFooterSize(self):
221 """Tests with footer size which grows faster than partition size."""
222
223 def _SizeCalculator(partition_size):
224 """Max image size is the power of 0.95 of partition size."""
225 # Max image size grows less than partition size, which means
226 # footer size grows faster than partition size.
227 return int(math.pow(partition_size, 0.95))
228
229 for image_size in self._image_sizes:
230 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
231 # Checks min_partition_size can accommodate image_size.
232 self.assertGreaterEqual(
233 _SizeCalculator(min_partition_size),
234 image_size)
235 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
236 self.assertLess(
237 _SizeCalculator(min_partition_size - BLOCK_SIZE),
238 image_size)