From 579a0f95b8492572ae01a8450a159f9082eb4a16 Mon Sep 17 00:00:00 2001 From: Abhishek Kekane Date: Mon, 27 Nov 2017 04:37:06 +0000 Subject: [PATCH] Fix 500 from image-import on 'active' image If you run image-import api on any image which is in active state will return 500 error as it raises InvalidImageStatusTransition because Image status transition from active to importing is not allowed. Caught InvalidImageStatusTransition exception in controller and raised HTTConflict exception to return HTTP 409 error to the user. Change-Id: I3ed1e40122063fe563ba27b7b7db07f62bef2382 Closes-Bug: #1733803 --- glance/api/v2/images.py | 2 ++ glance/tests/unit/v2/test_images_resource.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index fa462d038f..e8ddae362f 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -109,6 +109,8 @@ class ImagesController(object): raise webob.exc.HTTPForbidden(explanation=e.msg) except exception.Conflict as e: raise webob.exc.HTTPConflict(explanation=e.msg) + except exception.InvalidImageStatusTransition as e: + raise webob.exc.HTTPConflict(explanation=e.msg) return image_id diff --git a/glance/tests/unit/v2/test_images_resource.py b/glance/tests/unit/v2/test_images_resource.py index bb53bc8ab9..4add128e27 100644 --- a/glance/tests/unit/v2/test_images_resource.py +++ b/glance/tests/unit/v2/test_images_resource.py @@ -612,6 +612,19 @@ class TestImagesController(base.IsolatedUnitTest): self.controller.import_image, request, UUID4, {}) + def test_image_import_raises_conflict_for_invalid_status_change(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=exception.InvalidImageStatusTransition): + self.assertRaises(webob.exc.HTTPConflict, + self.controller.import_image, request, UUID4, + {}) + def test_create(self): request = unit_test_utils.get_fake_request() image = {'name': 'image-1'}