cros_generate_update_payload: Use new paygen library

This is where things getting interesting. Basically this patch starts calling
the chromite's paygen library instead of directly calling delta_generator. This
makes everything much cleaner and extensible.

Added these flags:
--check: Verifies the integrity of the payload.
--work_dir: This was mistakenly removed in the past. Added it back.

Removed a lot of unused and unnecessary flags.

Also changed the partition_lib API a bit to accommodate for optionally
truncating the root partition.

With this change the run time of cros_generate_update_payload increases by about
20~30 seconds. The reason being is an added extra copy that happens either from
the cache to a temporary tgt_image.bin file. It might not be a good idea to get
rid of that extra copy because if the file changes in the middle, the paygen can
get screwed up. So we might better be safe than sorry!! I think the value we
gain from this change from software engineering POV is much higher than the
slight loss in the performance. We could potentially improve this by smarter
caching logic in the future.

BUG=chromium:862679
TEST=cros flash inside and outside chroot
TEST=unittests
TEST=payload-tryjob

Change-Id: I66a02df093ffb65f2797904572c6b87fb76d782d
Reviewed-on: https://chromium-review.googlesource.com/1369108
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
diff --git a/scripts/cros_generate_update_payload_unittest.py b/scripts/cros_generate_update_payload_unittest.py
index 219af01..5d732c7 100644
--- a/scripts/cros_generate_update_payload_unittest.py
+++ b/scripts/cros_generate_update_payload_unittest.py
@@ -7,42 +7,60 @@
 
 from __future__ import print_function
 
-import os
-import tempfile
-
-from chromite.lib import cros_build_lib
 from chromite.lib import cros_test_lib
-from chromite.lib import osutils
+from chromite.lib import partial_mock
+
 from chromite.lib.paygen import partition_lib
+from chromite.lib.paygen import paygen_payload_lib
+
 from chromite.scripts import cros_generate_update_payload
 
-class DeltaGeneratorTest(cros_test_lib.RunCommandTempDirTestCase):
+
+class CrOSGenerateUpdatePayloadTest(cros_test_lib.MockTestCase):
   """Test correct arguments passed to delta_generator."""
-  def testDeltaGenerator(self):
+
+  def testGenerateUpdatePayload(self):
     """Test correct arguments propagated to delta_generator call."""
-    temp = os.path.join(self.tempdir, 'temp.bin')
-    osutils.WriteFile(temp, '0123456789')
-    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', ''),
-    )
-    self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
-                     return_value=fake_partitions)
-    self.PatchObject(tempfile, 'NamedTemporaryFile',
-                     return_value=type('file', (object,), {'name': temp}))
+    paygen_mock = self.PatchObject(paygen_payload_lib, 'GenerateUpdatePayload')
+
     cros_generate_update_payload.main([
-        '--image', '/dev/null',
-        '--src_image', '/dev/null',
-        '--output', '/dev/null',
+        '--image', 'foo-image',
+        '--src_image', 'foo-src-image',
+        '--output', 'foo-output',
+        '--check',
+        '--private_key', 'foo-private-key',
+        '--out_metadata_hash_file', 'foo-metadata-hash',
+        '--work_dir', 'foo-work-dir',
     ])
 
-    self.assertCommandContains([
-        '--major_version=2',
-        '--partition_names=root:kernel',
-        '--new_partitions=' + ':'.join([temp, temp]),
-        '--new_postinstall_config_file=' + temp,
-        '--old_partitions=' + ':'.join([temp, temp]),
-        '--rootfs_partition_size=4',
+    paygen_mock.assert_called_once_with(
+        partial_mock.HasString('foo-image'),
+        partial_mock.HasString('foo-output'),
+        src_image=partial_mock.HasString('foo-src-image'),
+        work_dir=partial_mock.HasString('foo-work-dir'),
+        private_key=partial_mock.HasString('foo-private-key'),
+        check=True,
+        out_metadata_hash_file=partial_mock.HasString('foo-metadata-hash'))
+
+  def testExtractPartitions(self):
+    """Test extracting partitions."""
+
+    kernel_mock = self.PatchObject(partition_lib, 'ExtractKernel')
+    root_mock = self.PatchObject(partition_lib, 'ExtractRoot')
+
+    cros_generate_update_payload.main([
+        '--image', 'foo-image',
+        '--extract',
+        '--kern_path', 'foo-kernel',
+        '--root_path', 'foo-root',
+        '--root_pretruncate_path', 'foo-pretruncate',
     ])
+
+    kernel_mock.assert_called_once_with(partial_mock.HasString('foo-image'),
+                                        partial_mock.HasString('foo-kernel'))
+    root_mock.assert_calls_with(partial_mock.HasString('foo-image'),
+                                partial_mock.HasString('foo-root'))
+    root_mock.assert_calls_with(partial_mock.HasString('foo-image'),
+                                partial_mock.HasString('foo-pretruncate'),
+                                truncate=False)