From f0d99f0ca2e7aed9b6d70132d029a48da6e90e31 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 27 Nov 2014 07:14:25 -0500 Subject: [PATCH] remove test.ReplaceModule from test.py The ReplaceModule fixture is buggy and very sensitive to the sequence things are imported in. It was only used in 3 places until the functional test split landed (and had to be removed from there). This removes the use of ReplaceModule for fake_ldap. Unwinding the xenapi test code is a bit more challenging especially given how spidery all that code is. So instead of fixing that move the ReplaceModule into the xenapi test code where it's used and leave it as an exercise for the xenapi folks to move to more real mocks or monkey patching. This gets it out of test.py so that no one else will accidentally use it. Change-Id: I2398a83039ef50a926c1adecbb680c935bae7ed8 --- nova/test.py | 18 ------------------ nova/tests/unit/network/test_manager.py | 4 +++- nova/tests/unit/virt/xenapi/stubs.py | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/nova/test.py b/nova/test.py index 45e7015715d1..904abd72fe76 100644 --- a/nova/test.py +++ b/nova/test.py @@ -30,7 +30,6 @@ import inspect import logging import os import shutil -import sys import uuid import fixtures @@ -150,23 +149,6 @@ class SampleNetworks(fixtures.Fixture): network.set_network_host(ctxt, net) -class ReplaceModule(fixtures.Fixture): - """Replace a module with a fake module.""" - - def __init__(self, name, new_value): - self.name = name - self.new_value = new_value - - def _restore(self, old_value): - sys.modules[self.name] = old_value - - def setUp(self): - super(ReplaceModule, self).setUp() - old_value = sys.modules.get(self.name) - sys.modules[self.name] = self.new_value - self.addCleanup(self._restore, old_value) - - class ServiceFixture(fixtures.Fixture): """Run a service as a test fixture.""" diff --git a/nova/tests/unit/network/test_manager.py b/nova/tests/unit/network/test_manager.py index 776160cd0c3e..eca7a663734e 100644 --- a/nova/tests/unit/network/test_manager.py +++ b/nova/tests/unit/network/test_manager.py @@ -3286,7 +3286,9 @@ class LdapDNSTestCase(test.TestCase): def setUp(self): super(LdapDNSTestCase, self).setUp() - self.useFixture(test.ReplaceModule('ldap', fake_ldap)) + self.useFixture(fixtures.MonkeyPatch( + 'nova.network.ldapdns.ldap', + fake_ldap)) dns_class = 'nova.network.ldapdns.LdapDNS' self.driver = importutils.import_object(dns_class) diff --git a/nova/tests/unit/virt/xenapi/stubs.py b/nova/tests/unit/virt/xenapi/stubs.py index ad13ca41df85..a6395b6004be 100644 --- a/nova/tests/unit/virt/xenapi/stubs.py +++ b/nova/tests/unit/virt/xenapi/stubs.py @@ -15,7 +15,9 @@ import pickle import random +import sys +import fixtures from oslo.serialization import jsonutils from nova import test @@ -263,6 +265,23 @@ def stub_out_vm_methods(stubs): stubs.Set(vm_utils, '_wait_for_device', fake_wait_for_device) +class ReplaceModule(fixtures.Fixture): + """Replace a module with a fake module.""" + + def __init__(self, name, new_value): + self.name = name + self.new_value = new_value + + def _restore(self, old_value): + sys.modules[self.name] = old_value + + def setUp(self): + super(ReplaceModule, self).setUp() + old_value = sys.modules.get(self.name) + sys.modules[self.name] = self.new_value + self.addCleanup(self._restore, old_value) + + class FakeSessionForVolumeTests(fake.SessionBase): """Stubs out a XenAPISession for Volume tests.""" def VDI_introduce(self, _1, uuid, _2, _3, _4, _5, @@ -354,12 +373,12 @@ class FakeSessionForFailedMigrateTests(FakeSessionForVMTests): class XenAPITestBase(test.TestCase): def setUp(self): super(XenAPITestBase, self).setUp() - self.useFixture(test.ReplaceModule('XenAPI', fake)) + self.useFixture(ReplaceModule('XenAPI', fake)) fake.reset() class XenAPITestBaseNoDB(test.NoDBTestCase): def setUp(self): super(XenAPITestBaseNoDB, self).setUp() - self.useFixture(test.ReplaceModule('XenAPI', fake)) + self.useFixture(ReplaceModule('XenAPI', fake)) fake.reset()