2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2021-04-08 16:43:39 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2020-09-15 14:01:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func PrepareTestContainer(t *testing.T, version string) (func(), string) {
|
2022-09-22 19:00:56 +00:00
|
|
|
env := []string{
|
|
|
|
"POSTGRES_PASSWORD=secret",
|
|
|
|
"POSTGRES_DB=database",
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:33:17 +00:00
|
|
|
_, cleanup, url, _ := prepareTestContainer(t, "postgres", "docker.mirror.hashicorp.services/postgres", version, "secret", true, false, false, env)
|
2022-09-22 19:00:56 +00:00
|
|
|
|
|
|
|
return cleanup, url
|
2022-03-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:52:24 +00:00
|
|
|
// PrepareTestContainerWithVaultUser will setup a test container with a Vault
|
|
|
|
// admin user configured so that we can safely call rotate-root without
|
|
|
|
// rotating the root DB credentials
|
|
|
|
func PrepareTestContainerWithVaultUser(t *testing.T, ctx context.Context, version string) (func(), string) {
|
|
|
|
env := []string{
|
|
|
|
"POSTGRES_PASSWORD=secret",
|
|
|
|
"POSTGRES_DB=database",
|
|
|
|
}
|
|
|
|
|
|
|
|
runner, cleanup, url, id := prepareTestContainer(t, "postgres", "docker.mirror.hashicorp.services/postgres", version, "secret", true, false, false, env)
|
|
|
|
|
|
|
|
cmd := []string{"psql", "-U", "postgres", "-c", "CREATE USER vaultadmin WITH LOGIN PASSWORD 'vaultpass' SUPERUSER"}
|
|
|
|
_, err := runner.RunCmdInBackground(ctx, id, cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not run command (%v) in container: %v", cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanup, url
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:33:55 +00:00
|
|
|
func PrepareTestContainerWithPassword(t *testing.T, version, password string) (func(), string) {
|
2022-09-22 19:00:56 +00:00
|
|
|
env := []string{
|
|
|
|
"POSTGRES_PASSWORD=" + password,
|
|
|
|
"POSTGRES_DB=database",
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:33:17 +00:00
|
|
|
_, cleanup, url, _ := prepareTestContainer(t, "postgres", "docker.mirror.hashicorp.services/postgres", version, password, true, false, false, env)
|
2022-09-22 19:00:56 +00:00
|
|
|
|
|
|
|
return cleanup, url
|
2022-03-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
func PrepareTestContainerRepmgr(t *testing.T, name, version string, envVars []string) (*docker.Runner, func(), string, string) {
|
|
|
|
env := append(envVars,
|
|
|
|
"REPMGR_PARTNER_NODES=psql-repl-node-0,psql-repl-node-1",
|
|
|
|
"REPMGR_PRIMARY_HOST=psql-repl-node-0",
|
|
|
|
"REPMGR_PASSWORD=repmgrpass",
|
|
|
|
"POSTGRESQL_PASSWORD=secret")
|
|
|
|
|
2022-11-02 17:33:17 +00:00
|
|
|
return prepareTestContainer(t, name, "docker.mirror.hashicorp.services/bitnami/postgresql-repmgr", version, "secret", false, true, true, env)
|
2022-09-22 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareTestContainer(t *testing.T, name, repo, version, password string,
|
|
|
|
addSuffix, forceLocalAddr, doNotAutoRemove bool, envVars []string,
|
|
|
|
) (*docker.Runner, func(), string, string) {
|
2020-09-15 14:01:26 +00:00
|
|
|
if os.Getenv("PG_URL") != "" {
|
2022-09-22 19:00:56 +00:00
|
|
|
return nil, func() {}, "", os.Getenv("PG_URL")
|
2020-09-15 14:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if version == "" {
|
|
|
|
version = "11"
|
|
|
|
}
|
2022-03-29 14:33:55 +00:00
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
runOpts := docker.RunOptions{
|
|
|
|
ContainerName: name,
|
|
|
|
ImageRepo: repo,
|
|
|
|
ImageTag: version,
|
|
|
|
Env: envVars,
|
|
|
|
Ports: []string{"5432/tcp"},
|
|
|
|
DoNotAutoRemove: doNotAutoRemove,
|
|
|
|
}
|
|
|
|
if repo == "bitnami/postgresql-repmgr" {
|
|
|
|
runOpts.NetworkID = os.Getenv("POSTGRES_MULTIHOST_NET")
|
|
|
|
}
|
|
|
|
|
|
|
|
runner, err := docker.NewServiceRunner(runOpts)
|
2020-09-15 14:01:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker Postgres: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
svc, containerID, err := runner.StartNewService(context.Background(), addSuffix, forceLocalAddr, connectPostgres(password, repo))
|
2020-09-15 14:01:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker Postgres: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
return runner, svc.Cleanup, svc.Config.URL().String(), containerID
|
2020-09-15 14:01:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
func connectPostgres(password, repo string) docker.ServiceAdapter {
|
2022-03-29 14:33:55 +00:00
|
|
|
return func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "postgres",
|
|
|
|
User: url.UserPassword("postgres", password),
|
|
|
|
Host: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
Path: "postgres",
|
|
|
|
RawQuery: "sslmode=disable",
|
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
|
2022-05-23 19:49:18 +00:00
|
|
|
db, err := sql.Open("pgx", u.String())
|
2022-03-29 14:33:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
2020-09-15 14:01:26 +00:00
|
|
|
|
2022-09-22 19:00:56 +00:00
|
|
|
if err = db.Ping(); err != nil {
|
2022-03-29 14:33:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return docker.NewServiceURL(u), nil
|
2020-09-15 14:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-22 19:00:56 +00:00
|
|
|
|
|
|
|
func StopContainer(t *testing.T, ctx context.Context, runner *docker.Runner, containerID string) {
|
|
|
|
if err := runner.Stop(ctx, containerID); err != nil {
|
|
|
|
t.Fatalf("Could not stop docker Postgres: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RestartContainer(t *testing.T, ctx context.Context, runner *docker.Runner, containerID string) {
|
|
|
|
if err := runner.Restart(ctx, containerID); err != nil {
|
|
|
|
t.Fatalf("Could not restart docker Postgres: %s", err)
|
|
|
|
}
|
|
|
|
}
|