releasetools: Add a common base class for unittests.

All the releasetools unittests extend the common base class of
test_utils.ReleaseToolsTestCase. Define tearDown() in the base class to
do the clean-up works.

Test: `pylint --rcfile=pylintrc test_*.py`
Test: `python -m unittest discover .`
Change-Id: I51775d964ef032dcdf3bb89c55e1a31371cde708
diff --git a/tools/releasetools/test_add_img_to_target_files.py b/tools/releasetools/test_add_img_to_target_files.py
index cc7b887..ad22b72 100644
--- a/tools/releasetools/test_add_img_to_target_files.py
+++ b/tools/releasetools/test_add_img_to_target_files.py
@@ -16,7 +16,6 @@
 
 import os
 import os.path
-import unittest
 import zipfile
 
 import common
@@ -30,14 +29,11 @@
 OPTIONS = common.OPTIONS
 
 
-class AddImagesToTargetFilesTest(unittest.TestCase):
+class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
 
   def setUp(self):
     OPTIONS.input_tmp = common.MakeTempDir()
 
-  def tearDown(self):
-    common.Cleanup()
-
   def _verifyCareMap(self, expected, file_name):
     """Parses the care_map.pb; and checks the content in plain text."""
     text_file = common.MakeTempFile(prefix="caremap-", suffix=".txt")
diff --git a/tools/releasetools/test_blockimgdiff.py b/tools/releasetools/test_blockimgdiff.py
index 124b4d5..857026e 100644
--- a/tools/releasetools/test_blockimgdiff.py
+++ b/tools/releasetools/test_blockimgdiff.py
@@ -14,17 +14,14 @@
 # limitations under the License.
 #
 
-from __future__ import print_function
-
-import unittest
-
 import common
-from blockimgdiff import (BlockImageDiff, EmptyImage, HeapItem, ImgdiffStats,
-                          Transfer)
+from blockimgdiff import (
+    BlockImageDiff, EmptyImage, HeapItem, ImgdiffStats, Transfer)
 from rangelib import RangeSet
+from test_utils import ReleaseToolsTestCase
 
 
-class HealpItemTest(unittest.TestCase):
+class HealpItemTest(ReleaseToolsTestCase):
 
   class Item(object):
     def __init__(self, score):
@@ -54,7 +51,7 @@
     self.assertFalse(item)
 
 
-class BlockImageDiffTest(unittest.TestCase):
+class BlockImageDiffTest(ReleaseToolsTestCase):
 
   def test_GenerateDigraphOrder(self):
     """Make sure GenerateDigraph preserves the order.
@@ -245,7 +242,7 @@
         block_image_diff.imgdiff_stats.stats)
 
 
-class ImgdiffStatsTest(unittest.TestCase):
+class ImgdiffStatsTest(ReleaseToolsTestCase):
 
   def test_Log(self):
     imgdiff_stats = ImgdiffStats()
diff --git a/tools/releasetools/test_build_image.py b/tools/releasetools/test_build_image.py
index a2df278..6f853e8 100644
--- a/tools/releasetools/test_build_image.py
+++ b/tools/releasetools/test_build_image.py
@@ -18,15 +18,15 @@
 import math
 import os.path
 import random
-import unittest
 
 import common
 from build_image import (
     AVBCalcMinPartitionSize, BLOCK_SIZE, BuildImageError, CheckHeadroom,
     SetUpInDirAndFsConfig)
+from test_utils import ReleaseToolsTestCase
 
 
-class BuildImageTest(unittest.TestCase):
+class BuildImageTest(ReleaseToolsTestCase):
 
   # Available: 1000 blocks.
   EXT4FS_OUTPUT = (
@@ -39,9 +39,6 @@
     self._image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset
                          for offset in range(BLOCK_SIZE)]
 
-  def tearDown(self):
-    common.Cleanup()
-
   def test_CheckHeadroom_SizeUnderLimit(self):
     # Required headroom: 1000 blocks.
     prop_dict = {
diff --git a/tools/releasetools/test_common.py b/tools/releasetools/test_common.py
index ec86eb2..c99049a 100644
--- a/tools/releasetools/test_common.py
+++ b/tools/releasetools/test_common.py
@@ -19,7 +19,6 @@
 import subprocess
 import tempfile
 import time
-import unittest
 import zipfile
 from hashlib import sha1
 
@@ -44,7 +43,8 @@
     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,
               test_file_name=None, expected_stat=None, expected_mode=0o644,
               expected_compress_type=zipfile.ZIP_STORED):
@@ -359,7 +359,7 @@
       os.remove(zip_file.name)
 
 
-class CommonApkUtilsTest(unittest.TestCase):
+class CommonApkUtilsTest(test_utils.ReleaseToolsTestCase):
   """Tests the APK utils related functions."""
 
   APKCERTS_TXT1 = (
@@ -407,9 +407,6 @@
   def setUp(self):
     self.testdata_dir = test_utils.get_testdata_dir()
 
-  def tearDown(self):
-    common.Cleanup()
-
   @staticmethod
   def _write_apkcerts_txt(apkcerts_txt, additional=None):
     if additional is None:
@@ -523,14 +520,11 @@
         {})
 
 
-class CommonUtilsTest(unittest.TestCase):
+class CommonUtilsTest(test_utils.ReleaseToolsTestCase):
 
   def setUp(self):
     self.testdata_dir = test_utils.get_testdata_dir()
 
-  def tearDown(self):
-    common.Cleanup()
-
   def test_GetSparseImage_emptyBlockMapFile(self):
     target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip')
     with zipfile.ZipFile(target_files, 'w') as target_files_zip:
@@ -935,7 +929,7 @@
           AssertionError, common.LoadInfoDict, target_files_zip, True)
 
 
-class InstallRecoveryScriptFormatTest(unittest.TestCase):
+class InstallRecoveryScriptFormatTest(test_utils.ReleaseToolsTestCase):
   """Checks the format of install-recovery.sh.
 
   Its format should match between common.py and validate_target_files.py.
