From 5ab43c81e9e4cfdee5c762b469c0626c5f556f80 Mon Sep 17 00:00:00 2001 From: Cao ShuFeng Date: Mon, 4 Apr 2016 17:33:05 +0800 Subject: [PATCH] Fix invalid error message of volume create When create a volume with image name, the error message return to user is invalid if there are several image share the same name. This change provides a more user-friendly message. Change-Id: Idac6149ce6944986af9716e60738b3cd62434a5a Closes-bug: 1565653 --- cinder/api/v2/volumes.py | 7 +++++-- cinder/tests/unit/api/v2/test_volumes.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index 4a4d16d23c0..ad4a9158e27 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -277,11 +277,14 @@ class VolumeController(wsgi.Controller): if len(images) > 1: msg = _("Multiple matches found for '%s', use an ID to be more" " specific.") % image_ref - raise exc.HTTPConflict(msg) + raise exc.HTTPConflict(explanation=msg) for img in images: return img['id'] + except exc.HTTPConflict: + raise except Exception: - # Pass and let default not found error handling take care of it + # Pass the other exception and let default not found error + # handling take care of it pass msg = _("Invalid image identifier or unable to " diff --git a/cinder/tests/unit/api/v2/test_volumes.py b/cinder/tests/unit/api/v2/test_volumes.py index 062a8245c66..1234d4922b1 100644 --- a/cinder/tests/unit/api/v2/test_volumes.py +++ b/cinder/tests/unit/api/v2/test_volumes.py @@ -423,6 +423,9 @@ class VolumeApiTest(test.TestCase): def test_volume_create_with_image_ref_not_uuid_format(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.stubs.Set(fake_image._FakeImageService, + "detail", + stubs.stub_image_service_detail) self.ext_mgr.extensions = {'os-image-create': 'fake'} vol = self._vol_in_request_body(availability_zone="cinder", image_ref="12345") @@ -435,6 +438,9 @@ class VolumeApiTest(test.TestCase): def test_volume_create_with_image_ref_with_empty_string(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.stubs.Set(fake_image._FakeImageService, + "detail", + stubs.stub_image_service_detail) self.ext_mgr.extensions = {'os-image-create': 'fake'} vol = self._vol_in_request_body(availability_zone="cinder", image_ref="") @@ -477,6 +483,9 @@ class VolumeApiTest(test.TestCase): def test_volume_create_with_image_id_not_uuid_format(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.stubs.Set(fake_image._FakeImageService, + "detail", + stubs.stub_image_service_detail) self.ext_mgr.extensions = {'os-image-create': 'fake'} vol = self._vol_in_request_body(availability_zone="cinder", image_id="12345") @@ -489,6 +498,9 @@ class VolumeApiTest(test.TestCase): def test_volume_create_with_image_id_with_empty_string(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.stubs.Set(fake_image._FakeImageService, + "detail", + stubs.stub_image_service_detail) self.ext_mgr.extensions = {'os-image-create': 'fake'} vol = self._vol_in_request_body(availability_zone="cinder", image_id="") @@ -533,7 +545,7 @@ class VolumeApiTest(test.TestCase): image_ref=test_id) body = {"volume": vol} req = fakes.HTTPRequest.blank('/v2/volumes') - self.assertRaises(webob.exc.HTTPBadRequest, + self.assertRaises(webob.exc.HTTPConflict, self.controller.create, req, body)