test cleanup: Use oslotest's Timeout fixture

Nova had its own copy of oslotest's Timeout fixture (probably predating
that one, actually). DRY it up.

A future change should be able to remove references to this fixture from
nova.test.TestCase by inheriting from oslotest.base.BaseTestCase instead
of testtools.TestCase. But we may or may not want to do the same for
NovaMigrationsCheckers subclasses (TestNovaMigrations*) since that would
pull in fixtures we don't care about (output and tempfile/tempdir).

Change-Id: I813a80fae306334abb653e6d3603eabe2d2332bf
This commit is contained in:
Eric Fried 2019-09-27 10:50:51 -05:00
parent e11a021062
commit 5617b1ae96
4 changed files with 8 additions and 60 deletions

View File

@ -47,6 +47,7 @@ from oslo_utils import timeutils
from oslo_versionedobjects import fixture as ovo_fixture
from oslotest import mock_fixture
from oslotest import moxstubout
from oslotest import timeout
import six
from six.moves import builtins
import testtools
@ -177,9 +178,9 @@ class TestCase(testtools.TestCase):
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
self.useFixture(nova_fixtures.Timeout(
os.environ.get('OS_TEST_TIMEOUT', 0),
self.TIMEOUT_SCALING_FACTOR))
# The Timeout fixture picks up env.OS_TEST_TIMEOUT, defaulting to 0.
self.useFixture(timeout.Timeout(
scaling_factor=self.TIMEOUT_SCALING_FACTOR))
self.useFixture(nova_fixtures.OpenStackSDKFixture())

View File

@ -231,35 +231,6 @@ class OutputStreamCapture(fixtures.Fixture):
return self.out._details["stdout"].as_text()
class Timeout(fixtures.Fixture):
"""Setup per test timeouts.
In order to avoid test deadlocks we support setting up a test
timeout parameter read from the environment. In almost all
cases where the timeout is reached this means a deadlock.
A class level TIMEOUT_SCALING_FACTOR also exists, which allows
extremely long tests to specify they need more time.
"""
def __init__(self, timeout, scaling=1):
super(Timeout, self).__init__()
try:
self.test_timeout = int(timeout)
except ValueError:
# If timeout value is invalid do not set a timeout.
self.test_timeout = 0
if scaling >= 1:
self.test_timeout *= scaling
else:
raise ValueError('scaling value must be >= 1')
def setUp(self):
super(Timeout, self).setUp()
if self.test_timeout > 0:
self.useFixture(fixtures.Timeout(self.test_timeout, gentle=True))
class DatabasePoisonFixture(fixtures.Fixture):
def setUp(self):
super(DatabasePoisonFixture, self).setUp()

View File

@ -42,6 +42,7 @@ from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import test_fixtures
from oslo_db.sqlalchemy import test_migrations
from oslo_db.sqlalchemy import utils as oslodbutils
from oslotest import timeout
import sqlalchemy
from sqlalchemy.engine import reflection
import sqlalchemy.exc
@ -96,13 +97,9 @@ class NovaMigrationsCheckers(test_migrations.ModelsMigrationsSync,
self.useFixture(nova_fixtures.StandardLogging())
super(NovaMigrationsCheckers, self).setUp()
# NOTE(rpodolyaka): we need to repeat the functionality of the base
# test case a bit here as this gets overridden by oslotest base test
# case and nova base test case cleanup must be the last one (as it
# deletes attributes of test case instances)
self.useFixture(nova_fixtures.Timeout(
os.environ.get('OS_TEST_TIMEOUT', 0),
self.TIMEOUT_SCALING_FACTOR))
# The Timeout fixture picks up env.OS_TEST_TIMEOUT, defaulting to 0.
self.useFixture(timeout.Timeout(
scaling_factor=self.TIMEOUT_SCALING_FACTOR))
self.engine = enginefacade.writer.get_engine()
def assertColumnExists(self, engine, table_name, column):

View File

@ -145,27 +145,6 @@ class TestLogging(testtools.TestCase):
self.assertIn("at debug", stdlog.logger.output)
class TestTimeout(testtools.TestCase):
"""Tests for our timeout fixture.
Testing the actual timeout mechanism is beyond the scope of this
test, because it's a pretty clear pass through to fixtures'
timeout fixture, which tested in their tree.
"""
def test_scaling(self):
# a bad scaling factor
self.assertRaises(ValueError, fixtures.Timeout, 1, 0.5)
# various things that should work.
timeout = fixtures.Timeout(10)
self.assertEqual(10, timeout.test_timeout)
timeout = fixtures.Timeout("10")
self.assertEqual(10, timeout.test_timeout)
timeout = fixtures.Timeout("10", 2)
self.assertEqual(20, timeout.test_timeout)
class TestOSAPIFixture(testtools.TestCase):
@mock.patch('nova.objects.Service.get_by_host_and_binary')
@mock.patch('nova.objects.Service.create')