summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Waldon <brian.waldon@rackspace.com>2011-09-19 19:56:51 +0000
committerTarmac <>2011-09-19 19:56:51 +0000
commit08d3053aea0f77f38f1614e7cb867bddb8e55302 (patch)
tree1268c5316e1bd72eb37834d36a0cab5e1e1202ae
parenteea1afce94dd1f1303a5bda4c157894a45ee88cf (diff)
parent4c29835b6e5bd89460846588994f75543e5c235a (diff)
Fixing case where OSAPI server create would return 500 on malformed body.
-rw-r--r--nova/api/openstack/servers.py6
-rw-r--r--nova/tests/api/openstack/test_servers.py52
2 files changed, 57 insertions, 1 deletions
diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py
index 0ef246852..856c3c613 100644
--- a/nova/api/openstack/servers.py
+++ b/nova/api/openstack/servers.py
@@ -656,7 +656,11 @@ class ControllerV11(Controller):
def _get_key_name(self, req, body):
if 'server' in body:
- return body['server'].get('key_name')
+ try:
+ return body['server'].get('key_name')
+ except AttributeError:
+ msg = _("Malformed server entity")
+ raise exc.HTTPBadRequest(explanation=msg)
def _image_ref_from_req_data(self, data):
try:
diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py
index a2c8b3b04..890ff7de0 100644
--- a/nova/tests/api/openstack/test_servers.py
+++ b/nova/tests/api/openstack/test_servers.py
@@ -2194,6 +2194,58 @@ class ServersTest(test.TestCase):
res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)
+ def test_create_instance_v1_1_malformed_entity(self):
+ self._setup_for_create_instance()
+ req = webob.Request.blank('/v1.1/fake/servers')
+ req.method = 'POST'
+ req.body = json.dumps({'server': 'string'})
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 400)
+
+ def test_create_instance_v1_1_malformed_body_string(self):
+ self._setup_for_create_instance()
+ req = webob.Request.blank('/v1.1/fake/servers')
+ req.method = 'POST'
+ req.body = 'string'
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 400)
+
+ def test_create_instance_v1_1_malformed_body_list(self):
+ self._setup_for_create_instance()
+ body = ['string']
+ req = webob.Request.blank('/v1.1/fake/servers')
+ req.method = 'POST'
+ req.body = json.dumps(['string'])
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 422)
+
+ def test_create_instance_v1_0_malformed_entity(self):
+ req = webob.Request.blank('/v1.0/servers')
+ req.method = 'POST'
+ req.body = json.dumps({'server': 'string'})
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 400)
+
+ def test_create_instance_v1_0_malformed_body_string(self):
+ req = webob.Request.blank('/v1.0/servers')
+ req.method = 'POST'
+ req.body = 'string'
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 400)
+
+ def test_create_instance_v1_0_malformed_body_list(self):
+ req = webob.Request.blank('/v1.0/servers')
+ req.method = 'POST'
+ req.body = json.dumps(['string'])
+ req.headers['content-type'] = "application/json"
+ res = req.get_response(fakes.wsgi_app())
+ self.assertEqual(res.status_int, 422)
+
def test_update_server_no_body(self):
req = webob.Request.blank('/v1.0/servers/1')
req.method = 'PUT'