Merge "releasetools: Add a common base class for unittests."

This commit is contained in:
Tao Bao 2018-10-12 18:33:56 +00:00 committed by Gerrit Code Review
commit e95680a60f
10 changed files with 38 additions and 83 deletions

View File

@ -16,7 +16,6 @@
import os import os
import os.path import os.path
import unittest
import zipfile import zipfile
import common import common
@ -30,14 +29,11 @@ from rangelib import RangeSet
OPTIONS = common.OPTIONS OPTIONS = common.OPTIONS
class AddImagesToTargetFilesTest(unittest.TestCase): class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
def setUp(self): def setUp(self):
OPTIONS.input_tmp = common.MakeTempDir() OPTIONS.input_tmp = common.MakeTempDir()
def tearDown(self):
common.Cleanup()
def _verifyCareMap(self, expected, file_name): def _verifyCareMap(self, expected, file_name):
"""Parses the care_map.pb; and checks the content in plain text.""" """Parses the care_map.pb; and checks the content in plain text."""
text_file = common.MakeTempFile(prefix="caremap-", suffix=".txt") text_file = common.MakeTempFile(prefix="caremap-", suffix=".txt")

View File

@ -14,17 +14,14 @@
# limitations under the License. # limitations under the License.
# #
from __future__ import print_function
import unittest
import common import common
from blockimgdiff import (BlockImageDiff, EmptyImage, HeapItem, ImgdiffStats, from blockimgdiff import (
Transfer) BlockImageDiff, EmptyImage, HeapItem, ImgdiffStats, Transfer)
from rangelib import RangeSet from rangelib import RangeSet
from test_utils import ReleaseToolsTestCase
class HealpItemTest(unittest.TestCase): class HealpItemTest(ReleaseToolsTestCase):
class Item(object): class Item(object):
def __init__(self, score): def __init__(self, score):
@ -54,7 +51,7 @@ class HealpItemTest(unittest.TestCase):
self.assertFalse(item) self.assertFalse(item)
class BlockImageDiffTest(unittest.TestCase): class BlockImageDiffTest(ReleaseToolsTestCase):
def test_GenerateDigraphOrder(self): def test_GenerateDigraphOrder(self):
"""Make sure GenerateDigraph preserves the order. """Make sure GenerateDigraph preserves the order.
@ -245,7 +242,7 @@ class BlockImageDiffTest(unittest.TestCase):
block_image_diff.imgdiff_stats.stats) block_image_diff.imgdiff_stats.stats)
class ImgdiffStatsTest(unittest.TestCase): class ImgdiffStatsTest(ReleaseToolsTestCase):
def test_Log(self): def test_Log(self):
imgdiff_stats = ImgdiffStats() imgdiff_stats = ImgdiffStats()

View File

