From 30c0f797814f09d00ba4d27811a1f87da86ceabb Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 9 Feb 2016 14:36:45 +0100 Subject: [PATCH] Port API v1 and v2 to Python 3 * Replace jsonutils.dumps() with jsonutils.dump_as_bytes() since output is used for the HTTP body, and HTTP body type is bytes, not Unicode. On Python 3, jsonutils.dumps() returns Unicode. * FakeHttplibSocket: replace io.StringIO with io.BytesIO, HTTP body type is bytes. Encode Unicode to UTF-8. * Fix FakeHttplibSocket.makefile() API: only the first parameter is mandatory. On Python 3, it's only called with the first 'mode' parameter. * Use bytes strings to test HTTP bodies (JSON or XML). * Replace dict.values() with list(dict.values()) to get a list on Python 3. * Replace error.message with encodeutils.exception_to_unicode(error), exceptions loose their message attribute in Python 3. * Update JSON serializer test in test_wsgi.py to use bytes. * tests-py3.txt: add cinder.tests.unit.api.v1 and cinder.tests.unit.api.v2 Partial-Implements: blueprint cinder-python3 Change-Id: I0f0048f4a1344feaa3434cbf7ebd31e3f12d6ae4 --- cinder/api/openstack/wsgi.py | 2 +- cinder/api/v1/limits.py | 2 +- cinder/api/v2/limits.py | 2 +- cinder/api/v2/snapshots.py | 4 +- cinder/tests/unit/api/openstack/test_wsgi.py | 4 +- cinder/tests/unit/api/v1/test_limits.py | 15 +++--- .../unit/api/v1/test_snapshot_metadata.py | 42 ++++++++-------- cinder/tests/unit/api/v1/test_types.py | 2 +- .../tests/unit/api/v1/test_volume_metadata.py | 42 ++++++++-------- cinder/tests/unit/api/v2/test_limits.py | 15 +++--- .../unit/api/v2/test_snapshot_metadata.py | 42 ++++++++-------- cinder/tests/unit/api/v2/test_types.py | 4 +- .../tests/unit/api/v2/test_volume_metadata.py | 48 +++++++++---------- tests-py3.txt | 3 +- 14 files changed, 118 insertions(+), 109 deletions(-) diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index f49047173..779018b45 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -409,7 +409,7 @@ class JSONDictSerializer(DictSerializer): """Default JSON request body serialization.""" def default(self, data): - return jsonutils.dumps(data) + return jsonutils.dump_as_bytes(data) class XMLDictSerializer(DictSerializer): diff --git a/cinder/api/v1/limits.py b/cinder/api/v1/limits.py index b0124deae..f1143d7f6 100644 --- a/cinder/api/v1/limits.py +++ b/cinder/api/v1/limits.py @@ -436,7 +436,7 @@ class WsgiLimiterProxy(object): self.limiter_address = limiter_address def check_for_delay(self, verb, path, username=None): - body = jsonutils.dumps({"verb": verb, "path": path}) + body = jsonutils.dump_as_bytes({"verb": verb, "path": path}) headers = {"Content-Type": "application/json"} conn = http_client.HTTPConnection(self.limiter_address) diff --git a/cinder/api/v2/limits.py b/cinder/api/v2/limits.py index 0b6fb6139..f50376bfd 100644 --- a/cinder/api/v2/limits.py +++ b/cinder/api/v2/limits.py @@ -433,7 +433,7 @@ class WsgiLimiterProxy(object): self.limiter_address = limiter_address def check_for_delay(self, verb, path, username=None): - body = jsonutils.dumps({"verb": verb, "path": path}) + body = jsonutils.dump_as_bytes({"verb": verb, "path": path}) headers = {"Content-Type": "application/json"} conn = http_client.HTTPConnection(self.limiter_address) diff --git a/cinder/api/v2/snapshots.py b/cinder/api/v2/snapshots.py index ee0a30249..536b27854 100644 --- a/cinder/api/v2/snapshots.py +++ b/cinder/api/v2/snapshots.py @@ -16,6 +16,7 @@ """The volumes snapshots api.""" from oslo_log import log as logging +from oslo_utils import encodeutils from oslo_utils import strutils import webob from webob import exc @@ -177,7 +178,8 @@ class SnapshotsController(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 exception.InvalidParameterValue(err=msg) if force: diff --git a/cinder/tests/unit/api/openstack/test_wsgi.py b/cinder/tests/unit/api/openstack/test_wsgi.py index e156007ee..0c2cf9063 100644 --- a/cinder/tests/unit/api/openstack/test_wsgi.py +++ b/cinder/tests/unit/api/openstack/test_wsgi.py @@ -216,10 +216,10 @@ class XMLDictSerializerTest(test.TestCase): class JSONDictSerializerTest(test.TestCase): def test_json(self): input_dict = dict(servers=dict(a=(2, 3))) - expected_json = '{"servers":{"a":[2,3]}}' + expected_json = b'{"servers":{"a":[2,3]}}' serializer = wsgi.JSONDictSerializer() result = serializer.serialize(input_dict) - result = result.replace('\n', '').replace(' ', '') + result = result.replace(b'\n', b'').replace(b' ', b'') self.assertEqual(expected_json, result) diff --git a/cinder/tests/unit/api/v1/test_limits.py b/cinder/tests/unit/api/v1/test_limits.py index 7ca8afc94..d9f69fa47 100644 --- a/cinder/tests/unit/api/v1/test_limits.py +++ b/cinder/tests/unit/api/v1/test_limits.py @@ -569,7 +569,7 @@ class WsgiLimiterTest(BaseLimitTestSuite): def _request_data(self, verb, path): """Get data describing a limit request verb/path.""" - return jsonutils.dumps({"verb": verb, "path": path}) + return jsonutils.dump_as_bytes({"verb": verb, "path": path}) def _request(self, verb, url, username=None): """Assert that POSTing to given url triggers given action. @@ -636,9 +636,11 @@ class FakeHttplibSocket(object): def __init__(self, response_string): """Initialize new `FakeHttplibSocket`.""" - self._buffer = six.StringIO(response_string) + if isinstance(response_string, six.text_type): + response_string = response_string.encode('utf-8') + self._buffer = six.BytesIO(response_string) - def makefile(self, _mode, _other): + def makefile(self, mode, *args): """Returns the socket's internal buffer.""" return self._buffer @@ -754,8 +756,9 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite): delay, error = self.proxy.check_for_delay("GET", "/delayed") error = error.strip() - expected = ("60.00", "403 Forbidden\n\nOnly 1 GET request(s) can be " - "made to /delayed every minute.") + expected = ("60.00", + b"403 Forbidden\n\nOnly 1 GET request(s) can be " + b"made to /delayed every minute.") self.assertEqual(expected, (delay, error)) @@ -827,7 +830,7 @@ class LimitsXMLSerializationTest(test.TestCase): "absolute": {}}} output = serializer.serialize(fixture) - has_dec = output.startswith("") + has_dec = output.startswith(b"") self.assertTrue(has_dec) def test_index(self): diff --git a/cinder/tests/unit/api/v1/test_snapshot_metadata.py b/cinder/tests/unit/api/v1/test_snapshot_metadata.py index cfcbb39b8..94fbe1074 100644 --- a/cinder/tests/unit/api/v1/test_snapshot_metadata.py +++ b/cinder/tests/unit/api/v1/test_snapshot_metadata.py @@ -292,7 +292,7 @@ class SnapshotMetaDataTest(test.TestCase): body = {"metadata": {"key1": "value1", "key2": "value2", "key3": "value3"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res_dict = self.controller.create(req, self.req_id, body) self.assertEqual(body, res_dict) @@ -326,7 +326,7 @@ class SnapshotMetaDataTest(test.TestCase): "key2": "value2", "key3": "value3", "KEY4": "value4"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res_dict = self.controller.create(req, self.req_id, body) self.assertEqual(expected, res_dict) @@ -346,7 +346,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -358,7 +358,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -375,7 +375,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'POST' req.content_type = "application/json" body = {"metadata": {"key9": "value9"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, self.req_id, body) @@ -402,7 +402,7 @@ class SnapshotMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, expected) self.assertEqual(expected, res_dict) @@ -442,7 +442,7 @@ class SnapshotMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, body) self.assertEqual(expected, res_dict) @@ -465,7 +465,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, expected) self.assertEqual(expected, res_dict) @@ -477,7 +477,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'meta': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -491,7 +491,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': ['asdf']} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -503,7 +503,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" body = {'metadata': {'key10': 'value10'}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_all, req, '100', body) @@ -524,7 +524,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res_dict = self.controller.update(req, self.req_id, 'key1', body) expected = {'meta': {'key1': 'value1'}} @@ -537,7 +537,7 @@ class SnapshotMetaDataTest(test.TestCase): '/v1.1/fake/snapshots/asdf/metadata/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -562,7 +562,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -583,7 +583,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, @@ -605,7 +605,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": ("a" * 260)}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, @@ -618,7 +618,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1", "key2": "value2"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -631,7 +631,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/bad') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -656,18 +656,18 @@ class SnapshotMetaDataTest(test.TestCase): # test for long key data = {"metadata": {"a" * 260: "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, self.req_id, data) # test for long value data = {"metadata": {"key": "v" * 260}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, self.req_id, data) # test for empty key. data = {"metadata": {"": "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, req, self.req_id, data) diff --git a/cinder/tests/unit/api/v1/test_types.py b/cinder/tests/unit/api/v1/test_types.py index 773c3ce22..c5d220e38 100644 --- a/cinder/tests/unit/api/v1/test_types.py +++ b/cinder/tests/unit/api/v1/test_types.py @@ -181,7 +181,7 @@ class VolumeTypesSerializerTest(test.TestCase): # Just getting some input data vtypes = return_volume_types_get_all_types(None) - text = serializer.serialize({'volume_types': vtypes.values()}) + text = serializer.serialize({'volume_types': list(vtypes.values())}) tree = etree.fromstring(text) diff --git a/cinder/tests/unit/api/v1/test_volume_metadata.py b/cinder/tests/unit/api/v1/test_volume_metadata.py index 2b20b9b1d..09e1b0987 100644 --- a/cinder/tests/unit/api/v1/test_volume_metadata.py +++ b/cinder/tests/unit/api/v1/test_volume_metadata.py @@ -265,7 +265,7 @@ class volumeMetaDataTest(test.TestCase): body = {"metadata": {"key1": "value1", "key2": "value2", "key3": "value3", }} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -298,7 +298,7 @@ class volumeMetaDataTest(test.TestCase): "key2": "value2", "key3": "value3", "KEY4": "value4"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -323,7 +323,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -335,7 +335,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -353,7 +353,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'POST' req.content_type = "application/json" body = {"metadata": {"key9": "value9"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, self.req_id, body) @@ -372,7 +372,7 @@ class volumeMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -409,7 +409,7 @@ class volumeMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -428,7 +428,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -446,7 +446,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": ("a" * 260)}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -466,7 +466,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'meta': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -481,7 +481,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': ['asdf']} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -497,7 +497,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" body = {'metadata': {'key10': 'value10'}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_all, req, '100', body) @@ -510,7 +510,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -528,7 +528,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank('/v1.1/fake/volumes/asdf/metadata/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -554,7 +554,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -575,7 +575,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -594,7 +594,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1", "key2": "value2"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -607,7 +607,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/bad') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -625,7 +625,7 @@ class volumeMetaDataTest(test.TestCase): # test for long key data = {"metadata": {"a" * 260: "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -636,7 +636,7 @@ class volumeMetaDataTest(test.TestCase): # test for long value data = {"metadata": {"key": "v" * 260}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -647,7 +647,7 @@ class volumeMetaDataTest(test.TestCase): # test for empty key. data = {"metadata": {"": "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, diff --git a/cinder/tests/unit/api/v2/test_limits.py b/cinder/tests/unit/api/v2/test_limits.py index 7408d0193..5a62e69af 100644 --- a/cinder/tests/unit/api/v2/test_limits.py +++ b/cinder/tests/unit/api/v2/test_limits.py @@ -575,7 +575,7 @@ class WsgiLimiterTest(BaseLimitTestSuite): def _request_data(self, verb, path): """Get data describing a limit request verb/path.""" - return jsonutils.dumps({"verb": verb, "path": path}) + return jsonutils.dump_as_bytes({"verb": verb, "path": path}) def _request(self, verb, url, username=None): """POST request to given url by given username. @@ -641,9 +641,11 @@ class FakeHttplibSocket(object): def __init__(self, response_string): """Initialize new `FakeHttplibSocket`.""" - self._buffer = six.StringIO(response_string) + if isinstance(response_string, six.text_type): + response_string = response_string.encode('utf-8') + self._buffer = six.BytesIO(response_string) - def makefile(self, _mode, _other): + def makefile(self, mode, *args): """Returns the socket's internal buffer.""" return self._buffer @@ -761,8 +763,9 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite): delay, error = self.proxy.check_for_delay("GET", "/delayed") error = error.strip() - expected = ("60.00", "403 Forbidden\n\nOnly 1 GET request(s) can be " - "made to /delayed every minute.") + expected = ("60.00", + b"403 Forbidden\n\nOnly 1 GET request(s) can be " + b"made to /delayed every minute.") self.assertEqual(expected, (delay, error)) @@ -834,7 +837,7 @@ class LimitsXMLSerializationTest(test.TestCase): "absolute": {}}} output = serializer.serialize(fixture) - has_dec = output.startswith("") + has_dec = output.startswith(b"") self.assertTrue(has_dec) def test_index(self): diff --git a/cinder/tests/unit/api/v2/test_snapshot_metadata.py b/cinder/tests/unit/api/v2/test_snapshot_metadata.py index c3676c0f9..adbaf2d97 100644 --- a/cinder/tests/unit/api/v2/test_snapshot_metadata.py +++ b/cinder/tests/unit/api/v2/test_snapshot_metadata.py @@ -293,7 +293,7 @@ class SnapshotMetaDataTest(test.TestCase): body = {"metadata": {"key1": "value1", "key2": "value2", "key3": "value3"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res_dict = self.controller.create(req, self.req_id, body) self.assertEqual(body, res_dict) @@ -327,7 +327,7 @@ class SnapshotMetaDataTest(test.TestCase): "key2": "value2", "key3": "value3", "KEY4": "value4"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res_dict = self.controller.create(req, self.req_id, body) self.assertEqual(expected, res_dict) @@ -347,7 +347,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -359,7 +359,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -376,7 +376,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'POST' req.content_type = "application/json" body = {"metadata": {"key9": "value9"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, self.req_id, body) @@ -403,7 +403,7 @@ class SnapshotMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, expected) self.assertEqual(expected, res_dict) @@ -443,7 +443,7 @@ class SnapshotMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, body) self.assertEqual(expected, res_dict) @@ -466,7 +466,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) res_dict = self.controller.update_all(req, self.req_id, expected) self.assertEqual(expected, res_dict) @@ -478,7 +478,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'meta': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -491,7 +491,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': ['asdf']} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -503,7 +503,7 @@ class SnapshotMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" body = {'metadata': {'key10': 'value10'}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_all, req, '100', body) @@ -524,7 +524,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res_dict = self.controller.update(req, self.req_id, 'key1', body) expected = {'meta': {'key1': 'value1'}} @@ -537,7 +537,7 @@ class SnapshotMetaDataTest(test.TestCase): '/v2/fake/snapshots/asdf/metadata/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -562,7 +562,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -583,7 +583,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, @@ -605,7 +605,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": ("a" * 260)}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, @@ -618,7 +618,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1", "key2": "value2"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -631,7 +631,7 @@ class SnapshotMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/bad') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -656,18 +656,18 @@ class SnapshotMetaDataTest(test.TestCase): # test for long key data = {"metadata": {"a" * 260: "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, self.req_id, data) # test for long value data = {"metadata": {"key": "v" * 260}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, self.req_id, data) # test for empty key. data = {"metadata": {"": "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, req, self.req_id, data) diff --git a/cinder/tests/unit/api/v2/test_types.py b/cinder/tests/unit/api/v2/test_types.py index 57e46ec82..ddec2d39c 100644 --- a/cinder/tests/unit/api/v2/test_types.py +++ b/cinder/tests/unit/api/v2/test_types.py @@ -54,7 +54,7 @@ def return_volume_types_get_all_types(context, filters=None, marker=None, vol_type_3=stub_volume_type(3) ) if list_result: - return result.values() + return list(result.values()) return result @@ -416,7 +416,7 @@ class VolumeTypesSerializerTest(test.TestCase): # Just getting some input data vtypes = return_volume_types_get_all_types(None) - text = serializer.serialize({'volume_types': vtypes.values()}) + text = serializer.serialize({'volume_types': list(vtypes.values())}) tree = etree.fromstring(text) diff --git a/cinder/tests/unit/api/v2/test_volume_metadata.py b/cinder/tests/unit/api/v2/test_volume_metadata.py index 53730fda3..2ceff4e4c 100644 --- a/cinder/tests/unit/api/v2/test_volume_metadata.py +++ b/cinder/tests/unit/api/v2/test_volume_metadata.py @@ -277,7 +277,7 @@ class volumeMetaDataTest(test.TestCase): body = {"metadata": {"key1": "value1", "key2": "value2", "key3": "value3", }} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -299,7 +299,7 @@ class volumeMetaDataTest(test.TestCase): body = {"metadata": {"key1": "value1", "key2": "value2", "key3": "value3", }} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -333,7 +333,7 @@ class volumeMetaDataTest(test.TestCase): "key2": "value2", "key3": "value3", "KEY4": "value4"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -358,7 +358,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -370,7 +370,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -388,7 +388,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'POST' req.content_type = "application/json" body = {"metadata": {"key9": "value9"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, self.req_id, body) @@ -407,7 +407,7 @@ class volumeMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -432,7 +432,7 @@ class volumeMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -471,7 +471,7 @@ class volumeMetaDataTest(test.TestCase): 'KEY20': 'value20', }, } - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -490,7 +490,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -507,7 +507,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'meta': {}} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -520,7 +520,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': ['asdf']} - req.body = jsonutils.dumps(expected) + req.body = jsonutils.dump_as_bytes(expected) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update_all, req, self.req_id, @@ -532,7 +532,7 @@ class volumeMetaDataTest(test.TestCase): req.method = 'PUT' req.content_type = "application/json" body = {'metadata': {'key10': 'value10'}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_all, req, '100', body) @@ -545,7 +545,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -565,7 +565,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -584,7 +584,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank('/v2/fake/volumes/asdf/metadata/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -610,7 +610,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -631,7 +631,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {("a" * 260): "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -652,7 +652,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": ("a" * 260)}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" req.environ['cinder.context'] = fake_context @@ -671,7 +671,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1", "key2": "value2"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -684,7 +684,7 @@ class volumeMetaDataTest(test.TestCase): req = fakes.HTTPRequest.blank(self.url + '/bad') req.method = 'PUT' body = {"meta": {"key1": "value1"}} - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, @@ -702,7 +702,7 @@ class volumeMetaDataTest(test.TestCase): # test for long key data = {"metadata": {"a" * 260: "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -713,7 +713,7 @@ class volumeMetaDataTest(test.TestCase): # test for long value data = {"metadata": {"key": "v" * 260}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, @@ -724,7 +724,7 @@ class volumeMetaDataTest(test.TestCase): # test for empty key. data = {"metadata": {"": "value1"}} - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.environ['cinder.context'] = fake_context with mock.patch.object(self.controller.volume_api, diff --git a/tests-py3.txt b/tests-py3.txt index cc2d520cf..60fd3d1f8 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -27,7 +27,8 @@ cinder.tests.unit.api.test_common 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.api.v1 +cinder.tests.unit.api.v2 cinder.tests.unit.backup.drivers.test_backup_glusterfs cinder.tests.unit.backup.drivers.test_backup_nfs cinder.tests.unit.backup.drivers.test_backup_posix -- 2.45.2