]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tclient.py
0db5cc1d59fa5e0a61c138a3e886b63667a4857c
[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     if wvfailure_count() == initial_failures:
39         subprocess.call(['rm', '-rf', tmpdir])
40     
41
42 @wvtest
43 def test_multiple_suggestions():
44     initial_failures = wvfailure_count()
45     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
46     os.environ['BUP_MAIN_EXE'] = '../../../bup'
47     os.environ['BUP_DIR'] = bupdir = tmpdir
48     git.init_repo(bupdir)
49
50     lw = git.PackWriter()
51     lw.new_blob(s1)
52     lw.close()
53     lw = git.PackWriter()
54     lw.new_blob(s2)
55     lw.close()
56     WVPASSEQ(len(glob.glob(git.repo('objects/pack'+IDX_PAT))), 2)
57
58     c = client.Client(bupdir, create=True)
59     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 0)
60     rw = c.new_packwriter()
61     s1sha = rw.new_blob(s1)
62     WVPASS(rw.exists(s1sha))
63     s2sha = rw.new_blob(s2)
64     # This is a little hacky, but ensures that we test the code under test
65     while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
66            not c.conn.has_input()):
67         pass
68     rw.new_blob(s2)
69     WVPASS(rw.objcache.exists(s1sha))
70     WVPASS(rw.objcache.exists(s2sha))
71     rw.new_blob(s3)
72     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 2)
73     rw.close()
74     WVPASSEQ(len(glob.glob(c.cachedir+IDX_PAT)), 3)
75     if wvfailure_count() == initial_failures:
76         subprocess.call(['rm', '-rf', tmpdir])
77
78
79 @wvtest
80 def test_dumb_client_server():
81     initial_failures = wvfailure_count()
82     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
83     os.environ['BUP_MAIN_EXE'] = '../../../bup'
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     if wvfailure_count() == initial_failures:
101         subprocess.call(['rm', '-rf', tmpdir])
102
103
104 @wvtest
105 def test_midx_refreshing():
106     initial_failures = wvfailure_count()
107     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tclient-')
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     if wvfailure_count() == initial_failures:
143         subprocess.call(['rm', '-rf', tmpdir])
144
145
146 @wvtest
147 def test_remote_parsing():
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     )
158     for remote, values in tests:
159         WVPASSEQ(client.parse_remote(remote), values)
160     try:
161         client.parse_remote('http://asdf.com/bup')
162         WVFAIL()
163     except client.ClientError:
164         WVPASS()