Skip to content

Data-layer tests against Postgres, and the fixes they found - #199

Open
babourine wants to merge 50 commits into
patterninc:mainfrom
babourine:stan_test_db
Open

babourine wants to merge 50 commits into
patterninc:mainfrom
babourine:stan_test_db

Conversation

@babourine

Copy link
Copy Markdown
Contributor

Stacked on #198. Until #197 and #198 merge, this diff includes them too. Review from the commit that adds make test-integration; everything before it belongs to those PRs.

What this does

It gives the data layer real tests: each test runs against a fresh Postgres database. Then it fixes what those tests turned up. Every fix lands with the test that proves it, so each commit can be reviewed on its own, in order.

Before you deploy: one behaviour change needs a config check

Routing tags are now required. That covers tags and cluster_tags on commands, tags on clusters, and command_criteria and cluster_criteria on jobs.

An empty set used to match everything. Then it failed in one of three ways: a SQL syntax error (500), a panic (when the tags were left out entirely), or, for a sync job, the job ran but was never recorded. Routing should be intentional. If you really want "any cluster", give every cluster the same tag.

  • Over the API: a missing or empty set returns a 400 and nothing is stored.
  • At startup: a config with an untagged command or cluster fails to start. The error names the object, for example command ping-0.0.1: routing tags are required: cluster_tags.

Please run heimdall validate -conf <your config> on production configs before deploying. It runs the same check the server runs at startup.

Running the tests

make db-up # compose Postgres on :5433
make test-integration # drops and recreates heimdall_test, then runs every test against it

  • Opt-in: DB tests skip unless HEIMDALL_TEST_DATABASE is set. So make go-test and plain go test behave exactly as before.
  • Narrowing: PKG=./internal/pkg/heimdall/... limits the run.
  • Your dev data is safe: the target only touches the database named heimdall_test.
  • Upstream CI doesn't run these yet, because build.yml has no job for them. Happy to add one as a follow-up; it needs a Postgres service and one make call.

Commits

Test infrastructure

  1. make test-integration and a test/pkg/testdb helper.
    • The helper deploys the schema once per test binary. It takes an advisory lock, because go test ./... runs packages in parallel against one database.
    • The schema deploy is now tested as repeatable.
    • The janitor's DB test now uses the helper. It had been skipping silently: the env var it read didn't match the one CI set.
  2. Command and cluster DAL tests. They are written as mirror images, because the two DALs should behave the same.
  3. Job DAL tests:
    • insert and read-back, status updates, cancel from every status, list filters;
    • keyset paging over every sort, including ties on created_at;
    • unit tests for the cursor helpers, which run without a database.

Fixes (each turns a skipped TestWant… into a regular test)

Fix Before After Proved by
Malformed body, query value or cursor 500 400 TestMalformedBodyReturns400, e2e TestBadCursorReturns400
Missing or empty routing tags 500 or panic 400, or a startup error TestMissingRoutingTagsReturn400, TestValidateRejectsUntaggedCatalog
Re-registering a cluster with a new version kept the old version updates it (commands always did) TestClusterUpsert
Unknown job, command or cluster id, or job file 500 404 e2e TestNotFoundReturns404
Cancelling a finished job 500 409 (unknown job: 404) e2e TestCancelFinishedJobConflicts
Job criteria that match no command or cluster 500 400 TestUnroutableJobReturns400
Health check on a cluster with checks off 500 400 TestClusterHealth

TestStatusForError pins the full error-to-status mapping. Client-side errors now start with bad request: , as cursor errors already did. Nothing in the repo, including the UI, matches on the old messages.

Still open (skipped TestWant… tests, each with the proposed fix in its comment)

  • TestWantSubmitJobIgnoresClientStatus: a client can submit a job with "status": 6 and it's stored as SUCCEEDED.
  • TestWantCancelOnlyByOwner: any caller can cancel any job.
  • TestWantSyncJobRecordedBeforeReturn: a sync job is inserted in a goroutine after submit returns, and an insert error is dropped.
  • TestWantWrongMethodReturns405: a wrong method returns 404.
  • TestWantPayloadLatencyPerRequest: the payload_handler latency metric doesn't measure each request.
  • TestWantSensitiveKeysConcurrentSafe: the sensitive-keys registry isn't safe for concurrent use.
  • TestWantRedactsCommonSecretNames: redaction doesn't cover common secret names such as *_password, *_token, *_secret and *_key.

