Fix some python3 string issues in amphora agent

The amphora agent has some code that is not handling python3 strings
properly.
This patch resolves that issue.

Change-Id: Ib1af8287bf126aa2aa4626b5ac067bbe20eaa5d2
This commit is contained in:
German Eichberger 2016-12-16 11:47:33 -05:00 committed by Lubosz Kosnik (diltram)
parent 78b1a4a8b5
commit 264f3b58d1
6 changed files with 19 additions and 18 deletions

View File

@ -75,9 +75,9 @@ def compile_amphora_details():
def _get_version_of_installed_package(name):
cmd = "dpkg --status " + name
cmd = "dpkg --status {name}".format(name=name)
out = subprocess.check_output(cmd.split())
m = re.search('Version: .*', out)
m = re.search(b'Version: .*', out)
return m.group(0)[len('Version: '):]
@ -145,10 +145,7 @@ def _get_networks():
def get_interface(ip_addr):
try:
if six.PY2:
ip_version = ipaddress.ip_address(unicode(ip_addr)).version
else:
ip_version = ipaddress.ip_address(ip_addr).version
ip_version = ipaddress.ip_address(six.text_type(ip_addr)).version
except Exception:
return flask.make_response(
flask.jsonify(dict(message="Invalid IP address")), 400)

View File

@ -103,7 +103,7 @@ class Listener(object):
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
# mode 00600
mode = stat.S_IRUSR | stat.S_IWUSR
with os.fdopen(os.open(name, flags, mode), 'w') as file:
with os.fdopen(os.open(name, flags, mode), 'wb') as file:
b = stream.read(BUFFER)
while (b):
file.write(b)
@ -236,8 +236,9 @@ class Listener(object):
listener_id=listener_id, action=action))), 202)
details = (
'Configuration file is valid\nhaproxy daemon for {0} '.format(
listener_id) + 'started')
'Configuration file is valid\n'
'haproxy daemon for {0} started'.format(listener_id)
)
return flask.make_response(flask.jsonify(
dict(message='OK',
@ -380,7 +381,7 @@ class Listener(object):
flags = os.O_WRONLY | os.O_CREAT
# mode 00600
mode = stat.S_IRUSR | stat.S_IWUSR
with os.fdopen(os.open(file, flags, mode), 'w') as crt_file:
with os.fdopen(os.open(file, flags, mode), 'wb') as crt_file:
b = stream.read(BUFFER)
while (b):
crt_file.write(b)

View File

@ -102,8 +102,7 @@ class Server(object):
self.app.add_url_rule(rule=PATH_PREFIX + '/vrrp/<action>',
view_func=self.manage_service_vrrp,
methods=['PUT'])
self.app.add_url_rule(rule='/' + api_server.VERSION +
'/interface/<ip_addr>',
self.app.add_url_rule(rule=PATH_PREFIX + '/interface/<ip_addr>',
view_func=self.get_interface,
methods=['GET'])

View File

@ -15,6 +15,8 @@
import csv
import socket
import six
from octavia.common import constants as consts
@ -49,13 +51,14 @@ class HAProxyQuery(object):
raise Exception("HAProxy '{0}' query failed.".format(query))
try:
sock.send(query + '\n')
data = ''
sock.send(six.b(query + '\n'))
data = u''
while True:
x = sock.recv(1024)
if not x:
break
data += x
data += x.decode('ascii') if (
isinstance(x, six.binary_type)) else x
return data.rstrip()
finally:
sock.close()

View File

@ -108,7 +108,7 @@ class TestServerTestCase(base.TestCase):
data='test')
mode = stat.S_IRUSR | stat.S_IWUSR
mock_open.assert_called_with(file_name, flags, mode)
mock_fdopen.assert_called_with(123, 'w')
mock_fdopen.assert_called_with(123, 'wb')
self.assertEqual(202, rv.status_code)
handle = m()
handle.write.assert_called_once_with(six.b('test'))
@ -192,7 +192,7 @@ class TestServerTestCase(base.TestCase):
json.loads(rv.data.decode('utf-8')))
mode = stat.S_IRUSR | stat.S_IWUSR
mock_open.assert_called_with(file_name, flags, mode)
mock_fdopen.assert_called_with(123, 'w')
mock_fdopen.assert_called_with(123, 'wb')
handle = mock_fdopen()
handle.write.assert_called_with(six.b('test'))
mock_subprocess.assert_called_with(

View File

@ -15,6 +15,7 @@
import socket
import mock
import six
from octavia.amphorae.backends.utils import haproxy_query as query
import octavia.tests.unit.base as base
@ -75,7 +76,7 @@ class QueryTestCase(base.TestCase):
self.q._query('test')
sock.connect.assert_called_once_with('')
sock.send.assert_called_once_with('test' + '\n')
sock.send.assert_called_once_with(six.b('test\n'))
sock.recv.assert_called_with(1024)
self.assertTrue(sock.close.called)