~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/compute/test_api.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from nova.api import openstack as openstack_api
24
24
from nova.api.openstack import wsgi
25
25
import nova.context
 
26
from nova import exception
26
27
from nova.openstack.common import jsonutils
27
28
from nova import test
28
29
from nova.tests.api.openstack import fakes
120
121
        self.assertTrue('<computeFault' in resp.body, resp.body)
121
122
        self.assertEqual(resp.status_int, 500, resp.body)
122
123
 
 
124
    def _do_test_exception_safety_reflected_in_faults(self, expose):
 
125
        class ExceptionWithSafety(exception.NovaException):
 
126
            safe = expose
 
127
 
 
128
        @webob.dec.wsgify
 
129
        def fail(req):
 
130
            raise ExceptionWithSafety('some explanation')
 
131
 
 
132
        api = self._wsgi_app(fail)
 
133
        resp = webob.Request.blank('/').get_response(api)
 
134
        self.assertTrue('{"computeFault' in resp.body, resp.body)
 
135
        expected = ('ExceptionWithSafety: some explanation' if expose else
 
136
                    'The server has either erred or is incapable '
 
137
                    'of performing the requested operation.')
 
138
        self.assertTrue(expected in resp.body, resp.body)
 
139
        self.assertEqual(resp.status_int, 500, resp.body)
 
140
 
 
141
    def test_safe_exceptions_are_described_in_faults(self):
 
142
        self._do_test_exception_safety_reflected_in_faults(True)
 
143
 
 
144
    def test_unsafe_exceptions_are_not_described_in_faults(self):
 
145
        self._do_test_exception_safety_reflected_in_faults(False)
 
146
 
 
147
    def _do_test_exception_mapping(self, exception_type, msg):
 
148
        @webob.dec.wsgify
 
149
        def fail(req):
 
150
            raise exception_type(msg)
 
151
 
 
152
        api = self._wsgi_app(fail)
 
153
        resp = webob.Request.blank('/').get_response(api)
 
154
        self.assertTrue(msg in resp.body, resp.body)
 
155
        self.assertEqual(resp.status_int, exception_type.code, resp.body)
 
156
 
 
157
        if hasattr(exception_type, 'headers'):
 
158
            for (key, value) in exception_type.headers.iteritems():
 
159
                self.assertTrue(key in resp.headers)
 
160
                self.assertEquals(resp.headers[key], value)
 
161
 
 
162
    def test_quota_error_mapping(self):
 
163
        self._do_test_exception_mapping(exception.QuotaError, 'too many used')
 
164
 
 
165
    def test_non_nova_notfound_exception_mapping(self):
 
166
        class ExceptionWithCode(Exception):
 
167
            code = 404
 
168
 
 
169
        self._do_test_exception_mapping(ExceptionWithCode,
 
170
                                        'NotFound')
 
171
 
 
172
    def test_non_nova_exception_mapping(self):
 
173
        class ExceptionWithCode(Exception):
 
174
            code = 417
 
175
 
 
176
        self._do_test_exception_mapping(ExceptionWithCode,
 
177
                                        'Expectation failed')
 
178
 
 
179
    def test_exception_with_none_code_throws_500(self):
 
180
        class ExceptionWithNoneCode(Exception):
 
181
            code = None
 
182
 
 
183
        msg = 'Internal Server Error'
 
184
 
 
185
        @webob.dec.wsgify
 
186
        def fail(req):
 
187
            raise ExceptionWithNoneCode()
 
188
 
 
189
        api = self._wsgi_app(fail)
 
190
        resp = webob.Request.blank('/').get_response(api)
 
191
        self.assertEqual(500, resp.status_int)
 
192
 
123
193
    def test_request_id_in_response(self):
124
194
        req = webob.Request.blank('/')
125
195
        req.method = 'GET'