From d8c193012adb5a4f08982324324260656752480b Mon Sep 17 00:00:00 2001 From: Ollie Leahy Date: Wed, 17 Jul 2013 17:44:04 +0000 Subject: [PATCH] Delete snapshot metadata when snapshot is deleted 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 | 5 +++++ cinder/tests/test_volume.py | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 9544a2358..a2e4bf814 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -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 diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 32274011b..a35cd16d7 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -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 -- 2.45.2