blob: a3b935adc18d4b69c3d80f2cdaa533167afd980e [file] [log] [blame]
Tudor Brindus3e03eba2018-07-18 11:27:13 -07001# -*- 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
8from __future__ import print_function
9
10import os
Amin Hassani0e274ee2018-09-19 14:12:58 -070011import tempfile
Tudor Brindus3e03eba2018-07-18 11:27:13 -070012
13from chromite.lib import cros_build_lib
14from chromite.lib import cros_test_lib
15from chromite.lib import osutils
16from chromite.scripts import cros_generate_update_payload
17
18
19class 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
31class 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
50class 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='''
59Block size: %d
60Other thing: 123456798
61Not an integer: cdsad132csda
62Block 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
69class 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 Jonesea6dda62018-12-04 16:11:45 -070081 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 Brindus3e03eba2018-07-18 11:27:13 -070085 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
98class 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 Jonesea6dda62018-12-04 16:11:45 -0700109 fake_partitions = (
110 cros_build_lib.PartitionInfo(3, 0, 4, 4, 'fs', 'ROOT-A', ''),
111 )
Tudor Brindus3e03eba2018-07-18 11:27:13 -0700112 self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
113 return_value=fake_partitions)
Amin Hassani0e274ee2018-09-19 14:12:58 -0700114 self.PatchObject(tempfile, 'NamedTemporaryFile',
115 return_value=type('file', (object,), {'name': temp}))
Tudor Brindus3e03eba2018-07-18 11:27:13 -0700116 cros_generate_update_payload.main([
117 '--image', '/dev/null',
118 '--src_image', '/dev/null',
119 '--output', '/dev/null',
120 ])
121
122 self.assertCommandContains([
Amin Hassani0e274ee2018-09-19 14:12:58 -0700123 '--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 Brindus3e03eba2018-07-18 11:27:13 -0700128 '--rootfs_partition_size=4',
129 ])