From 62a682d20b6c7dc0556b1890ee29394edd00fea2 Mon Sep 17 00:00:00 2001 From: Xing Yang Date: Mon, 10 Aug 2015 17:15:50 -0400 Subject: [PATCH] Allow CG without snapshot to be deleted If a CG is created from CG Snapshot, it cannot be deleted until the CG Snapshot is deleted. This is wrong. This is because the query to find CG who are parents of CG Snapshot didn't filter out the CG uuid and as a result, it is not possible to delete any CG if there are any existing CG Snanpshot. This patch fixes the problem by fixing the function in db API that finds all CGs that have CG Snapshot depending on them. * CGs with any CG Snapshot depending on them still cannot be deleted until the CG Snapshot is deleted first. * CGs without any CG Snapshot depending on them should be allowed to be deleted. Change-Id: I1380b26c4a957946c7dc27285557966f558cf03d Closes-Bug: #1485783 --- cinder/db/sqlalchemy/api.py | 4 ++-- cinder/tests/unit/test_db_api.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 5906fbd4263..72a26a7122e 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -3765,10 +3765,10 @@ def _cgsnapshot_get_all(context, project_id=None, group_id=None, filters=None): query = query.filter_by(**filters) if project_id: - query.filter_by(project_id=project_id) + query = query.filter_by(project_id=project_id) if group_id: - query.filter_by(consistencygroup_id=group_id) + query = query.filter_by(consistencygroup_id=group_id) return query.all() diff --git a/cinder/tests/unit/test_db_api.py b/cinder/tests/unit/test_db_api.py index 06d68d5eda5..9cd95385a32 100644 --- a/cinder/tests/unit/test_db_api.py +++ b/cinder/tests/unit/test_db_api.py @@ -1215,6 +1215,32 @@ class DBAPICgsnapshotTestCase(BaseTest): self.ctxt, filters)) + def test_cgsnapshot_get_all_by_group(self): + cgsnapshot1 = db.cgsnapshot_create(self.ctxt, {'id': 1, + 'consistencygroup_id': 'g1'}) + cgsnapshot2 = db.cgsnapshot_create(self.ctxt, {'id': 2, + 'consistencygroup_id': 'g1'}) + db.cgsnapshot_create(self.ctxt, {'id': 3, + 'consistencygroup_id': 'g2'}) + tests = [ + ({'consistencygroup_id': 'g1'}, [cgsnapshot1, cgsnapshot2]), + ({'id': 3}, []), + ({'fake_key': 'fake'}, []), + ({'consistencygroup_id': 'g2'}, []), + (None, [cgsnapshot1, cgsnapshot2]), + ] + + for filters, expected in tests: + self._assertEqualListsOfObjects(expected, + db.cgsnapshot_get_all_by_group( + self.ctxt, + 'g1', + filters)) + + db.cgsnapshot_destroy(self.ctxt, '1') + db.cgsnapshot_destroy(self.ctxt, '2') + db.cgsnapshot_destroy(self.ctxt, '3') + def test_cgsnapshot_get_all_by_project(self): cgsnapshot1 = db.cgsnapshot_create(self.ctxt, {'id': 1,