Fix Python 3 issues in backup

* Replace (int, long) with six.integer_types: the "long" type has been
  removed in Python 3
* Replace str.encode("bas64") with base64.encodestring(str), base64
  text codec has been removed in Python 3. Same change for decode
  (base64.decodestring)
* On Python 3, encode JSON to UTF-8
* tox.ini: add the following tests to Python 3

  - cinder.tests.unit.test_backup
  - cinder.tests.unit.test_backup_driver_base

Blueprint cinder-python3
Change-Id: I86e04f8fbe9a3ce8849fd141dc3ee914e73c8796
This commit is contained in:
Victor Stinner 2015-06-23 11:20:47 +02:00
parent cde57ee995
commit 300d441d51
4 changed files with 10 additions and 4 deletions

@ -16,6 +16,7 @@
"""Base class for all backup drivers."""
import abc
import base64
from oslo_config import cfg
from oslo_log import log as logging
@ -355,7 +356,9 @@ class BackupDriver(base.Base):
:returns backup_url - a string describing the backup record
"""
retval = jsonutils.dumps(backup)
return retval.encode("base64")
if six.PY3:
retval = retval.encode('utf-8')
return base64.encodestring(retval)
def import_record(self, backup_url):
"""Import and verify backup record.
@ -367,7 +370,7 @@ class BackupDriver(base.Base):
:param backup_url: driver specific backup record string
:returns dictionary object with database updates
"""
return jsonutils.loads(backup_url.decode("base64"))
return jsonutils.loads(base64.decodestring(backup_url))
@six.add_metaclass(abc.ABCMeta)

@ -347,7 +347,7 @@ class DbQuotaDriver(object):
# Set up the reservation expiration
if expire is None:
expire = CONF.reservation_expire
if isinstance(expire, (int, long)):
if isinstance(expire, six.integer_types):
expire = datetime.timedelta(seconds=expire)
if isinstance(expire, datetime.timedelta):
expire = timeutils.utcnow() + expire

@ -14,6 +14,7 @@
# under the License.
""" Tests for the backup service base driver. """
import base64
import uuid
import mock
@ -74,7 +75,7 @@ class BackupBaseDriverTestCase(test.TestCase):
def test_export_record(self):
export_string = self.driver.export_record(self.backup)
export_dict = jsonutils.loads(export_string.decode("base64"))
export_dict = jsonutils.loads(base64.decodestring(export_string))
# Make sure we don't lose data when converting to string
for key in _backup_db_fields:
self.assertTrue(key in export_dict)

@ -30,6 +30,8 @@ downloadcache = ~/cache/pip
commands =
python -m testtools.run \
cinder.tests.unit.test_api_urlmap \
cinder.tests.unit.test_backup \
cinder.tests.unit.test_backup_driver_base \
cinder.tests.unit.test_block_device \
cinder.tests.unit.test_cloudbyte \
cinder.tests.unit.test_conf \