releasetools: Raise on image building errors.
The image building functions in build_image.py have been returning
(success, result) or special values to indicate the validity of the
result. The same logic can be better expressed by raising exceptions
instead, because
a) using a special value relies on caller to check for that magic
value;
b) exceptions can carry additional messages other than a boolean does,
e.g. the output from the failing command;
c) caller can have cleaner code flow without explicitly checking for
the validity of the result.
This CL changes such functions to raise on errors. The majority of these
functions are internal to build_image.py only, except for BuildImage()
that has a few callers in add_img_to_target_files.py (which all die upon
error anyway).
Test: `m dist`
Test: python -m unittest test_build_image
Test: python -m unittest test_add_img_to_target_files
Test: python -m unittest test_validate_target_files
Test: Inject an error to the depended binaries (e.g. avbtool), and check
that build_image.py exits with error messages.
Change-Id: Ibe4d51e267756bb1a00fa9238a213f9d55fd9b58
diff --git a/tools/releasetools/test_build_image.py b/tools/releasetools/test_build_image.py
index 0aaa847..94c31ee 100644
--- a/tools/releasetools/test_build_image.py
+++ b/tools/releasetools/test_build_image.py
@@ -22,8 +22,8 @@
import common
from build_image import (
- AVBCalcMinPartitionSize, BLOCK_SIZE,
- CheckHeadroom, RunCommand, SetUpInDirAndFsConfig)
+ AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom,
+ RunCommand, SetUpInDirAndFsConfig)
class BuildImageTest(unittest.TestCase):
@@ -49,7 +49,7 @@
'partition_headroom' : '4096000',
'mount_point' : 'system',
}
- self.assertTrue(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
+ CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict)
def test_CheckHeadroom_InsufficientHeadroom(self):
# Required headroom: 1001 blocks.
@@ -58,7 +58,8 @@
'partition_headroom' : '4100096',
'mount_point' : 'system',
}
- self.assertFalse(CheckHeadroom(self.EXT4FS_OUTPUT, prop_dict))
+ self.assertRaises(
+ BuildImageError, CheckHeadroom, self.EXT4FS_OUTPUT, prop_dict)
def test_CheckHeadroom_WrongFsType(self):
prop_dict = {
@@ -98,14 +99,14 @@
'partition_headroom' : '40960',
'mount_point' : 'system',
}
- self.assertTrue(CheckHeadroom(ext4fs_output, prop_dict))
+ CheckHeadroom(ext4fs_output, prop_dict)
prop_dict = {
'fs_type' : 'ext4',
'partition_headroom' : '413696',
'mount_point' : 'system',
}
- self.assertFalse(CheckHeadroom(ext4fs_output, prop_dict))
+ self.assertRaises(BuildImageError, CheckHeadroom, ext4fs_output, prop_dict)
def test_SetUpInDirAndFsConfig_SystemRootImageTrue_NonSystem(self):
prop_dict = {