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