generalizing unittests and adding some more
authorEduardo Robles Elvira <edulix@wadobo.com>
Wed, 26 Jun 2013 08:51:53 +0000 (10:51 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Wed, 26 Jun 2013 08:55:52 +0000 (10:55 +0200)
runtests.py
testing/test_multivol.py

index d07a277..4610920 100644 (file)
@@ -2,7 +2,7 @@
 
 import unittest
 
-from testing.test_multivol import MultivolTest
+from testing.test_multivol import MultivolGnuFormatTest, MultivolPaxFormatTest
 
 if __name__ == "__main__":
     unittest.main()
\ No newline at end of file
index 1a3a2ff..2d4dd53 100644 (file)
@@ -1,19 +1,34 @@
 import os, unittest, hashlib, string
 
-from deltatar.tarfile import TarFile, PAX_FORMAT
+from deltatar.tarfile import TarFile, PAX_FORMAT, GNU_FORMAT
 
 def new_volume_handler(tarobj, base_name, volume_number):
+    '''
+    Handles the new volumes
+    '''
     volume_path = "%s.%d" % (base_name, volume_number)
     tarobj.open_volume(volume_path)
 
 
-class MultivolTest(unittest.TestCase):
-    """Test multivolume support in tarfile"""
+class MultivolGnuFormatTest(unittest.TestCase):
+    """
+    Test multivolume support in tarfile. Tar Format is specified at class level.
+    """
+
+    tar_command_format = "gnu"
+    tarfile_format = GNU_FORMAT
 
     def tearDown(self):
+        '''
+        Remove temporal files created by unit tests
+        '''
         os.system("rm -rf big small small2 sample.tar*")
 
     def create_file(self, path, length):
+        '''
+        Creates a file with some gibberish inside, returning the md5sum of that
+        file. File path and length are specified as function arguments.
+        '''
         f = open(path, 'w')
         s = string.lowercase + string.digits + "\n"
         if len(s) < length:
@@ -24,6 +39,9 @@ class MultivolTest(unittest.TestCase):
         return self.md5sum(path)
 
     def md5sum(self, filename):
+        '''
+        Returns the md5sum of a file specified by its filename/path
+        '''
         md5 = hashlib.md5()
         with open(filename,'rb') as f:
             for chunk in iter(lambda: f.read(128*md5.block_size), b''):
@@ -31,14 +49,15 @@ class MultivolTest(unittest.TestCase):
         return md5.hexdigest()
 
     def test_no_volume(self):
-        """Test normal tarfile creation, no volumes """
+        """
+        Create a tar file with only one file inside and no extra volumes
+        """
 
         # create the content of the file to compress and hash it
         hash = self.create_file("big", 50000)
 
         # create the tar file with volumes
-        tarobj = TarFile.open("sample.tar",
-                           mode="w")
+        tarobj = TarFile.open("sample.tar", mode="w", format=self.tarfile_format)
         tarobj.add("big")
         tarobj.close()
 
@@ -55,7 +74,9 @@ class MultivolTest(unittest.TestCase):
         assert hash == self.md5sum("big")
 
     def test_volume_creation1(self):
-        """Test volumes creation"""
+        """
+        Create a tar file with two volumes, only one file inside
+        """
 
         # create the content of the file to compress and hash it
         hash = self.create_file("big", 50000)
@@ -63,6 +84,7 @@ class MultivolTest(unittest.TestCase):
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
                               mode="w",
+                              format=self.tarfile_format,
                               max_volume_size=30000,
                               new_volume_handler=new_volume_handler)
         tarobj.add("big")
@@ -82,7 +104,9 @@ class MultivolTest(unittest.TestCase):
         assert hash == self.md5sum("big")
 
     def test_volume_creation2(self):
-        """Test volumes creation with two volumes"""
+        """
+        Create a tar file with 2 extra volumes, only one file inside
+        """
 
         # create the content of the file to compress and hash it
         hash = self.create_file("big", 50000)
@@ -90,6 +114,7 @@ class MultivolTest(unittest.TestCase):
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
                               mode="w",
+                              format=self.tarfile_format,
                               max_volume_size=20000,
                               new_volume_handler=new_volume_handler)
         tarobj.add("big")
@@ -109,8 +134,10 @@ class MultivolTest(unittest.TestCase):
         assert os.path.exists("big")
         assert hash == self.md5sum("big")
 
-    def test_multiple_files_volumes(self):
-        # create the content of the file to compress and hash it
+    def test_multivol_multifiles(self):
+        '''
+        Create a tar file with two volumes and three files inside
+        '''
 
         # create sample data
         hash = dict()
@@ -121,6 +148,7 @@ class MultivolTest(unittest.TestCase):
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
                               mode="w",
+                              format=self.tarfile_format,
                               max_volume_size=20000,
                               new_volume_handler=new_volume_handler)
         tarobj.add("big")
@@ -146,7 +174,7 @@ class MultivolTest(unittest.TestCase):
 
     def test_volume_extract1(self):
         '''
-        Create a volume and extract it
+        Create a tar file with multiple volumes and one file and extract it
         '''
         # create the content of the file to compress and hash it
         hash = self.create_file("big", 5*1024*1024)
@@ -154,6 +182,7 @@ class MultivolTest(unittest.TestCase):
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
                               mode="w",
+                              format=self.tarfile_format,
                               max_volume_size=3*1024*1024,
                               new_volume_handler=new_volume_handler)
         tarobj.add("big")
