~zulcss/cinder/cinder-ca-g2

« back to all changes in this revision

Viewing changes to cinder/tests/api/openstack/test_faults.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-10-09 08:26:21 UTC
  • mfrom: (3.1.10 quantal)
  • Revision ID: package-import@ubuntu.com-20121009082621-stc86vcuyzyrp7ow
Tags: 2012.2-0ubuntu2
* debian/cinder_tgt.conf: Add missing configuration file. (LP: #1064366) 
* debian/README.Debian: Added note about migration from nova-volume
  to cinder-volume.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
from xml.dom import minidom
 
19
 
 
20
import webob
 
21
import webob.dec
 
22
import webob.exc
 
23
 
 
24
from cinder import test
 
25
from cinder.api.openstack import common
 
26
from cinder.api.openstack import wsgi
 
27
from cinder.openstack.common import jsonutils
 
28
 
 
29
 
 
30
class TestFaults(test.TestCase):
 
31
    """Tests covering `cinder.api.openstack.faults:Fault` class."""
 
32
 
 
33
    def _prepare_xml(self, xml_string):
 
34
        """Remove characters from string which hinder XML equality testing."""
 
35
        xml_string = xml_string.replace("  ", "")
 
36
        xml_string = xml_string.replace("\n", "")
 
37
        xml_string = xml_string.replace("\t", "")
 
38
        return xml_string
 
39
 
 
40
    def test_400_fault_json(self):
 
41
        """Test fault serialized to JSON via file-extension and/or header."""
 
42
        requests = [
 
43
            webob.Request.blank('/.json'),
 
44
            webob.Request.blank('/', headers={"Accept": "application/json"}),
 
45
        ]
 
46
 
 
47
        for request in requests:
 
48
            fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
 
49
            response = request.get_response(fault)
 
50
 
 
51
            expected = {
 
52
                "badRequest": {
 
53
                    "message": "scram",
 
54
                    "code": 400,
 
55
                },
 
56
            }
 
57
            actual = jsonutils.loads(response.body)
 
58
 
 
59
            self.assertEqual(response.content_type, "application/json")
 
60
            self.assertEqual(expected, actual)
 
61
 
 
62
    def test_413_fault_json(self):
 
63
        """Test fault serialized to JSON via file-extension and/or header."""
 
64
        requests = [
 
65
            webob.Request.blank('/.json'),
 
66
            webob.Request.blank('/', headers={"Accept": "application/json"}),
 
67
        ]
 
68
 
 
69
        for request in requests:
 
70
            exc = webob.exc.HTTPRequestEntityTooLarge
 
71
            fault = wsgi.Fault(exc(explanation='sorry',
 
72
                        headers={'Retry-After': 4}))
 
73
            response = request.get_response(fault)
 
74
 
 
75
            expected = {
 
76
                "overLimit": {
 
77
                    "message": "sorry",
 
78
                    "code": 413,
 
79
                    "retryAfter": 4,
 
80
                },
 
81
            }
 
82
            actual = jsonutils.loads(response.body)
 
83
 
 
84
            self.assertEqual(response.content_type, "application/json")
 
85
            self.assertEqual(expected, actual)
 
86
 
 
87
    def test_raise(self):
 
88
        """Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
 
89
        @webob.dec.wsgify
 
90
        def raiser(req):
 
91
            raise wsgi.Fault(webob.exc.HTTPNotFound(explanation='whut?'))
 
92
 
 
93
        req = webob.Request.blank('/.xml')
 
94
        resp = req.get_response(raiser)
 
95
        self.assertEqual(resp.content_type, "application/xml")
 
96
        self.assertEqual(resp.status_int, 404)
 
97
        self.assertTrue('whut?' in resp.body)
 
98
 
 
99
    def test_raise_403(self):
 
100
        """Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
 
101
        @webob.dec.wsgify
 
102
        def raiser(req):
 
103
            raise wsgi.Fault(webob.exc.HTTPForbidden(explanation='whut?'))
 
104
 
 
105
        req = webob.Request.blank('/.xml')
 
106
        resp = req.get_response(raiser)
 
107
        self.assertEqual(resp.content_type, "application/xml")
 
108
        self.assertEqual(resp.status_int, 403)
 
109
        self.assertTrue('resizeNotAllowed' not in resp.body)
 
110
        self.assertTrue('forbidden' in resp.body)
 
111
 
 
112
    def test_fault_has_status_int(self):
 
113
        """Ensure the status_int is set correctly on faults"""
 
114
        fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?'))
 
115
        self.assertEqual(fault.status_int, 400)
 
116
 
 
117
    def test_xml_serializer(self):
 
118
        """Ensure that a v1.1 request responds with a v1 xmlns"""
 
119
        request = webob.Request.blank('/v1',
 
120
                                      headers={"Accept": "application/xml"})
 
121
 
 
122
        fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
 
123
        response = request.get_response(fault)
 
124
 
 
125
        self.assertTrue(common.XML_NS_V1 in response.body)
 
126
        self.assertEqual(response.content_type, "application/xml")
 
127
        self.assertEqual(response.status_int, 400)
 
128
 
 
129
 
 
130
class FaultsXMLSerializationTestV11(test.TestCase):
 
131
    """Tests covering `cinder.api.openstack.faults:Fault` class."""
 
132
 
 
133
    def _prepare_xml(self, xml_string):
 
134
        xml_string = xml_string.replace("  ", "")
 
135
        xml_string = xml_string.replace("\n", "")
 
136
        xml_string = xml_string.replace("\t", "")
 
137
        return xml_string
 
138
 
 
139
    def test_400_fault(self):
 
140
        metadata = {'attributes': {"badRequest": 'code'}}
 
141
        serializer = wsgi.XMLDictSerializer(metadata=metadata,
 
142
                                            xmlns=common.XML_NS_V1)
 
143
 
 
144
        fixture = {
 
145
            "badRequest": {
 
146
                "message": "scram",
 
147
                "code": 400,
 
148
            },
 
149
        }
 
150
 
 
151
        output = serializer.serialize(fixture)
 
152
        actual = minidom.parseString(self._prepare_xml(output))
 
153
 
 
154
        expected = minidom.parseString(self._prepare_xml("""
 
155
                <badRequest code="400" xmlns="%s">
 
156
                    <message>scram</message>
 
157
                </badRequest>
 
158
            """) % common.XML_NS_V1)
 
159
 
 
160
        self.assertEqual(expected.toxml(), actual.toxml())
 
161
 
 
162
    def test_413_fault(self):
 
163
        metadata = {'attributes': {"overLimit": 'code'}}
 
164
        serializer = wsgi.XMLDictSerializer(metadata=metadata,
 
165
                                            xmlns=common.XML_NS_V1)
 
166
 
 
167
        fixture = {
 
168
            "overLimit": {
 
169
                "message": "sorry",
 
170
                "code": 413,
 
171
                "retryAfter": 4,
 
172
            },
 
173
        }
 
174
 
 
175
        output = serializer.serialize(fixture)
 
176
        actual = minidom.parseString(self._prepare_xml(output))
 
177
 
 
178
        expected = minidom.parseString(self._prepare_xml("""
 
179
                <overLimit code="413" xmlns="%s">
 
180
                    <message>sorry</message>
 
181
                    <retryAfter>4</retryAfter>
 
182
                </overLimit>
 
183
            """) % common.XML_NS_V1)
 
184
 
 
185
        self.assertEqual(expected.toxml(), actual.toxml())
 
186
 
 
187
    def test_404_fault(self):
 
188
        metadata = {'attributes': {"itemNotFound": 'code'}}
 
189
        serializer = wsgi.XMLDictSerializer(metadata=metadata,
 
190
                                            xmlns=common.XML_NS_V1)
 
191
 
 
192
        fixture = {
 
193
            "itemNotFound": {
 
194
                "message": "sorry",
 
195
                "code": 404,
 
196
            },
 
197
        }
 
198
 
 
199
        output = serializer.serialize(fixture)
 
200
        actual = minidom.parseString(self._prepare_xml(output))
 
201
 
 
202
        expected = minidom.parseString(self._prepare_xml("""
 
203
                <itemNotFound code="404" xmlns="%s">
 
204
                    <message>sorry</message>
 
205
                </itemNotFound>
 
206
            """) % common.XML_NS_V1)
 
207
 
 
208
        self.assertEqual(expected.toxml(), actual.toxml())