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:
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''):
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()
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)
# 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")
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)
# 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")
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()
# 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")
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)
# 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")
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")
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()
# 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")
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()
# create the tar file with volumes
tarobj = TarFile.open("sample.tar",
+ format=self.tarfile_format,
mode="w")
tarobj.add("big")
tarobj.add("small")
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