summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Prince <dprince@redhat.com>2012-08-15 13:07:19 -0400
committerDan Prince <dprince@redhat.com>2012-08-15 17:11:42 -0400
commit00f0b170ec15299b20c345fa1f66f21185c2dc79 (patch)
tree4804d141f4bea7c8eaf0642ca87a4b156d0895da
parent40a34547198b8eb32e4afbcf57b5d8f9207b35f3 (diff)
Update disk config to check for 'server' in req.
Updates the disk config extension to check for 'server' in the request body before trying to use it. This avoids a potential KeyError exception. Given that we already validate the 'server' dict in servers.py and raise a proper exception there this seems reasonable to me... no reason to duplicate all of the validation logic. The reason this change was able to make it into Nova to begin with is that the Extensions loader orders things differently depending on the OS/python version being used. I tested this by running SchedulerHintsTestCase.test_create_missing_server on both Fedora 16 (passes) and Fedora 17 (fails). The problem only manifests itself when the disk config extension gets called before the scheduler_hints extension. To fix this issue I've updated the scheduler_hints extensions in this patchset to simply pass when 'server' isn't defined which like above seems reasonable because we already validate this in servers.py. As part of this fix I'm also moving test_create_missing_server out of the Scheduler Hints tests and into a new ServersAllExtensionsTestCase class in test_servers.py which explicitly tests for things with *all* extensions enabled. As part of the move I also updated the test to look for HTTP 422 Unprocessable Entity which is what we throw when 'server' doesn't exist in a POST to servers (the correct exception). Fixes LP Bug #1037201. Change-Id: I912fc1234b2e7a76ecc54140f4a31c2e32fdfa98
-rw-r--r--nova/api/openstack/compute/contrib/disk_config.py3
-rw-r--r--nova/api/openstack/compute/contrib/scheduler_hints.py5
-rw-r--r--nova/tests/api/openstack/compute/contrib/test_scheduler_hints.py17
-rw-r--r--nova/tests/api/openstack/compute/test_servers.py27
4 files changed, 30 insertions, 22 deletions
diff --git a/nova/api/openstack/compute/contrib/disk_config.py b/nova/api/openstack/compute/contrib/disk_config.py
index 961457c46..293be7415 100644
--- a/nova/api/openstack/compute/contrib/disk_config.py
+++ b/nova/api/openstack/compute/contrib/disk_config.py
@@ -139,7 +139,8 @@ class ServerDiskConfigController(wsgi.Controller):
def create(self, req, 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)
diff --git a/nova/api/openstack/compute/contrib/scheduler_hints.py b/nova/api/openstack/compute/contrib/scheduler_hints.py
index 4bff779a5..e8d65a741 100644
--- a/nova/api/openstack/compute/contrib/scheduler_hints.py
+++ b/nova/api/openstack/compute/contrib/scheduler_hints.py
@@ -49,10 +49,7 @@ class SchedulerHintsController(wsgi.Controller):
if 'server' in body:
body['server']['scheduler_hints'] = hints
- yield
- else:
- msg = _("Missing server attribute")
- raise webob.exc.HTTPBadRequest(reason=msg)
+ yield
class Scheduler_hints(extensions.ExtensionDescriptor):
diff --git a/nova/tests/api/openstack/compute/contrib/test_scheduler_hints.py b/nova/tests/api/openstack/compute/contrib/test_scheduler_hints.py
index afb6d966b..c651444f4 100644
--- a/nova/tests/api/openstack/compute/contrib/test_scheduler_hints.py
+++ b/nova/tests/api/openstack/compute/contrib/test_scheduler_hints.py
@@ -94,20 +94,3 @@ class SchedulerHintsTestCase(test.TestCase):
req.body = jsonutils.dumps(body)
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
-
- def test_create_missing_server(self):
- """Test create with malformed body"""
-
- def fake_create(*args, **kwargs):
- raise Exception("Request should not reach the compute API.")
-
- self.stubs.Set(nova.compute.api.API, 'create', fake_create)
-
- req = fakes.HTTPRequest.blank('/fake/servers')
- req.method = 'POST'
- req.content_type = 'application/json'
- body = {'os:scheduler_hints': {'a': 'b'}}
-
- req.body = jsonutils.dumps(body)
- res = req.get_response(self.app)
- self.assertEqual(400, res.status_int)
diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py
index c37a64f8f..662a272f8 100644
--- a/nova/tests/api/openstack/compute/test_servers.py
+++ b/nova/tests/api/openstack/compute/test_servers.py
@@ -4750,3 +4750,30 @@ class ServerXMLSerializationTest(test.TestCase):
str(ip['version']))
self.assertEqual(str(ip_elem.get('addr')),
str(ip['addr']))
+
+
+class ServersAllExtensionsTestCase(test.TestCase):
+ """
+ Servers tests using default API router with all extensions enabled.
+ """
+
+ def setUp(self):
+ super(ServersAllExtensionsTestCase, self).setUp()
+ self.app = nova.api.openstack.compute.APIRouter()
+
+ def test_create_missing_server(self):
+ """Test create with malformed body"""
+
+ def fake_create(*args, **kwargs):
+ raise Exception("Request should not reach the compute API.")
+
+ self.stubs.Set(nova.compute.api.API, 'create', fake_create)
+
+ req = fakes.HTTPRequest.blank('/fake/servers')
+ req.method = 'POST'
+ 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)