@ -18,15 +18,15 @@ import filecmp
import math import math
import os.path import os.path
import random import random
import unittest
import common import common
from build_image import ( from build_image import (
AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom, AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom,
SetUpInDirAndFsConfig) SetUpInDirAndFsConfig)
from test_utils import ReleaseToolsTestCase
class BuildImageTest(unittest.TestCase): class BuildImageTest(ReleaseToolsTestCase):
# Available: 1000 blocks. # Available: 1000 blocks.
EXT4FS_OUTPUT = ( EXT4FS_OUTPUT = (
@ -39,9 +39,6 @@ class BuildImageTest(unittest.TestCase):
self._image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset self._image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset
for offset in range(BLOCK_SIZE)] for offset in range(BLOCK_SIZE)]
def tearDown(self):
common.Cleanup()
def test_CheckHeadroom_SizeUnderLimit(self): def test_CheckHeadroom_SizeUnderLimit(self):
# Required headroom: 1000 blocks. # Required headroom: 1000 blocks.
prop_dict = { prop_dict = {

View File

@ -19,7 +19,6 @@ import os
import subprocess import subprocess
import tempfile import tempfile
import time import time
import unittest
import zipfile import zipfile
from hashlib import sha1 from hashlib import sha1
@ -44,7 +43,8 @@ def get_2gb_string():
yield '\0' * (step_size - block_size) yield '\0' * (step_size - block_size)
class CommonZipTest(unittest.TestCase): class CommonZipTest(test_utils.ReleaseToolsTestCase):
def _verify(self, zip_file, zip_file_name, arcname, expected_hash, def _verify(self, zip_file, zip_file_name, arcname, expected_hash,
test_file_name=None, expected_stat=None, expected_mode=0o644, test_file_name=None, expected_stat=None, expected_mode=0o644,
expected_compress_type=zipfile.ZIP_STORED): expected_compress_type=zipfile.ZIP_STORED):
@ -359,7 +359,7 @@ class CommonZipTest(unittest.TestCase):
os.remove(zip_file.name) os.remove(zip_file.name)
class CommonApkUtilsTest(unittest.TestCase): class CommonApkUtilsTest(test_utils.ReleaseToolsTestCase):
"""Tests the APK utils related functions.""" """Tests the APK utils related functions."""
APKCERTS_TXT1 = ( APKCERTS_TXT1 = (
@ -407,9 +407,6 @@ class CommonApkUtilsTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = test_utils.get_testdata_dir()
def tearDown(self):
common.Cleanup()
@staticmethod @staticmethod
def _write_apkcerts_txt(apkcerts_txt, additional=None): def _write_apkcerts_txt(apkcerts_txt, additional=None):
if additional is None: if additional is None:
@ -523,14 +520,11 @@ class CommonApkUtilsTest(unittest.TestCase):
{}) {})
class CommonUtilsTest(unittest.TestCase): class CommonUtilsTest(test_utils.ReleaseToolsTestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = test_utils.get_testdata_dir()
def tearDown(self):
common.Cleanup()
def test_GetSparseImage_emptyBlockMapFile(self): def test_GetSparseImage_emptyBlockMapFile(self):
target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip')
with zipfile.ZipFile(target_files, 'w') as target_files_zip: with zipfile.ZipFile(target_files, 'w') as target_files_zip:
@ -935,7 +929,7 @@ class CommonUtilsTest(unittest.TestCase):
AssertionError, common.LoadInfoDict, target_files_zip, True) AssertionError, common.LoadInfoDict, target_files_zip, True)
class InstallRecoveryScriptFormatTest(unittest.TestCase): class InstallRecoveryScriptFormatTest(test_utils.ReleaseToolsTestCase):
"""Checks the format of install-recovery.sh. """Checks the format of install-recovery.sh.
Its format should match between common.py and validate_target_files.py. Its format should match between common.py and validate_target_files.py.
@ -994,6 +988,3 @@ class InstallRecoveryScriptFormatTest(unittest.TestCase):
recovery_image, boot_image, self._info) recovery_image, boot_image, self._info)
validate_target_files.ValidateInstallRecoveryScript(self._tempdir, validate_target_files.ValidateInstallRecoveryScript(self._tempdir,
self._info) self._info)
def tearDown(self):
common.Cleanup()

View File

