~ed.so/duplicity/reuse-passphrase-for-signing-fix

« back to all changes in this revision

Viewing changes to testing/patchdirtest.py

  • Committer: bescoto
  • Date: 2002-10-29 01:49:46 UTC
  • Revision ID: vcs-imports@canonical.com-20021029014946-3m4rmm5plom7pl6q
Initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
import sys
 
3
sys.path.insert(0, "../src")
 
4
import os, unittest
 
5
import diffdir, patchdir, log, selection, tarfile
 
6
from path import *
 
7
 
 
8
log.setverbosity(3)
 
9
                
 
10
 
 
11
class PatchingTest(unittest.TestCase):
 
12
        """Test patching"""
 
13
        def copyfileobj(self, infp, outfp):
 
14
                """Copy in fileobj to out, closing afterwards"""
 
15
                blocksize = 32 * 1024
 
16
                while 1:
 
17
                        buf = infp.read(blocksize)
 
18
                        if not buf: break
 
19
                        outfp.write(buf)
 
20
                assert not infp.close()
 
21
                assert not outfp.close()
 
22
 
 
23
        def deltmp(self):
 
24
                """Delete temporary directories"""
 
25
                assert not os.system("rm -rf testfiles/output")
 
26
                os.mkdir("testfiles/output")
 
27
 
 
28
        def test_total(self):
 
29
                """Test cycle on dirx"""
 
30
                self.total_sequence(['testfiles/dir1',
 
31
                                                         'testfiles/dir2',
 
32
                                                         'testfiles/dir3'])
 
33
 
 
34
        def get_sel(self, path):
 
35
                """Get selection iter over the given directory"""
 
36
                return selection.Select(path).set_iter()
 
37
 
 
38
        def total_sequence(self, filelist):
 
39
                """Test signatures, diffing, and patching on directory list"""
 
40
                assert len(filelist) >= 2
 
41
                self.deltmp()
 
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
 
46
 
 
47
                # Write initial full backup to diff.tar
 
48
                for dirname in filelist:
 
49
                        old_path, new_path = new_path, Path(dirname)
 
50
                        if old_path:
 
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),
 
54
                                                                                          sig.open("rb"))
 
55
                        else: deltablock = diffdir.DirFull(self.get_sel(new_path))
 
56
                        diffdir.write_block_iter(deltablock, diff)
 
57
 
 
58
                        patchdir.Patch(seq_path, diff.open("rb"))
 
59
                        #print "#########", seq_path, new_path
 
60
                        assert seq_path.compare_recursive(new_path, 1)
 
61
 
 
62
        def test_root(self):
 
63
                """Test changing uid/gid, devices"""
 
64
                self.deltmp()
 
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")
 
70
 
 
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)
 
74
 
 
75
                patchdir.Patch(seq_path, diff.open("rb"))
 
76
 
 
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")
 
80
                assert file5.isreg()
 
81
                file4 = seq_path.append("file4")
 
82
                assert file4.type is None
 
83
 
 
84
        def test_root2(self):
 
85
                """Again test files we don't have access to, this time Tar_WriteSig"""
 
86
                self.deltmp()
 
87
                sig_path = Path("testfiles/output/sig.sigtar")
 
88
                tar_path = Path("testfiles/output/tar.tar")
 
89
                basis_path = Path("testfiles/root1")
 
90
 
 
91
                deltablock = diffdir.DirFull_WriteSig(self.get_sel(basis_path),
 
92
                                                                                          sig_path.open("wb"))
 
93
                diffdir.write_block_iter(deltablock, tar_path)
 
94
                
 
95
        def test_block_tar(self):
 
96
                """Test building block tar from a number of files"""
 
97
                def get_fileobjs():
 
98
                        """Return iterator yielding open fileobjs of tar files"""
 
99
                        for i in range(1, 4):
 
100
                                p = Path("testfiles/blocktartest/test%d.tar" % i)
 
101
                                fp = p.open("rb")
 
102
                                yield fp
 
103
                                fp.close()
 
104
 
 
105
                tf = patchdir.TarFile_FromFileobjs(get_fileobjs())
 
106
                namelist = []
 
107
                for tarinfo in tf: namelist.append(tarinfo.name)
 
108
                for i in range(1, 6):
 
109
                        assert ("tmp/%d" % i) in namelist, namelist
 
110
                
 
111
 
 
112
if __name__ == "__main__": unittest.main()