From 1710ceead5fac3716571e6ccde4bc7e6dd026391 Mon Sep 17 00:00:00 2001 From: Sirushti Murugesan Date: Fri, 28 Aug 2015 14:43:37 +0530 Subject: [PATCH] py34: heat.tests.api (1) * convert to bytes for response.body * assert against dicts of params instead of string url params * use py3 compatible stubout from mox3 than the default stubout module * NoneType and int aren't comparable, so use int(NoneType or 0) * use list() to use pop() over return value of filter() partial blueprint heat-python34-support Change-Id: Iadab94ff7446a3f5a01e45d5408f3219ef518c6a --- heat/api/openstack/v1/stacks.py | 2 +- heat/common/wsgi.py | 5 +-- heat/tests/api/openstack_v1/test_stacks.py | 2 +- .../api/openstack_v1/test_views_common.py | 10 +++--- heat/tests/api/openstack_v1/tools.py | 3 +- heat/tests/api/test_wsgi.py | 34 +++++++++---------- py3-testlist | 3 ++ 7 files changed, 33 insertions(+), 26 deletions(-) diff --git a/heat/api/openstack/v1/stacks.py b/heat/api/openstack/v1/stacks.py index b051d60b9a..bf70af7f89 100644 --- a/heat/api/openstack/v1/stacks.py +++ b/heat/api/openstack/v1/stacks.py @@ -598,7 +598,7 @@ class StackSerializer(serializers.JSONResponseSerializer): self._populate_response_header(response, result['stack']['links'][0]['href'], 201) - response.body = self.to_json(result) + response.body = six.b(self.to_json(result)) return response diff --git a/heat/common/wsgi.py b/heat/common/wsgi.py index cf9e3f3be8..7d6c7b4ba2 100644 --- a/heat/common/wsgi.py +++ b/heat/common/wsgi.py @@ -761,7 +761,7 @@ def is_json_content_type(request): if not content_type or content_type.startswith('text/plain'): content_type = 'application/json' if (content_type in ('JSON', 'application/json') - and request.body.startswith('{')): + and request.body.startswith(b'{')): return True return False @@ -773,7 +773,8 @@ class JSONRequestDeserializer(object): :param request: Webob.Request object """ - if request.content_length > 0 and is_json_content_type(request): + if (int(request.content_length or 0) > 0 and + is_json_content_type(request)): return True return False diff --git a/heat/tests/api/openstack_v1/test_stacks.py b/heat/tests/api/openstack_v1/test_stacks.py index 2e9b450525..f29b17aacf 100644 --- a/heat/tests/api/openstack_v1/test_stacks.py +++ b/heat/tests/api/openstack_v1/test_stacks.py @@ -2211,5 +2211,5 @@ class StackSerializerTest(common.HeatTestCase): response = webob.Response() response = self.serializer.create(response, result) self.assertEqual(201, response.status_int) - self.assertEqual('location', response.headers['Location']) + self.assertEqual(b'location', response.headers['Location']) self.assertEqual('application/json', response.headers['Content-Type']) diff --git a/heat/tests/api/openstack_v1/test_views_common.py b/heat/tests/api/openstack_v1/test_views_common.py index c9fbd377de..9b69efb8fb 100644 --- a/heat/tests/api/openstack_v1/test_views_common.py +++ b/heat/tests/api/openstack_v1/test_views_common.py @@ -39,7 +39,8 @@ class TestViewsCommon(common.HeatTestCase): links = views_common.get_collection_links(self.request, self.items) expected_params = {'marker': ['id2'], 'limit': ['2']} - next_link = filter(lambda link: link['rel'] == 'next', links).pop() + next_link = list(filter( + lambda link: link['rel'] == 'next', links)).pop() self.assertEqual('next', next_link['rel']) url_path, url_params = next_link['href'].split('?', 1) self.assertEqual(url_path, self.request.path_url) @@ -63,9 +64,9 @@ class TestViewsCommon(common.HeatTestCase): self.setUpGetCollectionLinks() self.request.params = {'limit': '2', 'marker': 'some_marker'} links = views_common.get_collection_links(self.request, self.items) - expected_params = {'marker': ['id2'], 'limit': ['2']} - next_link = filter(lambda link: link['rel'] == 'next', links).pop() + next_link = list(filter( + lambda link: link['rel'] == 'next', links)).pop() self.assertEqual('next', next_link['rel']) url_path, url_params = next_link['href'].split('?', 1) self.assertEqual(url_path, self.request.path_url) @@ -76,7 +77,8 @@ class TestViewsCommon(common.HeatTestCase): self.request.params = {'limit': '2', 'foo': 'bar'} links = views_common.get_collection_links(self.request, self.items) - next_link = filter(lambda link: link['rel'] == 'next', links).pop() + next_link = list( + filter(lambda link: link['rel'] == 'next', links)).pop() url = next_link['href'] query_string = urlparse.urlparse(url).query params = {} diff --git a/heat/tests/api/openstack_v1/tools.py b/heat/tests/api/openstack_v1/tools.py index 0e9ee03f58..1bddc9beee 100644 --- a/heat/tests/api/openstack_v1/tools.py +++ b/heat/tests/api/openstack_v1/tools.py @@ -14,6 +14,7 @@ from oslo_config import cfg from oslo_log import log from oslo_messaging._drivers import common as rpc_common +import six import webob.exc from heat.common import wsgi @@ -95,7 +96,7 @@ class ControllerTest(object): req = wsgi.Request(environ) req.context = utils.dummy_context('api_test_user', self.tenant) self.context = req.context - req.body = data + req.body = six.b(data) return req def _post(self, path, data, content_type='application/json'): diff --git a/heat/tests/api/test_wsgi.py b/heat/tests/api/test_wsgi.py index c7b32fe366..24989c2de7 100644 --- a/heat/tests/api/test_wsgi.py +++ b/heat/tests/api/test_wsgi.py @@ -20,9 +20,9 @@ import json import mock import six import socket -import stubout import webob +from mox import stubout from oslo_config import cfg from heat.api.aws import exception as aws_exception @@ -190,7 +190,7 @@ class ResourceTest(common.HeatTestCase): actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) - request.body = '{"foo" : "value"}' + request.body = b'{"foo" : "value"}' resource = wsgi.Resource(Controller(), wsgi.JSONRequestDeserializer(), None) @@ -210,7 +210,7 @@ class ResourceTest(common.HeatTestCase): actions = {'action': 'delete', 'id': 12, 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) - request.body = '{"foo" : "value"}' + request.body = b'{"foo" : "value"}' message_es = "No Encontrado" translated_ex = webob.exc.HTTPBadRequest(message_es) @@ -257,7 +257,7 @@ class ResourceExceptionHandlingTest(common.HeatTestCase): actions = {'action': 'raise_exception', 'body': 'data'} env = {'wsgiorg.routing_args': [None, actions]} request = wsgi.Request.blank('/tests/123', environ=env) - request.body = '{"foo" : "value"}' + request.body = b'{"foo" : "value"}' resource = wsgi.Resource(Controller(self.exception), wsgi.JSONRequestDeserializer(), None) @@ -271,7 +271,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_no_content_length(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = 'asdf' + request.body = b'asdf' request.headers.pop('Content-Length') request.headers['Content-Type'] = 'application/json' self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request)) @@ -279,7 +279,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_zero_content_length(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = 'asdf' + request.body = b'asdf' request.headers['Content-Length'] = 0 request.headers['Content-Type'] = 'application/json' self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request)) @@ -287,14 +287,14 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_has_content_length_no_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) def test_has_body_has_content_length_plain_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'text/plain' self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) @@ -302,7 +302,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_has_content_type_malformed(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = 'asdf' + request.body = b'asdf' self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/json' self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request)) @@ -310,7 +310,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_has_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/json' self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) @@ -318,7 +318,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_has_wrong_content_type(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/xml' self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request)) @@ -326,14 +326,14 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_has_aws_content_type_only(self): request = wsgi.Request.blank('/?ContentType=JSON') request.method = 'GET' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) def test_has_body_respect_aws_content_type(self): request = wsgi.Request.blank('/?ContentType=JSON') request.method = 'GET' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) request.headers['Content-Type'] = 'application/xml' self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) @@ -341,7 +341,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_has_body_content_type_with_get(self): request = wsgi.Request.blank('/') request.method = 'GET' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' self.assertIn('Content-Length', request.headers) self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request)) @@ -369,7 +369,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_default_with_body(self): request = wsgi.Request.blank('/') request.method = 'POST' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' actual = wsgi.JSONRequestDeserializer().default(request) expected = {"body": {"key": "value"}} self.assertEqual(expected, actual) @@ -377,7 +377,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_default_with_get_with_body(self): request = wsgi.Request.blank('/') request.method = 'GET' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' actual = wsgi.JSONRequestDeserializer().default(request) expected = {"body": {"key": "value"}} self.assertEqual(expected, actual) @@ -385,7 +385,7 @@ class JSONRequestDeserializerTest(common.HeatTestCase): def test_default_with_get_with_body_with_aws(self): request = wsgi.Request.blank('/?ContentType=JSON') request.method = 'GET' - request.body = '{"key": "value"}' + request.body = b'{"key": "value"}' actual = wsgi.JSONRequestDeserializer().default(request) expected = {"body": {"key": "value"}} self.assertEqual(expected, actual) diff --git a/py3-testlist b/py3-testlist index caf08182f8..e88539f380 100644 --- a/py3-testlist +++ b/py3-testlist @@ -2,7 +2,10 @@ heat.tests.api.aws.test_api_aws heat.tests.api.middleware.test_ssl_middleware heat.tests.api.middleware.test_version_negotiation_middleware heat.tests.api.openstack_v1.test_stacks_view +heat.tests.api.openstack_v1.test_stacks heat.tests.api.openstack_v1.test_util +heat.tests.api.openstack_v1.test_views_common +heat.tests.api.test_wsgi heat.tests.autoscaling heat.tests.autoscaling.test_heat_scaling_group heat.tests.autoscaling.test_heat_scaling_policy