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