blob: 6f853e85924bef262dfccd7fa9344ca365fd95a7 [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 -080021
Tao Baod8a953d2018-01-02 21:19:27 -080022import common
Bowgo Tsai040410c2018-09-20 16:40:01 +080023from build_image import (
Tao Baoc6bd70a2018-09-27 16:58:00 -070024 AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom,
Tao Bao986ee862018-10-04 15:46:16 -070025 SetUpInDirAndFsConfig)
Tao Bao65b94e92018-10-11 21:57:26 -070026from test_utils import ReleaseToolsTestCase
Tao Baod4349f22017-12-07 23:01:25 -080027
28
Tao Bao65b94e92018-10-11 21:57:26 -070029class BuildImageTest(ReleaseToolsTestCase):
Tao Baod4349f22017-12-07 23:01:25 -080030
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 Baod4349f22017-12-07 23:01:25 -080042 def test_CheckHeadroom_SizeUnderLimit(self):
Tao Baod8a953d2018-01-02 21:19:27 -080043 # Required headroom: 1000 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080044 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080045 'fs_type' : 'ext4',
46 'partition_headroom' : '4096000',
Tao Baod4349f22017-12-07 23:01:25 -080047 'mount_point' : 'system',
48 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070049 CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080050
51 def test_CheckHeadroom_InsufficientHeadroom(self):
Tao Baod8a953d2018-01-02 21:19:27 -080052 # Required headroom: 1001 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080053 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080054 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080055 'partition_headroom' : '4100096',
56 'mount_point' : 'system',
57 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070058 self.assertRaises(
59 BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod8a953d2018-01-02 21:19:27 -080060
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 Baod4349f22017-12-07 23:01:25 -080084
85 def test_CheckHeadroom_WithMke2fsOutput(self):
86 """Tests the result parsing from actual call to mke2fs."""
Tao Baod8a953d2018-01-02 21:19:27 -080087 input_dir = common.MakeTempDir()
88 output_image = common.MakeTempFile(suffix='.img')
Tianjie Xu57332222018-08-15 16:16:21 -070089 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080090 '/system', '409600', '-j', '0']
Tao Bao986ee862018-10-04 15:46:16 -070091 proc = common.Run(command)
92 ext4fs_output, _ = proc.communicate()
93 self.assertEqual(0, proc.returncode)
Tao Baod4349f22017-12-07 23:01:25 -080094
95 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080096 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080097 'partition_headroom' : '40960',
98 'mount_point' : 'system',
99 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700100 CheckHeadroom(ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800101
102 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -0800103 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -0800104 'partition_headroom' : '413696',
105 'mount_point' : 'system',
106 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700107 self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800108
Tao Baoc2606eb2018-07-20 14:44:46 -0700109 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 Cherryd14b8952018-08-09 14:26:00 -0700127 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700128 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 Baoc2606eb2018-07-20 14:44:46 -0700143 }
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 Cherryd14b8952018-08-09 14:26:00 -0700156 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700157 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 Baoc2606eb2018-07-20 14:44:46 -0700174 }
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 Tsai040410c2018-09-20 16:40:01 +0800189
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)