diff options
author | Amin Hassani <ahassani@google.com> | 2017-12-18 15:15:32 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-01-05 18:37:15 -0800 |
commit | b05a65a748bd2e2542e7b05f629d3bc69c8d8135 (patch) | |
tree | ed736d196e5f908deeb450ffdacdc16432d74b77 /scripts/update_payload/checker_unittest.py | |
parent | 8adc24346405a142e7d3b65c3e4aee0515320f73 (diff) |
update_payload: Fix most of lint styling issues.
This patch fixes a lot of pylint issues in the update_engine scripts. Majority
of this changes are based on recommendation found in:
https://www.chromium.org/chromium-os/python-style-guidelines
It is a good idea to do these changes now, because if there are many pylint
errors when performing 'repo upload', serious problems can be overshadowed by a
lot of noise and eventually cause problems.
These fixes include:
- Fixing executable shebangs to /usr/bin/python2.
- Fixing import-error problems by disabiling them.
- Removing pylint disables that are not valid anymore.
- Changing all imports to proper absolute import format.
- Change the import of PayloadError from update_payload.PayloadError for
simplicity.
- Add pydoc strings for functions and classes that were missing.
The remaining unchanged pylint problmes include:
- The header files of these scripts are in CrOS copyright format, but the
the cros lint hook is configured to AoSP copyright format.
- The test* functions in unittests are not compatible with CamelCase format.
BUG=chromium:796338
TEST=unittests pass
TEST=start_devserver
TEST=cros flash
TEST=scripts/paycheck.py
Change-Id: I7eed4d1625eb7c510c7949fada120de5a6a26c7b
Reviewed-on: https://chromium-review.googlesource.com/834875
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Sen Jiang <senj@chromium.org>
Diffstat (limited to 'scripts/update_payload/checker_unittest.py')
-rwxr-xr-x | scripts/update_payload/checker_unittest.py | 154 |
1 files changed, 63 insertions, 91 deletions
diff --git a/scripts/update_payload/checker_unittest.py b/scripts/update_payload/checker_unittest.py index 245da55e..974519d5 100755 --- a/scripts/update_payload/checker_unittest.py +++ b/scripts/update_payload/checker_unittest.py @@ -20,14 +20,16 @@ import unittest # pylint: disable=F0401 import mox -import checker -import common -import payload as update_payload # Avoid name conflicts later. -import test_utils -import update_metadata_pb2 +from update_payload import checker +from update_payload import common +from update_payload import test_utils +from update_payload import update_metadata_pb2 +from update_payload.error import PayloadError +from update_payload.payload import Payload # Avoid name conflicts later. def _OpTypeByName(op_name): + """Returns the type of an operation from itsname.""" op_name_to_type = { 'REPLACE': common.OpType.REPLACE, 'REPLACE_BZ': common.OpType.REPLACE_BZ, @@ -55,7 +57,7 @@ def _GetPayloadChecker(payload_gen_write_to_file_func, payload_gen_dargs=None, payload_file = cStringIO.StringIO() payload_gen_write_to_file_func(payload_file, **payload_gen_dargs) payload_file.seek(0) - payload = update_payload.Payload(payload_file) + payload = Payload(payload_file) payload.Init() return checker.PayloadChecker(payload, **checker_init_dargs) @@ -65,7 +67,7 @@ def _GetPayloadCheckerWithData(payload_gen): payload_file = cStringIO.StringIO() payload_gen.WriteToFile(payload_file) payload_file.seek(0) - payload = update_payload.Payload(payload_file) + payload = Payload(payload_file) payload.Init() return checker.PayloadChecker(payload) @@ -91,7 +93,7 @@ class PayloadCheckerTest(mox.MoxTestBase): def MockPayload(self): """Create a mock payload object, complete with a mock manifest.""" - payload = self.mox.CreateMock(update_payload.Payload) + payload = self.mox.CreateMock(Payload) payload.is_init = True payload.manifest = self.mox.CreateMock( update_metadata_pb2.DeltaArchiveManifest) @@ -195,7 +197,7 @@ class PayloadCheckerTest(mox.MoxTestBase): args = (msg, name, report, is_mandatory, is_submsg) kwargs = {'convert': convert, 'linebreak': linebreak, 'indent': indent} if is_mandatory and not is_present: - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckElem, *args, **kwargs) else: ret_val, ret_subreport = checker.PayloadChecker._CheckElem(*args, @@ -229,8 +231,7 @@ class PayloadCheckerTest(mox.MoxTestBase): # Test the method call. if is_mandatory and not is_present: - self.assertRaises(update_payload.PayloadError, tested_func, *args, - **kwargs) + self.assertRaises(PayloadError, tested_func, *args, **kwargs) else: ret_val = tested_func(*args, **kwargs) self.assertEquals(val if is_present else None, ret_val) @@ -254,7 +255,7 @@ class PayloadCheckerTest(mox.MoxTestBase): # Test the method call. if is_mandatory and not is_present: - self.assertRaises(update_payload.PayloadError, tested_func, *args) + self.assertRaises(PayloadError, tested_func, *args) else: ret_val, ret_subreport = tested_func(*args) self.assertEquals(val if is_present else None, ret_val) @@ -266,11 +267,9 @@ class PayloadCheckerTest(mox.MoxTestBase): None, None, 'foo', 'bar', 'baz')) self.assertIsNone(checker.PayloadChecker._CheckPresentIff( 'a', 'b', 'foo', 'bar', 'baz')) - self.assertRaises(update_payload.PayloadError, - checker.PayloadChecker._CheckPresentIff, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckPresentIff, 'a', None, 'foo', 'bar', 'baz') - self.assertRaises(update_payload.PayloadError, - checker.PayloadChecker._CheckPresentIff, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckPresentIff, None, 'b', 'foo', 'bar', 'baz') def DoCheckSha256SignatureTest(self, expect_pass, expect_subprocess_call, @@ -299,7 +298,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.assertIsNone(checker.PayloadChecker._CheckSha256Signature( sig_data, 'foo', expected_signed_hash, 'bar')) else: - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckSha256Signature, sig_data, 'foo', expected_signed_hash, 'bar') finally: @@ -359,31 +358,31 @@ class PayloadCheckerTest(mox.MoxTestBase): def testCheckBlocksFitLength_TooManyBlocks(self): """Tests _CheckBlocksFitLength(); fails due to excess blocks.""" - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 64, 5, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 60, 5, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 49, 5, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 48, 4, 16, 'foo') def testCheckBlocksFitLength_TooFewBlocks(self): """Tests _CheckBlocksFitLength(); fails due to insufficient blocks.""" - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 64, 3, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 60, 3, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 49, 3, 16, 'foo') - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, checker.PayloadChecker._CheckBlocksFitLength, 48, 2, 16, 'foo') @@ -476,8 +475,7 @@ class PayloadCheckerTest(mox.MoxTestBase): fail_old_rootfs_fs_size or fail_new_kernel_fs_size or fail_new_rootfs_fs_size) if should_fail: - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckManifest, report, + self.assertRaises(PayloadError, payload_checker._CheckManifest, report, rootfs_part_size, kernel_part_size) else: self.assertIsNone(payload_checker._CheckManifest(report, @@ -493,12 +491,10 @@ class PayloadCheckerTest(mox.MoxTestBase): self.assertIsNone(payload_checker._CheckLength( int(3.5 * block_size), 4, 'foo', 'bar')) # Fails, too few blocks. - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckLength, + self.assertRaises(PayloadError, payload_checker._CheckLength, int(3.5 * block_size), 3, 'foo', 'bar') # Fails, too many blocks. - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckLength, + self.assertRaises(PayloadError, payload_checker._CheckLength, int(3.5 * block_size), 5, 'foo', 'bar') def testCheckExtents(self): @@ -533,30 +529,26 @@ class PayloadCheckerTest(mox.MoxTestBase): # Fails, extent missing a start block. extents = self.NewExtentList((-1, 4), (8, 3), (1024, 16)) self.assertRaises( - update_payload.PayloadError, payload_checker._CheckExtents, - extents, (1024 + 16) * block_size, collections.defaultdict(int), - 'foo') + PayloadError, payload_checker._CheckExtents, extents, + (1024 + 16) * block_size, collections.defaultdict(int), 'foo') # Fails, extent missing block count. extents = self.NewExtentList((0, -1), (8, 3), (1024, 16)) self.assertRaises( - update_payload.PayloadError, payload_checker._CheckExtents, - extents, (1024 + 16) * block_size, collections.defaultdict(int), - 'foo') + PayloadError, payload_checker._CheckExtents, extents, + (1024 + 16) * block_size, collections.defaultdict(int), 'foo') # Fails, extent has zero blocks. extents = self.NewExtentList((0, 4), (8, 3), (1024, 0)) self.assertRaises( - update_payload.PayloadError, payload_checker._CheckExtents, - extents, (1024 + 16) * block_size, collections.defaultdict(int), - 'foo') + PayloadError, payload_checker._CheckExtents, extents, + (1024 + 16) * block_size, collections.defaultdict(int), 'foo') # Fails, extent exceeds partition boundaries. extents = self.NewExtentList((0, 4), (8, 3), (1024, 16)) self.assertRaises( - update_payload.PayloadError, payload_checker._CheckExtents, - extents, (1024 + 15) * block_size, collections.defaultdict(int), - 'foo') + PayloadError, payload_checker._CheckExtents, extents, + (1024 + 15) * block_size, collections.defaultdict(int), 'foo') def testCheckReplaceOperation(self): """Tests _CheckReplaceOperation() where op.type == REPLACE.""" @@ -578,22 +570,19 @@ class PayloadCheckerTest(mox.MoxTestBase): # Fail, src extents founds. op.src_extents = ['bar'] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, data_length, (data_length + block_size - 1) / block_size, 'foo') # Fail, missing data. op.src_extents = [] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, None, (data_length + block_size - 1) / block_size, 'foo') # Fail, length / block number mismatch. op.src_extents = ['bar'] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, data_length, (data_length + block_size - 1) / block_size + 1, 'foo') def testCheckReplaceBzOperation(self): @@ -616,22 +605,19 @@ class PayloadCheckerTest(mox.MoxTestBase): # Fail, src extents founds. op.src_extents = ['bar'] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, data_length, (data_length + block_size - 1) / block_size + 5, 'foo') # Fail, missing data. op.src_extents = [] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, None, (data_length + block_size - 1) / block_size, 'foo') # Fail, too few blocks to justify BZ. op.src_extents = [] self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckReplaceOperation, + PayloadError, payload_checker._CheckReplaceOperation, op, data_length, (data_length + block_size - 1) / block_size, 'foo') def testCheckMoveOperation_Pass(self): @@ -658,8 +644,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, 1024, 134, 134, 'foo') def testCheckMoveOperation_FailInsufficientSrcBlocks(self): @@ -673,8 +658,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckMoveOperation_FailInsufficientDstBlocks(self): @@ -688,8 +672,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 5))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckMoveOperation_FailExcessSrcBlocks(self): @@ -703,16 +686,14 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 5))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') self.AddToMessage(op.src_extents, self.NewExtentList((1, 4), (12, 2), (1024, 129))) self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckMoveOperation_FailExcessDstBlocks(self): @@ -726,8 +707,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((16, 128), (512, 7))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckMoveOperation_FailStagnantBlocks(self): @@ -741,8 +721,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((8, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckMoveOperation_FailZeroStartBlock(self): @@ -756,8 +735,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((8, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') self.AddToMessage(op.src_extents, @@ -765,8 +743,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.AddToMessage(op.dst_extents, self.NewExtentList((0, 128), (512, 6))) self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckMoveOperation, + PayloadError, payload_checker._CheckMoveOperation, op, None, 134, 134, 'foo') def testCheckAnyDiff(self): @@ -780,14 +757,12 @@ class PayloadCheckerTest(mox.MoxTestBase): # Fail, missing data blob. self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckAnyDiffOperation, + PayloadError, payload_checker._CheckAnyDiffOperation, op, None, 3, 'foo') # Fail, too big of a diff blob (unjustified). self.assertRaises( - update_payload.PayloadError, - payload_checker._CheckAnyDiffOperation, + PayloadError, payload_checker._CheckAnyDiffOperation, op, 10000, 2, 'foo') def testCheckSourceCopyOperation_Pass(self): @@ -799,15 +774,13 @@ class PayloadCheckerTest(mox.MoxTestBase): def testCheckSourceCopyOperation_FailContainsData(self): """Tests _CheckSourceCopyOperation(); message contains data.""" payload_checker = checker.PayloadChecker(self.MockPayload()) - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckSourceCopyOperation, + self.assertRaises(PayloadError, payload_checker._CheckSourceCopyOperation, 134, 0, 0, 'foo') def testCheckSourceCopyOperation_FailBlockCountsMismatch(self): """Tests _CheckSourceCopyOperation(); src and dst block totals not equal.""" payload_checker = checker.PayloadChecker(self.MockPayload()) - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckSourceCopyOperation, + self.assertRaises(PayloadError, payload_checker._CheckSourceCopyOperation, None, 0, 1, 'foo') def DoCheckOperationTest(self, op_type_name, is_last, allow_signature, @@ -942,8 +915,7 @@ class PayloadCheckerTest(mox.MoxTestBase): old_part_size, new_part_size, prev_data_offset, allow_signature, blob_hash_counts) if should_fail: - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckOperation, *args) + self.assertRaises(PayloadError, payload_checker._CheckOperation, *args) else: self.assertEqual(op.data_length if op.HasField('data_length') else 0, payload_checker._CheckOperation(*args)) @@ -965,6 +937,7 @@ class PayloadCheckerTest(mox.MoxTestBase): self.assertEqual(17, len(result)) def DoCheckOperationsTest(self, fail_nonexhaustive_full_update): + """Tests _CheckOperations().""" # Generate a test payload. For this test, we only care about one # (arbitrary) set of operations, so we'll only be generating kernel and # test with them. @@ -996,8 +969,7 @@ class PayloadCheckerTest(mox.MoxTestBase): args = (payload_checker.payload.manifest.install_operations, report, 'foo', 0, rootfs_part_size, rootfs_part_size, rootfs_part_size, 0, False) if fail_nonexhaustive_full_update: - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckOperations, *args) + self.assertRaises(PayloadError, payload_checker._CheckOperations, *args) else: self.assertEqual(rootfs_data_length, payload_checker._CheckOperations(*args)) @@ -1005,6 +977,7 @@ class PayloadCheckerTest(mox.MoxTestBase): def DoCheckSignaturesTest(self, fail_empty_sigs_blob, fail_missing_pseudo_op, fail_mismatched_pseudo_op, fail_sig_missing_fields, fail_unknown_sig_version, fail_incorrect_sig): + """Tests _CheckSignatures().""" # Generate a test payload. For this test, we only care about the signature # block and how it relates to the payload hash. Therefore, we're generating # a random (otherwise useless) payload for this purpose. @@ -1069,8 +1042,7 @@ class PayloadCheckerTest(mox.MoxTestBase): fail_unknown_sig_version or fail_incorrect_sig) args = (report, test_utils._PUBKEY_FILE_NAME) if should_fail: - self.assertRaises(update_payload.PayloadError, - payload_checker._CheckSignatures, *args) + self.assertRaises(PayloadError, payload_checker._CheckSignatures, *args) else: self.assertIsNone(payload_checker._CheckSignatures(*args)) @@ -1099,7 +1071,7 @@ class PayloadCheckerTest(mox.MoxTestBase): if should_succeed: self.assertIsNone(payload_checker._CheckManifestMinorVersion(*args)) else: - self.assertRaises(update_payload.PayloadError, + self.assertRaises(PayloadError, payload_checker._CheckManifestMinorVersion, *args) def DoRunTest(self, rootfs_part_size_provided, kernel_part_size_provided, @@ -1107,6 +1079,7 @@ class PayloadCheckerTest(mox.MoxTestBase): fail_mismatched_block_size, fail_excess_data, fail_rootfs_part_size_exceeded, fail_kernel_part_size_exceeded): + """Tests Run().""" # Generate a test payload. For this test, we generate a full update that # has sample kernel and rootfs operations. Since most testing is done with # internal PayloadChecker methods that are tested elsewhere, here we only @@ -1164,7 +1137,7 @@ class PayloadCheckerTest(mox.MoxTestBase): 'assert_type': 'delta' if fail_wrong_payload_type else 'full', 'block_size': use_block_size}} if fail_invalid_block_size: - self.assertRaises(update_payload.PayloadError, _GetPayloadChecker, + self.assertRaises(PayloadError, _GetPayloadChecker, payload_gen.WriteToFileWithData, **kwargs) else: payload_checker = _GetPayloadChecker(payload_gen.WriteToFileWithData, @@ -1178,8 +1151,7 @@ class PayloadCheckerTest(mox.MoxTestBase): fail_rootfs_part_size_exceeded or fail_kernel_part_size_exceeded) if should_fail: - self.assertRaises(update_payload.PayloadError, payload_checker.Run, - **kwargs) + self.assertRaises(PayloadError, payload_checker.Run, **kwargs) else: self.assertIsNone(payload_checker.Run(**kwargs)) |