]> arthur.barton.de Git - bup.git/blob - test/int/test_client.py
Remove Client __del__ in favor of context management
[bup.git] / test / int / test_client.py
1
2 from __future__ import absolute_import
3 import os, time, random, subprocess, glob
4 import pytest
5
6 from bup import client, git, path
7 from bup.compat import bytes_from_uint, environ, range
8
9 def randbytes(sz):
10     s = b''
11     for i in range(sz):
12         s += bytes_from_uint(random.randrange(0,256))
13     return s
14
15
16 s1 = randbytes(10000)
17 s2 = randbytes(10000)
18 s3 = randbytes(10000)
19
20 IDX_PAT = b'/*.idx'
21
22
23 def test_server_split_with_indexes(tmpdir):
24     environ[b'BUP_DIR'] = bupdir = tmpdir
25     git.init_repo(bupdir)
26     lw = git.PackWriter()
27     with client.Client(bupdir, create=True) as c:
28         rw = c.new_packwriter()
29
30         lw.new_blob(s1)
31         lw.close()
32
33         rw.new_blob(s2)
34         rw.breakpoint()
35         rw.new_blob(s1)
36         rw.close()
37
38
39 def test_multiple_suggestions(tmpdir):
40     environ[b'BUP_DIR'] = bupdir = tmpdir
41     git.init_repo(bupdir)
42
43     lw = git.PackWriter()
44     lw.new_blob(s1)
45     lw.close()
46     lw = git.PackWriter()
47     lw.new_blob(s2)
48     lw.close()
49     assert len(glob.glob(git.repo(b'objects/pack'+IDX_PAT))) == 2
50
51     with client.Client(bupdir, create=True) as c:
52         assert len(glob.glob(c.cachedir+IDX_PAT)) == 0
53         rw = c.new_packwriter()
54         s1sha = rw.new_blob(s1)
55         assert rw.exists(s1sha)
56         s2sha = rw.new_blob(s2)
57
58         # This is a little hacky, but ensures that we test the
59         # code under test. First, flush to ensure that we've
60         # actually sent all the command ('receive-objects-v2')
61         # and their data to the server. This may be needed if
62         # the output buffer size is bigger than the data (both
63         # command and objects) we're writing. To see the need
64         # for this, change the object sizes at the beginning
65         # of this file to be very small (e.g. 10 instead of 10k)
66         c.conn.outp.flush()
67
68         # Then, check if we've already received the idx files.
69         # This may happen if we're preempted just after writing
70         # the data, then the server runs and suggests, and only
71         # then we continue in PackWriter_Remote::_raw_write()
72         # and check the has_input(), in that case we'll receive
73         # the idx still in the rw.new_blob() calls above.
74         #
75         # In most cases though, that doesn't happen, and we'll
76         # get past the has_input() check before the server has
77         # a chance to respond - it has to actually hash the new
78         # object here, so it takes some time. So also break out
79         # of the loop if the server has sent something on the
80         # connection.
81         #
82         # Finally, abort this after a little while (about one
83         # second) just in case something's actually broken.
84         n = 0
85         while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
86                not c.conn.has_input() and n < 10):
87             time.sleep(0.1)
88             n += 1
89         assert len(glob.glob(c.cachedir+IDX_PAT)) == 2 or c.conn.has_input()
90         rw.new_blob(s2)
91         assert rw.objcache.exists(s1sha)
92         assert rw.objcache.exists(s2sha)
93         rw.new_blob(s3)
94         assert len(glob.glob(c.cachedir+IDX_PAT)) == 2
95         rw.close()
96         assert len(glob.glob(c.cachedir+IDX_PAT)) == 3
97
98
99 def test_dumb_client_server(tmpdir):
100     environ[b'BUP_DIR'] = bupdir = tmpdir
101     git.init_repo(bupdir)
102     open(git.repo(b'bup-dumb-server'), 'w').close()
103
104     lw = git.PackWriter()
105     lw.new_blob(s1)
106     lw.close()
107
108     with client.Client(bupdir, create=True) as c:
109         rw = c.new_packwriter()
110         assert len(glob.glob(c.cachedir+IDX_PAT)) == 1
111         rw.new_blob(s1)
112         assert len(glob.glob(c.cachedir+IDX_PAT)) == 1
113         rw.new_blob(s2)
114         rw.close()
115         assert len(glob.glob(c.cachedir+IDX_PAT)) == 2
116
117
118 def test_midx_refreshing(tmpdir):
119     environ[b'BUP_DIR'] = bupdir = tmpdir
120     git.init_repo(bupdir)
121     with client.Client(bupdir, create=True) as c:
122         rw = c.new_packwriter()
123         rw.new_blob(s1)
124         p1base = rw.breakpoint()
125         p1name = os.path.join(c.cachedir, p1base)
126         s1sha = rw.new_blob(s1)  # should not be written; it's already in p1
127         s2sha = rw.new_blob(s2)
128         p2base = rw.close()
129         p2name = os.path.join(c.cachedir, p2base)
130         del rw
131
132     pi = git.PackIdxList(bupdir + b'/objects/pack')
133     assert len(pi.packs) == 2
134     pi.refresh()
135     assert len(pi.packs) == 2
136     assert sorted([os.path.basename(i.name) for i in pi.packs]) == sorted([p1base, p2base])
137
138     p1 = git.open_idx(p1name)
139     assert p1.exists(s1sha)
140     p2 = git.open_idx(p2name)
141     assert not p2.exists(s1sha)
142     assert p2.exists(s2sha)
143
144     subprocess.call([path.exe(), b'midx', b'-f'])
145     pi.refresh()
146     assert len(pi.packs) == 1
147     pi.refresh(skip_midx=True)
148     assert len(pi.packs) == 2
149     pi.refresh(skip_midx=False)
150     assert len(pi.packs) == 1
151
152
153 def test_remote_parsing():
154     tests = (
155         (b':/bup', (b'file', None, None, b'/bup')),
156         (b'file:///bup', (b'file', None, None, b'/bup')),
157         (b'192.168.1.1:/bup', (b'ssh', b'192.168.1.1', None, b'/bup')),
158         (b'ssh://192.168.1.1:2222/bup', (b'ssh', b'192.168.1.1', b'2222', b'/bup')),
159         (b'ssh://[ff:fe::1]:2222/bup', (b'ssh', b'ff:fe::1', b'2222', b'/bup')),
160         (b'bup://foo.com:1950', (b'bup', b'foo.com', b'1950', None)),
161         (b'bup://foo.com:1950/bup', (b'bup', b'foo.com', b'1950', b'/bup')),
162         (b'bup://[ff:fe::1]/bup', (b'bup', b'ff:fe::1', None, b'/bup')),)
163     for remote, values in tests:
164         assert client.parse_remote(remote) == values
165
166     with pytest.raises(client.ClientError):
167         client.parse_remote(b'http://asdf.com/bup')