Merge "Test middleware test_faults to Python 3"
This commit is contained in:
commit
61985202dd
@ -481,7 +481,7 @@ class XMLDictSerializer(DictSerializer):
|
|||||||
collections = metadata.get('dict_collections', {})
|
collections = metadata.get('dict_collections', {})
|
||||||
if nodename in collections:
|
if nodename in collections:
|
||||||
metadata = collections[nodename]
|
metadata = collections[nodename]
|
||||||
for k, v in data.items():
|
for k, v in sorted(data.items()):
|
||||||
node = doc.createElement(metadata['item_name'])
|
node = doc.createElement(metadata['item_name'])
|
||||||
node.setAttribute(metadata['item_key'], str(k))
|
node.setAttribute(metadata['item_key'], str(k))
|
||||||
text = doc.createTextNode(str(v))
|
text = doc.createTextNode(str(v))
|
||||||
@ -489,7 +489,7 @@ class XMLDictSerializer(DictSerializer):
|
|||||||
result.appendChild(node)
|
result.appendChild(node)
|
||||||
return result
|
return result
|
||||||
attrs = metadata.get('attributes', {}).get(nodename, {})
|
attrs = metadata.get('attributes', {}).get(nodename, {})
|
||||||
for k, v in data.items():
|
for k, v in sorted(data.items()):
|
||||||
if k in attrs:
|
if k in attrs:
|
||||||
result.setAttribute(k, str(v))
|
result.setAttribute(k, str(v))
|
||||||
else:
|
else:
|
||||||
|
@ -18,6 +18,7 @@ from xml.dom import minidom
|
|||||||
import mock
|
import mock
|
||||||
from oslo_i18n import fixture as i18n_fixture
|
from oslo_i18n import fixture as i18n_fixture
|
||||||
from oslo_serialization import jsonutils
|
from oslo_serialization import jsonutils
|
||||||
|
import six
|
||||||
import webob.dec
|
import webob.dec
|
||||||
|
|
||||||
from cinder.api import common
|
from cinder.api import common
|
||||||
@ -26,20 +27,24 @@ from cinder.i18n import _
|
|||||||
from cinder import test
|
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."""
|
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestFaults, self).setUp()
|
super(TestFaults, self).setUp()
|
||||||
self.useFixture(i18n_fixture.ToggleLazy(True))
|
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):
|
def test_400_fault_json(self):
|
||||||
"""Test fault serialized to JSON via file-extension and/or header."""
|
"""Test fault serialized to JSON via file-extension and/or header."""
|
||||||
requests = [
|
requests = [
|
||||||
@ -97,7 +102,7 @@ class TestFaults(test.TestCase):
|
|||||||
resp = req.get_response(raiser)
|
resp = req.get_response(raiser)
|
||||||
self.assertEqual("application/xml", resp.content_type)
|
self.assertEqual("application/xml", resp.content_type)
|
||||||
self.assertEqual(404, resp.status_int)
|
self.assertEqual(404, resp.status_int)
|
||||||
self.assertIn('whut?', resp.body)
|
self.assertIn(b'whut?', resp.body)
|
||||||
|
|
||||||
def test_raise_403(self):
|
def test_raise_403(self):
|
||||||
"""Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
|
"""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("application/xml", resp.content_type)
|
||||||
self.assertEqual(403, resp.status_int)
|
self.assertEqual(403, resp.status_int)
|
||||||
self.assertNotIn('resizeNotAllowed', resp.body)
|
self.assertNotIn('resizeNotAllowed', resp.body)
|
||||||
self.assertIn('forbidden', resp.body)
|
self.assertIn(b'forbidden', resp.body)
|
||||||
|
|
||||||
@mock.patch('cinder.api.openstack.wsgi.i18n.translate')
|
@mock.patch('cinder.api.openstack.wsgi.i18n.translate')
|
||||||
def test_raise_http_with_localized_explanation(self, mock_translate):
|
def test_raise_http_with_localized_explanation(self, mock_translate):
|
||||||
@ -130,7 +135,7 @@ class TestFaults(test.TestCase):
|
|||||||
resp = req.get_response(raiser)
|
resp = req.get_response(raiser)
|
||||||
self.assertEqual("application/xml", resp.content_type)
|
self.assertEqual("application/xml", resp.content_type)
|
||||||
self.assertEqual(404, resp.status_int)
|
self.assertEqual(404, resp.status_int)
|
||||||
self.assertIn(("Mensaje traducido"), resp.body)
|
self.assertIn(b"Mensaje traducido", resp.body)
|
||||||
self.stubs.UnsetAll()
|
self.stubs.UnsetAll()
|
||||||
|
|
||||||
def test_fault_has_status_int(self):
|
def test_fault_has_status_int(self):
|
||||||
@ -146,20 +151,14 @@ class TestFaults(test.TestCase):
|
|||||||
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
|
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
|
||||||
response = request.get_response(fault)
|
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("application/xml", response.content_type)
|
||||||
self.assertEqual(400, response.status_int)
|
self.assertEqual(400, response.status_int)
|
||||||
|
|
||||||
|
|
||||||
class FaultsXMLSerializationTestV11(test.TestCase):
|
class FaultsXMLSerializationTestV11(TestCase):
|
||||||
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
|
"""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):
|
def test_400_fault(self):
|
||||||
metadata = {'attributes': {"badRequest": 'code'}}
|
metadata = {'attributes': {"badRequest": 'code'}}
|
||||||
serializer = wsgi.XMLDictSerializer(metadata=metadata,
|
serializer = wsgi.XMLDictSerializer(metadata=metadata,
|
||||||
@ -197,6 +196,8 @@ class FaultsXMLSerializationTestV11(test.TestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
output = serializer.serialize(fixture)
|
output = serializer.serialize(fixture)
|
||||||
|
if six.PY3:
|
||||||
|
output = output.decode('utf-8')
|
||||||
actual = minidom.parseString(self._prepare_xml(output))
|
actual = minidom.parseString(self._prepare_xml(output))
|
||||||
|
|
||||||
expected = minidom.parseString(self._prepare_xml("""
|
expected = minidom.parseString(self._prepare_xml("""
|
||||||
@ -221,6 +222,8 @@ class FaultsXMLSerializationTestV11(test.TestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
output = serializer.serialize(fixture)
|
output = serializer.serialize(fixture)
|
||||||
|
if six.PY3:
|
||||||
|
output = output.decode('utf-8')
|
||||||
actual = minidom.parseString(self._prepare_xml(output))
|
actual = minidom.parseString(self._prepare_xml(output))
|
||||||
|
|
||||||
expected = minidom.parseString(self._prepare_xml("""
|
expected = minidom.parseString(self._prepare_xml("""
|
||||||
@ -232,15 +235,9 @@ class FaultsXMLSerializationTestV11(test.TestCase):
|
|||||||
self.assertEqual(expected.toxml(), actual.toxml())
|
self.assertEqual(expected.toxml(), actual.toxml())
|
||||||
|
|
||||||
|
|
||||||
class FaultsXMLSerializationTestV2(test.TestCase):
|
class FaultsXMLSerializationTestV2(TestCase):
|
||||||
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
|
"""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):
|
def test_400_fault(self):
|
||||||
metadata = {'attributes': {"badRequest": 'code'}}
|
metadata = {'attributes': {"badRequest": 'code'}}
|
||||||
serializer = wsgi.XMLDictSerializer(metadata=metadata,
|
serializer = wsgi.XMLDictSerializer(metadata=metadata,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
cinder.tests.unit.api.contrib
|
cinder.tests.unit.api.contrib
|
||||||
|
cinder.tests.unit.api.middleware.test_faults
|
||||||
cinder.tests.unit.api.openstack.test_wsgi
|
cinder.tests.unit.api.openstack.test_wsgi
|
||||||
cinder.tests.unit.api.test_common
|
cinder.tests.unit.api.test_common
|
||||||
cinder.tests.unit.api.test_extensions
|
cinder.tests.unit.api.test_extensions
|
||||||
|
Loading…
Reference in New Issue
Block a user