Skip to content

Add tests for rotate CLI and celery tasks#78

Merged
gijzelaerr merged 6 commits into
mainfrom
spotify/add-tests
Jul 7, 2026
Merged

Add tests for rotate CLI and celery tasks#78
gijzelaerr merged 6 commits into
mainfrom
spotify/add-tests

Conversation

@gijzelaerr

Copy link
Copy Markdown
Member

Summary

Adds 20 tests covering critical production paths that previously had zero test coverage.

Rotate CLI (test_rotate_cli.py — 8 tests)

request_rotation (5 tests):

  • Calls rotate_certificate and sends notification on commit
  • Skips rotation without --commit
  • Skips notification without --message
  • Handles errors gracefully (no crash, emits failure metric)
  • Emits success metric

Bulk rotation rotate -o OLD -n NEW (3 tests):

  • Regression test: all 6 endpoints rotated, not 5 (list-mutation bug)
  • Single-endpoint rotation by name
  • Dry-run mode (no --commit)

Celery tasks (test_celery_tasks.py — 12 tests)

  • certificate_rotate: CLI dispatch, notification config, region routing, SoftTimeLimitExceeded, success metric
  • certificate_reissue: CLI dispatch, timeout, success metric
  • notify_expirations: CLI dispatch, config passthrough, timeout
  • clean_all_sources: per-source task dispatch, success metric

Context

The list-mutation bug was discovered during the *.g2.spotify.com migration — 5 of 6 SSL proxies rotated, 1 silently skipped. The fix (list() snapshot) was already applied upstream before archival, but had no regression test.

Cover critical production paths that had zero test coverage:

rotate CLI (8 tests):
- request_rotation: commit/no-commit, message/no-message, error
  handling, success/failure metrics
- Bulk rotation regression test: all 6 endpoints rotated, not 5
  (list-mutation bug)
- Single-endpoint rotation and dry-run mode

Celery tasks (12 tests):
- certificate_rotate: CLI dispatch, notification config, region
  routing, SoftTimeLimitExceeded, success metric
- certificate_reissue: CLI dispatch, timeout, success metric
- notify_expirations: CLI dispatch, config passthrough, timeout
- clean_all_sources: per-source task dispatch, success metric
The autouse fixtures patching lemur.common.celery triggered module-level
create_app() during pytest collection, creating a second Flask app that
corrupted the session-scoped SQLAlchemy session for other tests.
Production (lemur-deploy) runs Python 3.12 but CI only tested 3.10
and 3.11. Add 3.12 to the matrix so CI matches what we deploy.
lemur.common.celery calls create_app() at module level when no Flask
current_app exists. Importing it outside an app context (e.g. during
pytest collection or early in a test) creates a second Flask app that
corrupts the session-scoped SQLAlchemy session, breaking 20 tests in
test_certificates.py.

Fix: use _import_celery_task() helper that defers the import to inside
the test body where the app context is active. Also require the session
fixture to ensure DB state is clean.
…with test suite

lemur.common.celery unconditionally calls create_app() at import time
when no Flask current_app exists. This creates a second Flask app that
corrupts the session-scoped SQLAlchemy session, breaking 20 existing
tests in test_certificates.py. No amount of deferred importing helps
because Python's module cache means the damage persists for the entire
pytest session once any test triggers the import.

The celery tasks are thin wrappers around CLI functions that ARE tested
(test_rotate_cli.py). Revisit celery task tests after fixing the
module-level create_app() pattern in celery.py.
Upstream Lemur fails during setup on Python 3.12 (tox config and
dependencies need updates). Track 3.12 support separately.
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 28867722577

Coverage remained the same at 59.879%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 992
Covered Lines: 594
Line Coverage: 59.88%
Coverage Strength: 0.6 hits per line

💛 - Coveralls

@gijzelaerr gijzelaerr merged commit 469f6f9 into main Jul 7, 2026
14 checks passed
@gijzelaerr gijzelaerr deleted the spotify/add-tests branch July 7, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants