added new unittest test_multivol_compress with bigger random-data file
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 9 Jun 2016 16:01:37 +0000 (18:01 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 15 Jun 2016 11:18:03 +0000 (13:18 +0200)
testing/test_multivol.py

index e452d4a..58d90fb 100644 (file)
@@ -19,6 +19,7 @@ import os, unittest
 
 from deltatar.tarfile import TarFile, PAX_FORMAT, GNU_FORMAT, BLOCKSIZE
 from . import BaseTest, new_volume_handler, closing_new_volume_handler
+from .create_pseudo_random_files import create_file as create_random_file
 
 class MultivolGnuFormatTest(BaseTest):
     """
@@ -270,6 +271,59 @@ class MultivolGnuFormatTest(BaseTest):
         assert os.path.exists("big")
         assert hash == self.md5sum("big")
 
+    def test_multivol_compress(self):
+        ''' check creation of multiple volumes when compression is on '''
+
+        # create a random file that is not so easy to compress...
+        filename = create_random_file('./', 100000)
+        hash = self.md5sum(filename)
+
+        # need own volume handler so files maintain their suffix for gunzip
+        def my_volume_handler(tarobj, base_name, volume_number):
+            if not base_name.endswith('.tar.gz'):
+                raise ValueError('need .tar.gz file!')
+            tarobj.fileobj.close()
+            new_name = '{}.{}.tar.gz'.format(base_name[:-7], volume_number)
+            tarobj.open_volume(new_name)
+
+        # create the tar file with volumes and compression
+        tarobj = TarFile.open("sample.tar.gz",
+                              mode="w#gz",
+                              format=self.tarfile_format,
+                              max_volume_size=30000,
+                              new_volume_handler=my_volume_handler,
+                              debug=3)
+        tarobj.add(filename)
+        tarobj.list()
+        tarobj.close()
+
+        # data fits into 2 volumes -- check that no third is created
+        assert os.path.exists("sample.tar.gz")
+        assert os.path.exists("sample.1.tar.gz")
+        assert not os.path.exists("sample.tar.gz.1")
+        assert not os.path.exists("sample.tar.gz.2")
+        assert not os.path.exists("sample.2.tar.gz.2")
+
+        os.unlink(filename)
+        assert not os.path.exists(filename)
+
+        # extract with shell means; slightly complicated because the linux
+        # tar/gunzip cannot do gzipped-multi-volume archives
+        print('unpacking:')
+        import subprocess
+        for cmd in 'gunzip -v sample.tar.gz', 'gunzip -v sample.1.tar.gz', \
+                'tar xvfM sample.tar --file=sample.1.tar':
+            print(cmd)
+            output = subprocess.check_output(cmd.split(),
+                                             universal_newlines=True)
+            for line in output.splitlines():
+                print(line.rstrip())
+        assert os.path.exists(filename)
+        assert hash == self.md5sum(filename)
+
+        os.unlink('sample.tar')
+        os.unlink('sample.1.tar')
+
     def test_volume_extract1(self):
         '''
         Create a tar file with multiple volumes and one file and extract it