From 6ca03af8454f77bfd214b33da9f592cb121e9981 Mon Sep 17 00:00:00 2001 From: Eduardo Robles Elvira Date: Fri, 28 Jun 2013 11:17:09 +0200 Subject: [PATCH] adding unit test for volume error handling --- testing/test_multivol.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) diff --git a/testing/test_multivol.py b/testing/test_multivol.py index 7399b72..688bd41 100644 --- a/testing/test_multivol.py +++ b/testing/test_multivol.py @@ -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 -- 1.7.1