]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port dothill to Python 3
authorVictor Stinner <vstinner@redhat.com>
Mon, 29 Jun 2015 13:33:12 +0000 (15:33 +0200)
committerVictor Stinner <vstinner@redhat.com>
Wed, 1 Jul 2015 08:50:58 +0000 (10:50 +0200)
* Replace urllib2 with six.moves.urllib
* On Python 3, encode string to UTF-8 to hash it using MD5
* On Python 3, decode base64 from ASCII to get Unicode
* Replace "pattern in exc" with "pattern in exc.args"
* test_initialize_connection(): fix get_active_iscsi_target_portals()
  mock. Use the return_value attribute to return a dictionary, instead
  of returning a single IP address.
* tox.ini: add cinder.tests.unit.test_dothill to Python 3.4

Blueprint cinder-python3
Change-Id: Ib20bca813c2352eae447c374ded75c6dafb2e18d

cinder/tests/unit/test_dothill.py
cinder/volume/drivers/dothill/dothill_client.py
cinder/volume/drivers/dothill/dothill_common.py
tox.ini

index 4438da474144bd2fcbb1fd34f7897b651545fd48..e3a3cd7dad3e57955a4fa4ab0d456638440b318b 100644 (file)
 #
 """Unit tests for OpenStack Cinder DotHill driver."""
 
-import urllib2
 
 from lxml import etree
 import mock
+from six.moves import urllib
 
 from cinder import exception
 from cinder import test
@@ -149,7 +149,7 @@ class TestDotHillClient(test.TestCase):
         self.client = dothill.DotHillClient(self.ip, self.login, self.passwd,
                                             self.protocol)
 
-    @mock.patch('urllib2.urlopen')
+    @mock.patch('six.moves.urllib.request.urlopen')
     def test_login(self, mock_url_open):
         m = mock.Mock()
         m.read.side_effect = [resp_login]
@@ -175,13 +175,13 @@ class TestDotHillClient(test.TestCase):
                                              arg2='val2')
         self.assertEqual('http://10.0.0.1/api/path/arg2/val2/arg1/arg3', url)
 
-    @mock.patch('urllib2.urlopen')
+    @mock.patch('six.moves.urllib.request.urlopen')
     def test_request(self, mock_url_open):
         self.client._session_key = session_key
 
         m = mock.Mock()
         m.read.side_effect = [response_ok, malformed_xml,
-                              urllib2.URLError("error")]
+                              urllib.error.URLError("error")]
         mock_url_open.return_value = m
         ret = self.client._request('/path')
         self.assertTrue(type(ret) == etree._Element)
@@ -706,7 +706,7 @@ class TestDotHillISCSI(TestDotHillFC):
         self.driver.iscsi_ips = ['10.0.0.11']
         self.driver.initialize_iscsi_ports()
         mock_iqns.side_effect = [['id2']]
-        mock_portals.side_effect = {'10.0.0.11': 'Up', '10.0.0.12': 'Up'}
+        mock_portals.return_value = {'10.0.0.11': 'Up', '10.0.0.12': 'Up'}
 
         self.assertRaises(exception.Invalid,
                           self.driver.initialize_connection, test_volume,
index b5975dc8fe0d6c80be4e2d36656a5349a889a0f1..a2054bdaad33acd087595919161f0b564c6af2e6 100644 (file)
 from hashlib import md5
 import math
 import time
-import urllib2
 
 from lxml import etree
 from oslo_log import log as logging
+import six
+from six.moves import urllib
 
 from cinder import exception
 from cinder.i18n import _LE
@@ -44,13 +45,16 @@ class DotHillClient(object):
 
     def login(self):
         """Authenticates the service on the device."""
-        hash_ = md5("%s_%s" % (self._login, self._password))
+        hash_ = "%s_%s" % (self._login, self._password)
+        if six.PY3:
+            hash_ = hash_.encode('utf-8')
+        hash_ = md5(hash_)
         digest = hash_.hexdigest()
 
         url = self._base_url + "/login/" + digest
         try:
-            xml = urllib2.urlopen(url).read()
-        except urllib2.URLError:
+            xml = urllib.request.urlopen(url).read()
+        except urllib.error.URLError:
             raise exception.DotHillConnectionError
 
         self._get_auth_token(xml)
@@ -91,9 +95,9 @@ class DotHillClient(object):
 
         url = self._build_request_url(path, *args, **kargs)
         headers = {'dataType': 'api', 'sessionKey': self._session_key}
-        req = urllib2.Request(url, headers=headers)
+        req = urllib.request.Request(url, headers=headers)
         try:
-            xml = urllib2.urlopen(req).read()
+            xml = urllib.request.urlopen(req).read()
             tree = etree.XML(xml)
         except Exception:
             raise exception.DotHillConnectionError
@@ -106,7 +110,7 @@ class DotHillClient(object):
     def logout(self):
         url = self._base_url + '/exit'
         try:
-            urllib2.urlopen(url)
+            urllib.request.urlopen(url)
             return True
         except Exception:
             return False
index 271317705e8091a22a60350716e6ca497fc9aae8..9f23787e641bab20fe5750622aad60cb750e6cf6 100644 (file)
@@ -137,6 +137,8 @@ class DotHillCommon(object):
         uuid_str = name.replace("-", "")
         vol_uuid = uuid.UUID('urn:uuid:%s' % uuid_str)
         vol_encoded = base64.b64encode(vol_uuid.bytes)
+        if six.PY3:
+            vol_encoded = vol_encoded.decode('ascii')
         vol_encoded = vol_encoded.replace('=', '')
 
         # + is not a valid character for DotHill
@@ -272,7 +274,7 @@ class DotHillCommon(object):
             self.client.delete_volume(volume_name)
         except exception.DotHillRequestError as ex:
             # if the volume wasn't found, ignore the error
-            if 'The volume was not found on this system.' in ex:
+            if 'The volume was not found on this system.' in ex.args:
                 return
             LOG.exception(_LE("Deletion of volume %s failed."), volume['id'])
             raise exception.Invalid(ex)
@@ -411,7 +413,7 @@ class DotHillCommon(object):
             self.client.delete_snapshot(snap_name)
         except exception.DotHillRequestError as ex:
             # if the volume wasn't found, ignore the error
-            if 'The volume was not found on this system.' in ex:
+            if 'The volume was not found on this system.' in ex.args:
                 return
             LOG.exception(_LE("Deleting snapshot %s failed"), snapshot['id'])
             raise exception.Invalid(ex)
diff --git a/tox.ini b/tox.ini
index 0007c5604a2a0ee020049c1963feb943dd444d76..7c36705e4cad03307a18dd65c41b93407ed21e89 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -44,6 +44,7 @@ commands =
     cinder.tests.unit.test_dellfc \
     cinder.tests.unit.test_dellsc \
     cinder.tests.unit.test_dellscapi \
+    cinder.tests.unit.test_dothill \
     cinder.tests.unit.test_drbdmanagedrv \
     cinder.tests.unit.test_emc_xtremio \
     cinder.tests.unit.test_eqlx \