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