]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port EMC scaleio to Python 3
authorVictor Stinner <vstinner@redhat.com>
Wed, 7 Oct 2015 15:57:00 +0000 (17:57 +0200)
committerVictor Stinner <vstinner@redhat.com>
Wed, 7 Oct 2015 21:13:45 +0000 (23:13 +0200)
* Import urllib modules using six.moves.urllib
* MockHTTPSResponse: encode HTTP body to UTF-8 if it's Unicode,
  replace six.string_types with (bytes, six.text_type).
* _id_to_base64(): catch also binascii.Error when calling
  b16decode(); encode name to UTF-8 before calling b64encode();
  decode b64encode() from ASCII on Python 3 to get Unicode.
* tox.ini: add cinder.tests.unit.volume.drivers.emc.scaleio
  to Python 3.4

Partial-Implements: blueprint cinder-python3
Change-Id: I93353d48f80971528f47c9291cd04e198632dd0b

cinder/tests/unit/volume/drivers/emc/scaleio/mocks.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_create_cloned_volume.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_create_snapshot.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_create_volume_from_snapshot.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_delete_snapshot.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_delete_volume.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_extend_volume.py
cinder/tests/unit/volume/drivers/emc/scaleio/test_misc.py
cinder/volume/drivers/emc/scaleio.py
tests-py3.txt

index 91bdd687a29f2216b9cf693b6db1ad113258b145..5b8c630f699e30435a7bb2d9049f67d77dbabeb2 100644 (file)
@@ -99,18 +99,20 @@ class MockHTTPSResponse(requests.Response):
     def __init__(self, content, status_code=200):
         super(MockHTTPSResponse, self).__init__()
 
+        if isinstance(content, six.text_type):
+            content = content.encode('utf-8')
         self._content = content
         self.status_code = status_code
 
     def json(self, **kwargs):
-        if isinstance(self._content, six.string_types):
+        if isinstance(self._content, (bytes, six.text_type)):
             return super(MockHTTPSResponse, self).json(**kwargs)
 
         return self._content
 
     @property
     def text(self):
-        if not isinstance(self._content, six.string_types):
+        if not isinstance(self._content, (bytes, six.text_type)):
             return json.dumps(self._content)
 
         return super(MockHTTPSResponse, self).text
index eb959dc721877745e2d0b0eb160b23b28ac55211..a2ff534d6cddd6e0fa5eb5053ddd98d657af6c7c 100644 (file)
@@ -14,7 +14,8 @@
 #    under the License.
 
 import json
-import urllib
+
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -36,8 +37,8 @@ class TestCreateClonedVolume(scaleio.TestScaleIODriver):
         self.src_volume = fake_volume.fake_volume_obj(
             ctx, **{'provider_id': 'pid001'})
 
-        self.src_volume_name_2x_enc = urllib.quote(
-            urllib.quote(
+        self.src_volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(
                 self.driver._id_to_base64(self.src_volume.id)
             )
         )
@@ -51,8 +52,8 @@ class TestCreateClonedVolume(scaleio.TestScaleIODriver):
             ctx, **self.new_volume_extras
         )
 
-        self.new_volume_name_2x_enc = urllib.quote(
-            urllib.quote(
+        self.new_volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(
                 self.driver._id_to_base64(self.new_volume.id)
             )
         )
index c3c5afd7b99cbbb349cc3617321b7b2efed1225a..70a01cb68319b1e2abb76f038aa64854d46bf9a9 100644 (file)
@@ -13,7 +13,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 import json
-import urllib
+
+from six.moves import urllib
 
 from cinder import context
 from cinder import db
@@ -46,11 +47,12 @@ class TestCreateSnapShot(scaleio.TestScaleIODriver):
 
         self.mock_object(db, 'volume_get', self.return_fake_volume)
 
-        self.volume_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.snapshot.volume_id))
+        snap_vol_id = self.snapshot.volume_id
+        self.volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(snap_vol_id))
         )
