Catch exception.OverQuota when create image for volume backed instance

When create image for a volume backed instance, nova will
create snapshots for all volumes attached to the instance
in Cinder, and if quota exceed in Cinder, HTTP 500 will
raise, we should capture this error and raise 403.

Conflicts:
      nova/tests/unit/api/openstack/compute/test_server_actions.py

NOTE(mriedem): The conflict is due to the tests being refactored
to not use mox in Ocata: 612134ca7b

Change-Id: Ic62478e22a7477cfaefac3e63c383082d66bd635
Closes-Bug: #1689284
(cherry picked from commit 29c8ae3cd9)
(cherry picked from commit 1b59d60f95)
This commit is contained in:
Kevin_Zheng 2017-05-15 15:02:00 +08:00 committed by Zhenyu Zheng
parent 5d3c071f43
commit 5448a44c70
2 changed files with 19 additions and 4 deletions

View File

@ -1084,6 +1084,8 @@ class ServersController(wsgi.Controller):
'createImage', id)
except exception.Invalid as err:
raise exc.HTTPBadRequest(explanation=err.format_message())
except exception.OverQuota as e:
raise exc.HTTPForbidden(explanation=e.format_message())
# build location of newly-created image entity
image_id = str(image['id'])

View File

@ -927,7 +927,8 @@ class ServerActionsControllerTestV21(test.TestCase):
self.controller._action_create_image, self.req,
FAKE_UUID, body=body)
def _do_test_create_volume_backed_image(self, extra_properties):
def _do_test_create_volume_backed_image(
self, extra_properties, mock_vol_create_side_effect=None):
def _fake_id(x):
return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)
@ -986,8 +987,13 @@ class ServerActionsControllerTestV21(test.TestCase):
self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
volume_api = self.controller.compute_api.volume_api
volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)
if mock_vol_create_side_effect:
volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
mock_vol_create_side_effect)
else:
volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)
self.mox.ReplayAll()
@ -996,7 +1002,7 @@ class ServerActionsControllerTestV21(test.TestCase):
location = response.headers['Location']
image_id = location.replace(self.image_url or
glance.generate_image_url(''), '')
glance.generate_image_url(''), '')
image = image_service.show(None, image_id)
self.assertEqual(image['name'], 'snapshot_of_volume_backed')
@ -1024,6 +1030,13 @@ class ServerActionsControllerTestV21(test.TestCase):
self._do_test_create_volume_backed_image(dict(ImageType='Gold',
ImageVersion='2.0'))
def test_create_volume_backed_image_cinder_over_quota(self):
self.assertRaises(
webob.exc.HTTPForbidden,
self._do_test_create_volume_backed_image, {},
mock_vol_create_side_effect=exception.OverQuota(
overs='snapshot'))
def _test_create_volume_backed_image_with_metadata_from_volume(
self, extra_metadata=None):