Merge "Test middleware test_faults to Python 3"

This commit is contained in:
Jenkins 2016-02-19 16:53:25 +00:00 committed by Gerrit Code Review
commit 61985202dd
3 changed files with 26 additions and 28 deletions

View File

@ -481,7 +481,7 @@ class XMLDictSerializer(DictSerializer):
collections = metadata.get('dict_collections', {})
if nodename in collections:
metadata = collections[nodename]
for k, v in data.items():
for k, v in sorted(data.items()):
node = doc.createElement(metadata['item_name'])
node.setAttribute(metadata['item_key'], str(k))
text = doc.createTextNode(str(v))
@ -489,7 +489,7 @@ class XMLDictSerializer(DictSerializer):
result.appendChild(node)
return result
attrs = metadata.get('attributes', {}).get(nodename, {})
for k, v in data.items():
for k, v in sorted(data.items()):
if k in attrs:
result.setAttribute(k, str(v))
else:

View File

@ -18,6 +18,7 @@ from xml.dom import minidom
import mock
from oslo_i18n import fixture as i18n_fixture
from oslo_serialization import jsonutils
import six
import webob.dec
from cinder.api import common
@ -26,20 +27,24 @@ from cinder.i18n import _
from cinder import test
class TestFaults(test.TestCase):
class TestCase(test.TestCase):
def _prepare_xml(self, xml_string):
"""Remove characters from string which hinder XML equality testing."""
if six.PY3 and isinstance(xml_string, bytes):
xml_string = xml_string.decode('utf-8')
xml_string = xml_string.replace(" ", "")
xml_string = xml_string.replace("\n", "")
xml_string = xml_string.replace("\t", "")
return xml_string
class TestFaults(TestCase):
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
def setUp(self):
super(TestFaults, self).setUp()
self.useFixture(i18n_fixture.ToggleLazy(True))
def _prepare_xml(self, xml_string):
"""Remove characters from string which hinder XML equality testing."""
xml_string = xml_string.replace(" ", "")
xml_string = xml_string.replace("\n", "")
xml_string = xml_string.replace("\t", "")
return xml_string
def test_400_fault_json(self):
"""Test fault serialized to JSON via file-extension and/or header."""
requests = [
@ -97,7 +102,7 @@ class TestFaults(test.TestCase):
resp = req.get_response(raiser)
self.assertEqual("application/xml", resp.content_type)
self.assertEqual(404, resp.status_int)
self.assertIn('whut?', resp.body)
self.assertIn(b'whut?', resp.body)
def test_raise_403(self):
"""Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
@ -110,7 +115,7 @@ class TestFaults(test.TestCase):
self.assertEqual("application/xml", resp.content_type)
self.assertEqual(403, resp.status_int)
self.assertNotIn('resizeNotAllowed', resp.body)
self.assertIn('forbidden', resp.body)
self.assertIn(b'forbidden', resp.body)
@mock.patch('cinder.api.openstack.wsgi.i18n.translate')
def test_raise_http_with_localized_explanation(self, mock_translate):
@ -130,7 +135,7 @@ class TestFaults(test.TestCase):
resp = req.get_response(raiser)
self.assertEqual("application/xml", resp.content_type)
self.assertEqual(404, resp.status_int)
self.assertIn(("Mensaje traducido"), resp.body)
self.assertIn(b"Mensaje traducido", resp.body)
self.stubs.UnsetAll()
def test_fault_has_status_int(self):
@ -146,20 +151,14 @@ class TestFaults(test.TestCase):
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
response = request.get_response(fault)
self.assertIn(common.XML_NS_V2, response.body)
self.assertIn(common.XML_NS_V2, response.body.decode())
self.assertEqual("application/xml", response.content_type)
self.assertEqual(400, response.status_int)
class FaultsXMLSerializationTestV11(test.TestCase):
class FaultsXMLSerializationTestV11(TestCase):
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
def _prepare_xml(self, xml_string):
xml_string = xml_string.replace(" ", "")
xml_string = xml_string.replace("\n", "")
xml_string = xml_string.replace("\t", "")
return xml_string
def test_400_fault(self):
metadata = {'attributes': {"badRequest": 'code'}}
serializer = wsgi.XMLDictSerializer(metadata=metadata,
@ -197,6 +196,8 @@ class FaultsXMLSerializationTestV11(test.TestCase):
}
output = serializer.serialize(fixture)
if six.PY3:
output = output.decode('utf-8')
actual = minidom.parseString(self._prepare_xml(output))
expected = minidom.parseString(self._prepare_xml("""
@ -221,6 +222,8 @@ class FaultsXMLSerializationTestV11(test.TestCase):
}
output = serializer.serialize(fixture)
if six.PY3:
output = output.decode('utf-8')
actual = minidom.parseString(self._prepare_xml(output))
expected = minidom.parseString(self._prepare_xml("""
@ -232,15 +235,9 @@ class FaultsXMLSerializationTestV11(test.TestCase):
self.assertEqual(expected.toxml(), actual.toxml())
class FaultsXMLSerializationTestV2(test.TestCase):
class FaultsXMLSerializationTestV2(TestCase):
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
def _prepare_xml(self, xml_string):
xml_string = xml_string.replace(" ", "")
xml_string = xml_string.replace("\n", "")
xml_string = xml_string.replace("\t", "")
return xml_string
def test_400_fault(self):
metadata = {'attributes': {"badRequest": 'code'}}
serializer = wsgi.XMLDictSerializer(metadata=metadata,

View File

@ -1,4 +1,5 @@
cinder.tests.unit.api.contrib
cinder.tests.unit.api.middleware.test_faults
cinder.tests.unit.api.openstack.test_wsgi
cinder.tests.unit.api.test_common
cinder.tests.unit.api.test_extensions