Skip to content

fix(tests): skip database-backed tests when Postgres is unreachable (#150) - #151

Merged
rrskris merged 1 commit into
kaaval:mainfrom
HeaTTap:fix/smoke-test-skip-unreachable-db
Aug 13, 2026
Merged

rrskris merged 1 commit into
kaaval:mainfrom
HeaTTap:fix/smoke-test-skip-unreachable-db

Conversation

@HeaTTap

@HeaTTap HeaTTap commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Closes #150

Problem

When running pytest tests/ locally without a running Postgres database, tests/test_smoke.py fails with a red stack trace (sqlalchemy.exc.OperationalError: Connection refused), surprising first-time contributors.

Solution

  1. DB Reachability Probe & Skip Fixture: Added a database reachability check (SELECT 1) in control-plane/tests/test_smoke.py. If Postgres is unreachable and KAAVAL_REQUIRE_DB is not set to "1", pytest skips with:
    "no reachable Postgres (set DATABASE_URL; see CONTRIBUTING.md)"
  2. CI Enforcement Gate: Set KAAVAL_REQUIRE_DB: "1" in .github/workflows/ci.yml so an unreachable database in CI fails loudly rather than skipping silently.
  3. Module Load Guard: Wrapped top-level create_all in tests/test_error_surfacing.py and added DB reachability checks for database-dependent feed tests.

Verification

  • Running pytest tests/ without Postgres locally: 127 passed, 4 skipped.
  • Running KAAVAL_REQUIRE_DB=1 pytest tests/test_smoke.py without Postgres: fails loudly (1 failed).
  • Commit signed off with DCO (git commit -s).

@rrskris
rrskris merged commit 48b91ad into kaaval:main Aug 13, 2026
7 checks passed
@rrskris

rrskris commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Merged — thanks @HeaTTap, and sorry this sat for a week. That was my fault, not yours: your first PR from a fork needs a maintainer to approve the workflow runs before CI will start, and I didn't notice it was sitting in that state. Nothing was wrong with the PR the whole time it looked stalled.

I verified both directions rather than take the description's word for it, and both hold:

  • No Postgres, no env var → 154 passed, 4 skipped, no stack trace.
  • No Postgres, KAAVAL_REQUIRE_DB=1 → test_smoke.py::test_login_and_self_scan fails loudly with the OperationalError.

That second one is the part that makes this a real fix rather than a papered-over one — a skip-on-unreachable guard that also silences CI would have been worse than the original problem. Setting the env var in ci.yml in the same PR is what I'd have asked for if it weren't already there.

Three things I'd tidy in a follow-up, none of them blockers:

  1. _is_database_reachable() is duplicated in both test modules — it belongs in conftest.py.
  2. The two files guard differently: test_smoke.py uses an autouse fixture, test_error_surfacing.py repeats a 3-line inline guard in three tests. The fixture approach is the better one; worth applying to both.
  3. models.Base.metadata.create_all(...) is now wrapped in a bare except Exception: pass. That's right for the unreachable-DB case, but it also swallows a genuine schema error in CI, where you'd want it surfaced. Narrowing it to the connection error, or re-raising when KAAVAL_REQUIRE_DB=1, would keep the loud-in-CI property intact for that path too.

If you'd like to take any of those, say so and I'll assign it — (3) is the one with actual substance. Otherwise I'll file it.

Good first contribution: you found the pain point, fixed it, and closed the CI hole it would have opened.

rrskris added a commit that referenced this pull request Aug 13, 2026
Adds release-notes entries for the five contributor PRs merged today:
Grype ingestion (#123, @Diyaaa-12), combo-escalation remediation and rule
catalog (#133, @Maqbool61), the combination-takeover fixture (#152,
@Maqbool61), the Postgres-unreachable test skip (#151, @HeaTTap — first
contribution), and the CIS 5.1.6 acceptance check moving into CI (#124,
@donkk11). Also records the fixture-comment correction from #157.

Signed-off-by: Vamshi Krishna Santhapuri <[email protected]>
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.

test_smoke.py: skip with a clear reason when Postgres is unreachable

2 participants