]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Test middleware test_faults to Python 3
authorVictor Stinner <vstinner@redhat.com>
Tue, 24 Nov 2015 16:49:21 +0000 (17:49 +0100)
committerVictor Stinner <vstinner@redhat.com>
Thu, 18 Feb 2016 12:09:18 +0000 (13:09 +0100)
* 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
cinder/tests/unit/api/middleware/test_faults.py
tests-py3.txt

index 779018b45aad6fa44da6676762914a3be72b4169..f61b603f7faba3e69af3acf57867bf2b30908acc 100644 (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:
index f4761c06c7d26e2748a440b159969f3ff1ad5e12..eae64e564e79d25762f8a2820557eed12833f807 100644 (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):
-    """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,
index d57dd4658276df219dc9351f651785e01b56045b..61239e507905060c153b4edf0f46d1b2e4e96a52 100644 (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