Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for cros_generate_update_payload.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Amin Hassani | 0e274ee | 2018-09-19 14:12:58 -0700 | [diff] [blame^] | 11 | import tempfile |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 12 | |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import osutils |
| 16 | from chromite.scripts import cros_generate_update_payload |
| 17 | |
| 18 | |
| 19 | class CopyFileSegmentTest(cros_test_lib.TempDirTestCase): |
| 20 | """Test cros_generate_update_payload.CopyFileSegment""" |
| 21 | def testCopyFileSegment(self): |
| 22 | """Test copying on a simple file.""" |
| 23 | a = os.path.join(self.tempdir, 'a.txt') |
| 24 | osutils.WriteFile(a, '789') |
| 25 | b = os.path.join(self.tempdir, 'b.txt') |
| 26 | osutils.WriteFile(b, '0123') |
| 27 | cros_generate_update_payload.CopyFileSegment(a, 'r', 2, b, 'r+', in_seek=1) |
| 28 | self.assertEqual(osutils.ReadFile(b), '8923') |
| 29 | |
| 30 | |
| 31 | class ExtractRootTest(cros_test_lib.MockTempDirTestCase): |
| 32 | """Test cros_generate_update_payload.ExtractRoot""" |
| 33 | def testTruncate(self): |
| 34 | """Test truncating on extraction.""" |
| 35 | root = os.path.join(self.tempdir, 'root.bin') |
| 36 | root_pretruncate = os.path.join(self.tempdir, 'root_pretruncate.bin') |
| 37 | |
| 38 | content = '0123456789' |
| 39 | osutils.WriteFile(root, content) |
| 40 | self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile', |
| 41 | return_value=root) |
| 42 | self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize', |
| 43 | return_value=2) |
| 44 | |
| 45 | cros_generate_update_payload.ExtractRoot(None, root, root_pretruncate) |
| 46 | self.assertEqual(osutils.ReadFile(root), content[:2]) |
| 47 | self.assertEqual(osutils.ReadFile(root_pretruncate), content) |
| 48 | |
| 49 | |
| 50 | class Ext2FileSystemSizeTest(cros_test_lib.MockTestCase): |
| 51 | """Test cros_generate_update_payload.Ext2FileSystemSize""" |
| 52 | def testExt2FileSystemSize(self): |
| 53 | """Test on simple output.""" |
| 54 | block_size = 4096 |
| 55 | block_count = 123 |
| 56 | |
| 57 | self.PatchObject(cros_build_lib, 'RunCommand', |
| 58 | return_value=cros_build_lib.CommandResult(output=''' |
| 59 | Block size: %d |
| 60 | Other thing: 123456798 |
| 61 | Not an integer: cdsad132csda |
| 62 | Block count: %d |
| 63 | ''' % (block_size, block_count))) |
| 64 | |
| 65 | size = cros_generate_update_payload.Ext2FileSystemSize('/dev/null') |
| 66 | self.assertEqual(size, block_size * block_count) |
| 67 | |
| 68 | |
| 69 | class ExtractPartitionToTempFileTest(cros_test_lib.MockTempDirTestCase): |
| 70 | """Test cros_generate_update_payload.ExtractPartitionToTempFile""" |
| 71 | def testExtractPartitionToTempFile(self): |
| 72 | """Tests extraction on a simple image.""" |
| 73 | part_a_bin = '0123' |
| 74 | part_b_bin = '4567' |
| 75 | image_bin = part_a_bin + part_b_bin |
| 76 | |
| 77 | image = os.path.join(self.tempdir, 'image.bin') |
| 78 | osutils.WriteFile(image, image_bin) |
| 79 | part_a = os.path.join(self.tempdir, 'a.bin') |
| 80 | |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 81 | fake_partitions = ( |
| 82 | cros_build_lib.PartitionInfo(1, 0, 4, 4, 'fs', 'PART-A', ''), |
| 83 | cros_build_lib.PartitionInfo(2, 4, 8, 4, 'fs', 'PART-B', ''), |
| 84 | ) |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 85 | self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo', |
| 86 | return_value=fake_partitions) |
| 87 | |
| 88 | cros_generate_update_payload.ExtractPartitionToTempFile(image, 'PART-A', |
| 89 | temp_file=part_a) |
| 90 | self.assertEqual(osutils.ReadFile(part_a), part_a_bin) |
| 91 | |
| 92 | # Make sure we correctly generate new temp files. |
| 93 | tmp = cros_generate_update_payload.ExtractPartitionToTempFile(image, |
| 94 | 'PART-B') |
| 95 | self.assertEqual(osutils.ReadFile(tmp), part_b_bin) |
| 96 | |
| 97 | |
| 98 | class DeltaGeneratorTest(cros_test_lib.RunCommandTempDirTestCase): |
| 99 | """Test correct arguments passed to delta_generator.""" |
| 100 | def testDeltaGenerator(self): |
| 101 | """Test correct arguments propagated to delta_generator call.""" |
| 102 | temp = os.path.join(self.tempdir, 'temp.bin') |
| 103 | osutils.WriteFile(temp, '0123456789') |
| 104 | self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile', |
| 105 | return_value=temp) |
| 106 | self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize', |
| 107 | return_value=4) |
| 108 | |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 109 | fake_partitions = ( |
| 110 | cros_build_lib.PartitionInfo(3, 0, 4, 4, 'fs', 'ROOT-A', ''), |
| 111 | ) |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 112 | self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo', |
| 113 | return_value=fake_partitions) |
Amin Hassani | 0e274ee | 2018-09-19 14:12:58 -0700 | [diff] [blame^] | 114 | self.PatchObject(tempfile, 'NamedTemporaryFile', |
| 115 | return_value=type('file', (object,), {'name': temp})) |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 116 | cros_generate_update_payload.main([ |
| 117 | '--image', '/dev/null', |
| 118 | '--src_image', '/dev/null', |
| 119 | '--output', '/dev/null', |
| 120 | ]) |
| 121 | |
| 122 | self.assertCommandContains([ |
Amin Hassani | 0e274ee | 2018-09-19 14:12:58 -0700 | [diff] [blame^] | 123 | '--major_version=2', |
| 124 | '--partition_names=root:kernel', |
| 125 | '--new_partitions=' + ':'.join([temp, temp]), |
| 126 | '--new_postinstall_config_file=' + temp, |
| 127 | '--old_partitions=' + ':'.join([temp, temp]), |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 128 | '--rootfs_partition_size=4', |
| 129 | ]) |