From 39260d38946582b82687aa31a061f0e2b0f38c6d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 24 Nov 2015 17:49:21 +0100 Subject: [PATCH] Test middleware test_faults to Python 3 * XMLDictSerializer: sort dictionary items to get a reliable XML output. On Python 3, dictionary items are iterated in a random order, because the hash function is randomized by default. * Use byte strings for HTTP body * On Python 3, decode serialized data to get Unicode * Add a base TestCase class to factorize the _prepare_xml() method * tests-py3.txt: add cinder.tests.unit.api.middleware.test_faults Partial-Implements: blueprint cinder-python3 Change-Id: I81cd509230265f47f8c00ade14c71f66c717ca81 --- cinder/api/openstack/wsgi.py | 4 +- .../tests/unit/api/middleware/test_faults.py | 47 +++++++++---------- tests-py3.txt | 1 + 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 779018b45..f61b603f7 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -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: diff --git a/cinder/tests/unit/api/middleware/test_faults.py b/cinder/tests/unit/api/middleware/test_faults.py index f4761c06c..eae64e564 100644 --- a/cinder/tests/unit/api/middleware/test_faults.py +++ b/cinder/tests/unit/api/middleware/test_faults.py @@ -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): - """Tests covering `cinder.api.openstack.faults:Fault` class.""" - - def setUp(self): - super(TestFaults, self).setUp() - self.useFixture(i18n_fixture.ToggleLazy(True)) - +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 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, diff --git a/tests-py3.txt b/tests-py3.txt index d57dd4658..61239e507 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -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 -- 2.45.2