Skip to content

Commit 1956bab

Browse files
authored
Merge pull request #424 from eribeiro/411-connectstring-accept-list
KazooClient hosts list should accept a list of multiple endpoints.
2 parents 1960796 + 72a8d96 commit 1956bab

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

kazoo/hosts.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33

44
def collect_hosts(hosts):
5-
"""Collect a set of hosts and an optional chroot from a string."""
6-
host_ports, chroot = hosts.partition("/")[::2]
7-
chroot = "/" + chroot if chroot else None
5+
"""
6+
Collect a set of hosts and an optional chroot from
7+
a string or a list of strings.
8+
"""
9+
if isinstance(hosts, list):
10+
if hosts[-1].strip().startswith('/'):
11+
host_ports, chroot = hosts[:-1], hosts[-1]
12+
else:
13+
host_ports, chroot = hosts, None
14+
else:
15+
host_ports, chroot = hosts.partition("/")[::2]
16+
host_ports = host_ports.split(",")
17+
chroot = "/" + chroot if chroot else None
818

919
result = []
10-
for host_port in host_ports.split(","):
20+
for host_port in host_ports:
1121
# put all complexity of dealing with
1222
# IPv4 & IPv6 address:port on the urlsplit
1323
res = urllib_parse.urlsplit("xxx://" + host_port)

kazoo/tests/test_hosts.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest import TestCase
2+
from kazoo.hosts import collect_hosts
3+
4+
5+
class HostsTestCase(TestCase):
6+
7+
def test_ipv4(self):
8+
hosts, chroot = collect_hosts('127.0.0.1:2181, 192.168.1.2:2181, \
9+
132.254.111.10:2181')
10+
self.assertEquals([('127.0.0.1', 2181),
11+
('192.168.1.2', 2181),
12+
('132.254.111.10', 2181)], hosts)
13+
self.assertEquals(None, chroot)
14+
15+
hosts, chroot = collect_hosts(['127.0.0.1:2181',
16+
'192.168.1.2:2181',
17+
'132.254.111.10:2181'])
18+
self.assertEquals([('127.0.0.1', 2181),
19+
('192.168.1.2', 2181),
20+
('132.254.111.10', 2181)], hosts)
21+
self.assertEquals(None, chroot)
22+
23+
def test_ipv6(self):
24+
hosts, chroot = collect_hosts('[fe80::200:5aee:feaa:20a2]:2181')
25+
self.assertEquals([('fe80::200:5aee:feaa:20a2', 2181)], hosts)
26+
self.assertEquals(None, chroot)
27+
28+
hosts, chroot = collect_hosts(['[fe80::200:5aee:feaa:20a2]:2181'])
29+
self.assertEquals([('fe80::200:5aee:feaa:20a2', 2181)], hosts)
30+
self.assertEquals(None, chroot)
31+
32+
def test_hosts_list(self):
33+
34+
hosts, chroot = collect_hosts('zk01:2181, zk02:2181, zk03:2181')
35+
expected1 = [('zk01', 2181), ('zk02', 2181), ('zk03', 2181)]
36+
self.assertEquals(expected1, hosts)
37+
self.assertEquals(None, chroot)
38+
39+
hosts, chroot = collect_hosts(['zk01:2181', 'zk02:2181', 'zk03:2181'])
40+
self.assertEquals(expected1, hosts)
41+
self.assertEquals(None, chroot)
42+
43+
expected2 = '/test'
44+
hosts, chroot = collect_hosts('zk01:2181, zk02:2181, zk03:2181/test')
45+
self.assertEquals(expected1, hosts)
46+
self.assertEquals(expected2, chroot)
47+
48+
hosts, chroot = collect_hosts(['zk01:2181',
49+
'zk02:2181',
50+
'zk03:2181', '/test'])
51+
self.assertEquals(expected1, hosts)
52+
self.assertEquals(expected2, chroot)

0 commit comments

Comments
 (0)