]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Check volume_id consistent when creating backup
authorlisali <xiaoyan.li@intel.com>
Wed, 16 Mar 2016 07:27:05 +0000 (07:27 +0000)
committerlisali <xiaoyan.li@intel.com>
Wed, 16 Mar 2016 07:27:05 +0000 (07:27 +0000)
When creating backup from snapshot, users specify both volume id
and snapshot id, this patch is to make sure volume id is consistent
with snapshot.volume_id.

Change-Id: I1bdaf00299ac6f9f45128b754b79be6700f44304
Closes-bug: #1557922

cinder/backup/api.py
cinder/tests/unit/api/contrib/test_backups.py

index c447859ef6515d07efe5ae4ea6815abb2aab8cc3..4397e40ae10518e2d3311bab4afb821f421c4fad 100644 (file)
@@ -243,6 +243,12 @@ class API(base.Base):
         if snapshot_id:
             snapshot = self.volume_api.get_snapshot(context, snapshot_id)
 
+            if volume_id != snapshot.volume_id:
+                msg = (_('Volume %(vol1)s does not match with '
+                         'snapshot.volume_id %(vol2)s.')
+                       % {'vol1': volume_id,
+                          'vol2': snapshot.volume_id})
+                raise exception.InvalidVolume(reason=msg)
         if volume['status'] not in ["available", "in-use"]:
             msg = (_('Volume to be backed up must be available '
                      'or in-use, but the current status is "%s".')
index 38b2ad4856c92e56cfb11954939f533def2c9209..cab8dbf9e1ca197e662c073d49f030d3a141fcb5 100644 (file)
@@ -699,6 +699,42 @@ class BackupsAPITestCase(test.TestCase):
 
         db.volume_destroy(context.get_admin_context(), volume_id)
 
+    def test_create_backup_snapshot_with_inconsistent_volume(self):
+        volume_id = utils.create_volume(self.context, size=5,
+                                        status='available')['id']
+        volume_id2 = utils.create_volume(self.context, size=5,
+                                         status='available')['id']
+        snapshot_id = utils.create_snapshot(self.context,
+                                            volume_id,
+                                            status='available')['id']
+
+        self.addCleanup(db.volume_destroy,
+                        self.context.elevated(),
+                        volume_id)
+        self.addCleanup(db.volume_destroy,
+                        self.context.elevated(),
+                        volume_id2)
+        self.addCleanup(db.snapshot_destroy,
+                        self.context.elevated(),
+                        snapshot_id)
+        body = {"backup": {"display_name": "nightly001",
+                           "display_description":
+                           "Nightly Backup 03-Sep-2012",
+                           "volume_id": volume_id2,
+                           "snapshot_id": snapshot_id,
+                           "container": "nightlybackups",
+                           }
+                }
+        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.assertIsNotNone(res_dict['badRequest']['message'])
+
     @mock.patch('cinder.db.service_get_all_by_topic')
     @mock.patch(
         'cinder.api.openstack.wsgi.Controller.validate_name_and_description')