40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
)
|
|
|
|
// 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()
|
|
container, err := postgres.Run(ctx, "postgres:16-alpine",
|
|
postgres.WithDatabase("dns_ar_test"),
|
|
postgres.WithUsername("test"),
|
|
postgres.WithPassword("test"),
|
|
testcontainers.WithWaitStrategy(
|
|
wait.ForLog("database system is ready to accept connections").
|
|
WithOccurrence(2).WithStartupTimeout(60*time.Second)),
|
|
)
|
|
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
|
|
}
|