Fix VolumeAttachment OVO Volume lazy loading

Current code for VolumeAttachment OVO cannot lazy load the volume OVO
correctly and will always return a VolumeNotFound exception.

The reason for not being able to find the volume is that we are using
the VolumeAttachment id to search for the volume instead of the volume's
id.

This patch fixes this by using field "volume_id" instead of field "id"
when calling Volume's get_by_id method.

TrivialFix

Change-Id: Ibe02ca83a674c038ccec28cb104cc2af896426f9
This commit is contained in:
Gorka Eguileor 2017-11-07 11:29:39 +01:00
parent a867b12b76
commit 7993e49fda
2 changed files with 12 additions and 1 deletions

View File

@ -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])

View File

@ -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)