]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Initialize and terminate connection raise 500 err
authorhuangtianhua <huangtianhua@huawei.com>
Fri, 22 Nov 2013 09:02:32 +0000 (17:02 +0800)
committerAvishay Traeger <avishay@il.ibm.com>
Sun, 24 Nov 2013 08:48:27 +0000 (10:48 +0200)
Initialize and terminate connection should check whether "connector" is
in request body. It throws 500 error if "connector" is not present. We
should catch the KeyError and transfer it to 400 (HTTPBadRequest)
instead of.

Closes-Bug: #1253944
Change-Id: If38419592701c8a14df52f94fd46ed0fc7a17e04

cinder/api/contrib/volume_actions.py
cinder/tests/api/contrib/test_volume_actions.py

index 9c2e6cc0aa8c3d0addf4db34c2f5f9e066816878..40179b9c70d046750aedb9c1e2351884adca93e3 100644 (file)
@@ -184,8 +184,10 @@ class VolumeActionsController(wsgi.Controller):
             volume = self.volume_api.get(context, id)
         except exception.VolumeNotFound as error:
             raise webob.exc.HTTPNotFound(explanation=error.msg)
-
-        connector = body['os-initialize_connection']['connector']
+        try:
+            connector = body['os-initialize_connection']['connector']
+        except KeyError:
+            raise webob.exc.HTTPBadRequest("Must specify 'connector'")
         info = self.volume_api.initialize_connection(context,
                                                      volume,
                                                      connector)
@@ -199,8 +201,10 @@ class VolumeActionsController(wsgi.Controller):
             volume = self.volume_api.get(context, id)
         except exception.VolumeNotFound as error:
             raise webob.exc.HTTPNotFound(explanation=error.msg)
-
-        connector = body['os-terminate_connection']['connector']
+        try:
+            connector = body['os-terminate_connection']['connector']
+        except KeyError:
+            raise webob.exc.HTTPBadRequest("Must specify 'connector'")
         self.volume_api.terminate_connection(context, volume, connector)
         return webob.Response(status_int=202)
 
index 3312879cf9febc09b90ee80002eb237c85b02380..45658ac04e958bc69b98b413f4f254cf335c5260 100644 (file)
@@ -78,6 +78,21 @@ class VolumeActionsTest(test.TestCase):
         res = req.get_response(fakes.wsgi_app())
         self.assertEqual(res.status_int, 200)
 
+    def test_initialize_connection_without_connector(self):
+        def fake_initialize_connection(*args, **kwargs):
+            return {}
+        self.stubs.Set(volume.API, 'initialize_connection',
+                       fake_initialize_connection)
+
+        body = {'os-initialize_connection': {}}
+        req = webob.Request.blank('/v2/fake/volumes/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 test_terminate_connection(self):
         def fake_terminate_connection(*args, **kwargs):
             return {}
@@ -93,6 +108,21 @@ class VolumeActionsTest(test.TestCase):
         res = req.get_response(fakes.wsgi_app())
         self.assertEqual(res.status_int, 202)
 
+    def test_terminate_connection_without_connector(self):
+        def fake_terminate_connection(*args, **kwargs):
+            return {}
+        self.stubs.Set(volume.API, 'terminate_connection',
+                       fake_terminate_connection)
+
+        body = {'os-terminate_connection': {}}
+        req = webob.Request.blank('/v2/fake/volumes/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 test_attach_to_instance(self):
         body = {'os-attach': {'instance_uuid': 'fake',
                               'mountpoint': '/dev/vdc',