Harden integration tests against the post-boot Moodle login race#74
Merged
Conversation
The Docker-backed integration suite intermittently failed at fixture setup with 'Moodle login failed: invalid username or password', on whichever test a pytest-xdist worker ran first. This is a transient race: right after a freshly-started Moodle container begins serving its login page, the very first login attempt sometimes fails while Moodle finishes initializing caches/sessions; a moment later the same credentials work (which is why reruns always went green). Add a session-scoped, autouse warmup fixture that performs a retrying login (up to 6 attempts, 5s apart) once per session -- i.e. once per xdist worker -- before any integration test runs. A successful warmup means the Moodle server is warm, so every subsequent per-test (new-session) login is reliable. It is a no-op unless --integration is passed, so unit runs are unaffected. No library/CLI change; this only touches the integration test harness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates the recurring integration-test flake where fixture setup fails with
Moodle login failed: invalid username or password. Adds a session-scoped, autouse login warmup (retry) fixture so a freshly-booted Moodle is warm before any test logs in. Integration-harness only; no library/CLI change.Problem
Right after a fresh Moodle container starts serving
/login/index.php, the first login attempt intermittently fails with "invalid username or password" while Moodle finishes initializing (caches/sessions); a moment later the same credentials work — which is exactly why every occurrence went green on rerun. Underpytest-xdist -n autothis hit whichever test a worker ran first (observed ontest_create_and_delete_course,test_add_assign,test_list_courses, …), making the whole suite flaky and forcing repeated CI reruns.Fix
A
scope="session", autouse=Truefixture (_warm_up_moodle_login) intests/conftest.pythat, only when--integrationis active, performs a retrying warmup login (up to 6 attempts, 5s apart) once per session — i.e. once per xdist worker — before tests run. Once the warmup login succeeds the Moodle server is warm, so every subsequent per-test (new-session) login is reliable. If login never succeeds within the budget itpytest.exits with a clear message (a genuinely-down Moodle still fails fast-ish, not silently).Test plan
pytest tests/unit— green; the warmup fixture is a no-op without--integration, so unit runs are unaffected.make lint(black/isort/flake8) — clean.Backwards compatibility
No change to any library or CLI behavior; only the integration test harness. Unit tests, and any environment without
--integration, are unaffected.Notes for reviewers
This is the counterpart to #62 (which serialized course creation across workers) and #73 (ephemeral CI containers + boot diagnostics): together they target the main sources of integration flakiness. The warmup absorbs the login race specifically. Retry budget (6×5s) is generous but only spends time when the race is actually occurring — a first-try success is instant.