From 81780adef861d7a2be6b198589cecc059f0d01a7 Mon Sep 17 00:00:00 2001
From: Luong Anh Tuan <tuanla@vn.fujitsu.com>
Date: Thu, 15 Dec 2016 13:21:53 +0700
Subject: [PATCH] Use oslo_serialization.base64 to follow OpenStack Python3

This patch replaces python standard base64 library call to
oslo_serialization.base64 to follow OpenStack Python3 porting
standard [1].

Use base64 encoding takes 8-bit binary byte data and encodes it. On
Python3, A string is a sequence of Unicode characters thus base64 has
no idea what to do with Unicode data, it's not 8-bit[2]. We use
oslo_serialization.base64 for both python2 and python3.

[1] https://wiki.openstack.org/wiki/Python3
[2] http://stackoverflow.com/questions/8908287/base64-encoding-in-python-3

Change-Id: I1dafe878a71f37d223eaf58ea8e2e8c6b4422a14
---
 ironic_python_agent/tests/unit/test_utils.py | 4 ++--
 ironic_python_agent/utils.py                 | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/ironic_python_agent/tests/unit/test_utils.py b/ironic_python_agent/tests/unit/test_utils.py
index ebfd3071d..487e6aeee 100644
--- a/ironic_python_agent/tests/unit/test_utils.py
+++ b/ironic_python_agent/tests/unit/test_utils.py
@@ -13,7 +13,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import base64
 import errno
 import glob
 import io
@@ -27,6 +26,7 @@ import testtools
 from ironic_lib import utils as ironic_utils
 import mock
 from oslo_concurrency import processutils
+from oslo_serialization import base64
 from oslotest import base as test_base
 
 from ironic_python_agent import errors
@@ -386,7 +386,7 @@ class TestUtils(testtools.TestCase):
         io_dict = {'fake-name': io.BytesIO(bytes(contents))}
         data = utils.gzip_and_b64encode(io_dict=io_dict)
 
-        res = io.BytesIO(base64.b64decode(data))
+        res = io.BytesIO(base64.decode_as_bytes(data))
         with tarfile.open(fileobj=res) as tar:
             members = [(m.name, m.size) for m in tar]
             self.assertEqual([('fake-name', len(contents))], members)
diff --git a/ironic_python_agent/utils.py b/ironic_python_agent/utils.py
index e2ccb6810..6045f6b49 100644
--- a/ironic_python_agent/utils.py
+++ b/ironic_python_agent/utils.py
@@ -12,7 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import base64
 import copy
 import errno
 import glob
@@ -27,6 +26,7 @@ import time
 from ironic_lib import utils as ironic_utils
 from oslo_concurrency import processutils
 from oslo_log import log as logging
+from oslo_serialization import base64
 from oslo_utils import units
 from six.moves.urllib import parse
 
@@ -383,7 +383,7 @@ def gzip_and_b64encode(io_dict=None, file_list=None):
                 tar.add(f)
 
         fp.seek(0)
-        return base64.b64encode(fp.getvalue())
+        return base64.encode_as_bytes(fp.getvalue())
 
 
 def collect_system_logs(journald_max_lines=None):