From 5c35a84b72784ab16cb03659a9c004724ed38963 Mon Sep 17 00:00:00 2001 From: Adam Young Date: Wed, 6 Jun 2012 12:57:50 -0400 Subject: [PATCH] Speed up SQL unit tests. Using a cached copy of the SQLite Data base speeds up the tests: Before this change Ran 44 tests in 126.154s OK Slowest 5 tests took 15.87 secs: 3.34 test_delete_role (test_backend_sql.SqlIdentity) 3.17 test_delete_user_with_tenant_association (test_backend_sql.SqlIdentity) 3.17 test_create_tenant_invalid_name_fails (test_backend_sql.SqlIdentity) 3.10 test_get_user (test_backend_sql.SqlIdentity) 3.10 test_get_tenant_bad_tenant (test_backend_sql.SqlIdentity) After this change: Ran 44 tests in 58.996s OK Slowest 5 tests took 8.64 secs: 2.57 test_add_user_to_tenant (test_backend_sql.SqlIdentity) 1.58 test_update_tenant_long_name_fails (test_backend_sql.SqlIdentity) 1.51 test_rename_duplicate_user_name_fails (test_backend_sql.SqlIdentity) 1.50 test_delete_tenant_with_user_association (test_backend_sql.SqlIdentity) 1.49 test_delete_user_with_tenant_association (test_backend_sql.SqlIdentity) Slowest test was where the database was initialized Change-Id: Idf046763e8718762695bbcedce4d223f654054db --- .gitignore | 1 + keystone/common/sql/util.py | 11 ++++++++--- run_tests.py | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3eb4f30817..a7f29d9aee 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ AUTHORS build/ dist/ etc/keystone.conf +tests/test.db.pristine diff --git a/keystone/common/sql/util.py b/keystone/common/sql/util.py index 126b887eeb..8c98aeadbe 100644 --- a/keystone/common/sql/util.py +++ b/keystone/common/sql/util.py @@ -15,6 +15,7 @@ # under the License. import os +import shutil from keystone import config from keystone.common.sql import migration @@ -24,9 +25,13 @@ CONF = config.CONF def setup_test_database(): - # TODO(termie): be smart about this try: - os.unlink('test.db') + if os.path.exists('test.db'): + os.unlink('test.db') + if not os.path.exists('test.db.pristine'): + migration.db_sync() + shutil.copyfile('test.db', 'test.db.pristine') + else: + shutil.copyfile('test.db.pristine', 'test.db') except Exception: pass - migration.db_sync() diff --git a/run_tests.py b/run_tests.py index 9e8b676674..25ad7dc888 100644 --- a/run_tests.py +++ b/run_tests.py @@ -66,6 +66,8 @@ from nose import config from nose import core from nose import result +from keystone.common.sql import util + class _AnsiColorizer(object): """ @@ -357,4 +359,7 @@ if __name__ == '__main__': verbosity=c.verbosity, config=c, show_elapsed=show_elapsed) + if os.path.exists('tests/test.db.pristine'): + os.unlink('tests/test.db.pristine') + sys.exit(not core.run(config=c, testRunner=runner, argv=argv))