From 58db021240cafb33f602aba50fdaa9060c15d84a Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Fri, 22 Nov 2013 15:22:07 +0800 Subject: [PATCH] Migrate volume should check para "host" in request The server doesn't check whether the parameter "host" is in request body. So the 500 error has been thrown. We should catch the KeyError and transfer the KeyError to 400 (HTTPBadRequest) instead of 500. Closes-Bug: #1253904 Change-Id: I3fb07113816a87f284b47e32bacd57f78a32676c --- cinder/api/contrib/admin_actions.py | 5 ++++- cinder/tests/api/contrib/test_admin_actions.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cinder/api/contrib/admin_actions.py b/cinder/api/contrib/admin_actions.py index 6ccae0a1e..57e06b7d3 100644 --- a/cinder/api/contrib/admin_actions.py +++ b/cinder/api/contrib/admin_actions.py @@ -148,7 +148,10 @@ class VolumeAdminController(AdminController): except exception.NotFound: raise exc.HTTPNotFound() params = body['os-migrate_volume'] - host = params['host'] + try: + host = params['host'] + except KeyError: + raise exc.HTTPBadRequest("Must specify 'host'") force_host_copy = params.get('force_host_copy', False) if isinstance(force_host_copy, basestring): try: diff --git a/cinder/tests/api/contrib/test_admin_actions.py b/cinder/tests/api/contrib/test_admin_actions.py index 8ee821477..d4dad0bfc 100644 --- a/cinder/tests/api/contrib/test_admin_actions.py +++ b/cinder/tests/api/contrib/test_admin_actions.py @@ -584,6 +584,23 @@ class AdminActionsTest(test.TestCase): volume = self._migrate_volume_prep() self._migrate_volume_exec(ctx, volume, host, expected_status) + def test_migrate_volume_without_host_parameter(self): + expected_status = 400 + host = 'test3' + ctx = context.RequestContext('admin', 'fake', True) + volume = self._migrate_volume_prep() + # build request to migrate without host + req = webob.Request.blank('/v2/fake/volumes/%s/action' % volume['id']) + req.method = 'POST' + req.headers['content-type'] = 'application/json' + body = {'os-migrate_volume': {'host': host, + 'force_host_copy': False}} + req.body = jsonutils.dumps(body) + req.environ['cinder.context'] = ctx + resp = req.get_response(app()) + # verify status + self.assertEqual(resp.status_int, expected_status) + def test_migrate_volume_host_no_exist(self): expected_status = 400 host = 'test3' -- 2.45.2