releasetools: Move assertions into CheckHeadroom().

Assert the input arguments inside CheckHeadroom() function, and add
matching tests. Also use common.MakeTemp{File,Dir} in tests.

Test: `m dist`
Test: python -m unittest test_build_image
Change-Id: Ib45f24651af9591a9cfdd72aac3558f6c9a80f44
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 11a0055..3bb8b9c 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -384,17 +384,25 @@
 
   Returns:
     The check result.
+
+  Raises:
+    AssertionError: On invalid input.
   """
+  assert ext4fs_output is not None
+  assert prop_dict.get('fs_type', '').startswith('ext4')
+  assert 'partition_headroom' in prop_dict
+  assert 'mount_point' in prop_dict
+
   ext4fs_stats = re.compile(
       r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
       r'(?P<total_blocks>[0-9]+) blocks')
   m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
   used_blocks = int(m.groupdict().get('used_blocks'))
   total_blocks = int(m.groupdict().get('total_blocks'))
-  headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
+  headroom_blocks = int(prop_dict['partition_headroom']) / BLOCK_SIZE
   adjusted_blocks = total_blocks - headroom_blocks
   if used_blocks > adjusted_blocks:
-    mount_point = prop_dict.get("mount_point")
+    mount_point = prop_dict["mount_point"]
     print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
           "headroom: %d blocks, available: %d blocks)" % (
               mount_point, total_blocks, used_blocks, headroom_blocks,
@@ -578,7 +586,6 @@
 
   # Check if there's enough headroom space available for ext4 image.
   if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
-    assert ext4fs_output is not None
     if not CheckHeadroom(ext4fs_output, prop_dict):
       return False
 
diff --git a/tools/releasetools/test_build_image.py b/tools/releasetools/test_build_image.py
index 6566a5a..161faff 100644
--- a/tools/releasetools/test_build_image.py
+++ b/tools/releasetools/test_build_image.py
@@ -14,52 +14,81 @@
 # limitations under the License.
 #
 
-import shutil
-import tempfile
 import unittest
 
+import common
 from build_image import CheckHeadroom, RunCommand
 
 
 class BuildImageTest(unittest.TestCase):
 
+  # Available: 1000 blocks.
+  EXT4FS_OUTPUT = (
+      "Created filesystem with 2777/129024 inodes and 515099/516099 blocks")
+
   def test_CheckHeadroom_SizeUnderLimit(self):
-    ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
-                     "508140/516099 blocks")
+    # Required headroom: 1000 blocks.
     prop_dict = {
-        'partition_headroom' : '4194304',
+        'fs_type' : 'ext4',
+        'partition_headroom' : '4096000',
         'mount_point' : 'system',
     }
-    self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+    self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
 
   def test_CheckHeadroom_InsufficientHeadroom(self):
-    ext4fs_output = ("Created filesystem with 2777/129024 inodes and "
-                     "515099/516099 blocks")
+    # Required headroom: 1001 blocks.
     prop_dict = {
+        'fs_type' : 'ext4',
         'partition_headroom' : '4100096',
         'mount_point' : 'system',
     }
-    self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+    self.assertFalse(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
+
+  def test_CheckHeadroom_WrongFsType(self):
+    prop_dict = {
+        'fs_type' : 'f2fs',
+        'partition_headroom' : '4100096',
+        'mount_point' : 'system',
+    }
+    self.assertRaises(
+        AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
+
+  def test_CheckHeadroom_MissingProperties(self):
+    prop_dict = {
+        'fs_type' : 'ext4',
+        'partition_headroom' : '4100096',
+    }
+    self.assertRaises(
+        AssertionError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
+
+    prop_dict = {
+        'fs_type' : 'ext4',
+        'mount_point' : 'system',
+    }
+    self.assertRaises(
+        AssertionError, CheckHeadroom, self.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',
+    input_dir = common.MakeTempDir()
+    output_image = common.MakeTempFile(suffix='.img')
+    command = ['mkuserimg_mke2fs.sh', input_dir, output_image, 'ext4',
                '/system', '409600', '-j', '0']
     ext4fs_output, exit_code = RunCommand(command)
     self.assertEqual(0, exit_code)
 
     prop_dict = {
+        'fs_type' : 'ext4',
         'partition_headroom' : '40960',
         'mount_point' : 'system',
     }
     self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
 
     prop_dict = {
+        'fs_type' : 'ext4',
         'partition_headroom' : '413696',
         'mount_point' : 'system',
     }
     self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
 
-    shutil.rmtree(input_dir)
+    common.Cleanup()