babourine and others added 30 commits November 29, 2026 19:46
…all so forks dont havbe this workflow failing
…eave stale binaries and plugins'

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…tion so Init() call remains just a read call and does not change anything (support of validate and print commands)
…le different modes of operation -- running different containers on demand
* Add fork CI (stan-ci)

* make step blocking

---------

Co-authored-by: Stan Babourine <2166030+babourine@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 6, 2026 01:47
@wiz-55ccc8b716

wiz-55ccc8b716 Bot commented Dec 6, 2026

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities 1 Medium
Data Finding Sensitive Data -
Secret Finding Secrets 5 Low 1 Info
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings -
Software Management Finding Software Management Findings -
Total 1 Medium 5 Low 1 Info

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Malformed request bodies still bypass decoding and can persist zero-value statuses instead of returning HTTP 400.

4 open issues
1 file not reviewed
  • web/pnpm-lock.yaml: Generated file
What changed in this PR

Adds opt-in Postgres integration tests for Heimdall’s data layer and fixes the validation, persistence, and HTTP error handling they exposed. The stacked diff also includes build tooling and API tests from #197 and #198.

Changes:

  • Adds database, API, and unit coverage with repeatable schema deployment.
  • Requires explicit routing tags and corrects cluster updates and error responses.
  • Adds Make-based workflows, configuration redaction, and container updates.
