]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tindex.py
sampledata: use a local absolute symlink, not /etc
[bup.git] / lib / bup / t / tindex.py
1 import os
2 import time, tempfile
3 from bup import index, metadata
4 from bup.helpers import *
5 import bup.xstat as xstat
6 from wvtest import *
7
8 lib_t_dir = os.getcwd()
9 bup_tmp = os.path.realpath('../../../t/tmp')
10 mkdirp(bup_tmp)
11
12 @wvtest
13 def index_basic():
14     cd = os.path.realpath('../../../t')
15     WVPASS(cd)
16     sd = os.path.realpath(cd + '/sampledata')
17     WVPASSEQ(index.realpath(cd + '/sampledata'), sd)
18     WVPASSEQ(os.path.realpath(cd + '/sampledata/x'), sd + '/x')
19     WVPASSEQ(os.path.realpath(cd + '/sampledata/abs-symlink'),
20              sd + '/abs-symlink-target')
21     WVPASSEQ(index.realpath(cd + '/sampledata/abs-symlink'),
22              sd + '/abs-symlink')
23
24
25 @wvtest
26 def index_writer():
27     initial_failures = wvfailure_count()
28     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tindex-')
29     orig_cwd = os.getcwd()
30     try:
31         os.chdir(tmpdir)
32         ds = xstat.stat('.')
33         fs = xstat.stat(lib_t_dir + '/tindex.py')
34         ms = index.MetaStoreWriter('index.meta.tmp');
35         tmax = (time.time() - 1) * 10**9
36         w = index.Writer('index.tmp', ms, tmax)
37         w.add('/var/tmp/sporky', fs, 0)
38         w.add('/etc/passwd', fs, 0)
39         w.add('/etc/', ds, 0)
40         w.add('/', ds, 0)
41         ms.close()
42         w.close()
43     finally:
44         os.chdir(orig_cwd)
45     if wvfailure_count() == initial_failures:
46         subprocess.call(['rm', '-rf', tmpdir])
47
48
49 def dump(m):
50     for e in list(m):
51         print '%s%s %s' % (e.is_valid() and ' ' or 'M',
52                            e.is_fake() and 'F' or ' ',
53                            e.name)
54
55 def fake_validate(*l):
56     for i in l:
57         for e in i:
58             e.validate(0100644, index.FAKE_SHA)
59             e.repack()
60
61 def eget(l, ename):
62     for e in l:
63         if e.name == ename:
64             return e
65
66 @wvtest
67 def index_negative_timestamps():
68     initial_failures = wvfailure_count()
69     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tindex-')
70     # Makes 'foo' exist
71     f = file('foo', 'wb')
72     f.close()
73
74     # Dec 31, 1969
75     os.utime("foo", (-86400, -86400))
76     ns_per_sec = 10**9
77     tstart = time.time() * ns_per_sec
78     tmax = tstart - ns_per_sec
79     e = index.BlankNewEntry("foo", 0, tmax)
80     e.from_stat(xstat.stat("foo"), 0, tstart)
81     assert len(e.packed())
82     WVPASS()
83
84     # Jun 10, 1893
85     os.utime("foo", (-0x80000000, -0x80000000))
86     e = index.BlankNewEntry("foo", 0, tmax)
87     e.from_stat(xstat.stat("foo"), 0, tstart)
88     assert len(e.packed())
89     WVPASS()
90     if wvfailure_count() == initial_failures:
91         subprocess.call(['rm', '-rf', tmpdir])
92
93
94 @wvtest
95 def index_dirty():
96     initial_failures = wvfailure_count()
97     orig_cwd = os.getcwd()
98     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tindex-')
99     try:
100         os.chdir(tmpdir)
101         default_meta = metadata.Metadata()
102         ms1 = index.MetaStoreWriter('index.meta.tmp')
103         ms2 = index.MetaStoreWriter('index2.meta.tmp')
104         ms3 = index.MetaStoreWriter('index3.meta.tmp')
105         meta_ofs1 = ms1.store(default_meta)
106         meta_ofs2 = ms2.store(default_meta)
107         meta_ofs3 = ms3.store(default_meta)
108
109         ds = xstat.stat(lib_t_dir)
110         fs = xstat.stat(lib_t_dir + '/tindex.py')
111         tmax = (time.time() - 1) * 10**9
112
113         w1 = index.Writer('index.tmp', ms1, tmax)
114         w1.add('/a/b/x', fs, meta_ofs1)
115         w1.add('/a/b/c', fs, meta_ofs1)
116         w1.add('/a/b/', ds, meta_ofs1)
117         w1.add('/a/', ds, meta_ofs1)
118         #w1.close()
119         WVPASS()
120
121         w2 = index.Writer('index2.tmp', ms2, tmax)
122         w2.add('/a/b/n/2', fs, meta_ofs2)
123         #w2.close()
124         WVPASS()
125
126         w3 = index.Writer('index3.tmp', ms3, tmax)
127         w3.add('/a/c/n/3', fs, meta_ofs3)
128         #w3.close()
129         WVPASS()
130
131         r1 = w1.new_reader()
132         r2 = w2.new_reader()
133         r3 = w3.new_reader()
134         WVPASS()
135
136         r1all = [e.name for e in r1]
137         WVPASSEQ(r1all,
138                  ['/a/b/x', '/a/b/c', '/a/b/', '/a/', '/'])
139         r2all = [e.name for e in r2]
140         WVPASSEQ(r2all,
141                  ['/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
142         r3all = [e.name for e in r3]
143         WVPASSEQ(r3all,
144                  ['/a/c/n/3', '/a/c/n/', '/a/c/', '/a/', '/'])
145         all = [e.name for e in index.merge(r2, r1, r3)]
146         WVPASSEQ(all,
147                  ['/a/c/n/3', '/a/c/n/', '/a/c/',
148                   '/a/b/x', '/a/b/n/2', '/a/b/n/', '/a/b/c',
149                   '/a/b/', '/a/', '/'])
150         fake_validate(r1)
151         dump(r1)
152
153         print [hex(e.flags) for e in r1]
154         WVPASSEQ([e.name for e in r1 if e.is_valid()], r1all)
155         WVPASSEQ([e.name for e in r1 if not e.is_valid()], [])
156         WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
157                  ['/a/c/n/3', '/a/c/n/', '/a/c/',
158                   '/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
159
160         expect_invalid = ['/'] + r2all + r3all
161         expect_real = (set(r1all) - set(r2all) - set(r3all)) \
162                         | set(['/a/b/n/2', '/a/c/n/3'])
163         dump(index.merge(r2, r1, r3))
164         for e in index.merge(r2, r1, r3):
165             print e.name, hex(e.flags), e.ctime
166             eiv = e.name in expect_invalid
167             er  = e.name in expect_real
168             WVPASSEQ(eiv, not e.is_valid())
169             WVPASSEQ(er, e.is_real())
170         fake_validate(r2, r3)
171         dump(index.merge(r2, r1, r3))
172         WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()], [])
173
174         e = eget(index.merge(r2, r1, r3), '/a/b/c')
175         e.invalidate()
176         e.repack()
177         dump(index.merge(r2, r1, r3))
178         WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
179                  ['/a/b/c', '/a/b/', '/a/', '/'])        
180         w1.close()
181         w2.close()
182         w3.close()
183     finally:
184         os.chdir(orig_cwd)
185     if wvfailure_count() == initial_failures:
186         subprocess.call(['rm', '-rf', tmpdir])