1
from __future__ import generators
3
sys.path.insert(0, "../src")
6
import diffdir, selection, tarfile, log
10
class DDTest(unittest.TestCase):
11
"""Test functions in diffdir.py"""
12
def copyfileobj(self, infp, outfp):
13
"""Copy in fileobj to out, closing afterwards"""
16
buf = infp.read(blocksize)
19
assert not infp.close()
20
assert not outfp.close()
23
"""Delete temporary directories"""
24
assert not os.system("rm -rf testfiles/output")
25
os.mkdir("testfiles/output")
28
"""Test producing tar signature of various file types"""
30
select = selection.Select(Path("testfiles/various_file_types"))
32
sigtar = diffdir.SigTarBlockIter(select)
33
diffdir.write_block_iter(sigtar, "testfiles/output/sigtar")
36
for tarinfo in tarfile.TarFile("testfiles/output/sigtar", "r"):
38
assert i >= 5, "There should be at least 5 files in sigtar"
40
def test_empty_diff(self):
41
"""Test producing a diff against same sig; should be len 0"""
43
select = selection.Select(Path("testfiles/various_file_types"))
45
sigtar = diffdir.SigTarBlockIter(select)
46
diffdir.write_block_iter(sigtar, "testfiles/output/sigtar")
48
sigtar_fp = open("testfiles/output/sigtar")
49
select2 = selection.Select(Path("testfiles/various_file_types"))
51
diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp),
52
"testfiles/output/difftar")
54
size = os.stat("testfiles/output/difftar").st_size
55
assert size == 0 or size == 10240 # 10240 is size of one record
58
"""Test making a diff"""
60
sel1 = selection.Select(Path("testfiles/dir1"))
61
diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()),
62
"testfiles/output/dir1.sigtar")
64
sigtar_fp = open("testfiles/output/dir1.sigtar", "rb")
65
sel2 = selection.Select(Path("testfiles/dir2"))
66
delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp)
67
diffdir.write_block_iter(delta_tar,
68
"testfiles/output/dir1dir2.difftar")
70
changed_files = ["diff/changeable_permission",
72
"snapshot/symbolic_link/",
73
"deleted/deleted_file",
74
"snapshot/directory_to_file",
75
"snapshot/file_to_directory/"]
76
for tarinfo in tarfile.TarFile("testfiles/output/dir1dir2.difftar",
78
if tarinfo.name in changed_files:
79
changed_files.remove(tarinfo.name)
80
assert not changed_files, ("Following files not found:\n"
81
+ "\n".join(changed_files))
84
"""Another diff test - this one involves multivol support"""
86
sel1 = selection.Select(Path("testfiles/dir2"))
87
diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()),
88
"testfiles/output/dir2.sigtar")
90
sigtar_fp = open("testfiles/output/dir2.sigtar", "rb")
91
sel2 = selection.Select(Path("testfiles/dir3"))
92
delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp)
93
diffdir.write_block_iter(delta_tar,
94
"testfiles/output/dir2dir3.difftar")
97
tf = tarfile.TarFile("testfiles/output/dir2dir3.difftar", "r")
99
if tarinfo.name.startswith("multivol_diff/"):
100
buffer += tf.extractfile(tarinfo).read()
101
assert 3000000 < len(buffer) < 4000000
102
fout = open("testfiles/output/largefile.delta", "wb")
105
assert not os.system("rdiff patch testfiles/dir2/largefile "
106
"testfiles/output/largefile.delta "
107
"testfiles/output/largefile.patched")
108
dir3large = open("testfiles/dir3/largefile", "rb").read()
109
patchedlarge = open("testfiles/output/largefile.patched", "rb").read()
110
assert dir3large == patchedlarge
112
def test_dirdelta_write_sig(self):
113
"""Test simultaneous delta and sig generation
115
Generate signatures and deltas of dirs1, 2, 3, 4 and compare
116
those produced by DirDelta_WriteSig and other methods.
120
deltadir1 = Path("testfiles/output/dir.deltatar1")
121
deltadir2 = Path("testfiles/output/dir.deltatar2")
122
cur_full_sigs = Path("testfiles/output/fullsig.dir1")
124
cur_dir = Path("testfiles/dir1")
125
get_sel = lambda cur_dir: selection.Select(cur_dir).set_iter()
126
diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)),
129
sigstack = [cur_full_sigs]
130
for dirname in ['dir2', 'dir3', 'dir4']:
131
print "Processing ", dirname
133
cur_dir = Path("testfiles/" + dirname)
135
old_full_sigs = cur_full_sigs
136
cur_full_sigs = Path("testfiles/output/fullsig." + dirname)
138
delta1 = Path("testfiles/output/delta1." + dirname)
139
delta2 = Path("testfiles/output/delta2." + dirname)
140
incsig = Path("testfiles/output/incsig." + dirname)
142
# Write old-style delta to deltadir1
143
diffdir.write_block_iter(diffdir.DirDelta(get_sel(cur_dir),
144
old_full_sigs.open("rb")),
147
# Write new signature and delta to deltadir2 and sigdir2, compare
148
block_iter = diffdir.DirDelta_WriteSig(get_sel(cur_dir),
149
map(lambda p: p.open("rb"), sigstack),
151
sigstack.append(incsig)
152
diffdir.write_block_iter(block_iter, delta2)
154
print delta1.name, delta2.name
155
compare_tar(delta1.open("rb"), delta2.open("rb"))
156
assert not os.system("cmp %s %s" % (delta1.name, delta2.name))
158
# Write old-style signature to cur_full_sigs
159
diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)),
162
def test_combine_path_iters(self):
163
"""Test diffdir.combine_path_iters"""
165
def __init__(self, index, other = None):
168
def __repr__(self): return "(%s %s)" % (self.index, self.other)
173
yield Dummy((1, 5), 2)
184
result = diffdir.combine_path_iters([get_iter1(),
187
elem1 = result.next()
188
assert elem1.index == () and elem1.other == 3, elem1
189
elem2 = result.next()
190
assert elem2.index == (1,) and elem2.other is None, elem2
191
elem3 = result.next()
192
assert elem3.index == (1,5) and elem3.other is None
193
elem4 = result.next()
194
assert elem4.index == (2,) and elem4.other == 1
195
try: elem5 = result.next()
196
except StopIteration: pass
197
else: assert 0, elem5
200
def compare_tar(tarfile1, tarfile2):
201
"""Compare two tarfiles"""
202
tf1 = tarfile.TarFile("none", "r", tarfile1)
203
tf2 = tarfile.TarFile("none", "r", tarfile2)
207
try: ti2 = tf2_iter.next()
208
except StopIteration:
209
assert 0, ("Premature end to second tarfile, "
210
"ti1.name = %s" % ti1.name)
211
#print "Comparing ", ti1.name, ti2.name
212
assert tarinfo_eq(ti1, ti2), "%s %s" % (ti1.name, ti2.name)
214
fp1 = tf1.extractfile(ti1)
217
fp2 = tf2.extractfile(ti2)
221
try: ti2 = tf2_iter.next()
222
except StopIteration: pass
223
else: assert 0, ("Premature end to first tarfile, "
224
"ti2.name = %s" % ti2.name)
229
def tarinfo_eq(ti1, ti2):
230
if ti1.name != ti2.name:
231
print "Name:", ti1.name, ti2.name
233
if ti1.size != ti2.size:
234
print "Size:", ti1.size, ti2.size
236
if ti1.mtime != ti2.mtime:
237
print "Mtime:", ti1.mtime, ti2.mtime
239
if ti1.mode != ti2.mode:
240
print "Mode:", ti1.mode, ti2.mode
242
if ti1.type != ti2.type:
243
print "Type:", ti1.type, ti2.type
245
if ti1.issym() or ti1.islnk():
246
if ti1.linkname != ti2.linkname:
247
print "Linkname:", ti1.linkname, ti2.linkname
249
if ti1.uid != ti2.uid or ti1.gid != ti2.gid:
250
print "IDs:", ti1.uid, ti2.uid, ti1.gid, ti2.gid
252
if ti1.uname != ti2.uname or ti1.gname != ti2.gname:
253
print "Owner names:", ti1.uname, ti2.uname, ti1.gname, ti2.gname
257
if __name__ == "__main__": unittest.main()