remove use of explicit lockutils invocation in tests

Instead of passing lockutils loading explicitly in the test run
command line, provide the support for individual test classes to ask
that lock_path is setup with the REQUIRES_LOCKING = True class
parameter.

Augment all classes that currently fail without this with
REQUIRES_LOCKING = True. Hopefully many of those can be individually
removed in the future so we can get out of needing external locking in
any of our unit tests.

Change-Id: I56229e93ad61ac823646a4119a035d36c3400173
This commit is contained in:
Sean Dague 2014-10-28 09:33:49 -04:00 committed by Davanum Srinivas
parent 3adb279913
commit a2d8734881
16 changed files with 76 additions and 3 deletions

View File

@ -31,6 +31,7 @@ import logging
import os
import shutil
import sys
import tempfile
import uuid
import fixtures
@ -221,6 +222,7 @@ class TestCase(testtools.TestCase):
`NoDBTestCase` first.
"""
USES_DB = True
REQUIRES_LOCKING = False
# NOTE(rpodolyaka): this attribute can be overridden in subclasses in order
# to scale the global test timeout value set for each
@ -284,6 +286,29 @@ class TestCase(testtools.TestCase):
# Don't log every single DB migration step
logging.getLogger('migrate.versioning.api').setLevel(logging.WARNING)
# NOTE(sdague): because of the way we were using the lock
# wrapper we eneded up with a lot of tests that started
# relying on global external locking being set up for them. We
# consider all of these to be *bugs*. Tests should not require
# global external locking, or if they do, they should
# explicitly set it up themselves.
#
# The following REQUIRES_LOCKING class parameter is provided
# as a bridge to get us there. No new tests should be added
# that require it, and existing classes and tests should be
# fixed to not need it.
if self.REQUIRES_LOCKING:
lock_path = tempfile.mkdtemp()
def _cleanup_lock_path():
# because CONF is a global object, we actually have to
# explicitly zero it out afterwards
CONF.set_override('lock_path', None)
shutil.rmtree(lock_path)
self.addCleanup(_cleanup_lock_path)
CONF.set_override('lock_path', lock_path)
self.useFixture(conf_fixture.ConfFixture(CONF))
self.messaging_conf = messaging_conffixture.ConfFixture(CONF)

View File

@ -86,6 +86,9 @@ def get_instances_with_cached_ips(orig_func, *args, **kwargs):
class CinderCloudTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(CinderCloudTestCase, self).setUp()
ec2utils.reset_cache()

View File

@ -133,6 +133,9 @@ def get_instances_with_cached_ips(orig_func, get_floating,
class CloudTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(CloudTestCase, self).setUp()
self.useFixture(test.SampleNetworks())

View File

@ -151,6 +151,8 @@ class BaseMigrationTestCase(test.NoDBTestCase):
and configures the databases to run tests against.
"""
REQUIRES_LOCKING = True
# NOTE(jhesketh): It is expected that tests clean up after themselves.
# This is necessary for concurrency to allow multiple tests to work on
# one database.

View File

@ -63,6 +63,8 @@ def generate_new_element(items, prefix, numeric=False):
class _IntegratedTestBase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(_IntegratedTestBase, self).setUp()

View File

