]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Delete snapshot metadata when snapshot is deleted
authorOllie Leahy <oliver.leahy@hp.com>
Wed, 17 Jul 2013 17:44:04 +0000 (17:44 +0000)
committerOllie Leahy <oliver.leahy@hp.com>
Wed, 17 Jul 2013 17:54:24 +0000 (17:54 +0000)
When a volume is deleted all rows in the volume_metadata table for that
volume are deleted as part of the database operation. This patch updates
the snapshot delete operation to delete corresponding rows in the
snapshot_metadata table.

Change-Id: I17543a82be370bed57d4165ad8756ccb390bc0d9

cinder/db/sqlalchemy/api.py
cinder/tests/test_volume.py

index 9544a235870f238d0c71015bd73835141957e870..a2e4bf814d9667dcc42acdfe5bfc431e7fb62842 100644 (file)
@@ -1300,6 +1300,11 @@ def snapshot_destroy(context, snapshot_id):
                     'deleted': True,
                     'deleted_at': timeutils.utcnow(),
                     'updated_at': literal_column('updated_at')})
+        session.query(models.SnapshotMetadata).\
+            filter_by(snapshot_id=snapshot_id).\
+            update({'deleted': True,
+                    'deleted_at': timeutils.utcnow(),
+                    'updated_at': literal_column('updated_at')})
 
 
 @require_context
index 32274011b2d500e9c2c163341833257feab37c21..a35cd16d72767d9c8a8f2c7382bdc6e3623cfef7 100644 (file)
@@ -460,7 +460,7 @@ class VolumeTestCase(test.TestCase):
         pass
 
     @staticmethod
-    def _create_snapshot(volume_id, size='0'):
+    def _create_snapshot(volume_id, size='0', metadata=None):
         """Create a snapshot object."""
         snap = {}
         snap['volume_size'] = size
@@ -468,6 +468,8 @@ class VolumeTestCase(test.TestCase):
         snap['project_id'] = 'fake'
         snap['volume_id'] = volume_id
         snap['status'] = "creating"
+        if metadata is not None:
+            snap['metadata'] = metadata
         return db.snapshot_create(context.get_admin_context(), snap)
 
     def test_create_delete_snapshot(self):
@@ -555,6 +557,25 @@ class VolumeTestCase(test.TestCase):
                           snapshot_id)
         self.volume.delete_volume(self.context, volume['id'])
 
+    def test_create_delete_snapshot_with_metadata(self):
+        """Test snapshot can be created with metadata and deleted."""
+        test_meta = {'fake_key': 'fake_value'}
+        volume = self._create_volume(0, None)
+        volume_id = volume['id']
+        self.volume.create_volume(self.context, volume_id)
+        snapshot = self._create_snapshot(volume['id'], metadata=test_meta)
+        snapshot_id = snapshot['id']
+        result_meta = {
+            snapshot.snapshot_metadata[0].key:
+            snapshot.snapshot_metadata[0].value}
+        self.assertEqual(result_meta, test_meta)
+
+        self.volume.delete_snapshot(self.context, snapshot_id)
+        self.assertRaises(exception.NotFound,
+                          db.snapshot_get,
+                          self.context,
+                          snapshot_id)
+
     def test_cant_delete_volume_in_use(self):
         """Test volume can't be deleted in invalid stats."""
         # create a volume and assign to host