SetupPostgreSQLContainer sets up a real PostgreSQL instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error. Currently using https://index.docker.io/u/nornagon/postgres
(t *testing.T, dbname string)
| 323 | // or makes the test fail on error. |
| 324 | // Currently using https://index.docker.io/u/nornagon/postgres |
| 325 | func SetupPostgreSQLContainer(t *testing.T, dbname string) (c ContainerID, ip string) { |
| 326 | c, ip = setupContainer(t, postgresImage, 5432, 15*time.Second, func() (string, error) { |
| 327 | return run("-d", postgresImage) |
| 328 | }) |
| 329 | cleanupAndDie := func(err error) { |
| 330 | c.KillRemove(t) |
| 331 | t.Fatal(err) |
| 332 | } |
| 333 | rootdb, err := sql.Open("postgres", |
| 334 | fmt.Sprintf("user=%s password=%s host=%s dbname=postgres sslmode=disable", PostgresUsername, PostgresPassword, ip)) |
| 335 | if err != nil { |
| 336 | cleanupAndDie(fmt.Errorf("Could not open postgres rootdb: %v", err)) |
| 337 | } |
| 338 | if _, err := sqlExecRetry(rootdb, |
| 339 | "CREATE DATABASE "+dbname+" LC_COLLATE = 'C' TEMPLATE = template0", |
| 340 | 50); err != nil { |
| 341 | cleanupAndDie(fmt.Errorf("Could not create database %v: %v", dbname, err)) |
| 342 | } |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | // sqlExecRetry keeps calling http://golang.org/pkg/database/sql/#DB.Exec on db |
| 347 | // with stmt until it succeeds or until it has been tried maxTry times. |