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
This commit is contained in:
Abhishek Kekane 2017-11-27 04:37:06 +00:00
parent ee874a4dea
commit 579a0f95b8
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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'}