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