Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 1 | # |
| 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 Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 17 | import filecmp |
| 18 | import os.path |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 19 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 20 | import common |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 21 | import test_utils |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 22 | from build_image import ( |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 23 | BuildImageError, CheckHeadroom, GetFilesystemCharacteristics, |
| 24 | SetUpInDirAndFsConfig) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 25 | |
| 26 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 27 | class BuildImageTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 28 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 29 | # Available: 1000 blocks. |
| 30 | EXT4FS_OUTPUT = ( |
| 31 | "Created filesystem with 2777/129024 inodes and 515099/516099 blocks") |
| 32 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 33 | def test_CheckHeadroom_SizeUnderLimit(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 34 | # Required headroom: 1000 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 35 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 36 | 'fs_type' : 'ext4', |
| 37 | 'partition_headroom' : '4096000', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 38 | 'mount_point' : 'system', |
| 39 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 40 | CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 41 | |
| 42 | def test_CheckHeadroom_InsufficientHeadroom(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 43 | # Required headroom: 1001 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 44 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 45 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 46 | 'partition_headroom' : '4100096', |
| 47 | 'mount_point' : 'system', |
| 48 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 49 | self.assertRaises( |
| 50 | BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 51 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 52 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 53 | 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 Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 76 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 77 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 78 | def test_CheckHeadroom_WithMke2fsOutput(self): |
| 79 | """Tests the result parsing from actual call to mke2fs.""" |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 80 | input_dir = common.MakeTempDir() |
| 81 | output_image = common.MakeTempFile(suffix='.img') |
Tianjie Xu | 5733222 | 2018-08-15 16:16:21 -0700 | [diff] [blame] | 82 | command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 83 | '/system', '409600', '-j', '0'] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 84 | proc = common.Run(command) |
| 85 | ext4fs_output, _ = proc.communicate() |
| 86 | self.assertEqual(0, proc.returncode) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 87 | |
| 88 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 89 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 90 | 'partition_headroom' : '40960', |
| 91 | 'mount_point' : 'system', |
| 92 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 93 | CheckHeadroom(ext4fs_output, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 94 | |
| 95 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 96 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 97 | 'partition_headroom' : '413696', |
| 98 | 'mount_point' : 'system', |
| 99 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 100 | self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 101 | |
Yi-Yo Chiang | 18650c7 | 2022-10-12 18:29:14 +0800 | [diff] [blame^] | 102 | def test_SetUpInDirAndFsConfig_NonSystem(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 103 | prop_dict = { |
| 104 | 'fs_config': 'fs-config', |
| 105 | 'mount_point': 'vendor', |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 106 | } |
| 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 Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 119 | def test_SetUpInDirAndFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 120 | 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 Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 135 | } |
| 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 Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 148 | def test_SetUpInDirAndFsConfig_WithRootFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 149 | 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 Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 166 | } |
| 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 Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 181 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 182 | @test_utils.SkipIfExternalToolsUnavailable() |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 183 | 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 Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 189 | proc.communicate() |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 190 | 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 Han | c4b7b34 | 2021-10-27 14:48:22 +0900 | [diff] [blame] | 198 | fs_dict = GetFilesystemCharacteristics('ext4', output_file) |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 199 | 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'])) |