TestMain scanned os.Args for a literal -test.short, but go test passes -test.short=true, so beachcomber, conga, lagoon and lighthouse started a Postgres container even under -short. Parse the flags and use testing.Short() instead; skip cabana's newListService fixture under -short like adminGorm already does.
139 lines
3.0 KiB
Go
139 lines
3.0 KiB
Go
package lagoon
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"flag"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/testcontainers/testcontainers-go"
|
|
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
|
)
|
|
|
|
var (
|
|
lagoonPG *postgres.PostgresContainer
|
|
lagoonSQL *sql.DB
|
|
lagoonDSN string
|
|
lagoonPGErr error
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
code := 1
|
|
if !testShort() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
lagoonPGErr = startLagoonPostgres(ctx)
|
|
cancel()
|
|
if lagoonPGErr != nil {
|
|
fmt.Fprintf(os.Stderr, "lagoon: testcontainers postgres: %v\n", lagoonPGErr)
|
|
stopLagoonPostgres()
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
code = m.Run()
|
|
stopLagoonPostgres()
|
|
os.Exit(code)
|
|
}
|
|
|
|
// testShort reports whether -short was passed. TestMain runs before the
|
|
// testing flags are parsed, and go test passes the flag as -test.short=true,
|
|
// so parse the flags here instead of scanning os.Args.
|
|
func testShort() bool {
|
|
if !flag.Parsed() {
|
|
flag.Parse()
|
|
}
|
|
return testing.Short()
|
|
}
|
|
|
|
func startLagoonPostgres(ctx context.Context) error {
|
|
ctr, err := postgres.Run(ctx,
|
|
"postgres:16-alpine",
|
|
postgres.WithDatabase("lagoon"),
|
|
postgres.WithUsername("lagoon"),
|
|
postgres.WithPassword("lagoon"),
|
|
postgres.BasicWaitStrategies(),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lagoonPG = ctr
|
|
dsn, err := ctr.ConnectionString(ctx, "sslmode=disable")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
db, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := db.PingContext(ctx); err != nil {
|
|
_ = db.Close()
|
|
return err
|
|
}
|
|
lagoonSQL = db
|
|
lagoonDSN = dsn
|
|
return nil
|
|
}
|
|
|
|
func stopLagoonPostgres() {
|
|
if lagoonSQL != nil {
|
|
_ = lagoonSQL.Close()
|
|
}
|
|
if lagoonPG != nil {
|
|
_ = testcontainers.TerminateContainer(lagoonPG)
|
|
}
|
|
}
|
|
|
|
func lagoonDB(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
if testing.Short() {
|
|
t.Skip("requires testcontainers postgres")
|
|
}
|
|
if lagoonPGErr != nil {
|
|
t.Fatalf("postgres unavailable: %v", lagoonPGErr)
|
|
}
|
|
if lagoonSQL == nil {
|
|
t.Fatal("postgres unavailable: container was not started")
|
|
}
|
|
return lagoonSQL
|
|
}
|
|
|
|
func dedicatedDB(t *testing.T, name string) (*sql.DB, string) {
|
|
t.Helper()
|
|
admin := lagoonDB(t)
|
|
ctx := t.Context()
|
|
quoted := `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
|
|
if _, err := admin.ExecContext(ctx, `CREATE DATABASE `+quoted+` TEMPLATE template0 ENCODING 'UTF8'`); err != nil && !strings.Contains(err.Error(), "already exists") {
|
|
t.Fatalf("create %s: %v", name, err)
|
|
}
|
|
dsn, err := dsnWithDB(lagoonDSN, name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = db.Close()
|
|
_, _ = admin.ExecContext(context.Background(), `DROP DATABASE IF EXISTS `+quoted+` WITH (FORCE)`)
|
|
})
|
|
if err := db.PingContext(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return db, dsn
|
|
}
|
|
|
|
func dsnWithDB(dsn, name string) (string, error) {
|
|
u, err := url.Parse(dsn)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
u.Path = "/" + name
|
|
return u.String(), nil
|
|
}
|