]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port backup drivers to Python 3
authorVictor Stinner <vstinner@redhat.com>
Tue, 9 Feb 2016 10:47:31 +0000 (11:47 +0100)
committerVictor Stinner <vstinner@redhat.com>
Tue, 9 Feb 2016 10:52:42 +0000 (11:52 +0100)
* 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
cinder/tests/unit/backup/drivers/test_backup_nfs.py
cinder/tests/unit/backup/drivers/test_backup_posix.py
tests-py3.txt

index 0c80d9b66fd6bdcf0c2acf7265cc8e71bb42415d..5a00e2231282b67d9da278769509e063942fa20c 100644 (file)
@@ -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
index a2bcb4f149a77bcac20ca6841eb732c996460ca8..6c3cae67a1a9843d171dded76ca2c96103c65bf8 100644 (file)
@@ -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)
 
index 2f3431d7370735913e998ff312826d28ef717781..825e59ccf097b3282410ff8e4eed751ad421d8df 100644 (file)
@@ -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')
index b275f5734671284af19d4b634d966d38e158f57c..cc2d520cff9668166b4bb2e6b7d798a10a564166 100644 (file)
@@ -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