From f261afdfbed05cef9d534e418ddd8bcb26970cc6 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Sat, 25 May 2013 01:01:13 +0000 Subject: Speeding up scheduler tests Database access is costly in tests and many tests don't actually need it. This patch provides a simple way we can speed up the Nova tests by providing a new base class, `NoDBTestCase` which allows us to white-list tests not needing database access. The following speedups were observed: With TestCase: Ran 265 tests in 17.080s With NoDBTestCase: Ran 265 tests in 9.478s Further optimizaton could be done by fixing `test_host_filters` to not hit the database. This will require some effort though in order to maintain the quality of test coverage since part of what's doing is testing the code in the DB layer. Change-Id: Ic34c4c39d7f680ac501efb459fdeaa51331ffe1c --- nova/test.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'nova/test.py') diff --git a/nova/test.py b/nova/test.py index f0dba7c7c..d7502b4ea 100644 --- a/nova/test.py +++ b/nova/test.py @@ -189,7 +189,12 @@ class TestingException(Exception): class TestCase(testtools.TestCase): - """Test case base class for all unit tests.""" + """Test case base class for all unit tests. + + Due to the slowness of DB access, please consider deriving from + `NoDBTestCase` first. + """ + USES_DB = True def setUp(self): """Run before each test method to initialize test environment.""" @@ -217,13 +222,15 @@ class TestCase(testtools.TestCase): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.useFixture(conf_fixture.ConfFixture(CONF)) - global _DB_CACHE - if not _DB_CACHE: - _DB_CACHE = Database(session, migration, - sql_connection=CONF.sql_connection, - sqlite_db=CONF.sqlite_db, - sqlite_clean_db=CONF.sqlite_clean_db) - self.useFixture(_DB_CACHE) + if self.USES_DB: + global _DB_CACHE + if not _DB_CACHE: + _DB_CACHE = Database(session, migration, + sql_connection=CONF.sql_connection, + sqlite_db=CONF.sqlite_db, + sqlite_clean_db=CONF.sqlite_clean_db) + + self.useFixture(_DB_CACHE) mox_fixture = self.useFixture(MoxStubout()) self.mox = mox_fixture.mox @@ -274,3 +281,12 @@ class TimeOverride(fixtures.Fixture): super(TimeOverride, self).setUp() timeutils.set_time_override() self.addCleanup(timeutils.clear_time_override) + + +class NoDBTestCase(TestCase): + """ + `NoDBTestCase` differs from TestCase in that DB access is not supported. + This makes tests run significantly faster. If possible, all new tests + should derive from this class. + """ + USES_DB = False -- cgit