cros_generate_update_payload: Move utility funtions to paygen
There are a few utility functions in the cros_generate_update_payload that are
more suited to be moved to chromite/lib/paygen which they belong. Eventually the
cros_generate_update_payload will just call a function like
GenerateUpdatePayload() from lib/paygen instead of directly forking the
delta_generator.
The utility functions are mostly related to extracting partitions out of an
image. They are moved to a new file lib/paygen/partition_lib.py. Appropriate
unittests are also moved along. In addition these functions have been improved
and udpated. Specifically, they don't return the (potentially temporary file)
path to extracted partition anymore. The path needs to be passed to them.
BUG=chromium:862679
TEST=cros flash inside and outside chroot
TEST=unittests
Change-Id: Ic8cd2eb58f4e451b74250eea6789e5f2a415354a
Reviewed-on: https://chromium-review.googlesource.com/1278089
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/cros_generate_update_payload_unittest.py b/scripts/cros_generate_update_payload_unittest.py
index a3b935a..219af01 100644
--- a/scripts/cros_generate_update_payload_unittest.py
+++ b/scripts/cros_generate_update_payload_unittest.py
@@ -13,98 +13,17 @@
from chromite.lib import cros_build_lib
from chromite.lib import cros_test_lib
from chromite.lib import osutils
+from chromite.lib.paygen import partition_lib
from chromite.scripts import cros_generate_update_payload
-
-class CopyFileSegmentTest(cros_test_lib.TempDirTestCase):
- """Test cros_generate_update_payload.CopyFileSegment"""
- def testCopyFileSegment(self):
- """Test copying on a simple file."""
- a = os.path.join(self.tempdir, 'a.txt')
- osutils.WriteFile(a, '789')
- b = os.path.join(self.tempdir, 'b.txt')
- osutils.WriteFile(b, '0123')
- cros_generate_update_payload.CopyFileSegment(a, 'r', 2, b, 'r+', in_seek=1)
- self.assertEqual(osutils.ReadFile(b), '8923')
-
-
-class ExtractRootTest(cros_test_lib.MockTempDirTestCase):
- """Test cros_generate_update_payload.ExtractRoot"""
- def testTruncate(self):
- """Test truncating on extraction."""
- root = os.path.join(self.tempdir, 'root.bin')
- root_pretruncate = os.path.join(self.tempdir, 'root_pretruncate.bin')
-
- content = '0123456789'
- osutils.WriteFile(root, content)
- self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile',
- return_value=root)
- self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize',
- return_value=2)
-
- cros_generate_update_payload.ExtractRoot(None, root, root_pretruncate)
- self.assertEqual(osutils.ReadFile(root), content[:2])
- self.assertEqual(osutils.ReadFile(root_pretruncate), content)
-
-
-class Ext2FileSystemSizeTest(cros_test_lib.MockTestCase):
- """Test cros_generate_update_payload.Ext2FileSystemSize"""
- def testExt2FileSystemSize(self):
- """Test on simple output."""
- block_size = 4096
- block_count = 123
-
- self.PatchObject(cros_build_lib, 'RunCommand',
- return_value=cros_build_lib.CommandResult(output='''
-Block size: %d
-Other thing: 123456798
-Not an integer: cdsad132csda
-Block count: %d
-''' % (block_size, block_count)))
-
- size = cros_generate_update_payload.Ext2FileSystemSize('/dev/null')
- self.assertEqual(size, block_size * block_count)
-
-
-class ExtractPartitionToTempFileTest(cros_test_lib.MockTempDirTestCase):
- """Test cros_generate_update_payload.ExtractPartitionToTempFile"""
- def testExtractPartitionToTempFile(self):
- """Tests extraction on a simple image."""
- part_a_bin = '0123'
- part_b_bin = '4567'
- image_bin = part_a_bin + part_b_bin
-
- image = os.path.join(self.tempdir, 'image.bin')
- osutils.WriteFile(image, image_bin)
- part_a = os.path.join(self.tempdir, 'a.bin')
-
- fake_partitions = (
- cros_build_lib.PartitionInfo(1, 0, 4, 4, 'fs', 'PART-A', ''),
- cros_build_lib.PartitionInfo(2, 4, 8, 4, 'fs', 'PART-B', ''),
- )
- self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
- return_value=fake_partitions)
-
- cros_generate_update_payload.ExtractPartitionToTempFile(image, 'PART-A',
- temp_file=part_a)
- self.assertEqual(osutils.ReadFile(part_a), part_a_bin)
-
- # Make sure we correctly generate new temp files.
- tmp = cros_generate_update_payload.ExtractPartitionToTempFile(image,
- 'PART-B')
- self.assertEqual(osutils.ReadFile(tmp), part_b_bin)
-
-
class DeltaGeneratorTest(cros_test_lib.RunCommandTempDirTestCase):
"""Test correct arguments passed to delta_generator."""
def testDeltaGenerator(self):
"""Test correct arguments propagated to delta_generator call."""
temp = os.path.join(self.tempdir, 'temp.bin')
osutils.WriteFile(temp, '0123456789')
- self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile',
- return_value=temp)
- self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize',
- return_value=4)
+ self.PatchObject(partition_lib, 'ExtractPartition', return_value=temp)
+ self.PatchObject(partition_lib, 'Ext2FileSystemSize', return_value=4)
fake_partitions = (
cros_build_lib.PartitionInfo(3, 0, 4, 4, 'fs', 'ROOT-A', ''),