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
17import unittest
18
19import common
20from build_image import CheckHeadroom, RunCommand
21
22
23class BuildImageTest(unittest.TestCase):
24
25  # Available: 1000 blocks.
26  EXT4FS_OUTPUT = (
27      "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
28
29  def test_CheckHeadroom_SizeUnderLimit(self):
30    # Required headroom: 1000 blocks.
31    prop_dict = {
32        'fs_type' : 'ext4',
33        'partition_headroom' : '4096000',
34        'mount_point' : 'system',
35    }
36    self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
37
38  def test_CheckHeadroom_InsufficientHeadroom(self):
39    # Required headroom: 1001 blocks.
40    prop_dict = {
41        'fs_type' : 'ext4',
42        'partition_headroom' : '4100096',
43        'mount_point' : 'system',
44    }
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)
70
71  def test_CheckHeadroom_WithMke2fsOutput(self):
72    """Tests the result parsing from actual call to mke2fs."""
73    input_dir = common.MakeTempDir()
74    output_image = common.MakeTempFile(suffix='.img')
75    command = ['mkuserimg_mke2fs.sh', input_dir, output_image, 'ext4',
76               '/system', '409600', '-j', '0']
77    ext4fs_output, exit_code = RunCommand(command)
78    self.assertEqual(0, exit_code)
79
80    prop_dict = {
81        'fs_type' : 'ext4',
82        'partition_headroom' : '40960',
83        'mount_point' : 'system',
84    }
85    self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
86
87    prop_dict = {
88        'fs_type' : 'ext4',
89        'partition_headroom' : '413696',
90        'mount_point' : 'system',
91    }
92    self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
93
94    common.Cleanup()
95