From d8bcf8ba13a05f9dcd395ac2064c9099c759147a Mon Sep 17 00:00:00 2001 From: Abhishek Kekane Date: Fri, 24 Nov 2017 05:18:44 +0000 Subject: [PATCH] Fix 500 on ValueError during image-import ValueError is being raised during image-import, and it's not being caught, leading to a 500. So this change catches the ValueError and raises a HTTPBadRequest. Change-Id: Ide4ca9492b9296c74f8302d2d12e385bb5ad663b Closes-Bug: #1733810 --- glance/api/v2/images.py | 6 ++++++ glance/tests/unit/v2/test_images_resource.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index e8ddae362f..fe97fe5ecd 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -111,6 +111,12 @@ class ImagesController(object): raise webob.exc.HTTPConflict(explanation=e.msg) except exception.InvalidImageStatusTransition as e: raise webob.exc.HTTPConflict(explanation=e.msg) + except ValueError as e: + LOG.debug("Cannot import data for image %(id)s: %(e)s", + {'id': image_id, + 'e': encodeutils.exception_to_unicode(e)}) + raise webob.exc.HTTPBadRequest( + explanation=encodeutils.exception_to_unicode(e)) return image_id diff --git a/glance/tests/unit/v2/test_images_resource.py b/glance/tests/unit/v2/test_images_resource.py index 4add128e27..755750e225 100644 --- a/glance/tests/unit/v2/test_images_resource.py +++ b/glance/tests/unit/v2/test_images_resource.py @@ -625,6 +625,18 @@ class TestImagesController(base.IsolatedUnitTest): self.controller.import_image, request, UUID4, {}) + def test_image_import_raises_bad_request(self): + request = unit_test_utils.get_fake_request() + # NOTE(abhishekk): Due to + # https://bugs.launchpad.net/glance/+bug/1712463 taskflow is not + # executing. Once it is fixed instead of mocking spawn_n method + # we should mock execute method of _ImportToStore task. + with mock.patch.object(eventlet.GreenPool, 'spawn_n', + side_effect=ValueError): + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.import_image, request, UUID4, + {}) + def test_create(self): request = unit_test_utils.get_fake_request() image = {'name': 'image-1'}