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