]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Ensure 'status' in update_snapshot_status
authorzhangyanzi <zhangyanzi@huawei.com>
Fri, 22 Nov 2013 08:41:39 +0000 (16:41 +0800)
committerAvishay Traeger <avishay@il.ibm.com>
Sun, 24 Nov 2013 09:55:43 +0000 (11:55 +0200)
The function os-update_snapshot_status doesn't check whether the param
"status" is in request body. It throws 500 error. We should catch the
KeyError and return 400 (HTTPBadRequest) instead.

Change-Id: If3775e3c0299cf0edbdda5081a6633b38462011b

cinder/api/contrib/snapshot_actions.py
cinder/tests/api/contrib/test_snapshot_actions.py

index 781f7a863c7859202729643cfc6b5fece4c9b956..527891b5eb220a86cecbe6d94509eb9d8d6ec955 100644 (file)
@@ -46,7 +46,11 @@ class SnapshotActionsController(wsgi.Controller):
         authorize(context, 'update_snapshot_status')
 
         LOG.debug("body: %s" % body)
-        status = body['os-update_snapshot_status']['status']
+        try:
+            status = body['os-update_snapshot_status']['status']
+        except KeyError:
+            msg = _("'status' must be specified.")
+            raise webob.exc.HTTPBadRequest(explanation=msg)
 
         # Allowed state transitions
         status_map = {'creating': ['creating', 'available', 'error'],
index 5ec2fed1df2c54d86049cf9850d859bb16f135f3..f8be77f8b7901ddcb74a2d002b4a04135459a6f2 100644 (file)
@@ -56,6 +56,17 @@ class SnapshotActionsTest(test.TestCase):
         res = req.get_response(fakes.wsgi_app())
         self.assertEqual(res.status_int, 400)
 
+    def test_update_snapshot_status_without_status(self):
+        self.stubs.Set(db, 'snapshot_get', stub_snapshot_get)
+        body = {'os-update_snapshot_status': {}}
+        req = webob.Request.blank('/v2/fake/snapshots/1/action')
+        req.method = "POST"
+        req.body = jsonutils.dumps(body)
+        req.headers["content-type"] = "application/json"
+
+        res = req.get_response(fakes.wsgi_app())
+        self.assertEqual(res.status_int, 400)
+
 
 def stub_snapshot_get(context, snapshot_id):
     snapshot = stubs.stub_snapshot(snapshot_id)