xenapi: refactor get_all_vdis_in_sr

Making the code in vm_utils._get_all_vdis_in_sr more consistent with the
code in session.get_all_refs_and_recs.

Rather than catching all exceptions, the session.get_rec explicitly
catches the INVALID_HANDLE exception that xenapi might raise.

Change-Id: Ic69735e1a6caa893f63428141428ee0fd1a3ac65
This commit is contained in:
John Garbutt 2013-09-16 09:37:05 +01:00
parent d713c46f10
commit 18d35d0949
2 changed files with 22 additions and 4 deletions

View File

@ -1523,6 +1523,24 @@ class GetAllVdiForVMTestCase(VMUtilsTestBase):
self.assertEqual(expected, list(result))
class GetAllVdisTestCase(VMUtilsTestBase):
def test_get_all_vdis_in_sr(self):
def fake_get_rec(record_type, ref):
if ref == "2":
return "vdi_rec_2"
session = mock.Mock()
session.call_xenapi.return_value = ["1", "2"]
session.get_rec.side_effect = fake_get_rec
sr_ref = "sr_ref"
actual = list(vm_utils._get_all_vdis_in_sr(session, sr_ref))
self.assertEqual(actual, [('2', 'vdi_rec_2')])
session.call_xenapi.assert_called_once_with("SR.get_VDIs", sr_ref)
class SnapshotAttachedHereTestCase(VMUtilsTestBase):
@mock.patch.object(vm_utils, '_snapshot_attached_here_impl')
def test_snapshot_attached_here(self, mock_impl):

View File

@ -1820,11 +1820,11 @@ def _get_rrd(server, vm_uuid):
def _get_all_vdis_in_sr(session, sr_ref):
for vdi_ref in session.call_xenapi('SR.get_VDIs', sr_ref):
try:
vdi_rec = session.call_xenapi('VDI.get_record', vdi_ref)
vdi_rec = session.get_rec('VDI', vdi_ref)
# Check to make sure the record still exists. It may have
# been deleted between the get_all call and get_rec call
if vdi_rec:
yield vdi_ref, vdi_rec
except session.XenAPI.Failure:
continue
def get_instance_vdis_for_sr(session, vm_ref, sr_ref):