Rearrange client initialization so we can check that it's choosing the
expected index-cache ids. If these were to inadvertently change, the
client would end up duplicating the entire index-cache for a given
host.
---
Proposed for 0.33.x as a backstop, and we'll also transfer the tests
to main and fix the backward incompatibilities that has revealed.
lib/bup/client.py | 35 ++++++++++++++++++++---------------
test/int/test_client.py | 24 ++++++++++++++++++++++++
2 files changed, 44 insertions(+), 15 deletions(-)
diff --git a/lib/bup/client.py b/lib/bup/client.py
index 621c8b6f..18ecdc4c 100644
--- a/lib/bup/client.py
+++ b/lib/bup/client.py
@@ -67,27 +67,32 @@ def parse_remote(remote):
return b'ssh', rs[0], None, rs[1]
+def _remote_parts(reverse, remote):
+ if not reverse:
+ scheme, host, port, path = parse_remote(remote)
+ else:
+ assert not remote, remote
+ scheme, host, port, path = parse_remote(reverse + b':')
+ # The b'None' here matches python2's behavior of b'%s' % None == 'None',
+ # python3 will (as of version 3.7.5) do the same for str ('%s' % None),
+ # but crashes instead when doing b'%s' % None.
+ cache_id = re.sub(br'[^@\w]', b'_',
+ b'%s:%s' % (b'None' if host is None else host,
+ b'None' if path is None else path))
+ return scheme, host, port, path, cache_id
+
+
class Client:
def __init__(self, remote, create=False):
self.closed = False
self._busy = self.conn = None
self.sock = self.p = self.pout = self.pin = None
try:
- is_reverse = environ.get(b'BUP_SERVER_REVERSE')
- if is_reverse:
- assert(not remote)
- remote = b'%s:' % is_reverse
- (self.protocol, self.host, self.port, self.dir) = parse_remote(remote)
- # The b'None' here matches python2's behavior of b'%s' % None == 'None',
- # python3 will (as of version 3.7.5) do the same for str ('%s' % None),
- # but crashes instead when doing b'%s' % None.
- cachehost = b'None' if self.host is None else self.host
- cachedir = b'None' if self.dir is None else self.dir
- self.cachedir = git.repo(b'index-cache/%s'
- % re.sub(br'[^@\w]',
- b'_',
- b'%s:%s' % (cachehost, cachedir)))
- if is_reverse:
+ reverse = environ.get(b'BUP_SERVER_REVERSE')
+ self.protocol, self.host, self.port, self.dir, cache_id = \
+ _remote_parts(reverse, remote)
+ self.cachedir = git.repo(b'index-cache/' + cache_id)
+ if reverse:
self.pout = os.fdopen(3, 'rb')
self.pin = os.fdopen(4, 'wb')
self.conn = Conn(self.pout, self.pin)
diff --git a/test/int/test_client.py b/test/int/test_client.py
index 40580b33..3d7b67b6 100644
--- a/test/int/test_client.py
+++ b/test/int/test_client.py
@@ -157,3 +157,27 @@ def test_remote_parsing():
with pytest.raises(client.ClientError):
client.parse_remote(b'
http://asdf.com/bup')
+
+
+def test_remote_cache_ids():
+ def cid(reverse, remote):
+ scheme, host, port, path, cid = client._remote_parts(reverse, remote)
+ return cid
+ with pytest.raises(AssertionError):
+ assert cid(b'x', b'y')
+ with pytest.raises(TypeError):
+ assert cid(None, None)
+ # remotes
+ assert cid(None, b'') == b'None_'
+ assert cid(None, b':') == b'None_'
+ assert cid(None, b'-') == b'None__'
+ assert cid(None, b'h:') == b'h_'
+ assert cid(None, b':p') == b'None_p'
+ # reverses
+ with pytest.raises(TypeError): # currently same as None, None case
+ assert cid(b'', None)
+ assert cid(b':', None) == b'None__'
+ assert cid(b'-', None) == b'None_'
+ assert cid(b'x:', None) == b'x__'
+ assert cid(b':x', None) == b'None_x_'
+ assert cid(b'xy', None) == b'xy_'
--
2.47.3