@@ -994,6 +988,3 @@
                              recovery_image, boot_image, self._info)
     validate_target_files.ValidateInstallRecoveryScript(self._tempdir,
                                                         self._info)
-
-  def tearDown(self):
-    common.Cleanup()
diff --git a/tools/releasetools/test_ota_from_target_files.py b/tools/releasetools/test_ota_from_target_files.py
index 29e0d83..f75b3a7 100644
--- a/tools/releasetools/test_ota_from_target_files.py
+++ b/tools/releasetools/test_ota_from_target_files.py
@@ -17,7 +17,6 @@
 import copy
 import os
 import os.path
-import unittest
 import zipfile
 
 import common
@@ -104,7 +103,7 @@
     self.script.append(('AssertSomeThumbprint',) + args)
 
 
-class BuildInfoTest(unittest.TestCase):
+class BuildInfoTest(test_utils.ReleaseToolsTestCase):
 
   TEST_INFO_DICT = {
       'build.prop' : {
@@ -352,10 +351,7 @@
         script_writer.script)
 
 
-class LoadOemDictsTest(unittest.TestCase):
-
-  def tearDown(self):
-    common.Cleanup()
+class LoadOemDictsTest(test_utils.ReleaseToolsTestCase):
 
   def test_NoneDict(self):
     self.assertIsNone(_LoadOemDicts(None))
@@ -388,7 +384,7 @@
       self.assertEqual('{}'.format(i), oem_dict['ro.build.index'])
 
 
-class OtaFromTargetFilesTest(unittest.TestCase):
+class OtaFromTargetFilesTest(test_utils.ReleaseToolsTestCase):
 
   TEST_TARGET_INFO_DICT = {
       'build.prop' : {
@@ -430,9 +426,6 @@
     common.OPTIONS.search_path = test_utils.get_search_path()
     self.assertIsNotNone(common.OPTIONS.search_path)
 
-  def tearDown(self):
-    common.Cleanup()
-
   def test_GetPackageMetadata_abOta_full(self):
     target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
     target_info_dict['ab_update'] = 'true'
@@ -720,14 +713,11 @@
     )
 
 
-class PropertyFilesTest(unittest.TestCase):
+class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
 
   def setUp(self):
     common.OPTIONS.no_signing = False
 
-  def tearDown(self):
-    common.Cleanup()
-
   @staticmethod
   def construct_zip_package(entries):
     zip_file = common.MakeTempFile(suffix='.zip')
@@ -1151,7 +1141,7 @@
       property_files.Verify(zip_fp, raw_metadata)
 
 
-class PayloadSignerTest(unittest.TestCase):
+class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
 
   SIGFILE = 'sigfile.bin'
   SIGNED_SIGFILE = 'signed-sigfile.bin'
@@ -1167,9 +1157,6 @@
         common.OPTIONS.package_key : None,
     }
 
-  def tearDown(self):
-    common.Cleanup()
-
   def _assertFilesEqual(self, file1, file2):
     with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
       self.assertEqual(fp1.read(), fp2.read())
@@ -1230,7 +1217,7 @@
     self._assertFilesEqual(verify_file, signed_file)
 
 
-class PayloadTest(unittest.TestCase):
+class PayloadTest(test_utils.ReleaseToolsTestCase):
 
   def setUp(self):
     self.testdata_dir = test_utils.get_testdata_dir()
@@ -1244,9 +1231,6 @@
         common.OPTIONS.package_key : None,
     }
 
-  def tearDown(self):
-    common.Cleanup()
-
   @staticmethod
   def _create_payload_full(secondary=False):
     target_file = construct_target_files(secondary)
diff --git a/tools/releasetools/test_rangelib.py b/tools/releasetools/test_rangelib.py
index e181187..1251e11 100644
--- a/tools/releasetools/test_rangelib.py
+++ b/tools/releasetools/test_rangelib.py
@@ -14,11 +14,11 @@
 # limitations under the License.
 #
 
