8870b2e51c
* Refactor PG container creation. * Rework rotation tests to use shorter sleeps. * Refactor rotation tests. * Add a static role rotation test for MongoDB Atlas.
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package postgresql
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
|
"github.com/ory/dockertest"
|
|
)
|
|
|
|
func PrepareTestContainer(t *testing.T, version string) (cleanup func(), retURL string) {
|
|
if os.Getenv("PG_URL") != "" {
|
|
return func() {}, os.Getenv("PG_URL")
|
|
}
|
|
if version == "" {
|
|
version = "latest"
|
|
}
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
if err != nil {
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
}
|
|
|
|
resource, err := pool.Run("postgres", "latest", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=database"})
|
|
if err != nil {
|
|
t.Fatalf("Could not start local PostgreSQL docker container: %s", err)
|
|
}
|
|
|
|
cleanup = func() {
|
|
docker.CleanupResource(t, pool, resource)
|
|
}
|
|
|
|
retURL = fmt.Sprintf("postgres://postgres:secret@localhost:%s/database?sslmode=disable", resource.GetPort("5432/tcp"))
|
|
|
|
// exponential backoff-retry
|
|
if err = pool.Retry(func() error {
|
|
var err error
|
|
var db *sql.DB
|
|
db, err = sql.Open("postgres", retURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
return db.Ping()
|
|
}); err != nil {
|
|
cleanup()
|
|
t.Fatalf("Could not connect to PostgreSQL docker container: %s", err)
|
|
}
|
|
|
|
return
|
|
}
|