blob: 0aaa847e95a335149fbca96394bea764cfcdf982 [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 (
25 AVBCalcMinPartitionSize, BLOCK_SIZE,
26 CheckHeadroom, 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 Baod8a953d2018-01-02 21:19:27 -080052 self.assertTrue(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 Baod8a953d2018-01-02 21:19:27 -080061 self.assertFalse(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
62
63 def test_CheckHeadroom_WrongFsType(self):
64 prop_dict = {
65 'fs_type' : 'f2fs',
66 'partition_headroom' : '4100096',
67 'mount_point' : 'system',
68 }
69 self.assertRaises(
70 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
71
72 def test_CheckHeadroom_MissingProperties(self):
73 prop_dict = {
74 'fs_type' : 'ext4',
75 'partition_headroom' : '4100096',
76 }
77 self.assertRaises(
78 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
79
80 prop_dict = {
81 'fs_type' : 'ext4',
82 'mount_point' : 'system',
83 }
84 self.assertRaises(
85 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080086
87 def test_CheckHeadroom_WithMke2fsOutput(self):
88 """Tests the result parsing from actual call to mke2fs."""
Tao Baod8a953d2018-01-02 21:19:27 -080089 input_dir = common.MakeTempDir()
90 output_image = common.MakeTempFile(suffix='.img')
Tianjie Xu57332222018-08-15 16:16:21 -070091 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080092 '/system', '409600', '-j', '0']
93 ext4fs_output, exit_code = RunCommand(command)
94 self.assertEqual(0, exit_code)
95
96 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080097 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080098 'partition_headroom' : '40960',
99 'mount_point' : 'system',
100 }
101 self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
102
103 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -0800104 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -0800105 'partition_headroom' : '413696',
106 'mount_point' : 'system',
107 }
108 self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
109
Tao Baoc2606eb2018-07-20 14:44:46 -0700110 def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self):
111 prop_dict = {
112 'fs_config': 'fs-config',
113 'mount_point': 'vendor',
114 'system_root_image': 'true',
115 }
116 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
117 self.assertEqual('/path/to/in_dir', in_dir)
118 self.assertEqual('fs-config', fs_config)
119 self.assertEqual('vendor', prop_dict['mount_point'])
120
121 @staticmethod
122 def _gen_fs_config(partition):
123 fs_config = common.MakeTempFile(suffix='.txt')
124 with open(fs_config, 'w') as fs_config_fp:
125 fs_config_fp.write('fs-config-{}\n'.format(partition))
126 return fs_config
127
Tom Cherryd14b8952018-08-09 14:26:00 -0700128 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700129 root_dir = common.MakeTempDir()
130 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
131 init_fp.write('init')
132
133 origin_in = common.MakeTempDir()
134 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
135 in_fp.write('system-file')
136 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
137
138 fs_config_system = self._gen_fs_config('system')
139
140 prop_dict = {
141 'fs_config': fs_config_system,
142 'mount_point': 'system',
143 'root_dir': root_dir,
Tao Baoc2606eb2018-07-20 14:44:46 -0700144 }
145 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
146
147 self.assertTrue(filecmp.cmp(
148 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
149 self.assertTrue(filecmp.cmp(
150 os.path.join(in_dir, 'system', 'file'),
151 os.path.join(origin_in, 'file')))
152 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
153
154 self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
155 self.assertEqual('/', prop_dict['mount_point'])
156
Tom Cherryd14b8952018-08-09 14:26:00 -0700157 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700158 root_dir = common.MakeTempDir()
159 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
160 init_fp.write('init')
161
162 origin_in = common.MakeTempDir()
163 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
164 in_fp.write('system-file')
165 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
166
167 fs_config_system = self._gen_fs_config('system')
168 fs_config_root = self._gen_fs_config('root')
169
170 prop_dict = {
171 'fs_config': fs_config_system,
172 'mount_point': 'system',
173 'root_dir': root_dir,
174 'root_fs_config': fs_config_root,
Tao Baoc2606eb2018-07-20 14:44:46 -0700175 }
176 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
177
178 self.assertTrue(filecmp.cmp(
179 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
180 self.assertTrue(filecmp.cmp(
181 os.path.join(in_dir, 'system', 'file'),
182 os.path.join(origin_in, 'file')))
183 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
184
185 with open(fs_config) as fs_config_fp:
186 fs_config_data = fs_config_fp.readlines()
187 self.assertIn('fs-config-system\n', fs_config_data)
188 self.assertIn('fs-config-root\n', fs_config_data)
189 self.assertEqual('/', prop_dict['mount_point'])
Bowgo Tsai040410c2018-09-20 16:40:01 +0800190
191 def test_AVBCalcMinPartitionSize_LinearFooterSize(self):
192 """Tests with footer size which is linear to partition size."""
193 for image_size in self._image_sizes:
194 for ratio in 0.95, 0.56, 0.22:
195 expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio)))
196 self.assertEqual(
197 expected_size,
198 AVBCalcMinPartitionSize(image_size, lambda x: int(x * ratio)))
199
200 def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self):
201 """Tests with footer size which grows slower than partition size."""
202
203 def _SizeCalculator(partition_size):
204 """Footer size is the power of 0.95 of partition size."""
205 # Minus footer size to return max image size.
206 return partition_size - int(math.pow(partition_size, 0.95))
207
208 for image_size in self._image_sizes:
209 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
210 # Checks min_partition_size can accommodate image_size.
211 self.assertGreaterEqual(
212 _SizeCalculator(min_partition_size),
213 image_size)
214 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
215 self.assertLess(
216 _SizeCalculator(min_partition_size - BLOCK_SIZE),
217 image_size)
218
219 def test_AVBCalcMinPartitionSize_FasterGrowthFooterSize(self):
220 """Tests with footer size which grows faster than partition size."""
221
222 def _SizeCalculator(partition_size):
223 """Max image size is the power of 0.95 of partition size."""
224 # Max image size grows less than partition size, which means
225 # footer size grows faster than partition size.
226 return int(math.pow(partition_size, 0.95))
227
228 for image_size in self._image_sizes:
229 min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator)
230 # Checks min_partition_size can accommodate image_size.
231 self.assertGreaterEqual(
232 _SizeCalculator(min_partition_size),
233 image_size)
234 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
235 self.assertLess(
236 _SizeCalculator(min_partition_size - BLOCK_SIZE),
237 image_size)