From 5617b1ae96d16fe5a8b4227c426782563e4f9f3e Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Fri, 27 Sep 2019 10:50:51 -0500 Subject: [PATCH] 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 --- nova/test.py | 7 ++++--- nova/tests/fixtures.py | 29 --------------------------- nova/tests/unit/db/test_migrations.py | 11 ++++------ nova/tests/unit/test_fixtures.py | 21 ------------------- 4 files changed, 8 insertions(+), 60 deletions(-) diff --git a/nova/test.py b/nova/test.py index 6bfd876945c6..433722e3dde6 100644 --- a/nova/test.py +++ b/nova/test.py @@ -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()) diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index 4944afc6739d..2a4b2aa718f4 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -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() diff --git a/nova/tests/unit/db/test_migrations.py b/nova/tests/unit/db/test_migrations.py index de898f2fe33d..0539d505bf77 100644 --- a/nova/tests/unit/db/test_migrations.py +++ b/nova/tests/unit/db/test_migrations.py @@ -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): diff --git a/nova/tests/unit/test_fixtures.py b/nova/tests/unit/test_fixtures.py index d6f2aaa9a646..f9845212529f 100644 --- a/nova/tests/unit/test_fixtures.py +++ b/nova/tests/unit/test_fixtures.py @@ -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')