From f5e4e39664a233ebe655914f280b699138c6d9b4 Mon Sep 17 00:00:00 2001 From: Michael Kerrin Date: Tue, 3 Sep 2013 12:53:34 +0000 Subject: [PATCH] Fix errors in volume usage audit script The snapshot_get_active_by_window DB query was returning snapshots where we couldn't access the related volume object. This was due to a) the volume not been loaded and the join failing due to detached session object. and b) the related volume was deleted so it didn't show return from the join Fixes bug: 1220140 Change-Id: I0de993ba0ca482fa9ab80d4d5cdb0ff5cc8e4d7a --- cinder/db/sqlalchemy/api.py | 1 + cinder/db/sqlalchemy/models.py | 4 +--- cinder/tests/test_volume.py | 3 +++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 55ff173e3..d8a40eb5d 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -1584,6 +1584,7 @@ def snapshot_get_active_by_window(context, begin, end=None, project_id=None): query = model_query(context, models.Snapshot, read_deleted="yes") query = query.filter(or_(models.Snapshot.deleted_at == None, models.Snapshot.deleted_at > begin)) + query = query.options(joinedload(models.Snapshot.volume)) if end: query = query.filter(models.Snapshot.created_at < end) if project_id: diff --git a/cinder/db/sqlalchemy/models.py b/cinder/db/sqlalchemy/models.py index 363c76505..66f44a40a 100644 --- a/cinder/db/sqlalchemy/models.py +++ b/cinder/db/sqlalchemy/models.py @@ -375,9 +375,7 @@ class Snapshot(BASE, CinderBase): volume = relationship(Volume, backref="snapshots", foreign_keys=volume_id, - primaryjoin='and_(' - 'Snapshot.volume_id == Volume.id,' - 'Snapshot.deleted == False)') + primaryjoin='Snapshot.volume_id == Volume.id') class SnapshotMetadata(BASE, CinderBase): diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index a6f9469e5..86d3f3827 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -1869,8 +1869,11 @@ class GetActiveByWindowTestCase(BaseVolumeTestCase): datetime.datetime(1, 4, 1, 1, 1, 1)) self.assertEqual(len(snapshots), 3) self.assertEqual(snapshots[0].id, u'2') + self.assertEqual(snapshots[0].volume.id, u'1') self.assertEqual(snapshots[1].id, u'3') + self.assertEqual(snapshots[1].volume.id, u'1') self.assertEqual(snapshots[2].id, u'4') + self.assertEqual(snapshots[2].volume.id, u'1') class DriverTestCase(test.TestCase): -- 2.45.2