From 5867835a842b4a6553c79f4eea9691cf7bc80e8c Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Fri, 30 Nov 2012 13:41:26 -0800 Subject: [PATCH] Patch fake_libvirt_utils with fixtures.MonkeyPatch If tests are not run in the default order nosetests runs them in it is possible that the monkey patching of fake_libvirt utils is not properly cleaned up. Use fixtures.MonkeyPatch to monkey patch fake_libvirt_utils in test setUp ensuring the monkey patching is reverted properly after each test. Part of blueprint grizzly-testtools Change-Id: Ie691cbc777c01fa8a5affa3271f36776ea9648b9 --- nova/tests/test_imagebackend.py | 5 ++++- nova/tests/test_libvirt.py | 18 ++++++++++-------- nova/tests/test_virt_drivers.py | 26 ++++++++++++++++---------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/nova/tests/test_imagebackend.py b/nova/tests/test_imagebackend.py index 59eccdc4..eca2267a 100644 --- a/nova/tests/test_imagebackend.py +++ b/nova/tests/test_imagebackend.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import fixtures import os from nova.openstack.common import cfg @@ -48,7 +49,9 @@ class _ImageTestCase(test.TestCase): '_base') self.TEMPLATE_PATH = os.path.join(self.TEMPLATE_DIR, 'template') - imagebackend.libvirt_utils = fake_libvirt_utils + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.imagebackend.libvirt_utils', + fake_libvirt_utils)) def test_cache(self): self.mox.StubOutWithMock(os.path, 'exists') diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index c19f72da..5c90ebcd 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -18,6 +18,7 @@ import copy import errno import eventlet +import fixtures import json import mox import os @@ -474,11 +475,11 @@ class CacheConcurrencyTestCase(test.TestCase): self.stubs.Set(os.path, 'exists', fake_exists) self.stubs.Set(utils, 'execute', fake_execute) self.stubs.Set(imagebackend.disk, 'extend', fake_extend) - imagebackend.libvirt_utils = fake_libvirt_utils + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.imagebackend.libvirt_utils', + fake_libvirt_utils)) def tearDown(self): - imagebackend.libvirt_utils = libvirt_utils - # Make sure the lock_path for this test is cleaned up if os.path.exists(self.lock_path): shutil.rmtree(self.lock_path) @@ -559,8 +560,12 @@ class LibvirtConnTestCase(test.TestCase): self.flags(instances_path='') self.flags(libvirt_snapshots_directory='') self.call_libvirt_dependant_setup = False - libvirt_driver.libvirt_utils = fake_libvirt_utils - snapshots.libvirt_utils = fake_libvirt_utils + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.libvirt_utils', + fake_libvirt_utils)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.snapshots.libvirt_utils', + fake_libvirt_utils)) def fake_extend(image, size): pass @@ -581,7 +586,6 @@ class LibvirtConnTestCase(test.TestCase): 'instance_type_id': '5'} # m1.small def tearDown(self): - libvirt_driver.libvirt_utils = libvirt_utils nova.tests.image.fake.FakeImageService_reset() super(LibvirtConnTestCase, self).tearDown() @@ -2335,7 +2339,6 @@ class LibvirtConnTestCase(test.TestCase): self.create_fake_libvirt_mock() libvirt_driver.LibvirtDriver._conn.lookupByName = fake_lookup - libvirt_driver.libvirt_utils = fake_libvirt_utils conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) @@ -2387,7 +2390,6 @@ class LibvirtConnTestCase(test.TestCase): libvirt_driver.LibvirtDriver._conn.lookupByName = fake_lookup libvirt_driver.LibvirtDriver._flush_libvirt_console = _fake_flush libvirt_driver.LibvirtDriver._append_to_file = _fake_append_to_file - libvirt_driver.libvirt_utils = fake_libvirt_utils conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) diff --git a/nova/tests/test_virt_drivers.py b/nova/tests/test_virt_drivers.py index c298490f..83476354 100644 --- a/nova/tests/test_virt_drivers.py +++ b/nova/tests/test_virt_drivers.py @@ -16,6 +16,7 @@ import __builtin__ import base64 +import fixtures import mox import netaddr import StringIO @@ -77,12 +78,21 @@ class _FakeDriverBackendTestCase(test.TestCase): import nova.virt.libvirt.driver import nova.virt.libvirt.firewall - self.saved_libvirt_imagebackend = nova.virt.libvirt.driver.imagebackend - nova.virt.libvirt.driver.imagebackend = fake_imagebackend - nova.virt.libvirt.driver.libvirt = fakelibvirt - nova.virt.libvirt.driver.libvirt_utils = fake_libvirt_utils - nova.virt.libvirt.snapshots.libvirt_utils = fake_libvirt_utils - nova.virt.libvirt.firewall.libvirt = fakelibvirt + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.imagebackend', + fake_imagebackend)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.libvirt', + fakelibvirt)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.driver.libvirt_utils', + fake_libvirt_utils)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.snapshots.libvirt_utils', + fake_libvirt_utils)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.virt.libvirt.firewall.libvirt', + fakelibvirt)) self.flags(rescue_image_id="2", rescue_kernel_id="3", @@ -115,12 +125,8 @@ class _FakeDriverBackendTestCase(test.TestCase): # Restore libvirt import nova.virt.libvirt.driver import nova.virt.libvirt.firewall - nova.virt.libvirt.driver.imagebackend = self.saved_libvirt_imagebackend if self.saved_libvirt: sys.modules['libvirt'] = self.saved_libvirt - nova.virt.libvirt.driver.libvirt = self.saved_libvirt - nova.virt.libvirt.driver.libvirt_utils = self.saved_libvirt - nova.virt.libvirt.firewall.libvirt = self.saved_libvirt def setUp(self): super(_FakeDriverBackendTestCase, self).setUp()