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