2 from __future__ import absolute_import
3 import os, time, random, subprocess, glob
6 from bup import client, git, path
7 from bup.compat import bytes_from_uint, environ, range
12 s += bytes_from_uint(random.randrange(0,256))
23 def test_server_split_with_indexes(tmpdir):
24 environ[b'BUP_DIR'] = bupdir = tmpdir
26 with git.PackWriter() as lw, \
27 client.Client(bupdir, create=True) as c, \
28 c.new_packwriter() as rw:
37 def test_multiple_suggestions(tmpdir):
38 environ[b'BUP_DIR'] = bupdir = tmpdir
41 with git.PackWriter() as lw:
43 with git.PackWriter() as lw:
45 assert len(glob.glob(git.repo(b'objects/pack'+IDX_PAT))) == 2
47 with client.Client(bupdir, create=True) as c, \
48 c.new_packwriter() as rw:
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)
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)
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.
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
79 # Finally, abort this after a little while (about one
80 # second) just in case something's actually broken.
82 while (len(glob.glob(c.cachedir+IDX_PAT)) < 2 and
83 not c.conn.has_input() and n < 10):
86 assert len(glob.glob(c.cachedir+IDX_PAT)) == 2 or c.conn.has_input()
88 assert rw.objcache.exists(s1sha)
89 assert rw.objcache.exists(s2sha)
91 assert len(glob.glob(c.cachedir+IDX_PAT)) == 2
92 assert len(glob.glob(c.cachedir+IDX_PAT)) == 3
95 def test_dumb_client_server(tmpdir):
96 environ[b'BUP_DIR'] = bupdir = tmpdir
98 open(git.repo(b'bup-dumb-server'), 'w').close()
100 with git.PackWriter() as lw:
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
107 assert len(glob.glob(c.cachedir+IDX_PAT)) == 1
109 assert len(glob.glob(c.cachedir+IDX_PAT)) == 2
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:
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)
123 p2name = os.path.join(c.cachedir, p2base)
125 pi = git.PackIdxList(bupdir + b'/objects/pack')
126 assert len(pi.packs) == 2
128 assert len(pi.packs) == 2
129 assert sorted([os.path.basename(i.name) for i in pi.packs]) == sorted([p1base, p2base])
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)
137 subprocess.call([path.exe(), b'midx', b'-f'])
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
146 def test_remote_parsing():
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
159 with pytest.raises(client.ClientError):
160 client.parse_remote(b'http://asdf.com/bup')