Files
anchorage/test/helpers_integration_test.go
William Gill 12bf35caf8 anchorage v1.0 initial tree
Greenfield Go multi-tenant IPFS Pinning Service wire-compatible with the
IPFS Pinning Services API spec. Paired 1:1 with Kubo over localhost RPC,
clustered via embedded NATS JetStream, Postgres source-of-truth with
RLS-enforced tenancy, Fiber + huma v2 for the HTTP surface, Authentik
OIDC for session login with kid-rotated HS256 JWT API tokens.

Feature-complete against the 22-milestone build plan, including the
ship-it v1.0 gap items:

  * admin CLIs: drain/uncordon, maintenance, mint-token, rotate-key,
    prune-denylist, rebalance --dry-run, cache-stats, cluster-presences
  * TTL leader election via NATS KV, fence tokens, JetStream dedup
  * rebalancer (plan/apply split), reconciler, requeue sweeper
  * ristretto caches with NATS-backed cross-node invalidation
    (placements live-nodes + token denylist)
  * maintenance watchdog for stuck cluster-pause flag
  * Prometheus /metrics with CIDR ACL, HTTP/pin/scheduler/cache gauges
  * rate limiting: session (10/min) + anonymous global (120/min)
  * integration tests: rebalance, refcount multi-org, RLS belt
  * goreleaser (tar + deb/rpm/apk + Alpine Docker) targeting Gitea

Stack: Cobra/Viper, Fiber v2 + huma v2, embedded NATS JetStream,
pgx/sqlc/golang-migrate, ristretto, TypeID, prometheus/client_golang,
testcontainers-go.
2026-04-16 18:13:36 -05:00

112 lines
3.4 KiB
Go

//go:build integration
package test
import (
"context"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
"anchorage/internal/pkg/store/postgres"
)
// startPostgres brings up a disposable Postgres container seeded with
// anchorage's schema via the same migrations that the binary runs on
// boot. Shared by every *_integration_test.go in this package.
func startPostgres(t *testing.T) (*postgres.Store, func()) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
container, err := tcpostgres.Run(ctx, "postgres:17-alpine",
tcpostgres.WithDatabase("anchorage"),
tcpostgres.WithUsername("anchorage"),
tcpostgres.WithPassword("test-password"),
tcpostgres.BasicWaitStrategies(),
tcpostgres.WithSQLDriver("pgx"),
)
if err != nil {
t.Fatalf("pg container: %v", err)
}
dsn, err := container.ConnectionString(ctx, "sslmode=disable")
if err != nil {
t.Fatalf("dsn: %v", err)
}
if err := postgres.MigrateDSN(ctx, dsn, postgres.MigrateUp, postgres.MigrateOptions{}); err != nil {
_ = container.Terminate(context.Background())
t.Fatalf("migrate: %v", err)
}
pool, err := postgres.NewPool(ctx, postgres.PoolConfig{DSN: dsn, MaxConns: 5})
if err != nil {
_ = container.Terminate(context.Background())
t.Fatalf("pool: %v", err)
}
pingCtx, pingCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer pingCancel()
if err := pool.Ping(pingCtx); err != nil {
pool.Close()
_ = container.Terminate(context.Background())
t.Fatalf("ping: %v", err)
}
cleanup := func() {
pool.Close()
_ = container.Terminate(context.Background())
}
return postgres.New(pool), cleanup
}
// startPostgresWithPool is like startPostgres but also returns the raw
// pgxpool so tests that need to bypass the Store abstraction (e.g., RLS
// belt test) can run SQL directly.
func startPostgresWithPool(t *testing.T) (*postgres.Store, *pgxpool.Pool, func()) {
t.Helper()
// This reuses startPostgres and digs the pool out — but the Store
// doesn't expose its pool. We redo the construction here so the raw
// pool is available.
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
container, err := tcpostgres.Run(ctx, "postgres:17-alpine",
tcpostgres.WithDatabase("anchorage"),
tcpostgres.WithUsername("anchorage"),
tcpostgres.WithPassword("test-password"),
tcpostgres.BasicWaitStrategies(),
tcpostgres.WithSQLDriver("pgx"),
)
if err != nil {
t.Fatalf("pg container: %v", err)
}
dsn, err := container.ConnectionString(ctx, "sslmode=disable")
if err != nil {
t.Fatalf("dsn: %v", err)
}
if err := postgres.MigrateDSN(ctx, dsn, postgres.MigrateUp, postgres.MigrateOptions{}); err != nil {
_ = container.Terminate(context.Background())
t.Fatalf("migrate: %v", err)
}
pool, err := postgres.NewPool(ctx, postgres.PoolConfig{DSN: dsn, MaxConns: 5})
if err != nil {
_ = container.Terminate(context.Background())
t.Fatalf("pool: %v", err)
}
pingCtx, pingCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer pingCancel()
if err := pool.Ping(pingCtx); err != nil {
pool.Close()
_ = container.Terminate(context.Background())
t.Fatalf("ping: %v", err)
}
cleanup := func() {
pool.Close()
_ = container.Terminate(context.Background())
}
return postgres.New(pool), pool, cleanup
}