build_image: Factor out CheckHeadroom() and add tests.
The test is mostly trivial, but it ensures the result parsing from
actual calls to mke2fs.
Test: python -m unittest test_build_image
Test: `m dist` on aosp_marlin-userdebug (w/ and w/o defining
PRODUCT_SYSTEM_HEADROOM respectively).
Change-Id: I8b9964213950e76f6d7d5518414a1bab888b4706
diff --git a/tools/releasetools/test_build_image.py b/tools/releasetools/test_build_image.py
new file mode 100644
index 0000000..6566a5a
--- /dev/null
+++ b/tools/releasetools/test_build_image.py
@@ -0,0 +1,65 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import shutil
+import tempfile
+import unittest
+
+from build_image import CheckHeadroom, RunCommand
+
+
+class BuildImageTest(unittest.TestCase):
+
+ def test_CheckHeadroom_SizeUnderLimit(self):
+ ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
+ "508140/516099 blocks")
+ prop_dict = {
+ 'partition_headroom' : '4194304',
+ 'mount_point' : 'system',
+ }
+ self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+
+ def test_CheckHeadroom_InsufficientHeadroom(self):
+ ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
+ "515099/516099 blocks")
+ prop_dict = {
+ 'partition_headroom' : '4100096',
+ 'mount_point' : 'system',
+ }
+ self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+
+ def test_CheckHeadroom_WithMke2fsOutput(self):
+ """Tests the result parsing from actual call to mke2fs."""
+ input_dir = tempfile.mkdtemp()
+ output_image = tempfile.NamedTemporaryFile(suffix='.img')
+ command = ['mkuserimg_mke2fs.sh', input_dir, output_image.name, 'ext4',
+ '/system', '409600', '-j', '0']
+ ext4fs_output, exit_code = RunCommand(command)
+ self.assertEqual(0, exit_code)
+
+ prop_dict = {
+ 'partition_headroom' : '40960',
+ 'mount_point' : 'system',
+ }
+ self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+
+ prop_dict = {
+ 'partition_headroom' : '413696',
+ 'mount_point' : 'system',
+ }
+ self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+
+ shutil.rmtree(input_dir)