1
from __future__ import generators
3
sys.path.insert(0, "../src")
5
import diffdir, patchdir, log, selection, tarfile
11
class PatchingTest(unittest.TestCase):
13
def copyfileobj(self, infp, outfp):
14
"""Copy in fileobj to out, closing afterwards"""
17
buf = infp.read(blocksize)
20
assert not infp.close()
21
assert not outfp.close()
24
"""Delete temporary directories"""
25
assert not os.system("rm -rf testfiles/output")
26
os.mkdir("testfiles/output")
29
"""Test cycle on dirx"""
30
self.total_sequence(['testfiles/dir1',
34
def get_sel(self, path):
35
"""Get selection iter over the given directory"""
36
return selection.Select(path).set_iter()
38
def total_sequence(self, filelist):
39
"""Test signatures, diffing, and patching on directory list"""
40
assert len(filelist) >= 2
42
sig = Path("testfiles/output/sig.tar")
43
diff = Path("testfiles/output/diff.tar")
44
seq_path = Path("testfiles/output/sequence")
45
new_path, old_path = None, None # set below in for loop
47
# Write initial full backup to diff.tar
48
for dirname in filelist:
49
old_path, new_path = new_path, Path(dirname)
51
sigblock = diffdir.DirSig(self.get_sel(seq_path))
52
diffdir.write_block_iter(sigblock, sig)
53
deltablock = diffdir.DirDelta(self.get_sel(new_path),
55
else: deltablock = diffdir.DirFull(self.get_sel(new_path))
56
diffdir.write_block_iter(deltablock, diff)
58
patchdir.Patch(seq_path, diff.open("rb"))
59
#print "#########", seq_path, new_path
60
assert seq_path.compare_recursive(new_path, 1)
63
"""Test changing uid/gid, devices"""
65
os.system("cp -a testfiles/root1 testfiles/output/sequence")
66
seq_path = Path("testfiles/output/sequence")
67
new_path = Path("testfiles/root2")
68
sig = Path("testfiles/output/sig.tar")
69
diff = Path("testfiles/output/diff.tar")
71
diffdir.write_block_iter(diffdir.DirSig(self.get_sel(seq_path)), sig)
72
deltablock = diffdir.DirDelta(self.get_sel(new_path), sig.open("rb"))
73
diffdir.write_block_iter(deltablock, diff)
75
patchdir.Patch(seq_path, diff.open("rb"))
77
# since we are not running as root, don't even both comparing,
78
# just make sure file5 exists and file4 doesn't.
79
file5 = seq_path.append("file5")
81
file4 = seq_path.append("file4")
82
assert file4.type is None
85
"""Again test files we don't have access to, this time Tar_WriteSig"""
87
sig_path = Path("testfiles/output/sig.sigtar")
88
tar_path = Path("testfiles/output/tar.tar")
89
basis_path = Path("testfiles/root1")
91
deltablock = diffdir.DirFull_WriteSig(self.get_sel(basis_path),
93
diffdir.write_block_iter(deltablock, tar_path)
95
def test_block_tar(self):
96
"""Test building block tar from a number of files"""
98
"""Return iterator yielding open fileobjs of tar files"""
100
p = Path("testfiles/blocktartest/test%d.tar" % i)
105
tf = patchdir.TarFile_FromFileobjs(get_fileobjs())
107
for tarinfo in tf: namelist.append(tarinfo.name)
108
for i in range(1, 6):
109
assert ("tmp/%d" % i) in namelist, namelist
112
if __name__ == "__main__": unittest.main()