diff options
author | Andrew Laski <andrew.laski@rackspace.com> | 2015-03-16 13:17:09 -0400 |
---|---|---|
committer | Andrew Laski <andrew.laski@rackspace.com> | 2015-03-18 10:06:03 -0400 |
commit | fbb23bb785e1e5e3ea12019551a38fec9e45ba6c (patch) | |
tree | 4084a06bd54989ab326dd9f07be7e0a422161be2 /nova/tests/unit/test_fixtures.py | |
parent | 544039653e1f648f1d07a5263783b7defd6d3205 (diff) | |
download | nova-fbb23bb785e1e5e3ea12019551a38fec9e45ba6c.tar.gz |
Test fixture for the api database
In order to run fuctional tests against the database we should have a
fixture to handle the setup and cleanup. This adapts the current
Database fixture to work for either database. It is left out the Nova
TestCase test class and should be set up in specific test classes that
need it.
Change-Id: I98e98cb6dea0008d36397b4e2bf7fcf9ed721721
Diffstat (limited to 'nova/tests/unit/test_fixtures.py')
-rw-r--r-- | nova/tests/unit/test_fixtures.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/nova/tests/unit/test_fixtures.py b/nova/tests/unit/test_fixtures.py index 49e08a2dd1..b5d9fc5000 100644 --- a/nova/tests/unit/test_fixtures.py +++ b/nova/tests/unit/test_fixtures.py @@ -19,6 +19,7 @@ import sys import fixtures as fx from oslo_config import cfg from oslo_log import log as logging +from oslo_utils import uuidutils import testtools from nova.db.sqlalchemy import api as session @@ -206,6 +207,34 @@ class TestDatabaseFixture(testtools.TestCase): rows = result.fetchall() self.assertEqual(len(rows), 5, "Rows %s" % rows) + def test_api_fixture_reset(self): + # This sets up reasonable db connection strings + self.useFixture(conf_fixture.ConfFixture()) + self.useFixture(fixtures.Database(database='api')) + engine = session.get_api_engine() + conn = engine.connect() + result = conn.execute("select * from cell_mappings") + rows = result.fetchall() + self.assertEqual(len(rows), 0, "Rows %s" % rows) + + uuid = uuidutils.generate_uuid() + conn.execute("insert into cell_mappings (uuid, name) VALUES " + "('%s', 'fake-cell')" % (uuid,)) + result = conn.execute("select * from cell_mappings") + rows = result.fetchall() + self.assertEqual(len(rows), 1, "Rows %s" % rows) + + # reset by invoking the fixture again + # + # NOTE(sdague): it's important to reestablish the db + # connection because otherwise we have a reference to the old + # in mem db. + self.useFixture(fixtures.Database(database='api')) + conn = engine.connect() + result = conn.execute("select * from cell_mappings") + rows = result.fetchall() + self.assertEqual(len(rows), 0, "Rows %s" % rows) + def test_fixture_cleanup(self): # because this sets up reasonable db connection strings self.useFixture(conf_fixture.ConfFixture()) @@ -220,3 +249,28 @@ class TestDatabaseFixture(testtools.TestCase): conn = engine.connect() schema = "".join(line for line in conn.connection.iterdump()) self.assertEqual(schema, "BEGIN TRANSACTION;COMMIT;") + + def test_api_fixture_cleanup(self): + # This sets up reasonable db connection strings + self.useFixture(conf_fixture.ConfFixture()) + fix = fixtures.Database(database='api') + self.useFixture(fix) + + # No data inserted by migrations so we need to add a row + engine = session.get_api_engine() + conn = engine.connect() + uuid = uuidutils.generate_uuid() + conn.execute("insert into cell_mappings (uuid, name) VALUES " + "('%s', 'fake-cell')" % (uuid,)) + result = conn.execute("select * from cell_mappings") + rows = result.fetchall() + self.assertEqual(len(rows), 1, "Rows %s" % rows) + + # Manually do the cleanup that addCleanup will do + fix.cleanup() + + # Ensure the db contains nothing + engine = session.get_api_engine() + conn = engine.connect() + schema = "".join(line for line in conn.connection.iterdump()) + self.assertEqual(schema, "BEGIN TRANSACTION;COMMIT;") |