66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
//go:build integration
|
|
|
|
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
config "cloud.campbellwireless.net/git/barkstack/barkfile-parser"
|
|
)
|
|
|
|
func TestProvisionerCreatesPersistentPostgres18(t *testing.T) {
|
|
const (
|
|
database = "pawsql_integration_test"
|
|
volume = "pawsql-integration-test-data"
|
|
envName = "PAWSQL_INTEGRATION_POSTGRES_PASSWORD"
|
|
)
|
|
t.Setenv(envName, "integration-test-password")
|
|
provisioner := NewProvisioner(nil)
|
|
defer func() {
|
|
_, _ = provisioner.run(context.Background(), nil, "container", "rm", "--force", containerName(database))
|
|
_, _ = provisioner.run(context.Background(), nil, "volume", "rm", "--force", volume)
|
|
}()
|
|
|
|
cfg := config.Config{Databases: []config.DatabaseConfig{{
|
|
Name: database,
|
|
Postgres: &config.PostgresConfig{
|
|
Image: "postgres:18",
|
|
Volume: volume,
|
|
PasswordEnv: envName,
|
|
},
|
|
}}}
|
|
resolved, err := provisioner.Ensure(context.Background(), cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
address := resolved.Databases[0].Upstream
|
|
waitForAddress(t, address)
|
|
if err := provisioner.StopDatabase(context.Background(), database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resolved, err = provisioner.Ensure(context.Background(), cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitForAddress(t, resolved.Databases[0].Upstream)
|
|
}
|
|
|
|
func waitForAddress(t *testing.T, address string) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(90 * time.Second)
|
|
for {
|
|
connection, err := net.DialTimeout("tcp", address, time.Second)
|
|
if err == nil {
|
|
_ = connection.Close()
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("PostgreSQL at %s did not accept connections: %v", address, err)
|
|
}
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
}
|