]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tclient.py
Use absolute_import from the __future__ everywhere
[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 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_MAIN_EXE'] = '../../../bup'
31             os.environ['BUP_DIR'] = bupdir = tmpdir
32             git.init_repo(bupdir)
33             lw = git.PackWriter()
34             c = client.Client(bupdir, create=True)
35             rw = c.new_packwriter()
36
37             lw.new_blob(s1)
38             lw.close()
39
40             rw.new_blob(s2)
41             rw.breakpoint()
42             rw.new_blob(s1)
43             rw.close()
44     
45
46 @wvtest
47 def test_multiple_suggestions():
48     with no_lingering_errors():
49         with test_tempdir('bup-tclient-') as tmpdir:
50             os.environ['BUP_MAIN_EXE'] = '../../../bup'
51             os.environ['BUP_DIR'] = bupdir = tmpdir
52             git.init_repo(bupdir)
53
54             lw = git.PackWriter()
55             lw.new_blob(s1)
56             lw.close()
57             lw = git.PackWriter()
58             lw.new_blob(s2)
59             lw.close()
60             WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
61
62             c = client.Client(bupdir, create=True)
63             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
64             rw = c.new_packwriter()
65             s1sha = rw.new_blob(s1)
66             WVPASS(rw.exists(s1sha))
67             s2sha = rw.new_blob(s2)
68             # This is a little hacky, but ensures that we test the
69             # code under test
70             while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
71                    not c.conn.has_input()):
72                 pass
73             rw.new_blob(s2)
74             WVPASS(rw.objcache.exists(s1sha))
75             WVPASS(rw.objcache.exists(s2sha))
76             rw.new_blob(s3)
77             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
78             rw.close()
79             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
80
81
82 @wvtest
83 def test_dumb_client_server():
84     with no_lingering_errors():
85         with test_tempdir('bup-tclient-') as tmpdir:
86             os.environ['BUP_MAIN_EXE'] = '../../../bup'
87             os.environ['BUP_DIR'] = bupdir = tmpdir
88             git.init_repo(bupdir)
89             open(git.repo('bup-dumb-server'), 'w').close()
90
91             lw = git.PackWriter()
92             lw.new_blob(s1)
93             lw.close()
94
95             c = client.Client(bupdir, create=True)
96             rw = c.new_packwriter()
97             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
98             rw.new_blob(s1)
99             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 1)
100             rw.new_blob(s2)
101             rw.close()
102             WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
103
104
105 @wvtest
106 def test_midx_refreshing():
107     with no_lingering_errors():
108         with test_tempdir('bup-tclient-') as tmpdir:
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
144
145 @wvtest
146 def test_remote_parsing():
147     with no_lingering_errors():
148         tests = (
149             (':/bup', ('file', None, None, '/bup')),
150             ('file:///bup', ('file', None, None, '/bup')),
151             ('192.168.1.1:/bup', ('ssh', '192.168.1.1', None, '/bup')),
152             ('ssh://192.168.1.1:2222/bup', ('ssh', '192.168.1.1', '2222', '/bup')),
153             ('ssh://[ff:fe::1]:2222/bup', ('ssh', 'ff:fe::1', '2222', '/bup')),
154             ('bup://foo.com:1950', ('bup', 'foo.com', '1950', None)),
155             ('bup://foo.com:1950/bup', ('bup', 'foo.com', '1950', '/bup')),
156             ('bup://[ff:fe::1]/bup', ('bup', 'ff:fe::1', None, '/bup')),)
157         for remote, values in tests:
158             WVPASSEQ(client.parse_remote(remote), values)
159         try:
160             client.parse_remote('http://asdf.com/bup')
161             WVFAIL()
162         except client.ClientError:
163             WVPASS()