From 20898389cb152cec3a7bf169358e8d35cd57f205 Mon Sep 17 00:00:00 2001 From: Ken'ichi Ohmichi Date: Wed, 5 Oct 2016 14:14:30 -0700 Subject: [PATCH] Add error handling for delete-volume API Due to lack of an error handling, delete-volume API returns HTTP500 error when deleting an attached volume. This patch adds the error handling. The corresponding Tempest test is Idb6267be770bcf2541595babebf269cdc71c2b8d Closes-Bug: #1630783 Change-Id: Ia07556b2dc18678baa4c8fbd65820d8047362ef9 (cherry picked from commit 2afc4e466958b19a4cbb9147abb69f54a06bd507) --- nova/api/openstack/compute/volumes.py | 4 +++- nova/tests/unit/api/openstack/compute/test_volumes.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/compute/volumes.py b/nova/api/openstack/compute/volumes.py index 21d0d668ca58..1133b7771f43 100644 --- a/nova/api/openstack/compute/volumes.py +++ b/nova/api/openstack/compute/volumes.py @@ -118,7 +118,7 @@ class VolumeController(wsgi.Controller): @wsgi.Controller.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.response(202) - @extensions.expected_errors(404) + @extensions.expected_errors((400, 404)) def delete(self, req, id): """Delete a volume.""" context = req.environ['nova.context'] @@ -126,6 +126,8 @@ class VolumeController(wsgi.Controller): try: self.volume_api.delete(context, id) + except exception.InvalidInput as e: + raise exc.HTTPBadRequest(explanation=e.format_message()) except exception.VolumeNotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) diff --git a/nova/tests/unit/api/openstack/compute/test_volumes.py b/nova/tests/unit/api/openstack/compute/test_volumes.py index 6d1d49f4d9d4..625ee35c6e65 100644 --- a/nova/tests/unit/api/openstack/compute/test_volumes.py +++ b/nova/tests/unit/api/openstack/compute/test_volumes.py @@ -780,6 +780,14 @@ class BadRequestVolumeTestCaseV21(CommonBadRequestTestCase, controller_cls = volumes_v21.VolumeController bad_request = exception.ValidationError + @mock.patch.object(cinder.API, 'delete', + side_effect=exception.InvalidInput(reason='vol attach')) + def test_delete_invalid_status_volume(self, mock_delete): + req = fakes.HTTPRequest.blank('/v2.1/os-volumes') + req.method = 'DELETE' + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.delete, req, FAKE_UUID) + class BadRequestSnapshotTestCaseV21(CommonBadRequestTestCase, test.NoDBTestCase):