Files
waitfordb/internal/driver/driver_test.go
T
unkin-agent a466b3e07f
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/push/pre-commit Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Add valkey/redis driver with operator-secret auto-configuration
Apps fronted by valkey-operator instances need the same wait-for-ready
initContainer postgres workloads already get, and wiring per-field
secretKeyRefs for operator-generated secrets is boilerplate. waitfordb
now speaks enough RESP to AUTH and PING, and natively understands the
secret shapes CNPG and valkey-operator generate so an initContainer is
just envFrom plus a mode variable.

- valkey driver (alias: redis): fresh TCP connection per attempt,
  optional AUTH (ACL user or default), PING, reusing the existing
  retry/backoff wait loop; redis:///valkey:// DSNs; default port 6379
- WAITFORDB_SECRET_FORMAT=cnpg|valkey for envFrom-injected operator
  secrets: CNPG <cluster>-app host/port/dbname/user/password keys, and
  valkey-operator key-per-username secrets (_operator preferred, or
  WAITFORDB_USER's same-named key)
- autodetection from injected keys (CNPG keys -> postgres, valkey keys
  -> valkey); explicit WAITFORDB_DRIVER/WAITFORDB_* always win, PG*
  fallback and all existing flags unchanged
- valkey needs no user/database to be valid (unauthenticated PING)
- tests: secret shape parsing/autodetect/mismatch, fake RESP server
  covering NOAUTH/WRONGPASS/ACL auth/DSN, retry-until-up wait
- README: envFrom initContainer snippets for CNPG and valkey-operator
2026-08-23 16:36:52 +10:00

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", "valkey"} {
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: "appuser",
Password: "p@ss/w:rd", Database: "appdb",
SSLMode: "disable", ConnectTimeout: 5e9,
}
dsn := postgresDSN(cfg)
if !strings.HasPrefix(dsn, "postgres://appuser:") {
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, "/appdb") {
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)
}
}