Opened 11 minutes ago
#37561 new Cleanup/optimization
createsuperuser tests infinite loop without a system username
| Reported by: | Mike Edmunds | Owned by: | |
|---|---|---|---|
| Component: | contrib.auth | Version: | dev |
| Severity: | Low | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
In tests/auth_tests/test_management.py, CreatesuperuserManagementCommandTestCase.test_default_username and MultiDBCreatesuperuserTestCase.test_createsuperuser_command_suggested_username_with_database_option assume a default username is available. If django.contrib.auth.management.get_default_username() returns an empty string, the tests will repeatedly attempt to pass a blank username to the createsuperuser command, which rejects it with Error: This field cannot be blank.. And they'll keep at it until someone kills the test run (or GitHub times it out).
One way this can occur is running Django's test suite in a container with a numeric UID that doesn't correspond to a known container user. (Using runtest.py's --noinput doesn't prevent the problem because the tests explicitly invoke the management command interactively.)
To reproduce with django-docker-box, using the non-existent UID 4321:
docker compose run --rm --user 4321:4321 sqlite \ --noinput auth_tests.test_management # Or with podman: podman-compose --podman-run-args='--passwd=false' \ run --rm --user 4321:4321 sqlite \ --noinput auth_tests.test_management
(Docker doesn't automatically create container users for unknown ids; podman does unless you specify --passwd=false.)
Results:
... Error: This field cannot be blank. Error: This field cannot be blank. Error: This field cannot be blank. Error: This field cannot be blank. Error: This field cannot be blank. Error: This field cannot be blank. Error: This field cannot be blank. ...
Expected: successful test run
Workaround: set LOGNAME in the environment so get_system_username() won't return blank. E.g.:
podman-compose --podman-run-args='--passwd=false' \ run --rm --user 4321:4321 --env LOGNAME=test sqlite \ --noinput auth_tests.test_management
(I'm not sure this is worth fixing, but am creating the issue to document the behavior and workaround. I ran into it while trying to get django-docker-box tests running in a GitHub workflow. AI disclosure: Junie/GPT-6.1 Terra assisted in finding the cause and workaround, and in clarifying behavioral differences between podman and docker.)