From 18d35d0949868840aba7b6720a76c3d6c5ec9608 Mon Sep 17 00:00:00 2001 From: John Garbutt Date: Mon, 16 Sep 2013 09:37:05 +0100 Subject: [PATCH] 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 --- nova/tests/virt/xenapi/test_vm_utils.py | 18 ++++++++++++++++++ nova/virt/xenapi/vm_utils.py | 8 ++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/nova/tests/virt/xenapi/test_vm_utils.py b/nova/tests/virt/xenapi/test_vm_utils.py index 10d24e82617e..c3d909534ab4 100644 --- a/nova/tests/virt/xenapi/test_vm_utils.py +++ b/nova/tests/virt/xenapi/test_vm_utils.py @@ -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): diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index f902243199f3..1ac055b101ac 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -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):