Parallelize tests in CI (#67911)

This commit is contained in:
Agate 2022-08-03 10:56:29 +02:00
parent 244611eeb4
commit d2b5a32997
9 changed files with 41 additions and 24 deletions

2
Jenkinsfile vendored
View File

@ -6,7 +6,7 @@ pipeline {
stages {
stage('Unit Tests') {
steps {
sh 'tox -rv'
sh 'tox -rv -p 8'
}
post {
always {

19
hobo/test_utils.py Normal file
View File

@ -0,0 +1,19 @@
import hashlib
import os
def get_safe_db_name(prefix):
"""
PostgreSQL database name limit is 68 characters, which can become
an issue during testing, because we need to build a unique
database name using the branch and tox env.
Ergo, the following code to ensure the database name is always shorter than 68
characters.
"""
BRANCH_NAME = os.environ.get('BRANCH_NAME', '').replace('/', '-')
TOX_ENV_NAME = os.environ.get('TOX_ENV_NAME')
DB_NAME = '_'.join([part for part in [prefix, BRANCH_NAME, TOX_ENV_NAME] if part])
DB_NAME = hashlib.sha256(DB_NAME.encode()).hexdigest()[:10]
return DB_NAME

View File

@ -1,5 +1,7 @@
import os
import hobo.test_utils
LANGUAGE_CODE = 'en-us'
BROKER_URL = 'memory://'
@ -16,13 +18,10 @@ MIDDLEWARE = MIDDLEWARE + (
)
HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE', 'django.db.backends.sqlite3'),
'TEST': {
'NAME': ('hobo-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-'))[:63],
},
'NAME': hobo.test_utils.get_safe_db_name('test-hobo'),
}
}

View File

@ -3,6 +3,8 @@ import os
from mock import mock_open, patch
import hobo.test_utils
# Debian defaults
DEBUG = False
@ -13,10 +15,7 @@ open_backup = open
with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read())
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')
DATABASES['default']['TEST'] = {
'NAME': (DATABASES['default']['NAME'] + '-%s' % BRANCH_NAME)[:63],
}
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-authentic')
# Avoid conflic with real tenants
# that might exist in /var/lib/authentic2_multitenant/tenants

View File

@ -3,6 +3,7 @@ import os.path
from mock import mock_open, patch
import hobo.test_utils
from hobo.settings import *
LANGUAGE_CODE = 'en-us'
@ -13,10 +14,7 @@ open_backup = open
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())
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')
DATABASES['default']['TEST'] = {
'NAME': (DATABASES['default']['NAME'] + '-%s' % BRANCH_NAME)[:63],
}
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-multipublik')
CACHES = {
'default': {

View File

@ -3,6 +3,8 @@ import os
from mock import mock_open, patch
import hobo.test_utils
LANGUAGE_CODE = 'en-us'
INSTALLED_APPS = (
@ -50,10 +52,7 @@ open_backup = open
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())
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')
DATABASES['default']['TEST'] = {
'NAME': (DATABASES['default']['NAME'] + '-%s' % BRANCH_NAME)[:63],
}
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-multitenant')
TENANT_APPS = (
'django.contrib.auth',

View File

@ -3,6 +3,8 @@ import os
from mock import mock_open, patch
import hobo.test_utils
# Debian defaults
DEBUG = False
@ -15,10 +17,8 @@ open_backup = open
with patch.object(builtins, 'open', mock_open(read_data=b'xxx')):
exec(open_backup(os.environ['DEBIAN_CONFIG_COMMON']).read())
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')
DATABASES['default']['TEST'] = {
'NAME': (DATABASES['default']['NAME'] + '-%s' % BRANCH_NAME)[:63],
}
BRANCH_NAME = os.environ.get("BRANCH_NAME", "").replace('/', '-')[:15]
DATABASES['default']['NAME'] = hobo.test_utils.get_safe_db_name('test-passerelle')
# Avoid conflic with real tenants
# that might exist in /var/lib/passerelle/tenants

View File

@ -1,16 +1,18 @@
import os
import tempfile
import hobo.test_utils
TENANT_BASE = tempfile.mkdtemp('hobo-tenant-base')
TENANT_MODEL = 'multitenant.Tenant'
MIDDLEWARE = ('hobo.multitenant.middleware.TenantMiddleware',) + MIDDLEWARE
BRANCH_NAME = (
os.environ.get("BRANCH_NAME", "").replace('/', '_').replace('-', '_').encode('ascii', 'ignore').decode()
)
)[:15]
DATABASES = {
'default': {
'ENGINE': 'tenant_schemas.postgresql_backend',
'TEST': {'NAME': ('hobo_test_schemas_' + BRANCH_NAME)[:63]},
'NAME': hobo.test_utils.get_safe_db_name('test-schemas'),
}
}
DATABASE_ROUTERS = ('tenant_schemas.routers.TenantSyncRouter',)

View File

@ -14,6 +14,7 @@ usedevelop = True
setenv =
DISABLE_GLOBAL_HANDLERS=1
BRANCH_NAME={env:BRANCH_NAME:}
COVERAGE_FILE={envdir}/coverage
DB_ENGINE=django.db.backends.postgresql_psycopg2
SETUPTOOLS_USE_DISTUTILS=stdlib
JUNIT=--junitxml=junit-{envname}.xml
@ -31,7 +32,7 @@ setenv =
passerelle: DEBIAN_CONFIG_COMMON=debian/debian_config_common.py
passerelle: PASSERELLE_SETTINGS_FILE=tests_passerelle/settings.py
passerelle: DJANGO_SETTINGS_MODULE=passerelle.settings
coverage: COVERAGE=--cov-report xml:coverage-{envname}.xml --cov-report html:htmlcov-{envname} --cov=hobo/ --cov-config .coveragerc
coverage: COVERAGE=--cov-report xml:coverage-{envname}.xml --cov-report html:htmlcov-{envname} --cov=hobo/ --cov-config .coveragerc
fast: NOMIGRATIONS=--nomigrations
deps:
drf39: djangorestframework>=3.9.2,<3.10