From 300d441d5148c3f8437700298f88982185ebe73f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 23 Jun 2015 11:20:47 +0200 Subject: [PATCH] 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 --- cinder/backup/driver.py | 7 +++++-- cinder/quota.py | 2 +- cinder/tests/unit/test_backup_driver_base.py | 3 ++- tox.ini | 2 ++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cinder/backup/driver.py b/cinder/backup/driver.py index 3317118f6..622e2ef4a 100644 --- a/cinder/backup/driver.py +++ b/cinder/backup/driver.py @@ -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) diff --git a/cinder/quota.py b/cinder/quota.py index 18b31934f..6a93af892 100644 --- a/cinder/quota.py +++ b/cinder/quota.py @@ -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 diff --git a/cinder/tests/unit/test_backup_driver_base.py b/cinder/tests/unit/test_backup_driver_base.py index f95ad9713..b19664e17 100644 --- a/cinder/tests/unit/test_backup_driver_base.py +++ b/cinder/tests/unit/test_backup_driver_base.py @@ -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) diff --git a/tox.ini b/tox.ini index 44399fa41..b6e0f3994 100644 --- a/tox.ini +++ b/tox.ini @@ -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 \ -- 2.45.2