blob: c91d00deab5834fbcf1d387eec800be52c437b61 [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_SystemRootImageTrue_NonSystem(self):
100 prop_dict = {
101 'fs_config': 'fs-config',
102 'mount_point': 'vendor',
103 'system_root_image': 'true',
104 }
105 in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
106 self.assertEqual('/path/to/in_dir', in_dir)
107 self.assertEqual('fs-config', fs_config)
108 self.assertEqual('vendor', prop_dict['mount_point'])
109
110 @staticmethod
111 def _gen_fs_config(partition):
112 fs_config = common.MakeTempFile(suffix='.txt')
113 with open(fs_config, 'w') as fs_config_fp:
114 fs_config_fp.write('fs-config-{}\n'.format(partition))
115 return fs_config
116
Tom Cherryd14b8952018-08-09 14:26:00 -0700117 def test_SetUpInDirAndFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700118 root_dir = common.MakeTempDir()
119 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
120 init_fp.write('init')
121
122 origin_in = common.MakeTempDir()
123 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
124 in_fp.write('system-file')
125 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
126
127 fs_config_system = self._gen_fs_config('system')
128
129 prop_dict = {
130 'fs_config': fs_config_system,
131 'mount_point': 'system',
132 'root_dir': root_dir,
Tao Baoc2606eb2018-07-20 14:44:46 -0700133 }
134 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
135
136 self.assertTrue(filecmp.cmp(
137 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
138 self.assertTrue(filecmp.cmp(
139 os.path.join(in_dir, 'system', 'file'),
140 os.path.join(origin_in, 'file')))
141 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
142
143 self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
144 self.assertEqual('/', prop_dict['mount_point'])
145
Tom Cherryd14b8952018-08-09 14:26:00 -0700146 def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
Tao Baoc2606eb2018-07-20 14:44:46 -0700147 root_dir = common.MakeTempDir()
148 with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
149 init_fp.write('init')
150
151 origin_in = common.MakeTempDir()
152 with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
153 in_fp.write('system-file')
154 os.symlink('../etc', os.path.join(origin_in, 'symlink'))
155
156 fs_config_system = self._gen_fs_config('system')
157 fs_config_root = self._gen_fs_config('root')
158
159 prop_dict = {
160 'fs_config': fs_config_system,
161 'mount_point': 'system',
162 'root_dir': root_dir,
163 'root_fs_config': fs_config_root,
Tao Baoc2606eb2018-07-20 14:44:46 -0700164 }
165 in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
166
167 self.assertTrue(filecmp.cmp(
168 os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
169 self.assertTrue(filecmp.cmp(
170 os.path.join(in_dir, 'system', 'file'),
171 os.path.join(origin_in, 'file')))
172 self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
173
174 with open(fs_config) as fs_config_fp:
175 fs_config_data = fs_config_fp.readlines()
176 self.assertIn('fs-config-system\n', fs_config_data)
177 self.assertIn('fs-config-root\n', fs_config_data)
178 self.assertEqual('/', prop_dict['mount_point'])