From c9aa0f57b6200313ea1f6c3839d65828024e2d37 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Wed, 29 Feb 2012 23:38:56 +0000 Subject: Refactor spawn to use UndoManager. UndoManager provides a mechanism for automatically rolling back on exceptions. An additional benefit of this approach is that undo code is spatially close to the roll-forward code. This patch should be considered an intermediate step towards a more complete Command pattern based approach down the road. Change-Id: Ib429420d67324422a5d13cdea6872fd9c57c857e --- nova/tests/test_xenapi.py | 3 ++- nova/tests/xenapi/stubs.py | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index f8b0c1791..6336b3e7c 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -524,7 +524,7 @@ class XenAPIVMTestCase(test.TestCase): """ vdi_recs_start = self._list_vdis() - stubs.stubout_fetch_image_glance_disk(self.stubs) + stubs.stubout_fetch_image_glance_disk(self.stubs, raise_failure=True) self.assertRaises(xenapi_fake.Failure, self._test_spawn, 1, 2, 3) # No additional VDI should be found. @@ -590,6 +590,7 @@ class XenAPIVMTestCase(test.TestCase): self.check_vm_params_for_windows() def test_spawn_glance(self): + stubs.stubout_fetch_image_glance_disk(self.stubs) self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE, glance_stubs.FakeGlance.IMAGE_KERNEL, glance_stubs.FakeGlance.IMAGE_RAMDISK) diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py index 683bc91de..511bcdb05 100644 --- a/nova/tests/xenapi/stubs.py +++ b/nova/tests/xenapi/stubs.py @@ -124,14 +124,28 @@ def stubout_lookup_image(stubs): stubs.Set(vm_utils, 'lookup_image', f) -def stubout_fetch_image_glance_disk(stubs): +def stubout_fetch_image_glance_disk(stubs, raise_failure=False): """Simulates a failure in fetch image_glance_disk.""" @classmethod - def f(cls, *args): - raise fake.Failure("Test Exception raised by " + - "fake fetch_image_glance_disk") - stubs.Set(vm_utils.VMHelper, '_fetch_image_glance_disk', f) + def _fake_fetch_image_glance_disk(cls, context, session, instance, image, + image_type): + if raise_failure: + raise fake.Failure("Test Exception raised by " + "fake fetch_image_glance_disk") + elif image_type == vm_utils.ImageType.KERNEL: + filename = "kernel" + elif image_type == vm_utils.ImageType.RAMDISK: + filename = "ramdisk" + else: + filename = "unknown" + + return [dict(vdi_type=vm_utils.ImageType.to_string(image_type), + vdi_uuid=None, + file=filename)] + + stubs.Set(vm_utils.VMHelper, '_fetch_image_glance_disk', + _fake_fetch_image_glance_disk) def stubout_create_vm(stubs): -- cgit