blob: d4f7ccc98f65c04f343d02689538ce03249cb1f3 [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
18import os.path
Tao Baod4349f22017-12-07 23:01:25 -080019
Tao Baod8a953d2018-01-02 21:19:27 -080020import common
Tao Bao82490d32019-04-09 00:12:30 -070021import test_utils
Bowgo Tsai040410c2018-09-20 16:40:01 +080022from build_image import (
Tao Bao82490d32019-04-09 00:12:30 -070023 BuildImageError, CheckHeadroom, GetFilesystemCharacteristics,
24 SetUpInDirAndFsConfig)
Tao Baod4349f22017-12-07 23:01:25 -080025
26
Tao Bao82490d32019-04-09 00:12:30 -070027class BuildImageTest(test_utils.ReleaseToolsTestCase):
Tao Baod4349f22017-12-07 23:01:25 -080028
Tao Baod8a953d2018-01-02 21:19:27 -080029 # Available: 1000 blocks.
30 EXT4FS_OUTPUT = (
31 "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
32
Tao Baod4349f22017-12-07 23:01:25 -080033 def test_CheckHeadroom_SizeUnderLimit(self):
Tao Baod8a953d2018-01-02 21:19:27 -080034 # Required headroom: 1000 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080035 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080036 'fs_type' : 'ext4',
37 'partition_headroom' : '4096000',
Tao Baod4349f22017-12-07 23:01:25 -080038 'mount_point' : 'system',
39 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070040 CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080041
42 def test_CheckHeadroom_InsufficientHeadroom(self):
Tao Baod8a953d2018-01-02 21:19:27 -080043 # Required headroom: 1001 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080044 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080045 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080046 'partition_headroom' : '4100096',
47 'mount_point' : 'system',
48 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070049 self.assertRaises(
50 BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod8a953d2018-01-02 21:19:27 -080051
Tao Bao82490d32019-04-09 00:12:30 -070052 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baod8a953d2018-01-02 21:19:27 -080053 def test_CheckHeadroom_WrongFsType(self):
54 prop_dict = {
55 'fs_type' : 'f2fs',
56 'partition_headroom' : '4100096',
57 'mount_point' : 'system',
58 }
59 self.assertRaises(
60 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
61
62 def test_CheckHeadroom_MissingProperties(self):
63 prop_dict = {
64 'fs_type' : 'ext4',
65 'partition_headroom' : '4100096',
66 }
67 self.assertRaises(
68 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
69
70 prop_dict = {
71 'fs_type' : 'ext4',
72 'mount_point' : 'system',
73 }
74 self.assertRaises(
75 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080076
Tao Bao82490d32019-04-09 00:12:30 -070077 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baod4349f22017-12-07 23:01:25 -080078 def test_CheckHeadroom_WithMke2fsOutput(self):
79 """Tests the result parsing from actual call to mke2fs."""
Tao Baod8a953d2018-01-02 21:19:27 -080080 input_dir = common.MakeTempDir()
81 output_image = common.MakeTempFile(suffix='.img')
Tianjie Xu57332222018-08-15 16:16:21 -070082 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080083 '/system', '409600', '-j', '0']
Tao Bao986ee862018-10-04 15:46:16 -070084 proc = common.Run(command)
85 ext4fs_output, _ = proc.communicate()
86 self.assertEqual(0, proc.returncode)
Tao Baod4349f22017-12-07 23:01:25 -080087
88 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080089 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080090 'partition_headroom' : '40960',
91 'mount_point' : 'system',
92 }
Tao Baoc6bd70a2018-09-27 16:58:00 -070093 CheckHeadroom(ext4fs_output, prop_dict)
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' : '413696',
98 'mount_point' : 'system',
99 }
Tao Baoc6bd70a2018-09-27 16:58:00 -0700100 self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -0800101
Yi-Yo Chiang18650c72022-10-12 18:29:14 +0800102 def test_SetUpInDirAndFsConfig_NonSystem(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700103 prop_dict = {
104 'fs_config': 'fs-config',
105 'mount_point': 'vendor',
Tao Baoc2606eb2018-07-20 14:44:46 -0700106 }
107 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
108 self.assertEqual('/path/to/in_dir', in_dir)
109 self.assertEqual('fs-config', fs_config)
110 self.assertEqual('vendor', prop_dict['mount_point'])
111
112 @staticmethod
113 def _gen_fs_config(partition):
114 fs_config = common.MakeTempFile(suffix='.txt')
115 with open(fs_config, 'w') as fs_config_fp:
116 fs_config_fp.write('fs-config-{}\n'.format(partition))
117 return fs_config
118
Tom Cherryd14b8952018-08-09 14:26:00 -0700119 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700120 root_dir = common.MakeTempDir()
121 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
122 init_fp.write('init')
123
124 origin_in = common.MakeTempDir()
125 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
126 in_fp.write('system-file')
127 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
128
129 fs_config_system = self._gen_fs_config('system')
130
131 prop_dict = {
132 'fs_config': fs_config_system,
133 'mount_point': 'system',
134 'root_dir': root_dir,
Tao Baoc2606eb2018-07-20 14:44:46 -0700135 }
136 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
137
138 self.assertTrue(filecmp.cmp(
139 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
140 self.assertTrue(filecmp.cmp(
141 os.path.join(in_dir, 'system', 'file'),
142 os.path.join(origin_in, 'file')))
143 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
144
145 self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
146 self.assertEqual('/', prop_dict['mount_point'])
147
Tom Cherryd14b8952018-08-09 14:26:00 -0700148 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700149 root_dir = common.MakeTempDir()
150 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
151 init_fp.write('init')
152
153 origin_in = common.MakeTempDir()
154 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
155 in_fp.write('system-file')
156 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
157
158 fs_config_system = self._gen_fs_config('system')
159 fs_config_root = self._gen_fs_config('root')
160
161 prop_dict = {
162 'fs_config': fs_config_system,
163 'mount_point': 'system',
164 'root_dir': root_dir,
165 'root_fs_config': fs_config_root,
Tao Baoc2606eb2018-07-20 14:44:46 -0700166 }
167 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
168
169 self.assertTrue(filecmp.cmp(
170 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
171 self.assertTrue(filecmp.cmp(
172 os.path.join(in_dir, 'system', 'file'),
173 os.path.join(origin_in, 'file')))
174 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
175
176 with open(fs_config) as fs_config_fp:
177 fs_config_data = fs_config_fp.readlines()
178 self.assertIn('fs-config-system\n', fs_config_data)
179 self.assertIn('fs-config-root\n', fs_config_data)
180 self.assertEqual('/', prop_dict['mount_point'])
Mark Salyzyn780f5952018-10-19 13:44:36 -0700181
Tao Bao82490d32019-04-09 00:12:30 -0700182 @test_utils.SkipIfExternalToolsUnavailable()
Mark Salyzyn780f5952018-10-19 13:44:36 -0700183 def test_GetFilesystemCharacteristics(self):
184 input_dir = common.MakeTempDir()
185 output_image = common.MakeTempFile(suffix='.img')
186 command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
187 '/system', '409600', '-j', '0']
188 proc = common.Run(command)
Tao Bao82490d32019-04-09 00:12:30 -0700189 proc.communicate()
Mark Salyzyn780f5952018-10-19 13:44:36 -0700190 self.assertEqual(0, proc.returncode)
191
192 output_file = common.MakeTempFile(suffix='.img')
193 cmd = ["img2simg", output_image, output_file]
194 p = common.Run(cmd)
195 p.communicate()
196 self.assertEqual(0, p.returncode)
197
Jooyung Hanc4b7b342021-10-27 14:48:22 +0900198 fs_dict = GetFilesystemCharacteristics('ext4', output_file)
Mark Salyzyn780f5952018-10-19 13:44:36 -0700199 self.assertEqual(int(fs_dict['Block size']), 4096)
200 self.assertGreaterEqual(int(fs_dict['Free blocks']), 0) # expect ~88
201 self.assertGreater(int(fs_dict['Inode count']), 0) # expect ~64
202 self.assertGreaterEqual(int(fs_dict['Free inodes']), 0) # expect ~53
203 self.assertGreater(int(fs_dict['Inode count']), int(fs_dict['Free inodes']))