]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Handle exceptions about snapshot in backup create
authorlisali <xiaoyan.li@intel.com>
Thu, 3 Mar 2016 04:00:17 +0000 (04:00 +0000)
committerLisaLi <xiaoyan.li@intel.com>
Mon, 7 Mar 2016 02:14:00 +0000 (10:14 +0800)
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
cinder/tests/unit/api/contrib/test_backups.py

index dcbf80778d6d5c647ebbe65749fe23f744995409..c5d2d8ed492b8c04afef44ed18404ee6757d3390 100644 (file)
@@ -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)
index 5f0e529a6612aa73635920fcd8f9296d500f7fda..031ffb4cdbcd65b71e4f98bc1806a042a7406c38 100644 (file)
@@ -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')