File tree Expand file tree Collapse file tree 2 files changed +15
-16
lines changed Expand file tree Collapse file tree 2 files changed +15
-16
lines changed Original file line number Diff line number Diff line change 22utils for discovery cluster
33"""
44from distutils .version import StrictVersion
5+ import re
56from 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 \n END\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 :
Original file line number Diff line number Diff line change 1- from mock import patch , call
1+ from mock import patch , call , MagicMock
22from django_elasticache .cluster_utils import (
33 get_cluster_info , WrongProtocolData )
44from nose .tools import eq_ , raises
55
66TEST_PROTOCOL_1 = [
77 'VERSION 1.4.14' ,
8- '' ,
9- '1' ,
10- 'hostname|ip-address|port hostname||port'
8+ 'CONFIG cluster 0 138\r \n 1\n host|ip|port host||port\n \r \n END\r \n ' ,
119]
1210
1311TEST_PROTOCOL_2 = [
1412 'VERSION 1.4.13' ,
15- '' ,
16- '1' ,
17- 'hostname|ip-address|port hostname||port'
13+ 'CONFIG cluster 0 138\r \n 1\n host|ip|port host||port\n \r \n END\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
You can’t perform that action at this time.
0 commit comments