test(coverage): drive backend/postgres to 99.5% (every func >=95%)#28
Merged
Conversation
Add test seams to backend/postgres so the SQL/HTTP/k8s code paths can be
exercised in the mock-only coverage CI job (no live cluster). Production
defaults are unchanged — every seam defaults to the real function/value:
- pgConn interface + pgxConnect seam (wraps pgx.Connect) lets a fake conn
drive the CREATE DATABASE / CREATE USER / ALTER ROLE happy paths and each
Exec/QueryRow/Close error branch deterministically.
- randInt / randRead seams cover the crypto/rand-failure branches in
generatePassword and k8sRandHex.
- jsonMarshal / httpNewRequestWithContext / ioReadAll seams cover the
Neon/dedicated request-construction and read-body error wraps.
- k8sReadyTimeout/Interval + namespace-terminate timers and the cluster
router pollInterval become test-shrinkable (var/field, not const) so the
pod-ready timeout, terminating-namespace, and ticker branches run in ms.
- fake.Clientset (+ create-Service ClusterIP reactor and a preloaded Ready
pod) drives the full K8sBackend.Provision happy path, all apply* helpers,
rollback branches, StorageBytes/Regrade/Deprovision, and the route registry.
Whole-package coverage is 99.5%; every function is >= 95%. make gate is green
and the suite is clean under -race x5.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
bfc8b9b to
84e6407
Compare
…seam race The coverage push to 99.6% added seam-driven tests that swap the global pgxConnect var. ClusterRouter.Shutdown only closed `done` and returned without waiting for pollLoop to exit, so a poll goroutine could still be mid-pgxConnect after a test restored the seam — racing later tests. CI's build-and-test (go test ./... -race, no docker pg) flaked on TestLocalBackend_Provision_Success with a stale-DSN dial; the local docker pg on :5432 masked it. Shutdown now joins the poll goroutine via a sync.WaitGroup (Add in Start, Done in the Start closure so direct pollLoop callers are unaffected), making it a true barrier: no router goroutine touches pgxConnect after Shutdown returns. Production behaviour is strictly improved (clean teardown). Tests that start a router now install a fast seam / defer Shutdown so the join is deterministic. Verified: 5x go test -race with no pg env all green; coverage holds at 99.6% (every func >=95%). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Closes the residual coverage gap on
provisioner/internal/backend/postgres. PR #22 raised it to 91.6% but stopped at the SQL/HTTP/rand happy paths the author judged "unreachable without test seams." Per the new org policy (≥95%, no exceptions), this PR adds those seams and lands the package at 99.6%, every function ≥95%. Built on top of #22 — additive, no existing tests removed.Production behaviour is unchanged: every seam defaults to the real function/value; the shrinkable timers/intervals become
var/struct-field instead ofconst, defaulting to the original production durations.What the seams add (the missing ~8.4%)
pgConninterface +pgxConnect(wrapspgx.Connect) → drives CREATE DATABASE / CREATE USER / ALTER ROLE success paths and each Exec/QueryRow/Close error branch deterministically, with no live Postgres (the coverage CI job is mock-only). test(postgres): raise backend/postgres coverage to 91.6% #22's tests could only reach the connect-fails-then-rollback path; the seam reaches the rest.randInt/randReadseams → thecrypto/rand-failure branches ingeneratePasswordandk8sRandHex.jsonMarshal/httpNewRequestWithContext/ioReadAllseams → the Neon + dedicated request-construction and read-body error wraps.k8sReadyTimeout/Interval+ namespace-terminate timers + cluster-routerpollInterval(per-instance field, no shared global) → the pod-ready timeout, terminating-namespace recreate, and poll-ticker branches run in milliseconds.fake.Clientset(+ a create-Service ClusterIP reactor and a preloaded Ready pod) → the fullK8sBackend.Provisionhappy path + allapply*helpers + every rollback branch.Coverage
go tool cover -func | awk '<95'is empty).kubernetes.NewForConfigfailure) covered via a kubeconfig with an unknown auth-provider that parses but fails client construction.Test plan
make gategreen (build + vet +go test ./... -short— matches deploy.yml)go test ./internal/backend/postgres -short -coverprofile→ 99.6%, 0 funcs <95%-race -count=2clean (no leaked-goroutine / shared-seam races; pollers join via done channels)Note: the
scan / osv-scancheck fails on a pre-existing, unrelated infra issue (the osv-scanner action pins Go 1.23.7 while go.mod requires ≥1.25.0); not caused by this PR.🤖 Generated with Claude Code