- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
140 lines
2.9 KiB
Go
140 lines
2.9 KiB
Go
package lagoon
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"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)
|
|
}
|
|
|
|
func testShort() bool {
|
|
for _, a := range os.Args {
|
|
if a == "-test.short" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
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(),
|
|
testcontainers.WithEnv(map[string]string{
|
|
"POSTGRES_INITDB_ARGS": "--locale-provider=icu --icu-locale=pl-PL --encoding=UTF8",
|
|
}),
|
|
)
|
|
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' LOCALE_PROVIDER icu ICU_LOCALE 'pl-PL'`); 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
|
|
}
|