From 633bec8fd45897735e1fcb9844903fe597903b21 Mon Sep 17 00:00:00 2001 From: ankitagrawal Date: Fri, 10 Oct 2014 05:31:46 -0700 Subject: [PATCH] Improve error log for expired image location url If image is not present at the specified location while creating instance from image, then HTTPInternalServerError 500 response along with stack trace is logged on nova compute which does not help user to understand the exact cause of failure. Return HTTPNotFound error to the nova compute in case of image url got expired or image is not present at the given location to give clear indication of the cause of failure to user. Closes-Bug: #1198566 Change-Id: I9acd9112aeae8d3b3c0c3921f306e716e5808c2e --- glance/api/v2/image_data.py | 2 ++ .../tests/unit/v2/test_image_data_resource.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/glance/api/v2/image_data.py b/glance/api/v2/image_data.py index d99e62bc4e..045781eeea 100644 --- a/glance/api/v2/image_data.py +++ b/glance/api/v2/image_data.py @@ -212,6 +212,8 @@ class ResponseSerializer(wsgi.JSONResponseSerializer): # an iterator very strange response.app_iter = iter(image.get_data(offset=offset, chunk_size=chunk_size)) + except glance_store.NotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.msg) except exception.Forbidden as e: raise webob.exc.HTTPForbidden(explanation=e.msg) # NOTE(saschpe): "response.app_iter = ..." currently resets Content-MD5 diff --git a/glance/tests/unit/v2/test_image_data_resource.py b/glance/tests/unit/v2/test_image_data_resource.py index fb6ec08767..c865a099f3 100644 --- a/glance/tests/unit/v2/test_image_data_resource.py +++ b/glance/tests/unit/v2/test_image_data_resource.py @@ -469,6 +469,25 @@ class TestImageDataSerializer(test_utils.BaseTestCase): self.serializer.download, response, image) + def test_download_not_found(self): + """Test image download returns HTTPNotFound. + + Make sure that serializer returns 404 not found error in case of + image is not available at specified location. + """ + with mock.patch.object(glance.api.policy.ImageProxy, + 'get_data') as mock_get_data: + mock_get_data.side_effect = glance_store.NotFound() + + request = wsgi.Request.blank('/') + response = webob.Response() + response.request = request + image = FakeImage(size=3, data=iter('ZZZ')) + image.get_data = mock_get_data + self.assertRaises(webob.exc.HTTPNotFound, + self.serializer.download, + response, image) + def test_upload(self): request = webob.Request.blank('/') request.environ = {}