@@ -178,13 +207,15 @@ class MultivolTest(unittest.TestCase):
 
     def test_volume_extract2(self):
         '''
-        Create a volume with gnu tar command and extract it with our tarfiel lib
+        Create a multivolume tar file with gnu tar command, extract it with
+        tarfile library
         '''
         # create the content of the file to compress and hash it
         hash = self.create_file("big", 5*1024*1024)
 
         # create the tar file with volumes
-        os.system("tar cM -L 3M big --file=sample.tar --file=sample.tar.1")
+        os.system("tar cM --format=%s -L 3M big --file=sample.tar "\
+            "--file=sample.tar.1" % self.tar_command_format)
 
         # check that the tar volumes were correctly created
         assert os.path.exists("sample.tar")
@@ -203,8 +234,10 @@ class MultivolTest(unittest.TestCase):
         assert os.path.exists("big")
         assert hash == self.md5sum("big")
 
-    def test_multiple_files_volumes_extract(self):
-        # creates a multivolume tar file with multiple files and extracts it
+    def test_multivol_multifile_extract(self):
+        '''
+        create a multivolume tar file with multiple files and extracts it
+        '''
 
         # create sample data
         hash = dict()
@@ -215,6 +248,7 @@ class MultivolTest(unittest.TestCase):
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
                               mode="w",
+                              format=self.tarfile_format,
                               max_volume_size=20000,
                               new_volume_handler=new_volume_handler)
         tarobj.add("big")
@@ -244,7 +278,10 @@ class MultivolTest(unittest.TestCase):
             assert value == self.md5sum(key)
 
     def test_multiple_files_extract(self):
-        # creates a simple tar file with multiple files and extracts it
+        '''
+        creates a simple tar file with no volumes and with multiple files
+        inside and extracts it
+        '''
 
         # create sample data
         hash = dict()
@@ -254,6 +291,7 @@ class MultivolTest(unittest.TestCase):
 
         # create the tar file with volumes
         tarobj = TarFile.open("sample.tar",
+                              format=self.tarfile_format,
                               mode="w")
         tarobj.add("big")
         tarobj.add("small")
@@ -279,89 +317,11 @@ class MultivolTest(unittest.TestCase):
             assert os.path.exists(key)
             assert value == self.md5sum(key)
 
-    def test_multivolume_pax_compress(self):
-
-        # create the content of the file to compress and hash it
-        hash = self.create_file("big", 50000)
-
-        # create the tar file with volumes
-        tarobj = TarFile.open("sample.tar",
-                              mode="w",
-                              format=PAX_FORMAT,
-                              max_volume_size=30000,
-                              new_volume_handler=new_volume_handler)
-        tarobj.add("big")
-        tarobj.close()
-
-        # check that the tar volumes were correctly created
-        assert os.path.exists("sample.tar")
-        assert os.path.exists("sample.tar.1")
-        assert not os.path.exists("sample.tar.2")
-
-        os.unlink("big")
-        assert not os.path.exists("big")
-
-        # extract with normal tar and check output
-        os.system("tar xfM sample.tar --file=sample.tar.1")
-        assert os.path.exists("big")
-        assert hash == self.md5sum("big")
-
-    def test_multivolume_pax_extract(self):
-        '''
-        Create a volume with gnu tar command and extract it with our tarfiel lib
-        '''
-        # create the content of the file to compress and hash it
-        hash = self.create_file("big", 5*1024*1024)
-
-        # create the tar file with volumes
-        os.system("tar cM -L 3M big --format=pax --file=sample.tar --file=sample.tar.1")
-
-        # check that the tar volumes were correctly created
-        assert os.path.exists("sample.tar")
-        assert os.path.exists("sample.tar.1")
-        assert not os.path.exists("sample.tar.2")
 
-        os.unlink("big")
-        assert not os.path.exists("big")
+class MultivolPaxFormatTest(MultivolGnuFormatTest):
+    """
+    Test multivolume support in tarfile with PAX format
+    """
 
-        # extract and check output
-        tarobj = TarFile.open("sample.tar",
-                              mode="r",
-                              new_volume_handler=new_volume_handler)
-        tarobj.extractall()
-        tarobj.close()
-        assert os.path.exists("big")
-        assert hash == self.md5sum("big")
-
-    def test_multivolume_pax_compress_extract(self):
-
-        # create the content of the file to compress and hash it
-        hash = self.create_file("big", 50000)
-        os.system("cp big big1")
-
-        # create the tar file with volumes
-        tarobj = TarFile.open("sample.tar",
-                              mode="w",
-                              format=PAX_FORMAT,
-                              max_volume_size=30000,
-                              new_volume_handler=new_volume_handler)
-        tarobj.add("big")
-        tarobj.close()
-
-        # check that the tar volumes were correctly created
-        assert os.path.exists("sample.tar")
-        assert os.path.exists("sample.tar.1")
-        assert not os.path.exists("sample.tar.2")
-
-        os.unlink("big")
-        assert not os.path.exists("big")
-
-        # extract and check output
-        tarobj = TarFile.open("sample.tar",
-                              mode="r",
-                              new_volume_handler=new_volume_handler)
-        tarobj.extractall()
-        tarobj.close()
-        os.system("cp big big2")
-        assert os.path.exists("big")
-        assert hash == self.md5sum("big")
+    tar_command_format = "pax"
+    tarfile_format = PAX_FORMAT
\ No newline at end of file