Files
dns-autoresolver/internal/store/testhelper_test.go
T

38 lines
1.1 KiB
Go

package store
import (
"context"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
)
// startPostgres spins up an ephemeral PostgreSQL, applies migrations,
// and returns its DSN. Container is terminated on test cleanup.
func startPostgres(t *testing.T) string {
t.Helper()
ctx := context.Background()
// BasicWaitStrategies включает ForLog(...)x2 И ForListeningPort — второй
// критичен на macOS/Windows Docker Desktop (иначе flaky first-run).
container, err := postgres.Run(ctx, "postgres:16-alpine",
postgres.WithDatabase("dns_ar_test"),
postgres.WithUsername("test"),
postgres.WithPassword("test"),
postgres.BasicWaitStrategies(),
)
if err != nil {
t.Fatalf("start postgres: %v", err)
}
t.Cleanup(func() { _ = testcontainers.TerminateContainer(container) })
dsn, err := container.ConnectionString(ctx, "sslmode=disable")
if err != nil {
t.Fatalf("dsn: %v", err)
}
if err := Migrate(ctx, dsn); err != nil {
t.Fatalf("migrate: %v", err)
}
return dsn
}