23
23
import sys, unittest
24
sys.path.insert(0, "../")
24
sys.path.insert(0, u"../")
26
26
from duplicity import diffdir
27
27
from duplicity import patchdir
28
28
from duplicity import selection
29
from duplicity.path import * #@UnusedWildImport
29
from duplicity.path import * # pylint: disable=unused-wildcard-import,redefined-builtin
33
33
class RootTest(unittest.TestCase):
34
"""Test doing operations that only root can"""
34
u"""Test doing operations that only root can"""
37
37
# must run with euid/egid of root
39
39
# make sure uid/gid match euid/egid
40
40
os.setuid(os.geteuid())
41
41
os.setgid(os.getegid())
42
assert not os.system("tar xzf manual/rootfiles.tar.gz > /dev/null 2>&1")
42
assert not os.system(u"tar xzf manual/rootfiles.tar.gz > /dev/null 2>&1")
44
44
def tearDown(self):
45
assert not os.system("rm -rf testfiles tempdir temp2.tar")
45
assert not os.system(u"rm -rf testfiles tempdir temp2.tar")
47
47
def copyfileobj(self, infp, outfp):
48
"""Copy in fileobj to out, closing afterwards"""
48
u"""Copy in fileobj to out, closing afterwards"""
49
49
blocksize = 32 * 1024
51
51
buf = infp.read(blocksize)
55
55
assert not outfp.close()
58
"""Delete temporary directories"""
59
assert not os.system("rm -rf testfiles/output")
60
os.mkdir("testfiles/output")
58
u"""Delete temporary directories"""
59
assert not os.system(u"rm -rf testfiles/output")
60
os.mkdir(u"testfiles/output")
62
62
def get_sel(self, path):
63
"""Get selection iter over the given directory"""
63
u"""Get selection iter over the given directory"""
64
64
return selection.Select(path).set_iter()
66
66
def total_sequence(self, filelist):
67
"""Test signatures, diffing, and patching on directory list"""
67
u"""Test signatures, diffing, and patching on directory list"""
68
68
assert len(filelist) >= 2
70
assert not os.system("cp -pR %s testfiles/output/sequence" %
70
assert not os.system(u"cp -pR %s testfiles/output/sequence" %
72
seq_path = Path("testfiles/output/sequence")
73
sig = Path("testfiles/output/sig.tar")
74
diff = Path("testfiles/output/diff.tar")
72
seq_path = Path(u"testfiles/output/sequence")
73
sig = Path(u"testfiles/output/sig.tar")
74
diff = Path(u"testfiles/output/diff.tar")
75
75
for dirname in filelist[1:]:
76
76
new_path = Path(dirname)
77
77
diffdir.write_block_iter(
80
80
diffdir.write_block_iter(
81
81
diffdir.DirDelta(selection.Select(new_path).set_iter(),
85
patchdir.Patch(seq_path, diff.open("rb"))
85
patchdir.Patch(seq_path, diff.open(u"rb"))
87
87
assert seq_path.compare_recursive(new_path, 1)
89
89
def test_basic_cycle(self):
90
"""Test cycle on dir with devices, changing uid/gid, etc."""
91
self.total_sequence(['testfiles/root1', 'testfiles/root2'])
90
u"""Test cycle on dir with devices, changing uid/gid, etc."""
91
self.total_sequence([u'testfiles/root1', u'testfiles/root2'])
93
93
def test_patchdir(self):
94
"""Test changing uid/gid, devices"""
94
u"""Test changing uid/gid, devices"""
96
os.system("cp -pR testfiles/root1 testfiles/output/sequence")
97
seq_path = Path("testfiles/output/sequence")
98
new_path = Path("testfiles/root2")
99
sig = Path("testfiles/output/sig.tar")
100
diff = Path("testfiles/output/diff.tar")
96
os.system(u"cp -pR testfiles/root1 testfiles/output/sequence")
97
seq_path = Path(u"testfiles/output/sequence")
98
new_path = Path(u"testfiles/root2")
99
sig = Path(u"testfiles/output/sig.tar")
100
diff = Path(u"testfiles/output/diff.tar")
102
102
diffdir.write_block_iter(diffdir.DirSig(self.get_sel(seq_path)), sig)
103
deltablock = diffdir.DirDelta(self.get_sel(new_path), sig.open("rb"))
103
deltablock = diffdir.DirDelta(self.get_sel(new_path), sig.open(u"rb"))
104
104
diffdir.write_block_iter(deltablock, diff)
106
patchdir.Patch(seq_path, diff.open("rb"))
106
patchdir.Patch(seq_path, diff.open(u"rb"))
108
108
# since we are not running as root, don't even both comparing,
109
109
# just make sure file5 exists and file4 doesn't.
110
file5 = seq_path.append("file5")
110
file5 = seq_path.append(u"file5")
111
111
assert file5.isreg()
112
file4 = seq_path.append("file4")
112
file4 = seq_path.append(u"file4")
113
113
assert file4.type is None
115
115
def test_patchdir2(self):
116
"""Again test files we don't have access to, this time Tar_WriteSig"""
116
u"""Again test files we don't have access to, this time Tar_WriteSig"""
118
sig_path = Path("testfiles/output/sig.sigtar")
119
tar_path = Path("testfiles/output/tar.tar")
120
basis_path = Path("testfiles/root1")
118
sig_path = Path(u"testfiles/output/sig.sigtar")
119
tar_path = Path(u"testfiles/output/tar.tar")
120
basis_path = Path(u"testfiles/root1")
122
122
deltablock = diffdir.DirFull_WriteSig(self.get_sel(basis_path),
123
sig_path.open(u"wb"))
124
124
diffdir.write_block_iter(deltablock, tar_path)
126
126
def runtests(): unittest.main()
128
if __name__ == "__main__":
128
if __name__ == u"__main__":