Skip to content

Commit

Permalink
Initialize and terminate connection raise 500 err
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huangtianhua authored and avishay-traeger committed Nov 24, 2013
1 parent b4a9c25 commit 60f5b77
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cinder/api/contrib/volume_actions.py
Expand Up @@ -184,8 +184,10 @@ def _initialize_connection(self, req, id, body):
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)
Expand All @@ -199,8 +201,10 @@ def _terminate_connection(self, req, id, body):
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)

Expand Down
30 changes: 30 additions & 0 deletions cinder/tests/api/contrib/test_volume_actions.py
Expand Up @@ -78,6 +78,21 @@ def fake_initialize_connection(*args, **kwargs):
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 {}
Expand All @@ -93,6 +108,21 @@ def fake_terminate_connection(*args, **kwargs):
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',
Expand Down

0 comments on commit 60f5b77

Please sign in to comment.