blob: ddcb6c990c97f71d03fa584aec659b5b0da2be6e [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
11
12from chromite.lib import cros_build_lib
13from chromite.lib import cros_test_lib
14from chromite.lib import osutils
15from chromite.scripts import cros_generate_update_payload
16
17
18class CopyFileSegmentTest(cros_test_lib.TempDirTestCase):
19 """Test cros_generate_update_payload.CopyFileSegment"""
20 def testCopyFileSegment(self):
21 """Test copying on a simple file."""
22 a = os.path.join(self.tempdir, 'a.txt')
23 osutils.WriteFile(a, '789')
24 b = os.path.join(self.tempdir, 'b.txt')
25 osutils.WriteFile(b, '0123')
26 cros_generate_update_payload.CopyFileSegment(a, 'r', 2, b, 'r+', in_seek=1)
27 self.assertEqual(osutils.ReadFile(b), '8923')
28
29
30class ExtractRootTest(cros_test_lib.MockTempDirTestCase):
31 """Test cros_generate_update_payload.ExtractRoot"""
32 def testTruncate(self):
33 """Test truncating on extraction."""
34 root = os.path.join(self.tempdir, 'root.bin')
35 root_pretruncate = os.path.join(self.tempdir, 'root_pretruncate.bin')
36
37 content = '0123456789'
38 osutils.WriteFile(root, content)
39 self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile',
40 return_value=root)
41 self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize',
42 return_value=2)
43
44 cros_generate_update_payload.ExtractRoot(None, root, root_pretruncate)
45 self.assertEqual(osutils.ReadFile(root), content[:2])
46 self.assertEqual(osutils.ReadFile(root_pretruncate), content)
47
48
49class Ext2FileSystemSizeTest(cros_test_lib.MockTestCase):
50 """Test cros_generate_update_payload.Ext2FileSystemSize"""
51 def testExt2FileSystemSize(self):
52 """Test on simple output."""
53 block_size = 4096
54 block_count = 123
55
56 self.PatchObject(cros_build_lib, 'RunCommand',
57 return_value=cros_build_lib.CommandResult(output='''
58Block size: %d
59Other thing: 123456798
60Not an integer: cdsad132csda
61Block count: %d
62''' % (block_size, block_count)))
63
64 size = cros_generate_update_payload.Ext2FileSystemSize('/dev/null')
65 self.assertEqual(size, block_size * block_count)
66
67
68class ExtractPartitionToTempFileTest(cros_test_lib.MockTempDirTestCase):
69 """Test cros_generate_update_payload.ExtractPartitionToTempFile"""
70 def testExtractPartitionToTempFile(self):
71 """Tests extraction on a simple image."""
72 part_a_bin = '0123'
73 part_b_bin = '4567'
74 image_bin = part_a_bin + part_b_bin
75
76 image = os.path.join(self.tempdir, 'image.bin')
77 osutils.WriteFile(image, image_bin)
78 part_a = os.path.join(self.tempdir, 'a.bin')
79
80 fake_partitions = {
81 'PART-A': cros_build_lib.PartitionInfo(1, 0, 4, 4, 'fs', 'PART-A', ''),
82 'PART-B': cros_build_lib.PartitionInfo(2, 4, 8, 4, 'fs', 'PART-B', ''),
83 }
84 self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
85 return_value=fake_partitions)
86
87 cros_generate_update_payload.ExtractPartitionToTempFile(image, 'PART-A',
88 temp_file=part_a)
89 self.assertEqual(osutils.ReadFile(part_a), part_a_bin)
90
91 # Make sure we correctly generate new temp files.
92 tmp = cros_generate_update_payload.ExtractPartitionToTempFile(image,
93 'PART-B')
94 self.assertEqual(osutils.ReadFile(tmp), part_b_bin)
95
96
97class DeltaGeneratorTest(cros_test_lib.RunCommandTempDirTestCase):
98 """Test correct arguments passed to delta_generator."""
99 def testDeltaGenerator(self):
100 """Test correct arguments propagated to delta_generator call."""
101 temp = os.path.join(self.tempdir, 'temp.bin')
102 osutils.WriteFile(temp, '0123456789')
103 self.PatchObject(cros_generate_update_payload, 'ExtractPartitionToTempFile',
104 return_value=temp)
105 self.PatchObject(cros_generate_update_payload, 'Ext2FileSystemSize',
106 return_value=4)
107
108 fake_partitions = {
109 'ROOT-A': cros_build_lib.PartitionInfo(3, 0, 4, 4, 'fs', 'ROOT-A', ''),
110 }
111 self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
112 return_value=fake_partitions)
113 cros_generate_update_payload.main([
114 '--image', '/dev/null',
115 '--src_image', '/dev/null',
116 '--output', '/dev/null',
117 ])
118
119 self.assertCommandContains([
Amin Hassanif182be22018-10-29 17:18:23 +0000120 '--major_version=1',
121 '--new_image=' + temp,
122 '--new_kernel=' + temp,
123 '--old_image=' + temp,
124 '--old_kernel=' + temp,
Tudor Brindus3e03eba2018-07-18 11:27:13 -0700125 '--rootfs_partition_size=4',
126 ])