From 3a21e5d31e94b2cbb9d1f581347bacf41afdef7e Mon Sep 17 00:00:00 2001 From: Zhang Jinnan Date: Sat, 26 Sep 2015 00:51:00 +0800 Subject: [PATCH] Volume extend error does not catch exception Volume extension API does not throw HTTPBadRequest response, when input to the API is invalid. This patch handles invalid input to the volume extension API and return HTTPBadRequest. APIImpact Change-Id: I93e81514c9c1bea9fa80cfb14c5f1d4b00dd0c28 --- cinder/api/contrib/volume_actions.py | 6 +++++- .../unit/api/contrib/test_volume_actions.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cinder/api/contrib/volume_actions.py b/cinder/api/contrib/volume_actions.py index 71123bff4..c6e0c7195 100644 --- a/cinder/api/contrib/volume_actions.py +++ b/cinder/api/contrib/volume_actions.py @@ -315,7 +315,11 @@ class VolumeActionsController(wsgi.Controller): raise webob.exc.HTTPBadRequest(explanation=msg) size = int(body['os-extend']['new_size']) - self.volume_api.extend(context, volume, size) + try: + self.volume_api.extend(context, volume, size) + except exception.InvalidVolume as error: + raise webob.exc.HTTPBadRequest(explanation=error.msg) + return webob.Response(status_int=202) @wsgi.action('os-update_readonly_flag') diff --git a/cinder/tests/unit/api/contrib/test_volume_actions.py b/cinder/tests/unit/api/contrib/test_volume_actions.py index 2aa931e22..14930d095 100644 --- a/cinder/tests/unit/api/contrib/test_volume_actions.py +++ b/cinder/tests/unit/api/contrib/test_volume_actions.py @@ -342,6 +342,22 @@ class VolumeActionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(202, res.status_int) + def test_extend_volume_invalid_status(self): + def fake_extend_volume(*args, **kwargs): + msg = "Volume status must be available" + raise exception.InvalidVolume(reason=msg) + self.stubs.Set(volume.API, 'extend', + fake_extend_volume) + + body = {'os-extend': {'new_size': 5}} + 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(400, res.status_int) + def test_update_readonly_flag(self): def fake_update_readonly_flag(*args, **kwargs): return {} -- 2.45.2