diff --git a/cinder/objects/volume_attachment.py b/cinder/objects/volume_attachment.py index c2bd484c4ba..28938b13d96 100644 --- a/cinder/objects/volume_attachment.py +++ b/cinder/objects/volume_attachment.py @@ -110,7 +110,7 @@ class VolumeAttachment(base.CinderPersistentObject, base.CinderObject, objtype=self.obj_name()) if attrname == 'volume': - volume = objects.Volume.get_by_id(self._context, self.id) + volume = objects.Volume.get_by_id(self._context, self.volume_id) self.volume = volume self.obj_reset_changes(fields=[attrname]) diff --git a/cinder/tests/unit/objects/test_volume_attachment.py b/cinder/tests/unit/objects/test_volume_attachment.py index fe2d0411f21..4407b99454e 100644 --- a/cinder/tests/unit/objects/test_volume_attachment.py +++ b/cinder/tests/unit/objects/test_volume_attachment.py @@ -35,6 +35,17 @@ class TestVolumeAttachment(test_objects.BaseObjectsTestCase): fake.ATTACHMENT_ID) self._compare(self, attachment_obj, attachment) + @mock.patch.object(objects.Volume, 'get_by_id') + def test_lazy_load_volume(self, volume_get_mock): + volume = objects.Volume(self.context, id=fake.VOLUME_ID) + volume_get_mock.return_value = volume + attach = objects.VolumeAttachment(self.context, id=fake.ATTACHMENT_ID, + volume_id=volume.id) + + r = attach.volume + self.assertEqual(volume, r) + volume_get_mock.assert_called_once_with(self.context, volume.id) + @mock.patch('cinder.db.volume_attachment_update') def test_save(self, volume_attachment_update): attachment = fake_volume.fake_volume_attachment_obj(self.context)