@ -17,7 +17,6 @@
import copy import copy
import os import os
import os.path import os.path
import unittest
import zipfile import zipfile
import common import common
@ -104,7 +103,7 @@ class MockScriptWriter(object):
self.script.append(('AssertSomeThumbprint',) + args) self.script.append(('AssertSomeThumbprint',) + args)
class BuildInfoTest(unittest.TestCase): class BuildInfoTest(test_utils.ReleaseToolsTestCase):
TEST_INFO_DICT = { TEST_INFO_DICT = {
'build.prop' : { 'build.prop' : {
@ -352,10 +351,7 @@ class BuildInfoTest(unittest.TestCase):
script_writer.script) script_writer.script)
class LoadOemDictsTest(unittest.TestCase): class LoadOemDictsTest(test_utils.ReleaseToolsTestCase):
def tearDown(self):
common.Cleanup()
def test_NoneDict(self): def test_NoneDict(self):
self.assertIsNone(_LoadOemDicts(None)) self.assertIsNone(_LoadOemDicts(None))
@ -388,7 +384,7 @@ class LoadOemDictsTest(unittest.TestCase):
self.assertEqual('{}'.format(i), oem_dict['ro.build.index']) self.assertEqual('{}'.format(i), oem_dict['ro.build.index'])
class OtaFromTargetFilesTest(unittest.TestCase): class OtaFromTargetFilesTest(test_utils.ReleaseToolsTestCase):
TEST_TARGET_INFO_DICT = { TEST_TARGET_INFO_DICT = {
'build.prop' : { 'build.prop' : {
@ -430,9 +426,6 @@ class OtaFromTargetFilesTest(unittest.TestCase):
common.OPTIONS.search_path = test_utils.get_search_path() common.OPTIONS.search_path = test_utils.get_search_path()
self.assertIsNotNone(common.OPTIONS.search_path) self.assertIsNotNone(common.OPTIONS.search_path)
def tearDown(self):
common.Cleanup()
def test_GetPackageMetadata_abOta_full(self): def test_GetPackageMetadata_abOta_full(self):
target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
target_info_dict['ab_update'] = 'true' target_info_dict['ab_update'] = 'true'
@ -720,14 +713,11 @@ class TestPropertyFiles(PropertyFiles):
) )
class PropertyFilesTest(unittest.TestCase): class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
def setUp(self): def setUp(self):
common.OPTIONS.no_signing = False common.OPTIONS.no_signing = False
def tearDown(self):
common.Cleanup()
@staticmethod @staticmethod
def construct_zip_package(entries): def construct_zip_package(entries):
zip_file = common.MakeTempFile(suffix='.zip') zip_file = common.MakeTempFile(suffix='.zip')
@ -1151,7 +1141,7 @@ class NonAbOtaPropertyFilesTest(PropertyFilesTest):
property_files.Verify(zip_fp, raw_metadata) property_files.Verify(zip_fp, raw_metadata)
class PayloadSignerTest(unittest.TestCase): class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
SIGFILE = 'sigfile.bin' SIGFILE = 'sigfile.bin'
SIGNED_SIGFILE = 'signed-sigfile.bin' SIGNED_SIGFILE = 'signed-sigfile.bin'
@ -1167,9 +1157,6 @@ class PayloadSignerTest(unittest.TestCase):
common.OPTIONS.package_key : None, common.OPTIONS.package_key : None,
} }
def tearDown(self):
common.Cleanup()
def _assertFilesEqual(self, file1, file2): def _assertFilesEqual(self, file1, file2):
with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2: with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
self.assertEqual(fp1.read(), fp2.read()) self.assertEqual(fp1.read(), fp2.read())
@ -1230,7 +1217,7 @@ class PayloadSignerTest(unittest.TestCase):
self._assertFilesEqual(verify_file, signed_file) self._assertFilesEqual(verify_file, signed_file)
class PayloadTest(unittest.TestCase): class PayloadTest(test_utils.ReleaseToolsTestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = test_utils.get_testdata_dir()
@ -1244,9 +1231,6 @@ class PayloadTest(unittest.TestCase):
common.OPTIONS.package_key : None, common.OPTIONS.package_key : None,
} }
def tearDown(self):
common.Cleanup()
@staticmethod @staticmethod
def _create_payload_full(secondary=False): def _create_payload_full(secondary=False):
target_file = construct_target_files(secondary) target_file = construct_target_files(secondary)

View File

@ -14,11 +14,11 @@
# limitations under the License. # limitations under the License.
# #
import unittest
from rangelib import RangeSet from rangelib import RangeSet
from test_utils import ReleaseToolsTestCase
class RangeSetTest(unittest.TestCase):
class RangeSetTest(ReleaseToolsTestCase):
def test_union(self): def test_union(self):
self.assertEqual(RangeSet("10-19 30-34").union(RangeSet("18-29")), self.assertEqual(RangeSet("10-19 30-34").union(RangeSet("18-29")),
@ -129,8 +129,8 @@ class RangeSetTest(unittest.TestCase):
self.assertEqual( self.assertEqual(
RangeSet.parse_raw(RangeSet("0-9").to_string_raw()), RangeSet.parse_raw(RangeSet("0-9").to_string_raw()),
RangeSet("0-9")) RangeSet("0-9"))
self.assertEqual(RangeSet.parse_raw( self.assertEqual(
RangeSet("2-10 12").to_string_raw()), RangeSet.parse_raw(RangeSet("2-10 12").to_string_raw()),
RangeSet("2-10 12")) RangeSet("2-10 12"))
self.assertEqual( self.assertEqual(
RangeSet.parse_raw(RangeSet("11 2-10 12 1 0").to_string_raw()), RangeSet.parse_raw(RangeSet("11 2-10 12 1 0").to_string_raw()),

View File

@ -14,11 +14,8 @@
# limitations under the License. # limitations under the License.
# #
from __future__ import print_function
import base64 import base64
import os.path import os.path
import unittest
import zipfile import zipfile
import common import common
@ -28,7 +25,7 @@ from sign_target_files_apks import (
ReplaceVerityKeyId, RewriteProps) ReplaceVerityKeyId, RewriteProps)
class SignTargetFilesApksTest(unittest.TestCase): class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase):
MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?> MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?>
<policy> <policy>
@ -39,9 +36,6 @@ class SignTargetFilesApksTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = test_utils.get_testdata_dir()
def tearDown(self):
common.Cleanup()
def test_EditTags(self): def test_EditTags(self):
self.assertEqual(EditTags('dev-keys'), ('release-keys')) self.assertEqual(EditTags('dev-keys'), ('release-keys'))
self.assertEqual(EditTags('test-keys'), ('release-keys')) self.assertEqual(EditTags('test-keys'), ('release-keys'))

View File

@ -21,6 +21,7 @@ Utils for running unittests.
import os import os
import os.path import os.path
import struct import struct
import unittest
import common import common
@ -110,3 +111,10 @@ def construct_sparse_image(chunks):
fp.write(os.urandom(data_size)) fp.write(os.urandom(data_size))
return sparse_image return sparse_image
class ReleaseToolsTestCase(unittest.TestCase):
"""A common base class for all the releasetools unittests."""
def tearDown(self):
common.Cleanup()

View File

@ -16,12 +16,9 @@
"""Unittests for validate_target_files.py.""" """Unittests for validate_target_files.py."""
from __future__ import print_function
import os import os
import os.path import os.path
import shutil import shutil
import unittest
import build_image import build_image
import common import common
@ -29,14 +26,11 @@ import test_utils
from validate_target_files import ValidateVerifiedBootImages from validate_target_files import ValidateVerifiedBootImages
class ValidateTargetFilesTest(unittest.TestCase): class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = test_utils.get_testdata_dir()
def tearDown(self):
common.Cleanup()
def _generate_boot_image(self, output_file): def _generate_boot_image(self, output_file):
kernel = common.MakeTempFile(prefix='kernel-') kernel = common.MakeTempFile(prefix='kernel-')
with open(kernel, 'wb') as kernel_fp: with open(kernel, 'wb') as kernel_fp:

View File

@ -16,25 +16,22 @@
"""Unittests for verity_utils.py.""" """Unittests for verity_utils.py."""
from __future__ import print_function
import os.path import os.path
import unittest
import build_image import build_image
import common import common
import sparse_img import sparse_img
import test_utils
from rangelib import RangeSet from rangelib import RangeSet
from test_utils import get_testdata_dir, ReleaseToolsTestCase
from verity_utils import ( from verity_utils import (
CreateHashtreeInfoGenerator, HashtreeInfo, CreateHashtreeInfoGenerator, HashtreeInfo,
VerifiedBootVersion1HashtreeInfoGenerator) VerifiedBootVersion1HashtreeInfoGenerator)
class VerifiedBootVersion1HashtreeInfoGeneratorTest(unittest.TestCase): class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase):
def setUp(self): def setUp(self):
self.testdata_dir = test_utils.get_testdata_dir() self.testdata_dir = get_testdata_dir()
self.partition_size = 1024 * 1024 self.partition_size = 1024 * 1024
self.prop_dict = { self.prop_dict = {
@ -50,9 +47,6 @@ class VerifiedBootVersion1HashtreeInfoGeneratorTest(unittest.TestCase):
self.expected_root_hash = \ self.expected_root_hash = \
"0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d" "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d"
def tearDown(self):
common.Cleanup()
def _create_simg(self, raw_data): def _create_simg(self, raw_data):
output_file = common.MakeTempFile() output_file = common.MakeTempFile()
raw_image = common.MakeTempFile() raw_image = common.MakeTempFile()