Skip to content

Commit

Permalink
Merge pull request #1 from 18F/py3
Browse files Browse the repository at this point in the history
Python3 support + django 1.7 in tests
  • Loading branch information
kaitlin committed Dec 30, 2014
2 parents 26f3cbe + 6f13633 commit d535423
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -12,9 +12,9 @@ Requirements
------------

* pylibmc
* Django 1.3+.
* Django 1.5+.

It was written and tested on Python 2.7.
It was written and tested on Python 2.7 and 3.4.

Installation
------------
Expand Down
26 changes: 14 additions & 12 deletions django_elasticache/cluster_utils.py
Expand Up @@ -2,6 +2,7 @@
utils for discovery cluster
"""
from distutils.version import StrictVersion
from django.utils.encoding import smart_text
import re
from telnetlib import Telnet

Expand All @@ -28,20 +29,20 @@ def get_cluster_info(host, port):
}
"""
client = Telnet(host, int(port))
client.write('version\n')
res = client.read_until('\r\n').strip()
version_list = res.split(' ')
if len(version_list) != 2 or version_list[0] != 'VERSION':
client.write(b'version\n')
res = client.read_until(b'\r\n').strip()
version_list = res.split(b' ')
if len(version_list) != 2 or version_list[0] != b'VERSION':
raise WrongProtocolData('version', res)
version = version_list[1]
if StrictVersion(version) >= StrictVersion('1.4.14'):
cmd = 'config get cluster\n'
if StrictVersion(smart_text(version)) >= StrictVersion('1.4.14'):
cmd = b'config get cluster\n'
else:
cmd = 'get AmazonElastiCache:cluster\n'
cmd = b'get AmazonElastiCache:cluster\n'
client.write(cmd)
res = client.read_until('\n\r\nEND\r\n')
res = client.read_until(b'\n\r\nEND\r\n')
client.close()
ls = filter(None, re.compile(r'\r?\n').split(res))
ls = list(filter(None, re.compile(br'\r?\n').split(res)))
if len(ls) != 4:
raise WrongProtocolData(cmd, res)

Expand All @@ -51,9 +52,10 @@ def get_cluster_info(host, port):
raise WrongProtocolData(cmd, res)
nodes = []
try:
for node in ls[2].split(' '):
host, ip, port = node.split('|')
nodes.append('{}:{}'.format(ip or host, port))
for node in ls[2].split(b' '):
host, ip, port = node.split(b'|')
nodes.append('{}:{}'.format(smart_text(ip or host),
smart_text(port)))
except ValueError:
raise WrongProtocolData(cmd, res)
return {
Expand Down
13 changes: 11 additions & 2 deletions tests/test_backend.py
@@ -1,6 +1,15 @@
from django.conf import global_settings
from mock import patch, Mock
from django.conf import global_settings, settings
from nose.tools import eq_, raises
import sys
if sys.version < '3':
from mock import patch, Mock
else:
from unittest.mock import patch, Mock


# Initialize django 1.7
settings.configure()
global_settings.configured = True


@patch('django.conf.settings', global_settings)
Expand Down
23 changes: 14 additions & 9 deletions tests/test_protocol.py
@@ -1,16 +1,21 @@
from mock import patch, call, MagicMock
from django_elasticache.cluster_utils import (
get_cluster_info, WrongProtocolData)
from nose.tools import eq_, raises
import sys
if sys.version < '3':
from mock import patch, call, MagicMock
else:
from unittest.mock import patch, call, MagicMock


TEST_PROTOCOL_1 = [
'VERSION 1.4.14',
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
b'VERSION 1.4.14',
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
]

TEST_PROTOCOL_2 = [
'VERSION 1.4.13',
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
b'VERSION 1.4.13',
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
]


Expand All @@ -35,8 +40,8 @@ def test_last_versions(Telnet):
client.read_until.side_effect = TEST_PROTOCOL_1
get_cluster_info('', 0)
client.write.assert_has_calls([
call('version\n'),
call('config get cluster\n'),
call(b'version\n'),
call(b'config get cluster\n'),
])


Expand All @@ -46,6 +51,6 @@ def test_prev_versions(Telnet):
client.read_until.side_effect = TEST_PROTOCOL_2
get_cluster_info('', 0)
client.write.assert_has_calls([
call('version\n'),
call('get AmazonElastiCache:cluster\n'),
call(b'version\n'),
call(b'get AmazonElastiCache:cluster\n'),
])

0 comments on commit d535423

Please sign in to comment.