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 | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 17 | import unittest |
| 18 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 19 | import common |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 20 | from build_image import CheckHeadroom, RunCommand |
| 21 | |
| 22 | |
| 23 | class BuildImageTest(unittest.TestCase): |
| 24 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 25 | # Available: 1000 blocks. |
| 26 | EXT4FS_OUTPUT = ( |
| 27 | "Created filesystem with 2777/129024 inodes and 515099/516099 blocks") |
| 28 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 29 | def test_CheckHeadroom_SizeUnderLimit(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 30 | # Required headroom: 1000 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 31 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 32 | 'fs_type' : 'ext4', |
| 33 | 'partition_headroom' : '4096000', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 34 | 'mount_point' : 'system', |
| 35 | } |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 36 | self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 37 | |
| 38 | def test_CheckHeadroom_InsufficientHeadroom(self): |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 39 | # Required headroom: 1001 blocks. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 40 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 41 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 42 | 'partition_headroom' : '4100096', |
| 43 | 'mount_point' : 'system', |
| 44 | } |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 45 | self.assertFalse(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)) |
| 46 | |
| 47 | def test_CheckHeadroom_WrongFsType(self): |
| 48 | prop_dict = { |
| 49 | 'fs_type' : 'f2fs', |
| 50 | 'partition_headroom' : '4100096', |
| 51 | 'mount_point' : 'system', |
| 52 | } |
| 53 | self.assertRaises( |
| 54 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
| 55 | |
| 56 | def test_CheckHeadroom_MissingProperties(self): |
| 57 | prop_dict = { |
| 58 | 'fs_type' : 'ext4', |
| 59 | 'partition_headroom' : '4100096', |
| 60 | } |
| 61 | self.assertRaises( |
| 62 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
| 63 | |
| 64 | prop_dict = { |
| 65 | 'fs_type' : 'ext4', |
| 66 | 'mount_point' : 'system', |
| 67 | } |
| 68 | self.assertRaises( |
| 69 | AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 70 | |
| 71 | def test_CheckHeadroom_WithMke2fsOutput(self): |
| 72 | """Tests the result parsing from actual call to mke2fs.""" |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 73 | input_dir = common.MakeTempDir() |
| 74 | output_image = common.MakeTempFile(suffix='.img') |
| 75 | command = ['mkuserimg_mke2fs.sh', input_dir, output_image, 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 76 | '/system', '409600', '-j', '0'] |
| 77 | ext4fs_output, exit_code = RunCommand(command) |
| 78 | self.assertEqual(0, exit_code) |
| 79 | |
| 80 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 81 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 82 | 'partition_headroom' : '40960', |
| 83 | 'mount_point' : 'system', |
| 84 | } |
| 85 | self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict)) |
| 86 | |
| 87 | prop_dict = { |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 88 | 'fs_type' : 'ext4', |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 89 | 'partition_headroom' : '413696', |
| 90 | 'mount_point' : 'system', |
| 91 | } |
| 92 | self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict)) |
| 93 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame^] | 94 | common.Cleanup() |