Skip to content

Commit 3249a2f

Browse files
committed
change protocol parser
1 parent 8876956 commit 3249a2f

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

django_elasticache/cluster_utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
utils for discovery cluster
33
"""
44
from distutils.version import StrictVersion
5+
import re
56
from telnetlib import Telnet
67

78

@@ -38,17 +39,19 @@ def get_cluster_info(host, port):
3839
else:
3940
cmd = 'get AmazonElastiCache:cluster\n'
4041
client.write(cmd)
41-
client.read_until('\r\n')
42-
res = client.read_until('\r\n').strip()
42+
res = client.read_until('\n\r\nEND\r\n')
43+
client.close()
44+
ls = filter(None, re.compile(r'\r?\n').split(res))
45+
if len(ls) != 4:
46+
raise WrongProtocolData(cmd, res)
47+
4348
try:
44-
version = int(res)
49+
version = int(ls[1])
4550
except ValueError:
4651
raise WrongProtocolData(cmd, res)
47-
res = client.read_until('\r\n').strip()
48-
client.close()
4952
nodes = []
5053
try:
51-
for node in res.split(' '):
54+
for node in ls[2].split(' '):
5255
host, ip, port = node.split('|')
5356
nodes.append('{}:{}'.format(ip or host, port))
5457
except ValueError:

tests/test_protocol.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
from mock import patch, call
1+
from mock import patch, call, MagicMock
22
from django_elasticache.cluster_utils import (
33
get_cluster_info, WrongProtocolData)
44
from nose.tools import eq_, raises
55

66
TEST_PROTOCOL_1 = [
77
'VERSION 1.4.14',
8-
'',
9-
'1',
10-
'hostname|ip-address|port hostname||port'
8+
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
119
]
1210

1311
TEST_PROTOCOL_2 = [
1412
'VERSION 1.4.13',
15-
'',
16-
'1',
17-
'hostname|ip-address|port hostname||port'
13+
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1814
]
1915

2016

@@ -24,12 +20,12 @@ def test_happy_path(Telnet):
2420
client.read_until.side_effect = TEST_PROTOCOL_1
2521
info = get_cluster_info('', 0)
2622
eq_(info['version'], 1)
27-
eq_(info['nodes'], ['ip-address:port', 'hostname:port'])
23+
eq_(info['nodes'], ['ip:port', 'host:port'])
2824

2925

3026
@raises(WrongProtocolData)
31-
@patch('django_elasticache.cluster_utils.Telnet')
32-
def test_bad_protocol(Telnet):
27+
@patch('django_elasticache.cluster_utils.Telnet', MagicMock())
28+
def test_bad_protocol():
3329
get_cluster_info('', 0)
3430

3531

0 commit comments

Comments
 (0)