blob: 19b5e0828f68bb4057ed288af6ab7a09f45ec238 [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 -080019import unittest
20
Tao Baod8a953d2018-01-02 21:19:27 -080021import common
Tao Baoc2606eb2018-07-20 14:44:46 -070022from build_image import CheckHeadroom, RunCommand, SetUpInDirAndFsConfig
Tao Baod4349f22017-12-07 23:01:25 -080023
24
25class BuildImageTest(unittest.TestCase):
26
Tao Baod8a953d2018-01-02 21:19:27 -080027 # Available: 1000 blocks.
28 EXT4FS_OUTPUT = (
29 "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
30
Tao Baoc2606eb2018-07-20 14:44:46 -070031 def tearDown(self):
32 common.Cleanup()
33
Tao Baod4349f22017-12-07 23:01:25 -080034 def test_CheckHeadroom_SizeUnderLimit(self):
Tao Baod8a953d2018-01-02 21:19:27 -080035 # Required headroom: 1000 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080036 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080037 'fs_type' : 'ext4',
38 'partition_headroom' : '4096000',
Tao Baod4349f22017-12-07 23:01:25 -080039 'mount_point' : 'system',
40 }
Tao Baod8a953d2018-01-02 21:19:27 -080041 self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
Tao Baod4349f22017-12-07 23:01:25 -080042
43 def test_CheckHeadroom_InsufficientHeadroom(self):
Tao Baod8a953d2018-01-02 21:19:27 -080044 # Required headroom: 1001 blocks.
Tao Baod4349f22017-12-07 23:01:25 -080045 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080046 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080047 'partition_headroom' : '4100096',
48 'mount_point' : 'system',
49 }
Tao Baod8a953d2018-01-02 21:19:27 -080050 self.assertFalse(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
51
52 def test_CheckHeadroom_WrongFsType(self):
53 prop_dict = {
54 'fs_type' : 'f2fs',
55 'partition_headroom' : '4100096',
56 'mount_point' : 'system',
57 }
58 self.assertRaises(
59 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
60
61 def test_CheckHeadroom_MissingProperties(self):
62 prop_dict = {
63 'fs_type' : 'ext4',
64 'partition_headroom' : '4100096',
65 }
66 self.assertRaises(
67 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
68
69 prop_dict = {
70 'fs_type' : 'ext4',
71 'mount_point' : 'system',
72 }
73 self.assertRaises(
74 AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
Tao Baod4349f22017-12-07 23:01:25 -080075
76 def test_CheckHeadroom_WithMke2fsOutput(self):
77 """Tests the result parsing from actual call to mke2fs."""
Tao Baod8a953d2018-01-02 21:19:27 -080078 input_dir = common.MakeTempDir()
79 output_image = common.MakeTempFile(suffix='.img')
80 command = ['mkuserimg_mke2fs.sh', input_dir, output_image, 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080081 '/system', '409600', '-j', '0']
82 ext4fs_output, exit_code = RunCommand(command)
83 self.assertEqual(0, exit_code)
84
85 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080086 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080087 'partition_headroom' : '40960',
88 'mount_point' : 'system',
89 }
90 self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
91
92 prop_dict = {
Tao Baod8a953d2018-01-02 21:19:27 -080093 'fs_type' : 'ext4',
Tao Baod4349f22017-12-07 23:01:25 -080094 'partition_headroom' : '413696',
95 'mount_point' : 'system',
96 }
97 self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
98
Tao Baoc2606eb2018-07-20 14:44:46 -070099 def test_SetUpInDirAndFsConfig_SystemRootImageFalse(self):
100 prop_dict = {
101 'fs_config': 'fs-config',
102 'mount_point': 'system',
103 }
104 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
105 self.assertEqual('/path/to/in_dir', in_dir)
106 self.assertEqual('fs-config', fs_config)
107 self.assertEqual('system', prop_dict['mount_point'])
108
109 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
127 def test_SetUpInDirAndFsConfig_SystemRootImageTrue(self):
128 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,
143 'system_root_image': 'true',
144 }
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
157 def test_SetUpInDirAndFsConfig_SystemRootImageTrue_WithRootFsConfig(self):
158 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,
175 'system_root_image': 'true',
176 }
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'])