@ -288,6 +288,8 @@ def get_associated(context, network_id, host=None, address=None):
class LinuxNetworkTestCase(test.NoDBTestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(LinuxNetworkTestCase, self).setUp()
self.driver = driver.load_network_driver()

View File

@ -176,6 +176,9 @@ vifs = [{'id': 0,
class FlatNetworkTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(FlatNetworkTestCase, self).setUp()
self.tempdir = self.useFixture(fixtures.TempDir()).path
@ -793,6 +796,9 @@ class FlatNetworkTestCase(test.TestCase):
class FlatDHCPNetworkTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(FlatDHCPNetworkTestCase, self).setUp()
self.useFixture(test.SampleNetworks())
@ -839,6 +845,9 @@ class FlatDHCPNetworkTestCase(test.TestCase):
class VlanNetworkTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(VlanNetworkTestCase, self).setUp()
self.useFixture(test.SampleNetworks())
@ -1884,6 +1893,8 @@ class FakeNetwork(object):
class CommonNetworkTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(CommonNetworkTestCase, self).setUp()
self.context = context.RequestContext('fake', 'fake')
@ -2560,6 +2571,9 @@ class TestFloatingIPManager(floating_ips.FloatingIP,
class AllocateTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(AllocateTestCase, self).setUp()
dns = 'nova.network.noop_dns_driver.NoopDNSDriver'
@ -2674,6 +2688,9 @@ class AllocateTestCase(test.TestCase):
class FloatingIPTestCase(test.TestCase):
"""Tests nova.network.manager.FloatingIP."""
REQUIRES_LOCKING = True
def setUp(self):
super(FloatingIPTestCase, self).setUp()
self.tempdir = self.useFixture(fixtures.TempDir()).path

View File

@ -36,6 +36,8 @@ CONF.import_opt('compute_driver', 'nova.virt.driver')
class QuotaIntegrationTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(QuotaIntegrationTestCase, self).setUp()
self.flags(compute_driver='nova.virt.fake.FakeDriver',

View File

@ -410,6 +410,8 @@ class FakeNodeDevice(object):
class LibvirtConnTestCase(test.NoDBTestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(LibvirtConnTestCase, self).setUp()
self.flags(fake_call=True)

View File

@ -783,6 +783,9 @@ class FakeConnectionTestCase(_VirtDriverTestCase, test.TestCase):
class LibvirtConnTestCase(_VirtDriverTestCase, test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
# Point _VirtDriverTestCase at the right module
self.driver_module = 'nova.virt.libvirt.LibvirtDriver'

View File

@ -33,6 +33,9 @@ from nova.virt.vmwareapi import vmops
class ConfigDriveTestCase(test.NoDBTestCase):
REQUIRES_LOCKING = True
@mock.patch.object(driver.VMwareVCDriver, '_register_openstack_extension')
def setUp(self, mock_register):
super(ConfigDriveTestCase, self).setUp()

View File

@ -193,6 +193,8 @@ class VMwareSessionTestCase(test.NoDBTestCase):
class VMwareAPIVMTestCase(test.NoDBTestCase):
"""Unit tests for Vmware API connection calls."""
REQUIRES_LOCKING = True
@mock.patch.object(driver.VMwareVCDriver, '_register_openstack_extension')
def setUp(self, mock_register, create_connection=True):
super(VMwareAPIVMTestCase, self).setUp()

View File

@ -31,6 +31,9 @@ CONF = cfg.CONF
class ImageCacheManagerTestCase(test.NoDBTestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(ImageCacheManagerTestCase, self).setUp()
self._session = mock.Mock(name='session')

View File

@ -1597,6 +1597,8 @@ class XenAPIDiffieHellmanTestCase(test.NoDBTestCase):
class XenAPIMigrateInstance(stubs.XenAPITestBase):
"""Unit test for verifying migration-related actions."""
REQUIRES_LOCKING = True
def setUp(self):
super(XenAPIMigrateInstance, self).setUp()
self.flags(connection_url='test_url',
@ -2507,6 +2509,8 @@ class XenAPIBWCountersTestCase(stubs.XenAPITestBaseNoDB):
# FIXME(sirp): convert this to use XenAPITestBaseNoDB
class XenAPIDom0IptablesFirewallTestCase(stubs.XenAPITestBase):
REQUIRES_LOCKING = True
_in_rules = [
'# Generated by iptables-save v1.4.10 on Sat Feb 19 00:03:19 2011',
'*nat',

View File

@ -117,7 +117,7 @@ function run_tests {
# provided.
testrargs="discover ./nova/tests"
fi
${wrapper} python -m nova.openstack.common.lockutils python -m testtools.run $testropts $testrargs
${wrapper} python -m testtools.run $testropts $testrargs
# Short circuit because all of the testr and coverage stuff
# below does not make sense when running testtools.run for
@ -184,7 +184,7 @@ function run_pep8 {
}
TESTRTESTS="python -m nova.openstack.common.lockutils python setup.py testr"
TESTRTESTS="python setup.py testr"
if [ $never_venv -eq 0 ]
then

View File

@ -3,4 +3,4 @@
set -o pipefail
TESTRARGS=$1
python -m nova.openstack.common.lockutils python setup.py testr --slowest --testr-args="--subunit $TESTRARGS" | $(dirname $0)/subunit-trace.py -f
python setup.py testr --slowest --testr-args="--subunit $TESTRARGS" | $(dirname $0)/subunit-trace.py -f