]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port API contribs to Python 3
authorVictor Stinner <vstinner@redhat.com>
Tue, 9 Feb 2016 17:40:21 +0000 (18:40 +0100)
committerVictor Stinner <vstinner@redhat.com>
Mon, 15 Feb 2016 14:13:58 +0000 (15:13 +0100)
* Replace dict.iteritems() with dict.items(), dict.iteritems() was
  removed in Python 3.
* Replace dict.values() with list(dict.values()) to get a list on
  Python 3.
* HTTP body type must be bytes:

  - Fix unit tests to use bytes strings for HTTP body
  - Encode XML to UTF-8 on Python 3.

* Use "%r" instead of "%s" to format request and response in test to
  avoid BytesWarning on Python 3.
* tests-py3.txt: add cinder.tests.unit.api.contrib

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

cinder/api/contrib/volume_actions.py
cinder/api/contrib/volume_type_encryption.py
cinder/tests/unit/api/contrib/test_backups.py
cinder/tests/unit/api/contrib/test_volume_image_metadata.py
cinder/tests/unit/api/contrib/test_volume_replication.py
cinder/tests/unit/api/contrib/test_volume_transfer.py
cinder/tests/unit/api/contrib/test_volume_type_access.py
cinder/tests/unit/api/contrib/test_volume_type_encryption.py
tests-py3.txt

index 225ad5e272d7978f1e4d9be863b7fa6b33679a3b..d2db66c365e07e0a24a450ed5bd849e012f30666 100644 (file)
@@ -15,6 +15,7 @@
 
 from oslo_log import log as logging
 import oslo_messaging as messaging
+from oslo_utils import encodeutils
 from oslo_utils import strutils
 import six
 import webob
@@ -261,7 +262,8 @@ class VolumeActionsController(wsgi.Controller):
         try:
             force = strutils.bool_from_string(force, strict=True)
         except ValueError as error:
-            msg = _("Invalid value for 'force': '%s'") % error.message
+            err_msg = encodeutils.exception_to_unicode(error)
+            msg = _("Invalid value for 'force': '%s'") % err_msg
             raise webob.exc.HTTPBadRequest(explanation=msg)
 
         try:
@@ -333,7 +335,8 @@ class VolumeActionsController(wsgi.Controller):
             readonly_flag = strutils.bool_from_string(readonly_flag,
                                                       strict=True)
         except ValueError as error:
-            msg = _("Invalid value for 'readonly': '%s'") % error.message
+            err_msg = encodeutils.exception_to_unicode(error)
+            msg = _("Invalid value for 'readonly': '%s'") % err_msg
             raise webob.exc.HTTPBadRequest(explanation=msg)
 
         self.volume_api.update_readonly_flag(context, volume, readonly_flag)
@@ -373,7 +376,8 @@ class VolumeActionsController(wsgi.Controller):
             bootable = strutils.bool_from_string(bootable,
                                                  strict=True)
         except ValueError as error:
-            msg = _("Invalid value for 'bootable': '%s'") % error.message
+            err_msg = encodeutils.exception_to_unicode(error)
+            msg = _("Invalid value for 'bootable': '%s'") % err_msg
             raise webob.exc.HTTPBadRequest(explanation=msg)
 
         update_dict = {'bootable': bootable}
index c0aeb72bfbd03528f2d9ed778e11fced10b50eb8..5ffc7d026344de4c495813f0eb6e32c4650e680e 100644 (file)
@@ -46,7 +46,7 @@ class VolumeTypeEncryptionController(wsgi.Controller):
         encryption_specs = {}
         if not encryption_ref:
             return encryption_specs
-        for key, value in encryption_ref.iteritems():
+        for key, value in encryption_ref.items():
             encryption_specs[key] = value
         return encryption_specs
 
