In Py3 decode the output of base64.decode

The return from a base64.encode in python 3 is bytes. Bytes cannot be
json.dumps() so when we base64 encode some data we need to convert it
to a string before passing it through to the request layer.

This isn't shown by unit tests because the fakeclient mocking layer
intercepts the request before it gets to the json encoding code.

Closes-Bug: #1323450
Change-Id: Ibbbb24f64c17069178e3bf0ee9998b806bc629ff
This commit is contained in:
Jamie Lennox 2014-05-27 10:30:47 +10:00
parent c7c653d513
commit ecebc308b3
4 changed files with 14 additions and 12 deletions

View File

@ -159,8 +159,8 @@ class ShellTest(utils.TestCase):
def test_boot_user_data(self):
testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
data = open(testfile).read()
expected_file_data = base64.b64encode(data.encode('utf-8'))
data = open(testfile).read().encode('utf-8')
expected_file_data = base64.b64encode(data).decode('utf-8')
self.run_command(
'boot --flavor 1 --image 1 --user_data %s some-server' % testfile)
self.assert_called_anytime(
@ -533,7 +533,7 @@ class ShellTest(utils.TestCase):
def test_boot_files(self):
testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
data = open(testfile).read()
expected_file_data = base64.b64encode(data.encode('utf-8'))
expected = base64.b64encode(data.encode('utf-8')).decode('utf-8')
cmd = ('boot some-server --flavor 1 --image 1'
' --file /tmp/foo=%s --file /tmp/bar=%s')
@ -548,8 +548,8 @@ class ShellTest(utils.TestCase):
'min_count': 1,
'max_count': 1,
'personality': [
{'path': '/tmp/bar', 'contents': expected_file_data},
{'path': '/tmp/foo', 'contents': expected_file_data},
{'path': '/tmp/bar', 'contents': expected},
{'path': '/tmp/foo', 'contents': expected},
]
}},
)

View File

@ -226,6 +226,7 @@ class ShellTest(utils.TestCase):
mock_open.assert_called_once_with(testfile)
user_data = base64.b64encode(file_text.encode('utf-8')).decode('utf-8')
self.assert_called_anytime(
'POST', '/servers',
{'server': {
@ -234,9 +235,7 @@ class ShellTest(utils.TestCase):
'image_ref': '1',
'os-multiple-create:min_count': 1,
'os-multiple-create:max_count': 1,
'os-user-data:user_data': base64.b64encode(
file_text.encode('utf-8'))
}},
'os-user-data:user_data': user_data}},
)
def test_boot_avzone(self):

View File

@ -455,7 +455,8 @@ class ServerManager(base.BootingManagerWithFind):
else:
userdata = strutils.safe_encode(userdata)
body["server"]["user_data"] = base64.b64encode(userdata)
userdata_b64 = base64.b64encode(userdata).decode('utf-8')
body["server"]["user_data"] = userdata_b64
if meta:
body["server"]["metadata"] = meta
if reservation_id:
@ -491,9 +492,11 @@ class ServerManager(base.BootingManagerWithFind):
data = file_or_string.read()
else:
data = file_or_string
cont = base64.b64encode(data.encode('utf-8')).decode('utf-8')
personality.append({
'path': filepath,
'contents': base64.b64encode(data.encode('utf-8')),
'contents': cont,
})
if availability_zone:

View File

@ -404,8 +404,8 @@ class ServerManager(base.BootingManagerWithFind):
else:
userdata = strutils.safe_encode(userdata)
body["server"][
"os-user-data:user_data"] = base64.b64encode(userdata)
data = base64.b64encode(userdata).decode('utf-8')
body["server"]["os-user-data:user_data"] = data
if meta:
body["server"]["metadata"] = meta
if reservation_id: