Return HTTP 422 on bad server update PUT request.

This patch updates the /servers API and the disk config
extension so that they properly handle incorrectly formatted
server update/PUT requests.

Includes a new test cause that verifies (with extensions enabled)
that a poorly formatted PUT request to /servers returns HTTP 422
instead of a 500.

Also updated the previous test_create_missing_server test to use
test.TestException instead of Exception.

Fixes LP Bug #1038227.

Change-Id: I6b11602790778465348647bb5825b7326d50065a
This commit is contained in:
Dan Prince 2012-08-17 15:34:28 -04:00
parent a10be151ad
commit 1a605cccb1
3 changed files with 23 additions and 2 deletions

View File

@ -148,7 +148,8 @@ class ServerDiskConfigController(wsgi.Controller):
def update(self, req, id, body):
context = req.environ['nova.context']
if authorize(context):
self._set_disk_config(body['server'])
if 'server' in body:
self._set_disk_config(body['server'])
resp_obj = (yield)
self._show(req, resp_obj)

View File

@ -813,6 +813,9 @@ class Controller(wsgi.Controller):
if not body:
raise exc.HTTPUnprocessableEntity()
if not 'server' in body:
raise exc.HTTPUnprocessableEntity()
ctxt = req.environ['nova.context']
update_dict = {}

View File

@ -4765,7 +4765,7 @@ class ServersAllExtensionsTestCase(test.TestCase):
"""Test create with malformed body"""
def fake_create(*args, **kwargs):
raise Exception("Request should not reach the compute API.")
raise test.TestingException("Should not reach the compute API.")
self.stubs.Set(nova.compute.api.API, 'create', fake_create)
@ -4777,3 +4777,20 @@ class ServersAllExtensionsTestCase(test.TestCase):
req.body = jsonutils.dumps(body)
res = req.get_response(self.app)
self.assertEqual(422, res.status_int)
def test_update_missing_server(self):
"""Test create with malformed body"""
def fake_update(*args, **kwargs):
raise test.TestingException("Should not reach the compute API.")
self.stubs.Set(nova.compute.api.API, 'create', fake_update)
req = fakes.HTTPRequest.blank('/fake/servers/1')
req.method = 'PUT'
req.content_type = 'application/json'
body = {'foo': {'a': 'b'}}
req.body = jsonutils.dumps(body)
res = req.get_response(self.app)
self.assertEqual(422, res.status_int)