tox: fixed inconsistent / obscure db names during tests (#67933)

This commit is contained in:
Agate 2022-08-04 09:31:48 +02:00
parent 1c02ad4517
commit 5e5125c97d
7 changed files with 19 additions and 18 deletions

View File

@ -1,19 +1,20 @@
import hashlib
import os import os
def get_safe_db_name(prefix): def get_safe_db_name():
""" """
PostgreSQL database name limit is 68 characters, which can become PostgreSQL database name limit is 63 characters, which can become
an issue during testing, because we need to build a unique an issue during testing, because we need to build a unique
database name using the branch and tox env. database name using the branch name and tox env.
Ergo, the following code to ensure the database name is always shorter than 68 Also, when running tests in parallel through `tox -p`,
characters. pytest django append the tox env name automatically
through a fixture so we have to skip this step.
""" """
BRANCH_NAME = os.environ.get('BRANCH_NAME', '').replace('/', '-') BRANCH_NAME = os.environ.get('BRANCH_NAME', '').replace('/', '-')[:15]
TOX_ENV_NAME = os.environ.get('TOX_ENV_NAME') parts = [BRANCH_NAME]
DB_NAME = '_'.join([part for part in [prefix, BRANCH_NAME, TOX_ENV_NAME] if part]) if not os.environ.get("TOX_PARALLEL_ENV"):
DB_NAME = hashlib.sha256(DB_NAME.encode()).hexdigest()[:10] # when we're in parallel mode, pytest-django will do this
# for us at a later point
return DB_NAME parts.append(os.environ.get('TOX_ENV_NAME'))
return '_'.join(parts)

View File

@ -21,7 +21,7 @@ HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.sqlite3'), 'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.sqlite3'),
'NAME': hobo.test_utils.get_safe_db_name('test-hobo'), 'NAME': hobo.test_utils.get_safe_db_name(),
} }
} }

View File

@ -15,7 +15,7 @@ open_backup = open
with patch.object(builtins, 'open', mock_open(read_data=b'xxx')): with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read()) exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read())
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-authentic') DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name()
# Avoid conflic with real tenants # Avoid conflic with real tenants
# that might exist in /var/lib/authentic2_multitenant/tenants # that might exist in /var/lib/authentic2_multitenant/tenants

View File

@ -14,7 +14,7 @@ open_backup = open
with patch.object(builtins, 'open', mock_open(read_data=b'xxx')): with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.path.join(os.path.dirname(__file__), '../debian/debian_config_common.py')).read()) exec(open_backup(os.path.join(os.path.dirname(__file__), '../debian/debian_config_common.py')).read())
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-multipublik') DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name()
CACHES = { CACHES = {
'default': { 'default': {

View File

@ -52,7 +52,7 @@ open_backup = open
with patch.object(builtins, 'open', mock_open(read_data=b'xxx')): with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.path.join(os.path.dirname(__file__), '../debian/debian_config_common.py')).read()) exec(open_backup(os.path.join(os.path.dirname(__file__), '../debian/debian_config_common.py')).read())
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-multitenant') DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name()
TENANT_APPS = ( TENANT_APPS = (
'django.contrib.auth', 'django.contrib.auth',

View File

@ -18,7 +18,7 @@ with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read()) exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read())
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')[:15] BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')[:15]
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-passerelle') DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name()
# Avoid conflic with real tenants # Avoid conflic with real tenants
# that might exist in /var/lib/passerelle/tenants # that might exist in /var/lib/passerelle/tenants

View File

@ -12,7 +12,7 @@ BRANCH_NAME = (
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'tenant_schemas.postgresql_backend', 'ENGINE': 'tenant_schemas.postgresql_backend',
'NAME': hobo.test_utils.get_safe_db_name('test-schemas'), 'NAME': hobo.test_utils.get_safe_db_name(),
} }
} }
DATABASE_ROUTERS = ('tenant_schemas.routers.TenantSyncRouter',) DATABASE_ROUTERS = ('tenant_schemas.routers.TenantSyncRouter',)