-        self.snapshot_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.snapshot.id))
+        self.snapshot_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(self.snapshot.id))
         )
 
         self.snapshot_reply = json.dumps(
index acf2832d78dff64ada540f15500717cdbc2ee61b..0965f0e055e4bdf60005a384da899071114d0c8f 100644 (file)
@@ -13,7 +13,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 import json
-import urllib
+
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -35,12 +36,12 @@ class TestCreateVolumeFromSnapShot(scaleio.TestScaleIODriver):
         ctx = context.RequestContext('fake', 'fake', auth_token=True)
 
         self.snapshot = fake_snapshot.fake_snapshot_obj(ctx)
-        self.snapshot_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.snapshot.id))
+        self.snapshot_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(self.snapshot.id))
         )
         self.volume = fake_volume.fake_volume_obj(ctx)
-        self.volume_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.volume.id))
+        self.volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(self.volume.id))
         )
 
         self.snapshot_reply = json.dumps(
index 0ae28d07701f232345e2c522587cdf7a48c680df..6d49f718bcde1dd250098281240b85fec43d0ce9 100644 (file)
@@ -12,7 +12,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-import urllib
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -34,8 +34,8 @@ class TestDeleteSnapShot(scaleio.TestScaleIODriver):
 
         self.snapshot = fake_snapshot_obj(
             ctx, **{'provider_id': 'snap_1'})
-        self.snapshot_name_2x_enc = urllib.quote(
-            urllib.quote(
+        self.snapshot_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(
                 self.driver._id_to_base64(self.snapshot.id)
             )
         )
index 475982e98eb9c6753fa17fd40edd293df03fdfc1..eab91d57cf4ea9defc1002e54a672306fd4d9c68 100644 (file)
@@ -12,7 +12,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-import urllib
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -34,8 +34,8 @@ class TestDeleteVolume(scaleio.TestScaleIODriver):
         self.volume = fake_volume.fake_volume_obj(
             ctx, **{'provider_id': 'pid_1'})
 
-        self.volume_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.volume.id))
+        self.volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(self.volume.id))
         )
 
         self.HTTPS_MOCK_RESPONSES = {
index fca55f40cdef911947de7f4b73d981567f64adeb..42dc498a2d7b3da33a0fe1dbaee74c361e1e8890 100644 (file)
@@ -12,7 +12,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-import urllib
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -42,8 +42,8 @@ class TestExtendVolume(scaleio.TestScaleIODriver):
 
         self.volume = fake_volume_obj(ctx, **{'id': 'fake_volume',
                                               'provider_id': 'pid_1'})
-        self.volume_name_2x_enc = urllib.quote(
-            urllib.quote(self.driver._id_to_base64(self.volume.id))
+        self.volume_name_2x_enc = urllib.parse.quote(
+            urllib.parse.quote(self.driver._id_to_base64(self.volume.id))
         )
 
         self.HTTPS_MOCK_RESPONSES = {
index e7e20a474705e6e1fbdcaea7571034787ecb1866..9e82a03766da47fb91ee3e8de058321ca2b5a54b 100644 (file)
@@ -12,7 +12,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-import urllib
+from six.moves import urllib
 
 from cinder import exception
 from cinder.tests.unit.volume.drivers.emc import scaleio
@@ -30,8 +30,8 @@ class TestMisc(scaleio.TestScaleIODriver):
         """
         super(TestMisc, self).setUp()
 
-        self.domain_name_enc = urllib.quote(self.DOMAIN_NAME)
-        self.pool_name_enc = urllib.quote(self.POOL_NAME)
+        self.domain_name_enc = urllib.parse.quote(self.DOMAIN_NAME)
+        self.pool_name_enc = urllib.parse.quote(self.POOL_NAME)
 
         self.HTTPS_MOCK_RESPONSES = {
             self.RESPONSE_MODE.Valid: {
index c74d3951e5367b42996ec9849c30788a6af7a61e..7d80597ceba25495e968b8797f61412fd18372c6 100644 (file)
@@ -17,6 +17,7 @@ Driver for EMC ScaleIO based on ScaleIO remote CLI.
 """
 
 import base64
+import binascii
 import json
 
 from os_brick.initiator import connector
@@ -25,7 +26,7 @@ from oslo_log import log as logging
 from oslo_utils import units
 import requests
 import six
-import urllib
+from six.moves import urllib
 
 from cinder import context
 from cinder import exception
@@ -250,9 +251,14 @@ class ScaleIODriver(driver.VolumeDriver):
         name = six.text_type(id).replace("-", "")
         try:
             name = base64.b16decode(name.upper())
-        except TypeError:
+        except (TypeError, binascii.Error):
             pass
-        encoded_name = base64.b64encode(name)
+        encoded_name = name
+        if isinstance(encoded_name, six.text_type):
+            encoded_name = encoded_name.encode('utf-8')
+        encoded_name = base64.b64encode(encoded_name)
+        if six.PY3:
+            encoded_name = encoded_name.decode('ascii')
         LOG.debug(
             "Converted id %(id)s to scaleio name %(name)s.",
             {'id': id, 'name': encoded_name})
@@ -307,7 +313,8 @@ class ScaleIODriver(driver.VolumeDriver):
                         " protection domain id.")
                 raise exception.VolumeBackendAPIException(data=msg)
 
-            encoded_domain_name = urllib.quote(self.protection_domain_name, '')
+            domain_name = self.protection_domain_name
+            encoded_domain_name = urllib.parse.quote(domain_name, '')
             req_vars = {'server_ip': self.server_ip,
                         'server_port': self.server_port,
                         'encoded_domain_name': encoded_domain_name}
@@ -341,7 +348,7 @@ class ScaleIODriver(driver.VolumeDriver):
         pool_name = self.storage_pool_name
         pool_id = self.storage_pool_id
         if pool_name:
-            encoded_domain_name = urllib.quote(pool_name, '')
+            encoded_domain_name = urllib.parse.quote(pool_name, '')
             req_vars = {'server_ip': self.server_ip,
                         'server_port': self.server_port,
                         'domain_id': domain_id,
@@ -702,7 +709,7 @@ class ScaleIODriver(driver.VolumeDriver):
                       {'domain': domain_name,
                        'pool': pool_name})
             # Get domain id from name.
-            encoded_domain_name = urllib.quote(domain_name, '')
+            encoded_domain_name = urllib.parse.quote(domain_name, '')
             req_vars = {'server_ip': self.server_ip,
                         'server_port': self.server_port,
                         'encoded_domain_name': encoded_domain_name}
@@ -738,7 +745,7 @@ class ScaleIODriver(driver.VolumeDriver):
             LOG.info(_LI("Domain id is %s."), domain_id)
 
             # Get pool id from name.
-            encoded_pool_name = urllib.quote(pool_name, '')
+            encoded_pool_name = urllib.parse.quote(pool_name, '')
             req_vars = {'server_ip': self.server_ip,
                         'server_port': self.server_port,
                         'domain_id': domain_id,
index e933f6a316be8899b3b6293e357bafaf33932538..6e06b4f16870214f38cb690c0950bfe8c86bd6ad 100644 (file)
@@ -91,6 +91,7 @@ cinder.tests.unit.test_volume_transfer
 cinder.tests.unit.test_volume_types
 cinder.tests.unit.test_volume_types_extra_specs
 cinder.tests.unit.test_volume_utils
+cinder.tests.unit.volume.drivers.emc.scaleio
 cinder.tests.unit.volume.flows.test_create_volume_flow
 cinder.tests.unit.windows.test_smbfs
 cinder.tests.unit.windows.test_vhdutils