← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/random-test-db-bug-987379 into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/random-test-db-bug-987379 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #987379 in MAAS: "Two instances of the test suite cannot be run concurrently."
  https://bugs.launchpad.net/maas/+bug/987379

For more details, see:
https://code.launchpad.net/~rvb/maas/random-test-db-bug-987379/+merge/103257

This branch adds the ability to use a randomly-named test database.  If the environment variable named RANDOM_TEST_DATABASE_NAME is present in the execution environment, the test database will be named 'test_maas_<random_string>' (instead of the default 'test_maas').  This will allow for concurrent test runs of the test suite.
-- 
https://code.launchpad.net/~rvb/maas/random-test-db-bug-987379/+merge/103257
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/random-test-db-bug-987379 into lp:maas.
=== modified file 'HACKING.txt'
--- HACKING.txt	2012-04-23 09:54:29 +0000
+++ HACKING.txt	2012-04-24 11:21:20 +0000
@@ -111,6 +111,13 @@
 
 .. _nose: http://readthedocs.org/docs/nose/en/latest/
 
+A test database named 'test_maas' will be created for the test suite
+to run.  This test database will be deleted at the end of the testing
+process.  If you want the database to use a random name (to be able to
+run many instances of the test suite concurrently for instance), set
+the environement variable RANDOM_TEST_DATABASE_NAME::
+
+    $ export RANDOM_TEST_DATABASE_NAME='true'
 
 Running tests with a real Cobbler
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

=== modified file 'src/maas/development.py'
--- src/maas/development.py	2012-04-16 10:00:51 +0000
+++ src/maas/development.py	2012-04-24 11:21:20 +0000
@@ -13,6 +13,8 @@
 
 import logging
 import os
+import random
+import string
 
 from maas import (
     import_local_settings,
@@ -51,6 +53,26 @@
 # Silent South during tests.
 logging.getLogger('south').setLevel(logging.WARNING)
 
+# If the environment variable RANDOM_TEST_DATABASE_NAME is set, the
+# test database will use a random name instead of the default (test_maas).
+TEST_DB_RANDOM_SIZE = 20
+
+
+def get_test_database():
+    if os.environ.get('RANDOM_TEST_DATABASE_NAME', None) is None:
+        return 'test_maas'
+    else:
+        # Generate a random database name of the form:
+        # 'test_maas_<random_string>'.
+        random_string = ''.join(
+            random.choice(string.letters)
+            for i in xrange(TEST_DB_RANDOM_SIZE))
+        return '%s_%s' % ('test_maas', random_string)
+
+
+TEST_NAME = get_test_database()
+
+
 DATABASES = {
     'default': {
         # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' etc.
@@ -59,6 +81,7 @@
         # For PostgreSQL, a "hostname" starting with a slash indicates a
         # Unix socket directory.
         'HOST': '%s/db' % os.getcwd(),
+        'TEST_NAME': TEST_NAME,
     }
 }
 

=== modified file 'src/maasserver/tests/test_configuration.py'
--- src/maasserver/tests/test_configuration.py	2012-04-16 10:00:51 +0000
+++ src/maasserver/tests/test_configuration.py	2012-04-24 11:21:20 +0000
@@ -14,6 +14,12 @@
 
 
 from django.conf import settings
+from fixtures import EnvironmentVariableFixture
+from maas.development import (
+    get_test_database,
+    TEST_DB_RANDOM_SIZE,
+    )
+from maasserver.testing.factory import factory
 from maasserver.testing.testcase import TestCase
 
 
@@ -24,3 +30,16 @@
         self.assertIn(
             'django.middleware.transaction.TransactionMiddleware',
             settings.MIDDLEWARE_CLASSES)
+
+
+class TestDevelopmentConfiguration(TestCase):
+
+    def test_get_test_database_returns_default_db_name(self):
+        self.assertEqual('test_maas', get_test_database())
+
+    def test_get_test_database_returns_randomized_db_name(self):
+        self.useFixture(
+            EnvironmentVariableFixture(
+                'RANDOM_TEST_DATABASE_NAME', factory.getRandomString()))
+        self.assertEqual(
+            len('test_maas_') + TEST_DB_RANDOM_SIZE, len(get_test_database()))