]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
[PY3] byte/string conversions and enable PY3 test
authordineshbhor <dinesh.bhor@nttdata.com>
Tue, 21 Jun 2016 14:41:13 +0000 (20:11 +0530)
committerChangBo Guo(gcb) <eric.guo@easystack.cn>
Thu, 17 Nov 2016 06:50:35 +0000 (14:50 +0800)
* The dict.items()[0] will raise a TypeError in PY3,
  as dict.items() doesn't return a list any more in PY3
  but a view of list.
* Webob response body should be bytes not strings so used
  oslo_utils.encodeutils.safe_decode to decode it.

Partially implements blueprint: goal-python35
Co-Authored-By: ChangBo Guo(gcb) <eric.guo@easystack.cn>
Change-Id: I38d416923bc0cec0ca98c4494dd1e06cd49671cf

nova/api/openstack/compute/volumes.py
nova/api/openstack/wsgi.py
nova/tests/unit/api/openstack/compute/test_api.py
nova/tests/unit/api/openstack/compute/test_volumes.py
nova/tests/unit/api/openstack/test_wsgi.py
nova/tests/unit/matchers.py
tests-py3.txt

index db8855bbcfb4541978ce8334b322c7c6e85afa17..05cdccdc43ec45e00a0bbb6441432f9b7724566e 100644 (file)
@@ -70,7 +70,7 @@ def _translate_volume_summary_view(context, vol):
         #                   'mountpoint': '/dev/sda/
         #                    }
         #                }
-        attachment = vol['attachments'].items()[0]
+        attachment = list(vol['attachments'].items())[0]
         d['attachments'] = [_translate_attachment_detail_view(vol['id'],
             attachment[0],
             attachment[1].get('mountpoint'))]
index 8c476824eba58e80ad19b81c772a67a70a06a1d0..4925501fa39b0557ed3f294a1781971c89313992 100644 (file)
@@ -19,6 +19,7 @@ import functools
 import microversion_parse
 from oslo_log import log as logging
 from oslo_serialization import jsonutils
+from oslo_utils import encodeutils
 from oslo_utils import strutils
 import six
 import webob
@@ -692,7 +693,8 @@ class Resource(wsgi.Application):
         if hasattr(response, 'headers'):
             for hdr, val in list(response.headers.items()):
                 # Headers must be utf-8 strings
-                response.headers[hdr] = utils.utf8(val)
+                response.headers[hdr] = encodeutils.safe_decode(
+                        utils.utf8(val))
 
             if not request.api_version_request.is_null():
                 response.headers[API_VERSION_REQUEST_HEADER] = \
index 35ef48837de1ed260e9235705c2e289ea9858739..683edc947252df1f0a0b8982be4bce643a65d74d 100644 (file)
@@ -14,6 +14,7 @@
 #    under the License.
 
 from oslo_serialization import jsonutils
+from oslo_utils import encodeutils
 import six
 import webob.dec
 import webob.exc
@@ -69,7 +70,8 @@ class APITest(test.NoDBTestCase):
         # Consider changing `api.openstack.wsgi.Resource._process_stack`
         # to encode header values in ASCII rather than UTF-8.
         # https://tools.ietf.org/html/rfc7230#section-3.2.4
-        self.assertEqual(res.headers.get('Content-Type').decode(), ctype)
+        content_type = res.headers.get('Content-Type')
+        self.assertEqual(ctype, encodeutils.safe_decode(content_type))
 
         jsonutils.loads(res.body)
 
index a2e1f3d8f37022f48caa496b5d33400066d0b9b9..4231017587c2196532837d0e7bf0dfae40dc8dde 100644 (file)
@@ -18,6 +18,7 @@ import datetime
 
 import mock
 from oslo_serialization import jsonutils
+from oslo_utils import encodeutils
 from six.moves import urllib
 import webob
 from webob import exc
@@ -319,7 +320,8 @@ class VolumeApiTestV21(test.NoDBTestCase):
         req = fakes.HTTPRequest.blank(self.url_prefix + '/os-volumes/456')
         resp = req.get_response(self.app)
         self.assertEqual(404, resp.status_int)
-        self.assertIn('Volume 456 could not be found.', resp.body)
+        self.assertIn('Volume 456 could not be found.',
+                      encodeutils.safe_decode(resp.body))
 
     def test_volume_delete(self):
         req = fakes.HTTPRequest.blank(self.url_prefix + '/os-volumes/123')
@@ -334,7 +336,8 @@ class VolumeApiTestV21(test.NoDBTestCase):
         req.method = 'DELETE'
         resp = req.get_response(self.app)
         self.assertEqual(404, resp.status_int)
-        self.assertIn('Volume 456 could not be found.', resp.body)
+        self.assertIn('Volume 456 could not be found.',
+                      encodeutils.safe_decode(resp.body))
 
 
 class VolumeAttachTestsV21(test.NoDBTestCase):
index 56f54a3c0152cd37b4bc36f98d9e6b13eb8ea472..e02fe9a6bf04640d5c881d2e0f49c3a33738c88d 100644 (file)
@@ -867,9 +867,9 @@ class ResourceTest(MicroversionedTest):
         for val in six.itervalues(response.headers):
             # All headers must be utf8
             self.assertThat(val, matchers.EncodedByUTF8())
-        self.assertEqual(b'1', response.headers['x-header1'])
-        self.assertEqual(b'header2', response.headers['x-header2'])
-        self.assertEqual(b'header3', response.headers['x-header3'])
+        self.assertEqual('1', response.headers['x-header1'])
+        self.assertEqual('header2', response.headers['x-header2'])
+        self.assertEqual('header3', response.headers['x-header3'])
 
     def test_resource_valid_utf8_body(self):
         class Controller(object):
index e3729fd3c2fc1a1d09cce8832e3f8794a353de07..c2df5e414996253e3f740e8efb065e93a9157b95 100644 (file)
@@ -547,6 +547,12 @@ class EncodedByUTF8(object):
                 except UnicodeDecodeError:
                     return testtools.matchers.Mismatch(
                         "%s is not encoded in UTF-8." % obj)
+        elif isinstance(obj, six.text_type):
+            try:
+                obj.encode("utf-8", "strict")
+            except UnicodeDecodeError:
+                return testtools.matchers.Mismatch(
+                        "%s cannot be encoded in UTF-8." % obj)
         else:
             reason = ("Type of '%(obj)s' is '%(obj_type)s', "
                       "should be '%(correct_type)s'."
index 540247a46483dd35d2352c4d02bef63e0d71a864..57f322c99ee410acb641e68b40390f5c79871a37 100644 (file)
@@ -29,7 +29,6 @@ nova.tests.unit.api.openstack.compute.test_user_data.ServersControllerCreateTest
 nova.tests.unit.api.openstack.compute.test_versions.VersionsTestV21
 nova.tests.unit.api.openstack.compute.test_versions.VersionsTestV21WithV2CompatibleWrapper
 nova.tests.unit.api.openstack.compute.test_volumes.BootFromVolumeTest
-nova.tests.unit.api.openstack.compute.test_volumes.VolumeApiTestV21
 nova.tests.unit.api.test_compute_req_id.RequestIdTest
 nova.tests.unit.compute.test_compute.ComputeAPITestCase.test_create_with_base64_user_data
 nova.tests.unit.compute.test_compute_cells.CellsComputeAPITestCase.test_create_with_base64_user_data