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 | import unittest |
| 20 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 21 | import common |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 22 | from build_image import CheckHeadroom, RunCommand, SetUpInDirAndFsConfig |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class BuildImageTest(unittest.TestCase): |
| 26 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 27 | # Available: 1000 blocks. |
| 28 | EXT4FS_OUTPUT = ( |
| 29 | "Created filesystem with 2777/129024 inodes and 515099/516099 blocks") |
| 30 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 31 | def tearDown(self): |
| 32 | common.Cleanup() |
| 33 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 34 | def test_CheckHeadroom_SizeUnderLimit(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 35 | # Required headroom: 1000 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 36 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 37 | 'fs_type' : 'ext4', |
| 38 | 'partition_headroom' : '4096000', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 39 | 'mount_point' : 'system', |
| 40 | } |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 41 | self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 42 | |
| 43 | def test_CheckHeadroom_InsufficientHeadroom(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 44 | # Required headroom: 1001 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 45 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 46 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 47 | 'partition_headroom' : '4100096', |
| 48 | 'mount_point' : 'system', |
| 49 | } |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 50 | 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 Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 75 | |
| 76 | def test_CheckHeadroom_WithMke2fsOutput(self): |
| 77 | """Tests the result parsing from actual call to mke2fs.""" |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 78 | input_dir = common.MakeTempDir() |
| 79 | output_image = common.MakeTempFile(suffix='.img') |
| 80 | command = ['mkuserimg_mke2fs.sh', input_dir, output_image, 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 81 | '/system', '409600', '-j', '0'] |
| 82 | ext4fs_output, exit_code = RunCommand(command) |
| 83 | self.assertEqual(0, exit_code) |
| 84 | |
| 85 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 86 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 87 | 'partition_headroom' : '40960', |
| 88 | 'mount_point' : 'system', |
| 89 | } |
| 90 | self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict)) |
| 91 | |
| 92 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 93 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 94 | 'partition_headroom' : '413696', |
| 95 | 'mount_point' : 'system', |
| 96 | } |
| 97 | self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict)) |
| 98 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 99 | 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 Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame^] | 117 | def test_SetUpInDirAndFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 118 | 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 Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 133 | } |
| 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 Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame^] | 146 | def test_SetUpInDirAndFsConfig_WithRootFsConfig(self): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 147 | 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 Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 164 | } |
| 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']) |