Skip to content

Commit 4988289

Browse files
committed
Use Telnet.expect() to test for success and failure
Make use of Telnet.expect() to match against the success (END) and failure case (ERROR) in the same call. Update protocol tests to handle patching expect() with output that was previously returned by read_until().
1 parent 08dfd40 commit 4988289

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

django_elasticache/cluster_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ def get_cluster_info(host, port, timeout=3):
4040
else:
4141
cmd = b'get AmazonElastiCache:cluster\n'
4242
client.write(cmd)
43-
res = client.read_until(b'\n\r\nEND\r\n', timeout)
43+
regex_index, match_object, res = client.expect([
44+
re.compile(b'\n\r\nEND\r\n'),
45+
re.compile(b'ERROR\r\n')
46+
], timeout)
4447
client.close()
4548

46-
if res == 'ERROR\r\n':
49+
if res == b'ERROR\r\n':
4750
return {
4851
'version': version,
4952
'nodes': [

tests/test_protocol.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,44 @@
88
from unittest.mock import patch, call, MagicMock
99

1010

11-
TEST_PROTOCOL_1 = [
11+
TEST_PROTOCOL_1_READ_UNTIL = [
1212
b'VERSION 1.4.14',
13-
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1413
]
1514

16-
TEST_PROTOCOL_2 = [
15+
TEST_PROTOCOL_1_EXPECT = [
16+
(0, None, b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n'), # NOQA
17+
]
18+
19+
TEST_PROTOCOL_2_READ_UNTIL = [
1720
b'VERSION 1.4.13',
18-
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1921
]
2022

21-
TEST_PROTOCOL_3 = [
23+
TEST_PROTOCOL_2_EXPECT = [
24+
(0, None, b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n'), # NOQA
25+
]
26+
27+
TEST_PROTOCOL_3_READ_UNTIL = [
2228
b'VERSION 1.4.14 (Ubuntu)',
23-
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
2429
]
2530

26-
TEST_PROTOCOL_4 = [
31+
TEST_PROTOCOL_3_EXPECT = [
32+
(0, None, b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n'), # NOQA
33+
]
34+
35+
TEST_PROTOCOL_4_READ_UNTIL = [
2736
b'VERSION 1.4.34',
28-
b'ERROR\r\n',
37+
]
38+
39+
TEST_PROTOCOL_4_EXPECT = [
40+
(0, None, b'ERROR\r\n'),
2941
]
3042

3143

3244
@patch('django_elasticache.cluster_utils.Telnet')
3345
def test_happy_path(Telnet):
3446
client = Telnet.return_value
35-
client.read_until.side_effect = TEST_PROTOCOL_1
47+
client.read_until.side_effect = TEST_PROTOCOL_1_READ_UNTIL
48+
client.expect.side_effect = TEST_PROTOCOL_1_EXPECT
3649
info = get_cluster_info('', 0)
3750
eq_(info['version'], 1)
3851
eq_(info['nodes'], ['ip:port', 'host:port'])
@@ -47,7 +60,8 @@ def test_bad_protocol():
4760
@patch('django_elasticache.cluster_utils.Telnet')
4861
def test_last_versions(Telnet):
4962
client = Telnet.return_value
50-
client.read_until.side_effect = TEST_PROTOCOL_1
63+
client.read_until.side_effect = TEST_PROTOCOL_1_READ_UNTIL
64+
client.expect.side_effect = TEST_PROTOCOL_1_EXPECT
5165
get_cluster_info('', 0)
5266
client.write.assert_has_calls([
5367
call(b'version\n'),
@@ -58,7 +72,8 @@ def test_last_versions(Telnet):
5872
@patch('django_elasticache.cluster_utils.Telnet')
5973
def test_prev_versions(Telnet):
6074
client = Telnet.return_value
61-
client.read_until.side_effect = TEST_PROTOCOL_2
75+
client.read_until.side_effect = TEST_PROTOCOL_2_READ_UNTIL
76+
client.expect.side_effect = TEST_PROTOCOL_2_EXPECT
6277
get_cluster_info('', 0)
6378
client.write.assert_has_calls([
6479
call(b'version\n'),
@@ -69,7 +84,8 @@ def test_prev_versions(Telnet):
6984
@patch('django_elasticache.cluster_utils.Telnet')
7085
def test_ubuntu_protocol(Telnet):
7186
client = Telnet.return_value
72-
client.read_until.side_effect = TEST_PROTOCOL_3
87+
client.read_until.side_effect = TEST_PROTOCOL_3_READ_UNTIL
88+
client.expect.side_effect = TEST_PROTOCOL_3_EXPECT
7389

7490
try:
7591
get_cluster_info('', 0)
@@ -85,7 +101,8 @@ def test_ubuntu_protocol(Telnet):
85101
@patch('django_elasticache.cluster_utils.Telnet')
86102
def test_no_configuration_protocol_support(Telnet):
87103
client = Telnet.return_value
88-
client.read_until.side_effect = TEST_PROTOCOL_4
104+
client.read_until.side_effect = TEST_PROTOCOL_4_READ_UNTIL
105+
client.expect.side_effect = TEST_PROTOCOL_4_EXPECT
89106
info = get_cluster_info('test', 0)
90107
client.write.assert_has_calls([
91108
call(b'version\n'),

0 commit comments

Comments
 (0)