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 filecmp
18import os.path
19
20import common
21import test_utils
22from build_image import (
23    BuildImageError, CheckHeadroom, GetFilesystemCharacteristics,
24    SetUpInDirAndFsConfig)
25
26
27class BuildImageTest(test_utils.ReleaseToolsTestCase):
28
29  # Available: 1000 blocks.
30  EXT4FS_OUTPUT = (
31      "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
32
33  def test_CheckHeadroom_SizeUnderLimit(self):
34    # Required headroom: 1000 blocks.
35    prop_dict = {
36        'fs_type' : 'ext4',
37        'partition_headroom' : '4096000',
38        'mount_point' : 'system',
39    }
40    CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)
41
42  def test_CheckHeadroom_InsufficientHeadroom(self):
43    # Required headroom: 1001 blocks.
44    prop_dict = {
45        'fs_type' : 'ext4',
46        'partition_headroom' : '4100096',
47        'mount_point' : 'system',
48    }
49    self.assertRaises(
50        BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
51
52  @test_utils.SkipIfExternalToolsUnavailable()
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)
76
77  @test_utils.SkipIfExternalToolsUnavailable()
78  def test_CheckHeadroom_WithMke2fsOutput(self):
79    """Tests the result parsing from actual call to mke2fs."""
80    input_dir = common.MakeTempDir()
81    output_image = common.MakeTempFile(suffix='.img')
82    command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
83               '/system', '409600', '-j', '0']
84    proc = common.Run(command)
85    ext4fs_output, _ = proc.communicate()
86    self.assertEqual(0, proc.returncode)
87
88    prop_dict = {
89        'fs_type' : 'ext4',
90        'partition_headroom' : '40960',
91        'mount_point' : 'system',
92    }
93    CheckHeadroom(ext4fs_output, prop_dict)
94
95    prop_dict = {
96        'fs_type' : 'ext4',
97        'partition_headroom' : '413696',
98        'mount_point' : 'system',
99    }
100    self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
101
102  def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self):
103    prop_dict = {
104        'fs_config': 'fs-config',
105        'mount_point': 'vendor',
106        'system_root_image': 'true',
107    }
108    in_dir, fs_config = SetUpInDirAndFsConfig('/path/to/in_dir', prop_dict)
109    self.assertEqual('/path/to/in_dir', in_dir)
110    self.assertEqual('fs-config', fs_config)
111    self.assertEqual('vendor', prop_dict['mount_point'])
112
113  @staticmethod
114  def _gen_fs_config(partition):
115    fs_config = common.MakeTempFile(suffix='.txt')
116    with open(fs_config, 'w') as fs_config_fp:
117      fs_config_fp.write('fs-config-{}\n'.format(partition))
118    return fs_config
119
120  def test_SetUpInDirAndFsConfig(self):
121    root_dir = common.MakeTempDir()
122    with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
123      init_fp.write('init')
124
125    origin_in = common.MakeTempDir()
126    with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
127      in_fp.write('system-file')
128    os.symlink('../etc', os.path.join(origin_in, 'symlink'))
129
130    fs_config_system = self._gen_fs_config('system')
131
132    prop_dict = {
133        'fs_config': fs_config_system,
134        'mount_point': 'system',
135        'root_dir': root_dir,
136    }
137    in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
138
139    self.assertTrue(filecmp.cmp(
140        os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
141    self.assertTrue(filecmp.cmp(
142        os.path.join(in_dir, 'system', 'file'),
143        os.path.join(origin_in, 'file')))
144    self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
145
146    self.assertTrue(filecmp.cmp(fs_config_system, fs_config))
147    self.assertEqual('/', prop_dict['mount_point'])
148
149  def test_SetUpInDirAndFsConfig_WithRootFsConfig(self):
150    root_dir = common.MakeTempDir()
151    with open(os.path.join(root_dir, 'init'), 'w') as init_fp:
152      init_fp.write('init')
153
154    origin_in = common.MakeTempDir()
155    with open(os.path.join(origin_in, 'file'), 'w') as in_fp:
156      in_fp.write('system-file')
157    os.symlink('../etc', os.path.join(origin_in, 'symlink'))
158
159    fs_config_system = self._gen_fs_config('system')
160    fs_config_root = self._gen_fs_config('root')
161
162    prop_dict = {
163        'fs_config': fs_config_system,
164        'mount_point': 'system',
165        'root_dir': root_dir,
166        'root_fs_config': fs_config_root,
167    }
168    in_dir, fs_config = SetUpInDirAndFsConfig(origin_in, prop_dict)
169
170    self.assertTrue(filecmp.cmp(
171        os.path.join(in_dir, 'init'), os.path.join(root_dir, 'init')))
172    self.assertTrue(filecmp.cmp(
173        os.path.join(in_dir, 'system', 'file'),
174        os.path.join(origin_in, 'file')))
175    self.assertTrue(os.path.islink(os.path.join(in_dir, 'system', 'symlink')))
176
177    with open(fs_config) as fs_config_fp:
178      fs_config_data = fs_config_fp.readlines()
179    self.assertIn('fs-config-system\n', fs_config_data)
180    self.assertIn('fs-config-root\n', fs_config_data)
181    self.assertEqual('/', prop_dict['mount_point'])
182
183  @test_utils.SkipIfExternalToolsUnavailable()
184  def test_GetFilesystemCharacteristics(self):
185    input_dir = common.MakeTempDir()
186    output_image = common.MakeTempFile(suffix='.img')
187    command = ['mkuserimg_mke2fs', input_dir, output_image, 'ext4',
188               '/system', '409600', '-j', '0']
189    proc = common.Run(command)
190    proc.communicate()
191    self.assertEqual(0, proc.returncode)
192
193    output_file = common.MakeTempFile(suffix='.img')
194    cmd = ["img2simg", output_image, output_file]
195    p = common.Run(cmd)
196    p.communicate()
197    self.assertEqual(0, p.returncode)
198
199    fs_dict = GetFilesystemCharacteristics(output_file)
200    self.assertEqual(int(fs_dict['Block size']), 4096)
201    self.assertGreaterEqual(int(fs_dict['Free blocks']), 0) # expect ~88
202    self.assertGreater(int(fs_dict['Inode count']), 0)      # expect ~64
203    self.assertGreaterEqual(int(fs_dict['Free inodes']), 0) # expect ~53
204    self.assertGreater(int(fs_dict['Inode count']), int(fs_dict['Free inodes']))
205