index 3016e4b5ab4d09ccbf802aeab355f222ec7dd6ec..dcd0e54adaef615cf5daa4b552c8f58f7fcd6128 100644 (file)
@@ -693,10 +693,14 @@ class BackupsAPITestCase(test.TestCase):
 
         volume_id = utils.create_volume(self.context, size=2)['id']
 
+        body = ('<backup display_name="backup-001" '
+                'display_description="Nightly Backup" '
+                'volume_id="%s" container="Container001"/>' % volume_id)
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
+
         req = webob.Request.blank('/v2/fake/backups')
-        req.body = ('<backup display_name="backup-001" '
-                    'display_description="Nightly Backup" '
-                    'volume_id="%s" container="Container001"/>' % volume_id)
+        req.body = body
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
@@ -1165,8 +1169,12 @@ class BackupsAPITestCase(test.TestCase):
                                         size=2,
                                         display_name=volume_name)['id']
 
+        body = '<restore volume_id="%s"/>' % volume_id
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
+
         req = webob.Request.blank('/v2/fake/backups/%s/restore' % backup_id)
-        req.body = '<restore volume_id="%s"/>' % volume_id
+        req.body = body
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
@@ -1792,14 +1800,17 @@ class BackupsAPITestCase(test.TestCase):
         _mock_import_record_rpc.return_value = None
         _mock_list_services.return_value = [backup_service]
 
-        req = webob.Request.blank('/v2/fake/backups/import_record')
         if six.PY2:
             backup_url = backup_url.encode('utf-8')
-        req.body = ('<backup-record backup_service="%(backup_service)s" '
-                    'backup_url="%(backup_url)s"/>') \
-            % {'backup_url': backup_url,
-               'backup_service': backup_service}
+        body = ('<backup-record backup_service="%(backup_service)s" '
+                'backup_url="%(backup_url)s"/>'
+                % {'backup_url': backup_url,
+                   'backup_service': backup_service})
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
 
+        req = webob.Request.blank('/v2/fake/backups/import_record')
+        req.body = body
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
index 207f2ac2deb2b969310a8259526ec93f78f4fc88..be986750ce0e7fc484b9cf05f4e34f69cf75f666 100644 (file)
@@ -341,5 +341,7 @@ class VolumeImageMetadataXMLTest(VolumeImageMetadataTest):
                 volume, 'volume_image_metadata'
             )
             for volume in volume_list]
-        return map(wsgi.MetadataXMLDeserializer().extract_metadata,
-                   image_metadata_list)
+
+        metadata_deserializer = wsgi.MetadataXMLDeserializer()
+        return [metadata_deserializer.extract_metadata(image_metadata)
+                for image_metadata in image_metadata_list]
index 20e6884612f1dd3f2174788625de4176510cb8dd..3a03857824be733c98dbc17264a395fd417f156a 100644 (file)
@@ -19,6 +19,7 @@ Tests for volume replication API code.
 import mock
 from oslo_config import cfg
 from oslo_serialization import jsonutils
+import six
 import webob
 
 from cinder import context
@@ -55,6 +56,8 @@ class VolumeReplicationAPITestCase(test.TestCase):
             body = '<os-%s-replica/>' % operation
             req.headers['Content-Type'] = 'application/xml'
             req.headers['Accept'] = 'application/xml'
+            if isinstance(body, six.text_type):
+                body = body.encode('utf-8')
             req.body = body
         else:
             body = {'os-%s-replica' % operation: ''}
@@ -68,12 +71,12 @@ class VolumeReplicationAPITestCase(test.TestCase):
 
     def test_promote_bad_id(self):
         (req, res) = self._get_resp('promote', 'fake')
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(404, res.status_int, msg)
 
     def test_promote_bad_id_xml(self):
         (req, res) = self._get_resp('promote', 'fake', xml=True)
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(404, res.status_int, msg)
 
     def test_promote_volume_not_replicated(self):
@@ -81,7 +84,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
             self.ctxt,
             **self.volume_params)
         (req, res) = self._get_resp('promote', volume['id'])
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(400, res.status_int, msg)
 
     def test_promote_volume_not_replicated_xml(self):
