]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tclient.py
fdc3e3a06f36069bcf89bae2d506f95e92199fbd
[bup.git] / lib / bup / t / tclient.py
1 import sys, os, stat, time, random, subprocess, glob, tempfile
2 from bup import client, git
3 from bup.helpers import mkdirp
4 from wvtest import *
5
6 bup_tmp = os.path.realpath('../../../t/tmp')
7 mkdirp(bup_tmp)
8
9 def randbytes(sz):
10     s = ''
11     for i in xrange(sz):
12         s += chr(random.randrange(0,256))
13     return s
14
15 s1 = randbytes(10000)
16 s2 = randbytes(10000)
17 s3 = randbytes(10000)
18
19 IDX_PAT = '/*.idx'
20     
21 @wvtest
22 def test_server_split_with_indexes():
23     initial_failures = wvfailure_count()
24     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
25     os.environ['BUP_MAIN_EXE'] = '../../../bup'
26     os.environ['BUP_DIR'] = bupdir = tmpdir
27     git.init_repo(bupdir)
28     lw = git.PackWriter()
29     c = client.Client(bupdir, create=True)
30     rw = c.new_packwriter()
31
32     lw.new_blob(s1)
33     lw.close()
34
35     rw.new_blob(s2)
36     rw.breakpoint()
37     rw.new_blob(s1)
38     rw.close()
39     if wvfailure_count() == initial_failures:
40         subprocess.call(['rm', '-rf', tmpdir])
41     
42
43 @wvtest
44 def test_multiple_suggestions():
45     initial_failures = wvfailure_count()
46     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
47     os.environ['BUP_MAIN_EXE'] = '../../../bup'
48     os.environ['BUP_DIR'] = bupdir = tmpdir
49     git.init_repo(bupdir)
50
51     lw = git.PackWriter()
52     lw.new_blob(s1)
53     lw.close()
54     lw = git.PackWriter()
55     lw.new_blob(s2)
56     lw.close()
57     WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
58
59     c = client.Client(bupdir, create=True)
60     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
61     rw = c.new_packwriter()
62     s1sha = rw.new_blob(s1)
63     WVPASS(rw.exists(s1sha))
64     s2sha = rw.new_blob(s2)
65     # This is a little hacky, but ensures that we test the code under test
66     while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
67            not c.conn.has_input()):
68         pass
69     rw.new_blob(s2)
70     WVPASS(rw.objcache.exists(s1sha))
71     WVPASS(rw.objcache.exists(s2sha))
72     rw.new_blob(s3)
73     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
74     rw.close()
75     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
76     if wvfailure_count() == initial_failures:
77         subprocess.call(['rm', '-rf', tmpdir])
78
79
80 @wvtest
81 def test_dumb_client_server():
82     initial_failures = wvfailure_count()
83     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
84     os.environ['BUP_MAIN_EXE'] = '../../../bup'
85     os.environ['BUP_DIR'] = bupdir = tmpdir
86     git.init_repo(bupdir)
87     open(git.repo('bup-dumb-server'), 'w').close()
88
89     lw = git.PackWriter()
90     lw.new_blob(s1)
91     lw.close()
92
93     c = client.Client(bupdir, create=True)
94     rw = c.new_packwriter()
95     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
96     rw.new_blob(s1)
97     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
98     rw.new_blob(s2)
99     rw.close()
100     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
101     if wvfailure_count() == initial_failures:
102         subprocess.call(['rm', '-rf', tmpdir])
103
104
105 @wvtest
106 def test_midx_refreshing():
107     initial_failures = wvfailure_count()
108     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
109     os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
110     os.environ['BUP_DIR'] = bupdir = tmpdir
111     git.init_repo(bupdir)
112     c = client.Client(bupdir, create=True)
113     rw = c.new_packwriter()
114     rw.new_blob(s1)
115     p1base = rw.breakpoint()
116     p1name = os.path.join(c.cachedir, p1base)
117     s1sha = rw.new_blob(s1)  # should not be written; it's already in p1
118     s2sha = rw.new_blob(s2)
119     p2base = rw.close()
120     p2name = os.path.join(c.cachedir, p2base)
121     del rw
122
123     pi = git.PackIdxList(bupdir + '/objects/pack')
124     WVPASSEQ(len(pi.packs), 2)
125     pi.refresh()
126     WVPASSEQ(len(pi.packs), 2)
127     WVPASSEQ(sorted([os.path.basename(i.name) for i in pi.packs]),
128              sorted([p1base, p2base]))
129
130     p1 = git.open_idx(p1name)
131     WVPASS(p1.exists(s1sha))
132     p2 = git.open_idx(p2name)
133     WVFAIL(p2.exists(s1sha))
134     WVPASS(p2.exists(s2sha))
135
136     subprocess.call([bupmain, 'midx', '-f'])
137     pi.refresh()
138     WVPASSEQ(len(pi.packs), 1)
139     pi.refresh(skip_midx=True)
140     WVPASSEQ(len(pi.packs), 2)
141     pi.refresh(skip_midx=False)
142     WVPASSEQ(len(pi.packs), 1)
143     if wvfailure_count() == initial_failures:
144         subprocess.call(['rm', '-rf', tmpdir])
145
146
147 @wvtest
148 def test_remote_parsing():
149     tests = (
150         (':/bup', ('file', None, None, '/bup')),
151         ('file:///bup', ('file', None, None, '/bup')),
152         ('192.168.1.1:/bup', ('ssh', '192.168.1.1', None, '/bup')),
153         ('ssh://192.168.1.1:2222/bup', ('ssh', '192.168.1.1', '2222', '/bup')),
154         ('ssh://[ff:fe::1]:2222/bup', ('ssh', 'ff:fe::1', '2222', '/bup')),
155         ('bup://foo.com:1950', ('bup', 'foo.com', '1950', None)),
156         ('bup://foo.com:1950/bup', ('bup', 'foo.com', '1950', '/bup')),
157         ('bup://[ff:fe::1]/bup', ('bup', 'ff:fe::1', None, '/bup')),
158     )
159     for remote, values in tests:
160         WVPASSEQ(client.parse_remote(remote), values)
161     try:
162         client.parse_remote('http://asdf.com/bup')
163         WVFAIL()
164     except client.ClientError:
165         WVPASS()