@@ -18,7 +18,7 @@ MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
- 'NAME': 'layersdb',
+ 'NAME': os.getenv('DATABASE_NAME', 'layersdb'),
'USER': os.getenv('DATABASE_USER', 'root'),
'PASSWORD': os.getenv('DATABASE_PASSWORD', 'testingpw'),
'HOST': os.getenv('DATABASE_HOST', 'layersdb'),
@@ -23,7 +23,6 @@
import sys
import os
-import shutil
import subprocess
import pytest
@@ -44,25 +43,22 @@ def db_access_without_rollback_and_truncate(request, django_db_setup, django_db_
request.addfinalizer(django_db_blocker.restore)
@pytest.fixture(scope="module")
-def backup_settings(tmpdir_factory):
- stmpdir = tmpdir_factory.mktemp('settings')
- settingsfile = os.path.join(basepath, 'settings.py')
- backupfile = os.path.join(stmpdir, 'settings.bak')
- shutil.copy(settingsfile, backupfile)
- yield settingsfile
- shutil.copy(backupfile, settingsfile)
-
-@pytest.fixture(scope="module")
-def hack_settings(backup_settings):
- # It's horrific to have to do this, but we need to have additional
- # scripts connect to the testing database and not whatever's in settings.py
- # on disk right now, and this appears to be the only real way to do that
+def script_database(django_db_setup):
+ """
+ Point the scripts we launch at the test database.
+
+ The scripts run in separate processes and so read the settings afresh,
+ which would connect them to the real database. The settings take the
+ database name from DATABASE_NAME, so set that in the environment the
+ scripts inherit.
+ """
from django.conf import settings
- with open(backup_settings, 'a') as f:
- f.write('\nDATABASES = %s\n' % settings.DATABASES)
+ with pytest.MonkeyPatch.context() as mp:
+ mp.setenv('DATABASE_NAME', settings.DATABASES['default']['NAME'])
+ yield
@pytest.fixture(scope="module")
-def import_layer(hack_settings):
+def import_layer(script_database):
run_cmd("layerindex/tools/import_layer.py https://git.openembedded.org/openembedded-core -s meta openembedded-core")
run_cmd("layerindex/tools/import_layer.py https://git.yoctoproject.org/meta-layerindex-test -s meta-layerindex-test")
run_cmd("layerindex/update.py -l meta-layerindex-test")
The update tests launch import_layer.py and update.py as separate processes, which read the settings afresh and so would connect to the real database instead of the test database the tests look in. To avoid that, the tests appended the test database configuration to settings.py in the source tree and restored the file afterwards. That only works when the scripts load that file. In the dev container DJANGO_SETTINGS_MODULE is docker.settings, so the appended configuration is ignored and the scripts import the test layers into the development database. An interrupted run also leaves settings.py modified. Take the database name in docker/settings.py from a new DATABASE_NAME environment variable, defaulting to "layersdb" as before, in the same way as the user, password, host and port. In the tests, replace the fixtures that rewrote settings.py with one that sets DATABASE_NAME to the name of the test database for the scripts to inherit. Tested by running tests/test_update.py in a copy of the tree with a separate LAYER_FETCH_DIR, both with DJANGO_SETTINGS_MODULE set to docker.settings and with docker/settings.py copied to settings.py as in the Docker image: all 5 tests pass, the settings files are unchanged and nothing is added to the development database. The remaining 89 tests still pass. Signed-off-by: Piotr Buliński <piotr@qbee.io> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --- docker/settings.py | 2 +- tests/test_update.py | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-)