tests: use testcontainers (#5341)

* tests: use testcontainers for postgres

* tests: use testcontainers for minio

* remove gcs test

* try installing docker

* skip docker on macos
This commit is contained in:
Caleb Doxsey 2024-10-30 13:33:30 -06:00 committed by GitHub
parent 0e6294d650
commit 8935ed17fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 168 additions and 249 deletions

View file

@ -1,55 +1,72 @@
// Package testutil contains helper functions for tests.
package testutil
import (
"context"
"encoding/hex"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/ory/dockertest/v3"
"github.com/pomerium/pomerium/internal/log"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// WithTestPostgres starts a test DB and runs the given handler with the connection to it.
func WithTestPostgres(handler func(dsn string) error) error {
ctx, clearTimeout := context.WithTimeout(context.Background(), maxWait)
defer clearTimeout()
// WithTestPostgres starts a postgres database.
func WithTestPostgres(t *testing.T, handler func(dsn string)) {
t.Helper()
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
return err
}
ctx := GetContext(t, maxWait)
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "14",
Env: []string{"POSTGRES_DB=pomeriumtest", "POSTGRES_HOST_AUTH_METHOD=trust"},
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Name: "pomerium-postgres",
Image: "postgres:16",
ExposedPorts: []string{"5432/tcp"},
WaitingFor: wait.ForAll(
wait.ForLog("database system is ready to accept connections"),
wait.ForListeningPort("5432"),
),
Env: map[string]string{
"POSTGRES_DB": "pomeriumtest",
"POSTGRES_PASSWORD": "pomeriumtest",
"POSTGRES_USER": "pomeriumtest",
},
Cmd: []string{"-c", "max_connections=1000"},
},
Started: true,
Logger: testcontainers.TestLogger(t),
Reuse: true,
})
if err != nil {
return err
}
_ = resource.Expire(uint(maxWait.Seconds()))
dsn := fmt.Sprintf("postgresql://postgres@localhost:%s/pomeriumtest?sslmode=disable", resource.GetPort("5432/tcp"))
if err := pool.Retry(func() error {
conn, err := pgx.Connect(ctx, dsn)
if err != nil {
log.Ctx(ctx).Error().Err(err).Send()
return err
}
_ = conn.Close(ctx)
return nil
}); err != nil {
_ = pool.Purge(resource)
return err
t.Fatalf("testutil/postgres: failed to create container: %v", err)
}
e := handler(dsn)
if err := pool.Purge(resource); err != nil {
return err
port, err := container.MappedPort(ctx, "5432")
if err != nil {
t.Fatalf("testutil/postgres: failed to get mapped port: %v", err)
}
return e
// create the next database
id := uuid.New()
dbName := fmt.Sprintf("pomeriumtest%s", hex.EncodeToString(id[:]))
t.Logf("postgres: creating %s", dbName)
// run the test against the new database
db, err := pgx.Connect(ctx, fmt.Sprintf("postgres://pomeriumtest:pomeriumtest@localhost:%s/pomeriumtest?sslmode=disable", port.Port()))
if err != nil {
t.Fatalf("testutil/postgres: failed to connect to postgres: %v", err)
}
_, err = db.Exec(ctx, `CREATE DATABASE `+dbName)
if err != nil {
t.Fatalf("testutil/postgres: failed to create database: %v", err)
}
err = db.Close(ctx)
if err != nil {
t.Fatalf("testutil/postgres: failed to close database: %v", err)
}
handler(fmt.Sprintf("postgres://pomeriumtest:pomeriumtest@localhost:%s/%s?sslmode=disable", port.Port(), dbName))
}