From fa65d4730c58fa9de83d0b236ba660b1ce8d9e1c Mon Sep 17 00:00:00 2001 From: "Jens L." Date: Mon, 22 Dec 2025 15:39:05 +0100 Subject: [PATCH] blueprints: fix flaky tests (#19002) * blueprints: attempt to fix tests Signed-off-by: Jens Langhammer * fix postgres debug logging Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer --- .github/actions/test-results/action.yml | 2 +- Makefile | 2 +- authentik/blueprints/tests/test_v1_tasks.py | 2 +- authentik/blueprints/v1/meta/apply_blueprint.py | 2 +- authentik/blueprints/v1/tasks.py | 7 +------ authentik/root/test_runner.py | 4 ++++ 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.github/actions/test-results/action.yml b/.github/actions/test-results/action.yml index affaa07bc7..496314b4d9 100644 --- a/.github/actions/test-results/action.yml +++ b/.github/actions/test-results/action.yml @@ -20,7 +20,7 @@ runs: - name: PostgreSQL Logs shell: bash run: | - if [[ $ACTIONS_RUNNER_DEBUG == 'true' || $ACTIONS_STEP_DEBUG == 'true' ]]; then + if [[ $RUNNER_DEBUG == '1' ]]; then docker stop setup-postgresql-1 echo "::group::PostgreSQL Logs" docker logs setup-postgresql-1 diff --git a/Makefile b/Makefile index 3e3c3c92b6..66fec97596 100644 --- a/Makefile +++ b/Makefile @@ -334,6 +334,6 @@ ci-pending-migrations: ci--meta-debug uv run ak makemigrations --check ci-test: ci--meta-debug - uv run coverage run manage.py test --keepdb --randomly-seed ${CI_TEST_SEED} authentik + uv run coverage run manage.py test --keepdb authentik uv run coverage report uv run coverage xml diff --git a/authentik/blueprints/tests/test_v1_tasks.py b/authentik/blueprints/tests/test_v1_tasks.py index bf393e5a1c..fe406b7aa9 100644 --- a/authentik/blueprints/tests/test_v1_tasks.py +++ b/authentik/blueprints/tests/test_v1_tasks.py @@ -149,7 +149,7 @@ class TestBlueprintsV1Tasks(TransactionTestCase): instance.status, BlueprintInstanceStatus.UNKNOWN, ) - apply_blueprint(instance.pk) + apply_blueprint.send(instance.pk).get_result(block=True) instance.refresh_from_db() self.assertEqual(instance.last_applied_hash, "") self.assertEqual( diff --git a/authentik/blueprints/v1/meta/apply_blueprint.py b/authentik/blueprints/v1/meta/apply_blueprint.py index de518e4838..e8631e9ea9 100644 --- a/authentik/blueprints/v1/meta/apply_blueprint.py +++ b/authentik/blueprints/v1/meta/apply_blueprint.py @@ -44,7 +44,7 @@ class ApplyBlueprintMetaSerializer(PassiveSerializer): return MetaResult() LOGGER.debug("Applying blueprint from meta model", blueprint=self.blueprint_instance) - apply_blueprint(self.blueprint_instance.pk) + apply_blueprint.send(self.blueprint_instance.pk).get_result(block=True) return MetaResult() diff --git a/authentik/blueprints/v1/tasks.py b/authentik/blueprints/v1/tasks.py index 233041ea6f..7b7bf268c3 100644 --- a/authentik/blueprints/v1/tasks.py +++ b/authentik/blueprints/v1/tasks.py @@ -12,7 +12,6 @@ from django.db import DatabaseError, InternalError, ProgrammingError from django.utils.text import slugify from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ -from django_dramatiq_postgres.middleware import CurrentTaskNotFound from dramatiq.actor import actor from dramatiq.middleware import Middleware from structlog.stdlib import get_logger @@ -40,7 +39,6 @@ from authentik.events.utils import sanitize_dict from authentik.lib.config import CONFIG from authentik.tasks.apps import PRIORITY_HIGH from authentik.tasks.middleware import CurrentTask -from authentik.tasks.models import Task from authentik.tasks.schedules.models import Schedule from authentik.tenants.models import Tenant @@ -191,10 +189,7 @@ def check_blueprint_v1_file(blueprint: BlueprintFile): @actor(description=_("Apply single blueprint.")) def apply_blueprint(instance_pk: UUID): - try: - self = CurrentTask.get_task() - except CurrentTaskNotFound: - self = Task() + self = CurrentTask.get_task() self.set_uid(str(instance_pk)) instance: BlueprintInstance | None = None try: diff --git a/authentik/root/test_runner.py b/authentik/root/test_runner.py index 2a9bbf5653..96fe6daff3 100644 --- a/authentik/root/test_runner.py +++ b/authentik/root/test_runner.py @@ -96,6 +96,9 @@ class PytestTestRunner(DiscoverRunner): # pragma: no cover def add_arguments(cls, parser: ArgumentParser): """Add more pytest-specific arguments""" DiscoverRunner.add_arguments(parser) + default_seed = None + if seed := os.getenv("CI_TEST_SEED"): + default_seed = int(seed) parser.add_argument( "--randomly-seed", type=int, @@ -103,6 +106,7 @@ class PytestTestRunner(DiscoverRunner): # pragma: no cover "to reuse the seed from the previous run." "Default behaviour: use random.Random().getrandbits(32), so the seed is" "different on each run.", + default=default_seed, ) parser.add_argument( "--no-capture",