2b37986218
Small Go tool + distroless container used as a K8s initContainer to block an app until its database (Postgres/MySQL) is reachable. Env-var configured (WAITFORDB_* + libpq PG* fallback), configurable timeout/interval, redacted logs, exit codes. Woodpecker CI publishes docker-internal/waitfordb on tag.
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package driver
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.unkin.net/unkin/waitfordb/internal/config"
|
|
)
|
|
|
|
func TestGetRegisteredDrivers(t *testing.T) {
|
|
for _, name := range []string{"postgres", "mysql"} {
|
|
if _, err := Get(name); err != nil {
|
|
t.Errorf("Get(%q) failed: %v", name, err)
|
|
}
|
|
}
|
|
if _, err := Get("oracle"); err == nil {
|
|
t.Error("Get(oracle) should fail")
|
|
}
|
|
}
|
|
|
|
func TestPostgresDSN(t *testing.T) {
|
|
cfg := config.Config{
|
|
Host: "db.example", Port: "5432", User: "sonarr",
|
|
Password: "p@ss/w:rd", Database: "sonarr-main",
|
|
SSLMode: "disable", ConnectTimeout: 5e9,
|
|
}
|
|
dsn := postgresDSN(cfg)
|
|
if !strings.HasPrefix(dsn, "postgres://sonarr:") {
|
|
t.Errorf("unexpected prefix: %s", dsn)
|
|
}
|
|
// The special-character password must be percent-escaped, not raw.
|
|
if strings.Contains(dsn, "p@ss/w:rd") {
|
|
t.Errorf("password not escaped in DSN: %s", dsn)
|
|
}
|
|
if !strings.Contains(dsn, "db.example:5432") {
|
|
t.Errorf("missing host:port: %s", dsn)
|
|
}
|
|
if !strings.Contains(dsn, "sslmode=disable") {
|
|
t.Errorf("missing sslmode: %s", dsn)
|
|
}
|
|
if !strings.Contains(dsn, "connect_timeout=5") {
|
|
t.Errorf("missing connect_timeout: %s", dsn)
|
|
}
|
|
if !strings.Contains(dsn, "/sonarr-main") {
|
|
t.Errorf("missing dbname: %s", dsn)
|
|
}
|
|
}
|
|
|
|
func TestMySQLDSN(t *testing.T) {
|
|
cfg := config.Config{
|
|
Host: "db", Port: "3306", User: "u", Password: "pw",
|
|
Database: "app", ConnectTimeout: 5e9,
|
|
}
|
|
dsn := mysqlDSN(cfg)
|
|
if !strings.Contains(dsn, "@tcp(db:3306)/app") {
|
|
t.Errorf("unexpected mysql dsn: %s", dsn)
|
|
}
|
|
if !strings.Contains(dsn, "timeout=5s") {
|
|
t.Errorf("missing timeout: %s", dsn)
|
|
}
|
|
}
|