← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:shorten-test-instance-name into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:shorten-test-instance-name into launchpad:master.

Commit message:
Use slightly shorter test DB instance names

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/415903

Process IDs may nowadays be up to seven digits long, so the previous test database instance names we used could cause 'launchpad_ftest_template_{test_instance}' to be up to 65 characters long; but PostgreSQL identifiers are limited to 63 characters, so this could cause the tests in `lp.services.webapp.tests.test_dbpolicy.TestFastDowntimeRollout` to fail.  Use a slightly shorter random suffix to compensate for this.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:shorten-test-instance-name into launchpad:master.
diff --git a/lib/lp/testing/layers.py b/lib/lp/testing/layers.py
index d27ca18..1b6fcb0 100644
--- a/lib/lp/testing/layers.py
+++ b/lib/lp/testing/layers.py
@@ -45,6 +45,7 @@ __all__ = [
     'reconnect_stores',
     ]
 
+import base64
 from cProfile import Profile
 import datetime
 import errno
@@ -64,7 +65,6 @@ from unittest import (
     TestCase,
     TestResult,
     )
-import uuid
 
 from fixtures import (
     Fixture,
@@ -277,7 +277,13 @@ class BaseLayer:
         # We can only do unique test allocation and parallelisation if
         # LP_PERSISTENT_TEST_SERVICES is off.
         if not BaseLayer.persist_test_services:
-            test_instance = '%d_%s' % (os.getpid(), uuid.uuid1().hex)
+            # This should be at most 38 characters long, otherwise
+            # 'launchpad_ftest_template_{test_instance}' won't fit within
+            # PostgreSQL's 63-character limit for identifiers.  Linux
+            # currently allows up to 2^22 PIDs, so PIDs may be up to seven
+            # digits long.
+            test_instance = '%d_%s' % (
+                os.getpid(), base64.b16encode(os.urandom(12)).decode().lower())
             os.environ['LP_TEST_INSTANCE'] = test_instance
             cls.fixture.addCleanup(os.environ.pop, 'LP_TEST_INSTANCE', '')
             # Kill any Memcached or Librarian left running from a previous