-import unittest
-
 from rangelib import RangeSet
+from test_utils import ReleaseToolsTestCase
 
-class RangeSetTest(unittest.TestCase):
+
+class RangeSetTest(ReleaseToolsTestCase):
 
   def test_union(self):
     self.assertEqual(RangeSet("10-19 30-34").union(RangeSet("18-29")),
@@ -129,8 +129,8 @@
     self.assertEqual(
         RangeSet.parse_raw(RangeSet("0-9").to_string_raw()),
         RangeSet("0-9"))
-    self.assertEqual(RangeSet.parse_raw(
-        RangeSet("2-10 12").to_string_raw()),
+    self.assertEqual(
+        RangeSet.parse_raw(RangeSet("2-10 12").to_string_raw()),
         RangeSet("2-10 12"))
     self.assertEqual(
         RangeSet.parse_raw(RangeSet("11 2-10 12 1 0").to_string_raw()),
diff --git a/tools/releasetools/test_sign_target_files_apks.py b/tools/releasetools/test_sign_target_files_apks.py
index ac1b567..18762ee 100644
--- a/tools/releasetools/test_sign_target_files_apks.py
+++ b/tools/releasetools/test_sign_target_files_apks.py
@@ -14,11 +14,8 @@
 # limitations under the License.
 #
 
-from __future__ import print_function
-
 import base64
 import os.path
-import unittest
 import zipfile
 
 import common
@@ -28,7 +25,7 @@
     ReplaceVerityKeyId, RewriteProps)
 
 
-class SignTargetFilesApksTest(unittest.TestCase):
+class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase):
 
   MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?>
 <policy>
@@ -39,9 +36,6 @@
   def setUp(self):
     self.testdata_dir = test_utils.get_testdata_dir()
 
-  def tearDown(self):
-    common.Cleanup()
-
   def test_EditTags(self):
     self.assertEqual(EditTags('dev-keys'), ('release-keys'))
     self.assertEqual(EditTags('test-keys'), ('release-keys'))
diff --git a/tools/releasetools/test_utils.py b/tools/releasetools/test_utils.py
index a15ff5b..b9c8dc7 100644
--- a/tools/releasetools/test_utils.py
+++ b/tools/releasetools/test_utils.py
@@ -21,6 +21,7 @@
 import os
 import os.path
 import struct
+import unittest
 
 import common
 
@@ -110,3 +111,10 @@
         fp.write(os.urandom(data_size))
 
   return sparse_image
+
+
+class ReleaseToolsTestCase(unittest.TestCase):
+  """A common base class for all the releasetools unittests."""
+
+  def tearDown(self):
+    common.Cleanup()
diff --git a/tools/releasetools/test_validate_target_files.py b/tools/releasetools/test_validate_target_files.py
index ecb7fde..7b29ef8 100644
--- a/tools/releasetools/test_validate_target_files.py
+++ b/tools/releasetools/test_validate_target_files.py
@@ -16,12 +16,9 @@
 
 """Unittests for validate_target_files.py."""
 
-from __future__ import print_function
-
 import os
 import os.path
 import shutil
-import unittest
 
 import build_image
 import common
@@ -29,14 +26,11 @@
 from validate_target_files import ValidateVerifiedBootImages
 
 
-class ValidateTargetFilesTest(unittest.TestCase):
+class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase):
 
   def setUp(self):
     self.testdata_dir = test_utils.get_testdata_dir()
 
-  def tearDown(self):
-    common.Cleanup()
-
   def _generate_boot_image(self, output_file):
     kernel = common.MakeTempFile(prefix='kernel-')
     with open(kernel, 'wb') as kernel_fp:
diff --git a/tools/releasetools/test_verity_utils.py b/tools/releasetools/test_verity_utils.py
index f318b02..a9cd17b 100644
--- a/tools/releasetools/test_verity_utils.py
+++ b/tools/releasetools/test_verity_utils.py
@@ -16,25 +16,22 @@
 
 """Unittests for verity_utils.py."""
 
-from __future__ import print_function
-
 import os.path
-import unittest
 
 import build_image
 import common
 import sparse_img
-import test_utils
 from rangelib import RangeSet
+from test_utils import get_testdata_dir, ReleaseToolsTestCase
 from verity_utils import (
     CreateHashtreeInfoGenerator, HashtreeInfo,
     VerifiedBootVersion1HashtreeInfoGenerator)
 
 
-class VerifiedBootVersion1HashtreeInfoGeneratorTest(unittest.TestCase):
+class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase):
 
   def setUp(self):
-    self.testdata_dir = test_utils.get_testdata_dir()
+    self.testdata_dir = get_testdata_dir()
 
     self.partition_size = 1024 * 1024
     self.prop_dict = {
@@ -50,9 +47,6 @@
     self.expected_root_hash = \
         "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d"
 
-  def tearDown(self):
-    common.Cleanup()
-
   def _create_simg(self, raw_data):
     output_file = common.MakeTempFile()
     raw_image = common.MakeTempFile()