From c943cbbc40654827f537773a90d77577a54dbe0b Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 12 Mar 2015 16:55:55 -0400 Subject: [PATCH] ensure DatabaseFixture removes db on cleanup The DatabaseFixture was previously just ensuring that tests which used it had a clean database when they started. It did not ensure there was no working database around when it wasn't being used, so tests which leaked into the database might pass even if they were listed as a NoDBTest, because there was an in memory test db from a prior run. This adds a cleanup path to the fixture which blows away the db at the end of the test (which itself is tested). There were 46 tests which failed when we got this isolation correct, and this changes all those test classes containing those tests to require a db. Many of these could have narrower fixes, but this returns us to an isolated state. Change-Id: Id0480035d8e615ae602da9737cd2c94bc7967067 Closes-Bug: #1431519 Closes-Bug: #1431551 --- nova/tests/fixtures.py | 5 +++++ nova/tests/unit/api/ec2/test_ec2utils.py | 2 +- .../compute/contrib/test_block_device_mapping.py | 2 +- .../contrib/test_block_device_mapping_v1.py | 2 +- nova/tests/unit/network/test_api.py | 2 +- nova/tests/unit/network/test_manager.py | 2 +- nova/tests/unit/network/test_neutronv2.py | 4 ++-- nova/tests/unit/objects/test_instance.py | 2 +- nova/tests/unit/scheduler/test_scheduler.py | 2 +- nova/tests/unit/test_baserpc.py | 2 +- nova/tests/unit/test_fixtures.py | 15 +++++++++++++++ nova/tests/unit/test_pipelib.py | 2 +- 12 files changed, 31 insertions(+), 11 deletions(-) diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index f1941ec35bc5..70977e7935e7 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -199,6 +199,10 @@ class Database(fixtures.Fixture): DB_SCHEMA = "".join(line for line in conn.connection.iterdump()) engine.dispose() + def cleanup(self): + engine = session.get_engine() + engine.dispose() + def reset(self): self._cache_schema() engine = session.get_engine() @@ -209,6 +213,7 @@ class Database(fixtures.Fixture): def setUp(self): super(Database, self).setUp() self.reset() + self.addCleanup(self.cleanup) class RPCFixture(fixtures.Fixture): diff --git a/nova/tests/unit/api/ec2/test_ec2utils.py b/nova/tests/unit/api/ec2/test_ec2utils.py index 1a37fa04b652..9dceb7de1225 100644 --- a/nova/tests/unit/api/ec2/test_ec2utils.py +++ b/nova/tests/unit/api/ec2/test_ec2utils.py @@ -18,7 +18,7 @@ from nova import objects from nova import test -class EC2UtilsTestCase(test.NoDBTestCase): +class EC2UtilsTestCase(test.TestCase): def setUp(self): self.ctxt = context.get_admin_context() ec2utils.reset_cache() diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping.py b/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping.py index f431662db866..438c6ad0aa09 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping.py @@ -36,7 +36,7 @@ from nova.tests.unit import matchers CONF = cfg.CONF -class BlockDeviceMappingTestV21(test.NoDBTestCase): +class BlockDeviceMappingTestV21(test.TestCase): validation_error = exception.ValidationError def _setup_controller(self): diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping_v1.py b/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping_v1.py index 75746210ad33..047c7d96e111 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping_v1.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_block_device_mapping_v1.py @@ -34,7 +34,7 @@ from nova.tests.unit.image import fake CONF = cfg.CONF -class BlockDeviceMappingTestV21(test.NoDBTestCase): +class BlockDeviceMappingTestV21(test.TestCase): validation_error = exception.ValidationError def _setup_controller(self): diff --git a/nova/tests/unit/network/test_api.py b/nova/tests/unit/network/test_api.py index 49ce5ae6c6e2..4f848f29a4e0 100644 --- a/nova/tests/unit/network/test_api.py +++ b/nova/tests/unit/network/test_api.py @@ -45,7 +45,7 @@ from nova import utils FAKE_UUID = 'a47ae74e-ab08-547f-9eee-ffd23fc46c16' -class NetworkPolicyTestCase(test.NoDBTestCase): +class NetworkPolicyTestCase(test.TestCase): def setUp(self): super(NetworkPolicyTestCase, self).setUp() diff --git a/nova/tests/unit/network/test_manager.py b/nova/tests/unit/network/test_manager.py index 64d07ee0831d..8f7482bbb445 100644 --- a/nova/tests/unit/network/test_manager.py +++ b/nova/tests/unit/network/test_manager.py @@ -3301,7 +3301,7 @@ class FloatingIPTestCase(test.TestCase): 'public')) -class InstanceDNSTestCase(test.NoDBTestCase): +class InstanceDNSTestCase(test.TestCase): """Tests nova.network.manager instance DNS.""" def setUp(self): super(InstanceDNSTestCase, self).setUp() diff --git a/nova/tests/unit/network/test_neutronv2.py b/nova/tests/unit/network/test_neutronv2.py index 6e591c8e0b76..1059d93e93c3 100644 --- a/nova/tests/unit/network/test_neutronv2.py +++ b/nova/tests/unit/network/test_neutronv2.py @@ -193,7 +193,7 @@ class TestNeutronClient(test.NoDBTestCase): self.assertEqual('new_token1', token_store.admin_auth_token) -class TestNeutronv2Base(test.NoDBTestCase): +class TestNeutronv2Base(test.TestCase): def setUp(self): super(TestNeutronv2Base, self).setUp() @@ -2798,7 +2798,7 @@ class TestNeutronv2(TestNeutronv2Base): self.assertEqual(expected_results, has_pci_request_id) -class TestNeutronv2WithMock(test.NoDBTestCase): +class TestNeutronv2WithMock(test.TestCase): """Used to test Neutron V2 API with mock.""" def setUp(self): diff --git a/nova/tests/unit/objects/test_instance.py b/nova/tests/unit/objects/test_instance.py index 9111e467a65d..82bc580f6c85 100644 --- a/nova/tests/unit/objects/test_instance.py +++ b/nova/tests/unit/objects/test_instance.py @@ -1481,7 +1481,7 @@ class TestRemoteInstanceListObject(test_objects._RemoteTest, pass -class TestInstanceObjectMisc(test.NoDBTestCase): +class TestInstanceObjectMisc(test.TestCase): def test_expected_cols(self): self.stubs.Set(instance, '_INSTANCE_OPTIONAL_JOINED_FIELDS', ['bar']) self.assertEqual(['bar'], instance._expected_cols(['foo', 'bar'])) diff --git a/nova/tests/unit/scheduler/test_scheduler.py b/nova/tests/unit/scheduler/test_scheduler.py index 4d2ff2c71571..0da2da1441b3 100644 --- a/nova/tests/unit/scheduler/test_scheduler.py +++ b/nova/tests/unit/scheduler/test_scheduler.py @@ -73,7 +73,7 @@ class SchedulerManagerTestCase(test.NoDBTestCase): delete_aggregate.assert_called_once_with('agg') -class SchedulerV3PassthroughTestCase(test.NoDBTestCase): +class SchedulerV3PassthroughTestCase(test.TestCase): def setUp(self): super(SchedulerV3PassthroughTestCase, self).setUp() self.manager = manager.SchedulerManager() diff --git a/nova/tests/unit/test_baserpc.py b/nova/tests/unit/test_baserpc.py index d6f7a144a46a..1d427f6eed7e 100644 --- a/nova/tests/unit/test_baserpc.py +++ b/nova/tests/unit/test_baserpc.py @@ -27,7 +27,7 @@ from nova import test CONF = cfg.CONF -class BaseAPITestCase(test.NoDBTestCase): +class BaseAPITestCase(test.TestCase): def setUp(self): super(BaseAPITestCase, self).setUp() diff --git a/nova/tests/unit/test_fixtures.py b/nova/tests/unit/test_fixtures.py index be780e422190..49e08a2dd184 100644 --- a/nova/tests/unit/test_fixtures.py +++ b/nova/tests/unit/test_fixtures.py @@ -205,3 +205,18 @@ class TestDatabaseFixture(testtools.TestCase): result = conn.execute("select * from instance_types") rows = result.fetchall() self.assertEqual(len(rows), 5, "Rows %s" % rows) + + def test_fixture_cleanup(self): + # because this sets up reasonable db connection strings + self.useFixture(conf_fixture.ConfFixture()) + fix = fixtures.Database() + self.useFixture(fix) + + # manually do the cleanup that addCleanup will do + fix.cleanup() + + # ensure the db contains nothing + engine = session.get_engine() + conn = engine.connect() + schema = "".join(line for line in conn.connection.iterdump()) + self.assertEqual(schema, "BEGIN TRANSACTION;COMMIT;") diff --git a/nova/tests/unit/test_pipelib.py b/nova/tests/unit/test_pipelib.py index f3ba0bc3d8ff..9f8d65e234da 100644 --- a/nova/tests/unit/test_pipelib.py +++ b/nova/tests/unit/test_pipelib.py @@ -24,7 +24,7 @@ from nova import utils CONF = cfg.CONF -class PipelibTest(test.NoDBTestCase): +class PipelibTest(test.TestCase): def setUp(self): super(PipelibTest, self).setUp() self.cloudpipe = pipelib.CloudPipe()