@@ -89,7 +92,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
             self.ctxt,
             **self.volume_params)
         (req, res) = self._get_resp('promote', volume['id'], xml=True)
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(400, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.promote_replica')
@@ -101,7 +104,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = 'active',
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['available']:
@@ -110,7 +113,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = 'active',
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.promote_replica')
@@ -122,7 +125,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = 'active',
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['available']:
@@ -131,7 +134,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = 'active',
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.promote_replica')
@@ -143,7 +146,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['active', 'active-stopped']:
@@ -152,7 +155,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.promote_replica')
@@ -164,7 +167,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['active', 'active-stopped']:
@@ -173,17 +176,17 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('promote', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
 
     def test_reenable_bad_id(self):
         (req, res) = self._get_resp('reenable', 'fake')
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(404, res.status_int, msg)
 
     def test_reenable_bad_id_xml(self):
         (req, res) = self._get_resp('reenable', 'fake', xml=True)
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(404, res.status_int, msg)
 
     def test_reenable_volume_not_replicated(self):
@@ -191,7 +194,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
             self.ctxt,
             **self.volume_params)
         (req, res) = self._get_resp('reenable', volume['id'])
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(400, res.status_int, msg)
 
     def test_reenable_volume_not_replicated_xml(self):
@@ -199,7 +202,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
             self.ctxt,
             **self.volume_params)
         (req, res) = self._get_resp('reenable', volume['id'], xml=True)
