pomerium/internal/testutil/postgres.go
Caleb Doxsey 1c2aad2de6
postgres: databroker storage backend (#3370)
* wip

* storage: add filtering to SyncLatest

* don't increment the record version, so intermediate changes are requested

* databroker: add support for query filtering

* fill server and record version

* postgres: databroker storage backend

* wip

* serialize puts

* add test

* skip tests for macos

* add test

* return error from protojson

* set data

* exclude postgres from cover tests
2022-05-25 10:23:58 -06:00

55 lines
1.2 KiB
Go

package testutil
import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
"github.com/ory/dockertest/v3"
"github.com/pomerium/pomerium/internal/log"
)
// 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()
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
return err
}
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "14",
Env: []string{"POSTGRES_DB=pomeriumtest", "POSTGRES_HOST_AUTH_METHOD=trust"},
})
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.Error(ctx).Err(err).Send()
return err
}
_ = conn.Close(ctx)
return nil
}); err != nil {
_ = pool.Purge(resource)
return err
}
e := handler(dsn)
if err := pool.Purge(resource); err != nil {
return err
}
return e
}