From 31a2ef6e4c87cb580f261a6fc885d215cb8d19ed Mon Sep 17 00:00:00 2001 From: lisali Date: Thu, 3 Mar 2016 04:00:17 +0000 Subject: [PATCH] Handle exceptions about snapshot in backup create With snapshot backup, exceptions about Snapshot will be raised. This patch is to add handler about SnapshotNotFound and InvalidSnapshot. APIImpact This bug is changes return code of creating backup request when snapshot doesn't exist or is invalid. Change-Id: Ib6a80df5496f93fb8967ed01e40d214c0c38aabc Closes-Bug: #1549658 --- cinder/api/contrib/backups.py | 6 ++- cinder/tests/unit/api/contrib/test_backups.py | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cinder/api/contrib/backups.py b/cinder/api/contrib/backups.py index dcbf80778..c5d2d8ed4 100644 --- a/cinder/api/contrib/backups.py +++ b/cinder/api/contrib/backups.py @@ -274,9 +274,11 @@ class BackupsController(wsgi.Controller): volume_id, container, incremental, None, force, snapshot_id) - except exception.InvalidVolume as error: + except (exception.InvalidVolume, + exception.InvalidSnapshot) as error: raise exc.HTTPBadRequest(explanation=error.msg) - except exception.VolumeNotFound as error: + except (exception.VolumeNotFound, + exception.SnapshotNotFound) as error: raise exc.HTTPNotFound(explanation=error.msg) except exception.ServiceNotFound as error: raise exc.HTTPInternalServerError(explanation=error.msg) diff --git a/cinder/tests/unit/api/contrib/test_backups.py b/cinder/tests/unit/api/contrib/test_backups.py index 5f0e529a6..031ffb4cd 100644 --- a/cinder/tests/unit/api/contrib/test_backups.py +++ b/cinder/tests/unit/api/contrib/test_backups.py @@ -36,6 +36,7 @@ from cinder import objects from cinder.objects import fields from cinder import test from cinder.tests.unit.api import fakes +from cinder.tests.unit import fake_constants from cinder.tests.unit import utils # needed for stubs to work import cinder.volume @@ -715,6 +716,59 @@ class BackupsAPITestCase(test.TestCase): db.volume_destroy(context.get_admin_context(), volume_id) + def test_create_backup_with_invalid_snapshot(self): + volume_id = utils.create_volume(self.context, size=5, + status='available')['id'] + snapshot_id = utils.create_snapshot(self.context, volume_id, + status='error')['id'] + body = {"backup": {"display_name": "nightly001", + "display_description": + "Nightly Backup 03-Sep-2012", + "snapshot_id": snapshot_id, + "volume_id": volume_id, + } + } + self.addCleanup(db.volume_destroy, + self.context.elevated(), + volume_id) + self.addCleanup(db.snapshot_destroy, + self.context.elevated(), + snapshot_id) + req = webob.Request.blank('/v2/fake/backups') + req.method = 'POST' + req.headers['Content-Type'] = 'application/json' + req.body = jsonutils.dump_as_bytes(body) + res = req.get_response(fakes.wsgi_app()) + + res_dict = jsonutils.loads(res.body) + self.assertEqual(400, res.status_int) + self.assertEqual(400, res_dict['badRequest']['code']) + self.assertIsNotNone(res_dict['badRequest']['message']) + + def test_create_backup_with_non_existent_snapshot(self): + volume_id = utils.create_volume(self.context, size=5, + status='restoring')['id'] + body = {"backup": {"display_name": "nightly001", + "display_description": + "Nightly Backup 03-Sep-2012", + "snapshot_id": fake_constants.snapshot_id, + "volume_id": volume_id, + } + } + self.addCleanup(db.volume_destroy, + self.context.elevated(), + volume_id) + req = webob.Request.blank('/v2/fake/backups') + req.method = 'POST' + req.headers['Content-Type'] = 'application/json' + req.body = jsonutils.dump_as_bytes(body) + res = req.get_response(fakes.wsgi_app()) + + res_dict = jsonutils.loads(res.body) + self.assertEqual(404, res.status_int) + self.assertEqual(404, res_dict['itemNotFound']['code']) + self.assertIsNotNone(res_dict['itemNotFound']['message']) + @mock.patch('cinder.db.service_get_all_by_topic') @mock.patch( 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') -- 2.45.2