Files
mtusi2026lab2/internal/testenv/testenv.go
T

129 lines
3.3 KiB
Go

// Package testenv starts disposable PostgreSQL and Valkey containers for tests
// that exercise the real stores. Only _test files import it, so it never
// reaches the server binary. The images match compose.yaml.
package testenv
import (
"context"
"fmt"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"github.com/testcontainers/testcontainers-go"
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey"
"messenger/internal/platform/postgres"
)
const (
PostgresImage = "postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873"
ValkeyImage = "valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11"
)
// DB is a migrated PostgreSQL instance.
type DB struct {
Pool *pgxpool.Pool
DSN string
container *tcpostgres.PostgresContainer
}
func StartPostgres(ctx context.Context) (*DB, error) {
c, err := tcpostgres.Run(ctx, PostgresImage,
tcpostgres.WithDatabase("messenger"),
tcpostgres.WithUsername("messenger"),
tcpostgres.WithPassword("messenger"),
tcpostgres.BasicWaitStrategies(),
)
if err != nil {
return nil, fmt.Errorf("start postgres: %w", err)
}
db := &DB{container: c}
db.DSN, err = c.ConnectionString(ctx, "sslmode=disable")
if err != nil {
db.Close()
return nil, fmt.Errorf("postgres address: %w", err)
}
db.Pool, err = postgres.NewPool(ctx, db.DSN)
if err != nil {
db.Close()
return nil, err
}
if err := postgres.Migrate(ctx, db.Pool); err != nil {
db.Close()
return nil, err
}
return db, nil
}
// Reset empties every application table, so each test starts from an empty
// schema. The table list comes from the catalog: a table added by a later
// migration cannot be forgotten here.
func (d *DB) Reset(t testing.TB) {
t.Helper()
ctx := context.Background()
var tables string
err := d.Pool.QueryRow(ctx, `
SELECT coalesce(string_agg(format('%I.%I', schemaname, tablename), ', '), '')
FROM pg_tables
WHERE schemaname = 'public' AND tablename <> 'goose_db_version'`).Scan(&tables)
if err != nil {
t.Fatalf("list tables: %v", err)
}
if tables == "" {
return
}
if _, err := d.Pool.Exec(ctx, "TRUNCATE "+tables+" CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
}
func (d *DB) Close() {
if d.Pool != nil {
d.Pool.Close()
}
_ = testcontainers.TerminateContainer(d.container)
}
// KV is a Valkey instance without persistence or password.
type KV struct {
Client *redis.Client
URL string
container *tcvalkey.ValkeyContainer
}
func StartValkey(ctx context.Context) (*KV, error) {
c, err := tcvalkey.Run(ctx, ValkeyImage)
if err != nil {
return nil, fmt.Errorf("start valkey: %w", err)
}
kv := &KV{container: c}
kv.URL, err = c.ConnectionString(ctx)
if err != nil {
kv.Close()
return nil, fmt.Errorf("valkey address: %w", err)
}
opts, err := redis.ParseURL(kv.URL)
if err != nil {
kv.Close()
return nil, fmt.Errorf("valkey url: %w", err)
}
kv.Client = redis.NewClient(opts)
return kv, nil
}
func (k *KV) Reset(t testing.TB) {
t.Helper()
if err := k.Client.FlushDB(context.Background()).Err(); err != nil {
t.Fatalf("flushdb: %v", err)
}
}
func (k *KV) Close() {
if k.Client != nil {
_ = k.Client.Close()
}
_ = testcontainers.TerminateContainer(k.container)
}