From fca244f117f9de2d956619482cba0147b0b7afa1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 9 Feb 2016 11:47:31 +0100 Subject: [PATCH] Port backup drivers to Python 3 * PosixBackupDriver: open file in binary mode (to read/write). Update test_backup_posix.py for that. * test_backup_nfs: buffer() doesn't exist and is no more needed on Python 3, only use buffer() on Python 2. * tests-py3.txt: add cinder.tests.unit.backup Partial-Implements: blueprint cinder-python3 Change-Id: I250d7378547df474f3c78024a737a3e2fa9bbaf4 --- cinder/backup/drivers/posix.py | 4 ++-- .../unit/backup/drivers/test_backup_nfs.py | 20 ++++++++++++------- .../unit/backup/drivers/test_backup_posix.py | 4 ++-- tests-py3.txt | 4 ++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cinder/backup/drivers/posix.py b/cinder/backup/drivers/posix.py index 0c80d9b66fd..5a00e223128 100644 --- a/cinder/backup/drivers/posix.py +++ b/cinder/backup/drivers/posix.py @@ -108,7 +108,7 @@ class PosixBackupDriver(chunkeddriver.ChunkedBackupDriver): def get_object_writer(self, container, object_name, extra_metadata=None): path = os.path.join(self.backup_path, container, object_name) - f = open(path, 'w') + f = open(path, 'wb') permissions = ( stat.S_IRUSR | stat.S_IWUSR | @@ -119,7 +119,7 @@ class PosixBackupDriver(chunkeddriver.ChunkedBackupDriver): def get_object_reader(self, container, object_name, extra_metadata=None): path = os.path.join(self.backup_path, container, object_name) - return open(path, 'r') + return open(path, 'rb') def delete_object(self, container, object_name): # TODO(tbarron): clean up the container path if it is empty diff --git a/cinder/tests/unit/backup/drivers/test_backup_nfs.py b/cinder/tests/unit/backup/drivers/test_backup_nfs.py index a2bcb4f149a..6c3cae67a1a 100644 --- a/cinder/tests/unit/backup/drivers/test_backup_nfs.py +++ b/cinder/tests/unit/backup/drivers/test_backup_nfs.py @@ -27,6 +27,7 @@ import zlib import mock from os_brick.remotefs import remotefs as remotefs_brick from oslo_config import cfg +import six from cinder.backup.drivers import nfs from cinder import context @@ -620,11 +621,17 @@ class BackupNFSSwiftBasedTestCase(test.TestCase): self.assertEqual(compressor, bz2) self.assertRaises(ValueError, service._get_compressor, 'fake') + def create_buffer(self, size): + # Set up buffer of zeroed bytes + fake_data = bytearray(size) + if six.PY2: + # On Python 2, zlib.compressor() accepts buffer, but not bytearray + fake_data = buffer(fake_data) + return fake_data + def test_prepare_output_data_effective_compression(self): service = nfs.NFSBackupDriver(self.ctxt) - # Set up buffer of 128 zeroed bytes - fake_data = buffer(bytearray(128)) - + fake_data = self.create_buffer(128) result = service._prepare_output_data(fake_data) self.assertEqual('zlib', result[0]) @@ -633,8 +640,7 @@ class BackupNFSSwiftBasedTestCase(test.TestCase): def test_prepare_output_data_no_compresssion(self): self.flags(backup_compression_algorithm='none') service = nfs.NFSBackupDriver(self.ctxt) - # Set up buffer of 128 zeroed bytes - fake_data = buffer(bytearray(128)) + fake_data = self.create_buffer(128) result = service._prepare_output_data(fake_data) @@ -643,8 +649,8 @@ class BackupNFSSwiftBasedTestCase(test.TestCase): def test_prepare_output_data_ineffective_compression(self): service = nfs.NFSBackupDriver(self.ctxt) - # Set up buffer of 128 zeroed bytes - fake_data = buffer(bytearray(128)) + fake_data = self.create_buffer(128) + # Pre-compress so that compression in the driver will be ineffective. already_compressed_data = service.compressor.compress(fake_data) diff --git a/cinder/tests/unit/backup/drivers/test_backup_posix.py b/cinder/tests/unit/backup/drivers/test_backup_posix.py index 2f3431d7370..825e59ccf09 100644 --- a/cinder/tests/unit/backup/drivers/test_backup_posix.py +++ b/cinder/tests/unit/backup/drivers/test_backup_posix.py @@ -159,14 +159,14 @@ class PosixBackupDriverTestCase(test.TestCase): self.driver.get_object_writer(FAKE_CONTAINER, FAKE_OBJECT_NAME) os.chmod.assert_called_once_with(FAKE_OBJECT_PATH, 0o660) - builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'w') + builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'wb') def test_get_object_reader(self): self.mock_object(builtins, 'open', mock.mock_open()) self.driver.get_object_reader(FAKE_CONTAINER, FAKE_OBJECT_NAME) - builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'r') + builtins.open.assert_called_once_with(FAKE_OBJECT_PATH, 'rb') def test_delete_object(self): self.mock_object(os, 'remove') diff --git a/tests-py3.txt b/tests-py3.txt index b275f573467..cc2d520cff9 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -28,6 +28,10 @@ cinder.tests.unit.api.test_extensions cinder.tests.unit.api.test_versions cinder.tests.unit.api.test_xmlutil cinder.tests.unit.api.v2.test_volumes +cinder.tests.unit.backup.drivers.test_backup_glusterfs +cinder.tests.unit.backup.drivers.test_backup_nfs +cinder.tests.unit.backup.drivers.test_backup_posix +cinder.tests.unit.backup.test_rpcapi cinder.tests.unit.image.test_cache cinder.tests.unit.image.test_glance cinder.tests.unit.keymgr.test_barbican