diff options
author | Tianjie <xunchang@google.com> | 2020-06-19 00:22:59 -0700 |
---|---|---|
committer | Tianjie <xunchang@google.com> | 2020-07-06 11:24:36 -0700 |
commit | 55abd3cbae6bd150b3534728a63befd1cadd6c5e (patch) | |
tree | 0077b57b1ef96c194380908311ce2a993fb1a89c /scripts/payload_info_unittest.py | |
parent | 99d570d67bd5dab11de321068c4002ab76ae774a (diff) | |
parent | 694eeb0dece40f88e11ece3a776d995d855be79b (diff) |
Merge remote-tracking branch 'aosp/upstream-master' into merge
It's a merge from chrome OS with some reverts.
1. the fd watcher change, because the libbrillo version isn't
compatible in aosp.
commit 6955bcc4ffe4cc9d62a88186b9a7e75d095a7897
commit 493fecb3f48c8478fd3ef244d631d857730dd14d
2. two libcurl unittest. Because the RunOnce() of the fake message
loop seems to have different behavior in aosp.
commit d3d84218cafbc1a95e7d6bbb775b495d1bebf4d2
Put preprocessor guards to use the old code in aosp. And we can
switch to the new code in the other path after adopting the new
libbrillo & libchrome.
Test: unit tests pass, apply an OTA
Change-Id: Id613599834b0f44f92841dbeae6303601db5490d
Diffstat (limited to 'scripts/payload_info_unittest.py')
-rwxr-xr-x | scripts/payload_info_unittest.py | 183 |
1 files changed, 80 insertions, 103 deletions
diff --git a/scripts/payload_info_unittest.py b/scripts/payload_info_unittest.py index a4ee9d50..07bb679d 100755 --- a/scripts/payload_info_unittest.py +++ b/scripts/payload_info_unittest.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/env python # # Copyright (C) 2015 The Android Open Source Project # @@ -17,24 +17,31 @@ """Unit testing payload_info.py.""" +# Disable check for function names to avoid errors based on old code +# pylint: disable-msg=invalid-name + +from __future__ import absolute_import from __future__ import print_function -import StringIO -import collections -import mock import sys import unittest +from contextlib import contextmanager + +from six.moves import StringIO + +import mock # pylint: disable=import-error + import payload_info import update_payload -from contextlib import contextmanager - from update_payload import update_metadata_pb2 + class FakePayloadError(Exception): """A generic error when using the FakePayload.""" + class FakeOption(object): """Fake options object for testing.""" @@ -42,11 +49,12 @@ class FakeOption(object): self.list_ops = False self.stats = False self.signatures = False - for key, val in kwargs.iteritems(): + for key, val in kwargs.items(): setattr(self, key, val) if not hasattr(self, 'payload_file'): self.payload_file = None + class FakeOp(object): """Fake manifest operation for testing.""" @@ -54,48 +62,57 @@ class FakeOp(object): self.src_extents = src_extents self.dst_extents = dst_extents self.type = op_type - for key, val in kwargs.iteritems(): + for key, val in kwargs.items(): setattr(self, key, val) def HasField(self, field): return hasattr(self, field) + +class FakeExtent(object): + """Fake Extent for testing.""" + def __init__(self, start_block, num_blocks): + self.start_block = start_block + self.num_blocks = num_blocks + + +class FakePartitionInfo(object): + """Fake PartitionInfo for testing.""" + def __init__(self, size): + self.size = size + + class FakePartition(object): """Fake PartitionUpdate field for testing.""" - def __init__(self, partition_name, operations): + def __init__(self, partition_name, operations, old_size, new_size): self.partition_name = partition_name self.operations = operations + self.old_partition_info = FakePartitionInfo(old_size) + self.new_partition_info = FakePartitionInfo(new_size) + class FakeManifest(object): """Fake manifest for testing.""" - def __init__(self, major_version): - FakeExtent = collections.namedtuple('FakeExtent', - ['start_block', 'num_blocks']) - self.install_operations = [FakeOp([], - [FakeExtent(1, 1), FakeExtent(2, 2)], - update_payload.common.OpType.REPLACE_BZ, - dst_length=3*4096, - data_offset=1, - data_length=1)] - self.kernel_install_operations = [FakeOp( - [FakeExtent(1, 1)], - [FakeExtent(x, x) for x in xrange(20)], - update_payload.common.OpType.SOURCE_COPY, - src_length=4096)] - if major_version == payload_info.MAJOR_PAYLOAD_VERSION_BRILLO: - self.partitions = [FakePartition('root', self.install_operations), - FakePartition('kernel', - self.kernel_install_operations)] - self.install_operations = self.kernel_install_operations = [] + def __init__(self): + self.partitions = [ + FakePartition(update_payload.common.ROOTFS, + [FakeOp([], [FakeExtent(1, 1), FakeExtent(2, 2)], + update_payload.common.OpType.REPLACE_BZ, + dst_length=3*4096, + data_offset=1, + data_length=1) + ], 1 * 4096, 3 * 4096), + FakePartition(update_payload.common.KERNEL, + [FakeOp([FakeExtent(1, 1)], + [FakeExtent(x, x) for x in range(20)], + update_payload.common.OpType.SOURCE_COPY, + src_length=4096) + ], 2 * 4096, 4 * 4096), + ] self.block_size = 4096 self.minor_version = 4 - FakePartInfo = collections.namedtuple('FakePartInfo', ['size']) - self.old_rootfs_info = FakePartInfo(1 * 4096) - self.old_kernel_info = FakePartInfo(2 * 4096) - self.new_rootfs_info = FakePartInfo(3 * 4096) - self.new_kernel_info = FakePartInfo(4 * 4096) self.signatures_offset = None self.signatures_size = None @@ -103,26 +120,27 @@ class FakeManifest(object): """Fake HasField method based on the python members.""" return hasattr(self, field_name) and getattr(self, field_name) is not None + class FakeHeader(object): """Fake payload header for testing.""" - def __init__(self, version, manifest_len, metadata_signature_len): - self.version = version + def __init__(self, manifest_len, metadata_signature_len): + self.version = payload_info.MAJOR_PAYLOAD_VERSION_BRILLO self.manifest_len = manifest_len self.metadata_signature_len = metadata_signature_len @property def size(self): - return (20 if self.version == payload_info.MAJOR_PAYLOAD_VERSION_CHROMEOS - else 24) + return 24 + class FakePayload(object): """Fake payload for testing.""" - def __init__(self, major_version): - self._header = FakeHeader(major_version, 222, 0) + def __init__(self): + self._header = FakeHeader(222, 0) self.header = None - self._manifest = FakeManifest(major_version) + self._manifest = FakeManifest() self.manifest = None self._blobs = {} @@ -152,7 +170,7 @@ class FakePayload(object): def _AddSignatureToProto(proto, **kwargs): """Add a new Signature element to the passed proto.""" new_signature = proto.signatures.add() - for key, val in kwargs.iteritems(): + for key, val in kwargs.items(): setattr(new_signature, key, val) def AddPayloadSignature(self, **kwargs): @@ -170,6 +188,7 @@ class FakePayload(object): self._header.metadata_signature_len = len(blob) self._blobs[-len(blob)] = blob + class PayloadCommandTest(unittest.TestCase): """Test class for our PayloadCommand class.""" @@ -178,7 +197,7 @@ class PayloadCommandTest(unittest.TestCase): """A tool for capturing the sys.stdout""" stdout = sys.stdout try: - sys.stdout = StringIO.StringIO() + sys.stdout = StringIO() yield sys.stdout finally: sys.stdout = stdout @@ -192,52 +211,25 @@ class PayloadCommandTest(unittest.TestCase): with mock.patch.object(update_payload, 'Payload', return_value=payload), \ self.OutputCapturer() as output: payload_cmd.Run() - self.assertEquals(output.getvalue(), expected_out) + self.assertEqual(output.getvalue(), expected_out) def testDisplayValue(self): """Verify that DisplayValue prints what we expect.""" with self.OutputCapturer() as output: payload_info.DisplayValue('key', 'value') - self.assertEquals(output.getvalue(), 'key: value\n') + self.assertEqual(output.getvalue(), 'key: value\n') def testRun(self): """Verify that Run parses and displays the payload like we expect.""" payload_cmd = payload_info.PayloadCommand(FakeOption(action='show')) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_CHROMEOS) - expected_out = """Payload version: 1 -Manifest length: 222 -Number of operations: 1 -Number of kernel ops: 1 -Block size: 4096 -Minor version: 4 -""" - self.TestCommand(payload_cmd, payload, expected_out) - - def testListOpsOnVersion1(self): - """Verify that the --list_ops option gives the correct output.""" - payload_cmd = payload_info.PayloadCommand( - FakeOption(list_ops=True, action='show')) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_CHROMEOS) - expected_out = """Payload version: 1 + payload = FakePayload() + expected_out = """Payload version: 2 Manifest length: 222 -Number of operations: 1 -Number of kernel ops: 1 +Number of partitions: 2 + Number of "root" ops: 1 + Number of "kernel" ops: 1 Block size: 4096 Minor version: 4 - -Install operations: - 0: REPLACE_BZ - Data offset: 1 - Data length: 1 - Destination: 2 extents (3 blocks) - (1,1) (2,2) -Kernel install operations: - 0: SOURCE_COPY - Source: 1 extent (1 block) - (1,1) - Destination: 20 extents (190 blocks) - (0,0) (1,1) (2,2) (3,3) (4,4) (5,5) (6,6) (7,7) (8,8) (9,9) (10,10) - (11,11) (12,12) (13,13) (14,14) (15,15) (16,16) (17,17) (18,18) (19,19) """ self.TestCommand(payload_cmd, payload, expected_out) @@ -245,7 +237,7 @@ Kernel install operations: """Verify that the --list_ops option gives the correct output.""" payload_cmd = payload_info.PayloadCommand( FakeOption(list_ops=True, action='show')) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_BRILLO) + payload = FakePayload() expected_out = """Payload version: 2 Manifest length: 222 Number of partitions: 2 @@ -270,28 +262,11 @@ kernel install operations: """ self.TestCommand(payload_cmd, payload, expected_out) - def testStatsOnVersion1(self): - """Verify that the --stats option works correctly.""" - payload_cmd = payload_info.PayloadCommand( - FakeOption(stats=True, action='show')) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_CHROMEOS) - expected_out = """Payload version: 1 -Manifest length: 222 -Number of operations: 1 -Number of kernel ops: 1 -Block size: 4096 -Minor version: 4 -Blocks read: 11 -Blocks written: 193 -Seeks when writing: 18 -""" - self.TestCommand(payload_cmd, payload, expected_out) - def testStatsOnVersion2(self): """Verify that the --stats option works correctly on version 2.""" payload_cmd = payload_info.PayloadCommand( FakeOption(stats=True, action='show')) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_BRILLO) + payload = FakePayload() expected_out = """Payload version: 2 Manifest length: 222 Number of partitions: 2 @@ -309,11 +284,12 @@ Seeks when writing: 18 """Verify that the --signatures option works with unsigned payloads.""" payload_cmd = payload_info.PayloadCommand( FakeOption(action='show', signatures=True)) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_CHROMEOS) - expected_out = """Payload version: 1 + payload = FakePayload() + expected_out = """Payload version: 2 Manifest length: 222 -Number of operations: 1 -Number of kernel ops: 1 +Number of partitions: 2 + Number of "root" ops: 1 + Number of "kernel" ops: 1 Block size: 4096 Minor version: 4 No metadata signatures stored in the payload @@ -325,11 +301,11 @@ No payload signatures stored in the payload """Verify that the --signatures option shows the present signatures.""" payload_cmd = payload_info.PayloadCommand( FakeOption(action='show', signatures=True)) - payload = FakePayload(payload_info.MAJOR_PAYLOAD_VERSION_BRILLO) + payload = FakePayload() payload.AddPayloadSignature(version=1, - data='12345678abcdefgh\x00\x01\x02\x03') - payload.AddPayloadSignature(data='I am a signature so access is yes.') - payload.AddMetadataSignature(data='\x00\x0a\x0c') + data=b'12345678abcdefgh\x00\x01\x02\x03') + payload.AddPayloadSignature(data=b'I am a signature so access is yes.') + payload.AddMetadataSignature(data=b'\x00\x0a\x0c') expected_out = """Payload version: 2 Manifest length: 222 Number of partitions: 2 @@ -353,5 +329,6 @@ Payload signatures: (2 entries) """ self.TestCommand(payload_cmd, payload, expected_out) + if __name__ == '__main__': unittest.main() |