File Description
web/​src/​components/​ListPage/​__snapshots__/​ListPage.test.tsx.snap Refreshes list-page snapshots.
web/​src/​components/​DetailPage/​__snapshots__/​DetailPage.test.tsx.snap Refreshes detail-page snapshots.
web/​Makefile Adds web development targets.
test/​pkg/​testdb/​testdb.go Adds shared schema-deployment helper.
test/​e2e/​want_test.go Records deferred behavior tests.
test/​e2e/​sync_test.go Tests synchronous jobs.
test/​e2e/​routing_test.go Tests routing and missing resources.
test/​e2e/​jobs_list_test.go Tests filters, pagination, and cursors.
test/​e2e/​job_test.go Tests asynchronous job lifecycle.
test/​e2e/​health_test.go Tests cluster health endpoints.
test/​e2e/​client_test.go Adds HTTP test helpers.
test/​e2e/​catalog_test.go Tests catalog reads and statuses.
test/​e2e/​cancel_test.go Tests cancellation and conflicts.
test/​e2e/​admin_test.go Tests catalog registration and updates.
scripts/​ci/​smoke.sh Adds stack smoke checks.
scripts/​ci/​coverage.sh Adds coverage reporting and checks.
scripts/​ci/​coverage-baseline.txt Records coverage baseline.
README.md Documents Make and CLI usage.
pkg/​plugin/​auth.go Documents authentication contract.
pkg/​object/​status/​status_test.go Tests object status serialization.
pkg/​object/​object.go Adds required-tag validation helper.
pkg/​object/​object_test.go Tests object initialization.
pkg/​object/​job/​status/​status_test.go Tests job status serialization.
pkg/​object/​job/​job.go Validates routing criteria.
pkg/​object/​job/​job_test.go Tests initialization and criteria validation.
pkg/​object/​command/​command.go Requires command routing tags.
pkg/​object/​command/​command_test.go Tests caller rules and tags.
pkg/​object/​cluster/​cluster.go Requires cluster routing tags.
pkg/​object/​cluster/​cluster_test.go Tests required cluster tags.
pkg/​duration/​duration_test.go Tests duration parsing and serialization.
pkg/​context/​context.go Redacts YAML context output.
pkg/​context/​context_test.go Tests redaction and secret preservation.
Makefile Adds build, test, and development workflows.
internal/​pkg/​server/​server.go Adds request-body limit setting.
internal/​pkg/​rbac/​ranger/​print_test.go Tests Ranger password redaction.
internal/​pkg/​rbac/​ranger/​client.go Redacts passwords in YAML.
internal/​pkg/​object/​command/​starrocks/​job_context.go Removes a blank line.
internal/​pkg/​object/​command/​starrocks/​backend_client_pool.go Adds deprecation lint suppression.
internal/​pkg/​object/​command/​starrocks/​arrow_types.go Adds deprecation lint suppression.
internal/​pkg/​object/​command/​sparkeks/​sparkeks.go Adds upload deprecation suppressions.
internal/​pkg/​object/​command/​spark/​spark.go Adds upload deprecation suppressions.
internal/​pkg/​object/​command/​postgres/​postgres.go Removes unused mutex and wraps errors.
internal/​pkg/​object/​command/​ecs/​ecs.go Removes unused client field and import.
internal/​pkg/​janitor/​stale_jobs_test.go Uses database test helper.
internal/​pkg/​heimdall/​validate_test.go Tests startup routing validation.
internal/​pkg/​heimdall/​stdout_test.go Adds output and metric test helpers.
internal/​pkg/​heimdall/​queries/​cluster/​upsert.sql Updates version during cluster upsert.
internal/​pkg/​heimdall/​print_test.go Tests printed configuration redaction.
internal/​pkg/​heimdall/​keepalive.go Relocates default constant.
internal/​pkg/​heimdall/​job.go Validates routing and classifies job errors.
internal/​pkg/​heimdall/​job_dal.go Marks invalid cursors as client errors.
internal/​pkg/​heimdall/​job_dal_test.go Tests job persistence and pagination.
internal/​pkg/​heimdall/​job_cursor_test.go Tests cursor and seek helpers.
internal/​pkg/​heimdall/​heimdall.go Refactors CLI lifecycle and exact routing.
internal/​pkg/​heimdall/​health.go Classifies disabled checks as client errors.
internal/​pkg/​heimdall/​handler.go Adds body limits and error mapping.
internal/​pkg/​heimdall/​handler_test.go Tests parameter precedence and error statuses.
internal/​pkg/​heimdall/​dal_helpers_test.go Adds database fixture helpers.
internal/​pkg/​heimdall/​command_dal.go Validates tags before command writes.
internal/​pkg/​heimdall/​command_dal_test.go Tests command persistence.
internal/​pkg/​heimdall/​cluster_dal.go Validates tags before cluster writes.
internal/​pkg/​heimdall/​cluster_dal_test.go Tests cluster persistence.
internal/​pkg/​heimdall/​auth.go Rejects authentication plugin errors.
internal/​pkg/​database/​redact.go Redacts database connection passwords.
internal/​pkg/​database/​redact_test.go Tests connection-string redaction.
internal/​pkg/​database/​filter.go Removes shared template parsing state.
internal/​pkg/​database/​filter_test.go Tests rendering and concurrent use.
internal/​pkg/​database/​deploy_test.go Tests repeatable schema deployment.
internal/​pkg/​aws/​s3.go Adds upload deprecation suppression.
internal/​pkg/​auth/​auth.go Adjusts import grouping.
go.mod Updates Go and dependencies.
entrypoint.sh Uses new binary paths and server command.
Dockerfile Builds through Make targets.
docker-compose.yaml Adds health checks and build metadata.
configs/​local.yaml Updates plugin paths and test fixtures.
configs/​local-cli.yaml Adds host-based development configuration.
cmd/​heimdall/​heimdall.go Delegates CLI execution to Heimdall.
.github/​workflows/​build.yml Uses Make checks, builds, and tests.

Review effort: Balanced

@@ -84,7 +111,7 @@ func payloadHandler[T any](fn func(context.Context, *T) (any, error)) http.Handl
var payload T
if len(data) > 2 {
}
if body == nil {
writeAPIError(w, fmt.Errorf(formatFileNotFound, filename), nil)
writeAPIError(w, fmt.Errorf("%w: %s", ErrFileNotFound, filename), nil)
Comment on lines +310 to +311
for i, createdAt := range []int{100, 100, 100, 200, 200, 300, 300} {
id := string(rune('a' + i))
Comment thread test/pkg/testdb/testdb.go
Comment on lines +32 to +52
deployErr error
)

// Open returns the test database, deploying the schema on the first call in this test binary.
// It skips the test when HEIMDALL_TEST_DATABASE is not set.
func Open(t *testing.T) *database.Database {

t.Helper()

connectionString := os.Getenv(envVar)
if connectionString == `` {
t.Skipf("%s is not set (run `make test-integration`)", envVar)
}

db := &database.Database{ConnectionString: connectionString}
deployOnce.Do(func() { deployErr = deploySchema(db) })
if deployErr != nil {
t.Fatalf("deploy schema: %v", deployErr)
}

return db
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