-        msg = ("request: %s\nresult: %s" % (req, res))
+        msg = ("request: %r\nresult: %r" % (req, res))
         self.assertEqual(400, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.reenable_replication')
@@ -211,7 +214,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('reenable', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['inactive', 'active-stopped', 'error']:
@@ -220,7 +223,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('reenable', volume['id'])
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
 
     @mock.patch('cinder.volume.rpcapi.VolumeAPI.reenable_replication')
@@ -232,7 +235,7 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('reenable', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(400, res.status_int, msg)
 
         for status in ['inactive', 'active-stopped', 'error']:
@@ -241,5 +244,5 @@ class VolumeReplicationAPITestCase(test.TestCase):
                                                replication_status = status,
                                                **self.volume_params)
             (req, res) = self._get_resp('reenable', volume['id'], xml=True)
-            msg = ("request: %s\nresult: %s" % (req, res))
+            msg = ("request: %r\nresult: %r" % (req, res))
             self.assertEqual(202, res.status_int, msg)
index 3407c28f863fc925c141e7496045891f16d25569..685905e93d0f87dab0c37cba7ae1d43cb3a24ef7 100644 (file)
 Tests for volume transfer code.
 """
 
+import mock
 from xml.dom import minidom
 
-import mock
 from oslo_serialization import jsonutils
+import six
 import webob
 
 from cinder.api.contrib import volume_transfer
@@ -285,9 +286,12 @@ class VolumeTransferAPITestCase(test.TestCase):
         volume_size = 2
         volume_id = self._create_volume(status='available', size=volume_size)
 
+        body = '<transfer name="transfer-001" volume_id="%s"/>' % volume_id
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
+
         req = webob.Request.blank('/v2/fake/os-volume-transfer')
-        req.body = ('<transfer name="transfer-001" '
-                    'volume_id="%s"/>' % volume_id)
+        req.body = body
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
@@ -434,9 +438,13 @@ class VolumeTransferAPITestCase(test.TestCase):
         transfer = self._create_transfer(volume_id)
         svc = self.start_service('volume', host='fake_host')
 
+        body = '<accept auth_key="%s"/>' % transfer['auth_key']
+        if isinstance(body, six.text_type):
+            body = body.encode('utf-8')
+
         req = webob.Request.blank('/v2/fake/os-volume-transfer/%s/accept' %
                                   transfer['id'])
-        req.body = '<accept auth_key="%s"/>' % transfer['auth_key']
+        req.body = body
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
index f39bb802a3dccc8f45f308ac4600ebaca3a3631e..4fe3f214b13def9d0a5c18152cb1e983797b1339 100644 (file)
@@ -72,7 +72,7 @@ def fake_volume_type_get_all(context, inactive=False, filters=None,
                              sort_dirs=None, offset=None, list_result=False):
     if filters is None or filters['is_public'] is None:
         if list_result:
-            return VOLUME_TYPES.values()
+            return list(VOLUME_TYPES.values())
         return VOLUME_TYPES
     res = {}
     for k, v in VOLUME_TYPES.items():
@@ -82,7 +82,7 @@ def fake_volume_type_get_all(context, inactive=False, filters=None,
         if v['is_public'] == filters['is_public']:
             res.update({k: v})
     if list_result:
-        return res.values()
+        return list(res.values())
     return res
 
 
index 369589fe503c81c0d58aac38f5e00802ffe9f079..9cf51cf1997760f57d277d77d21699cbf9be7604 100644 (file)
@@ -166,7 +166,7 @@ class VolumeTypeEncryptionTest(test.TestCase):
         self.assertEqual(200, res.status_code)
         # Confirm that volume type has no encryption information
         # before create.
-        self.assertEqual('{}', res.body)
+        self.assertEqual(b'{}', res.body)
 
         # Create encryption specs for the volume type
         # with the defined body.
@@ -214,8 +214,8 @@ class VolumeTypeEncryptionTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/types/%s/encryption'
                                   % volume_type['id'])
         req.method = 'POST'
-        req.body = ('<encryption provider="test_provider" '
-                    'cipher="cipher" control_location="front-end" />')
+        req.body = (b'<encryption provider="test_provider" '
+                    b'cipher="cipher" control_location="front-end" />')
         req.headers['Content-Type'] = 'application/xml'
         req.headers['Accept'] = 'application/xml'
         res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))
index b275f5734671284af19d4b634d966d38e158f57c..29e8324cd71f08b8008ae228e01231e304dff2da 100644 (file)
@@ -1,27 +1,4 @@
-cinder.tests.unit.api.contrib.test_admin_actions
-cinder.tests.unit.api.contrib.test_availability_zones
-cinder.tests.unit.api.contrib.test_capabilities
-cinder.tests.unit.api.contrib.test_cgsnapshots
-cinder.tests.unit.api.contrib.test_extended_snapshot_attributes
-cinder.tests.unit.api.contrib.test_hosts
-cinder.tests.unit.api.contrib.test_qos_specs_manage
-cinder.tests.unit.api.contrib.test_quotas
-cinder.tests.unit.api.contrib.test_quotas_classes
-cinder.tests.unit.api.contrib.test_scheduler_hints
-cinder.tests.unit.api.contrib.test_scheduler_stats
-cinder.tests.unit.api.contrib.test_services
-cinder.tests.unit.api.contrib.test_snapshot_actions
-cinder.tests.unit.api.contrib.test_snapshot_manage
-cinder.tests.unit.api.contrib.test_snapshot_unmanage
-cinder.tests.unit.api.contrib.test_types_extra_specs
-cinder.tests.unit.api.contrib.test_types_manage
-cinder.tests.unit.api.contrib.test_used_limits
-cinder.tests.unit.api.contrib.test_volume_encryption_metadata
-cinder.tests.unit.api.contrib.test_volume_host_attribute
-cinder.tests.unit.api.contrib.test_volume_manage
-cinder.tests.unit.api.contrib.test_volume_unmanage
-cinder.tests.unit.api.contrib.test_volume_migration_status_attribute
-cinder.tests.unit.api.contrib.test_volume_tenant_attribute
+cinder.tests.unit.api.contrib
 cinder.tests.unit.api.openstack.test_wsgi
 cinder.tests.unit.api.test_common
 cinder.tests.unit.api.test_extensions