adding unit test for volume error handling
authorEduardo Robles Elvira <edulix@wadobo.com>
Fri, 28 Jun 2013 09:17:09 +0000 (11:17 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Fri, 28 Jun 2013 09:17:09 +0000 (11:17 +0200)
testing/test_multivol.py

index 7399b72..688bd41 100644 (file)
@@ -400,6 +400,61 @@ class MultivolGnuFormatTest(unittest.TestCase):
         assert os.path.exists("big")
         assert hash == self.md5sum("big")
 
+    def test_volume_not_found(self):
+        '''
+        Create a tar file with multiple volumes and one file and extract it, but
+        one of the volumes is missing
+        '''
+        # 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
+        tarobj = TarFile.open("sample.tar",
+                              mode="w",
+                              format=self.tarfile_format,
+                              max_volume_size=2*1024*1024,
+                              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 os.path.exists("sample.tar.2")
+        assert not os.path.exists("sample.tar.3")
+
+        os.unlink("big")
+        os.unlink("sample.tar.2")
+
+        class VolumeNotFound(Exception):
+            pass
+
+        def new_volume_handler2(tarobj, base_name, volume_number):
+            '''
+            Handles the new volumes
+            '''
+            volume_path = "%s.%d" % (base_name, volume_number)
+
+            try:
+                tarobj.open_volume(volume_path)
+            except IOError as e:
+                # only volume number 2 is missing
+                assert volume_number == 2
+                raise e
+
+        # extract and check output
+        tarobj = TarFile.open("sample.tar",
+                              mode="r",
+                              new_volume_handler=new_volume_handler2)
+        try:
+            tarobj.extractall()
+        except IOError as e:
+            pass
+        tarobj.close()
+        assert os.path.exists("big")
+        assert hash != self.md5sum("big")
+
+
 class MultivolPaxFormatTest(MultivolGnuFormatTest):
     """
     Test multivolume support in tarfile with PAX format