package main import ( "context" "database/sql" "errors" "io" "log/slog" "os" "path/filepath" "strings" "testing" "time" "highload/db/migrations" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/stdlib" "github.com/pressly/goose/v3" ) func TestConnectionSecretErrorsAreSafe(t *testing.T) { for _, test := range []struct { name string content string missing bool }{ {name: "missing", missing: true}, {name: "empty", content: " \n\t"}, {name: "malformed", content: "postgres://synthetic:fixture-password@localhost/%zz"}, } { t.Run(test.name, func(t *testing.T) { path := filepath.Join(t.TempDir(), "database_url") if !test.missing { if err := os.WriteFile(path, []byte(test.content), 0600); err != nil { t.Fatal("cannot create test secret") } } t.Setenv("DATABASE_URL_FILE", path) logger := slog.New(slog.NewJSONHandler(io.Discard, nil)) err := run(context.Background(), "up", time.Second, logger) if err == nil { t.Fatal("invalid connection secret was accepted") } if strings.Contains(err.Error(), "fixture-password") || strings.Contains(err.Error(), "postgres://") { t.Fatal("migration error exposed a connection secret") } var classified *migrationError if !errors.As(err, &classified) || classified.Unwrap() == nil { t.Fatal("migration error lost its classification/cause") } }) } } func TestEmbeddedMigrationsAreVersionedAndCollectable(t *testing.T) { cfg, err := pgx.ParseConfig("postgres://synthetic@localhost/synthetic?sslmode=disable") if err != nil { t.Fatal("cannot configure test driver") } db := stdlib.OpenDB(*cfg) defer db.Close() // NewProvider collects sources without connecting to PostgreSQL. provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.Files) if err != nil { t.Fatal("embedded migration names/versions cannot be collected by Goose") } sources := provider.ListSources() if len(sources) == 0 { t.Fatal("migrator build contains no SQL") } var previous int64 for _, source := range sources { if source.Version <= previous { t.Fatal("embedded versions are not unique and increasing") } previous = source.Version data, err := migrations.Files.ReadFile(source.Path) if err != nil || !strings.Contains(string(data), "-- +goose Up") { t.Fatal("embedded migration is unavailable or has no Up section") } } } type deadlineProbe struct { deadline time.Time cancel error } func (*deadlineProbe) SessionLock(context.Context, *sql.Conn) error { return nil } func (p *deadlineProbe) SessionUnlock(ctx context.Context, _ *sql.Conn) error { p.deadline, _ = ctx.Deadline() p.cancel = ctx.Err() return nil } func TestUnlockHasBudgetEvenWithDetachedContext(t *testing.T) { original, cancel := context.WithCancel(context.Background()) cancel() probe := &deadlineProbe{} locker := boundedSessionLocker{probe} started := time.Now() if err := locker.SessionUnlock(context.WithoutCancel(original), nil); err != nil { t.Fatal("unlock delegation failed") } if probe.cancel != nil || probe.deadline.IsZero() || probe.deadline.Before(started) || probe.deadline.After(started.Add(6*time.Second)) { t.Fatal("detached cleanup did not receive a bounded, live context") } }