191 lines
6.3 KiB
Go
191 lines
6.3 KiB
Go
package config_test
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"messenger/internal/platform/config"
|
|
)
|
|
|
|
func env(values map[string]string) func(string) (string, bool) {
|
|
return func(key string) (string, bool) {
|
|
v, ok := values[key]
|
|
return v, ok
|
|
}
|
|
}
|
|
|
|
func secretFile(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "secret")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
const (
|
|
databaseURL = "postgres://app:s3cret@db:5432/messenger"
|
|
sessionStoreURL = "redis://:s3cret@valkey:6379/0"
|
|
)
|
|
|
|
// required holds the variables without defaults, merged with extra.
|
|
func required(t *testing.T, extra map[string]string) map[string]string {
|
|
t.Helper()
|
|
values := map[string]string{
|
|
"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"),
|
|
"SESSION_STORE_URL_FILE": secretFile(t, sessionStoreURL+"\r\n"),
|
|
}
|
|
for k, v := range extra {
|
|
values[k] = v
|
|
}
|
|
return values
|
|
}
|
|
|
|
func TestDefaults(t *testing.T) {
|
|
cfg, err := config.Load(env(required(t, nil)))
|
|
if err != nil {
|
|
t.Fatalf("Load error: %v", err)
|
|
}
|
|
if cfg.Addr() != "0.0.0.0:8080" {
|
|
t.Fatalf("Addr = %q", cfg.Addr())
|
|
}
|
|
if cfg.Env != "lab" || cfg.Level != slog.LevelInfo {
|
|
t.Fatalf("cfg = %+v", cfg)
|
|
}
|
|
if cfg.ShutdownTimeout != 38*time.Second || cfg.MaxBodyBytes != 65536 {
|
|
t.Fatalf("cfg = %+v", cfg)
|
|
}
|
|
if cfg.DatabaseURL != databaseURL || cfg.SessionStoreURL != sessionStoreURL {
|
|
t.Fatalf("urls = %q, %q; want the file contents without line endings", cfg.DatabaseURL, cfg.SessionStoreURL)
|
|
}
|
|
if cfg.SessionTTL != time.Hour {
|
|
t.Fatalf("SessionTTL = %v, want 1h", cfg.SessionTTL)
|
|
}
|
|
if cfg.DrainDelay != 2*time.Second || cfg.IdempotencyTTL != 24*time.Hour || cfg.DBMaxConns != 10 || cfg.DebugAddr != "" {
|
|
t.Fatalf("lab3 defaults = drain %v, key ttl %v, pool %d, debug %q", cfg.DrainDelay, cfg.IdempotencyTTL, cfg.DBMaxConns, cfg.DebugAddr)
|
|
}
|
|
}
|
|
|
|
func TestDefaultShutdownTimeoutCoversBinaryLifecycle(t *testing.T) {
|
|
cfg, err := config.Load(env(required(t, nil)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Binary requests can use 30s, followed by 3s independent cleanup. Keep
|
|
// another 5s for shutdown scheduling and closing the remaining resources.
|
|
budget := 30*time.Second + 3*time.Second + 5*time.Second
|
|
if cfg.ShutdownTimeout < budget {
|
|
t.Fatalf("default shutdown budget %v cannot cover binary request and cleanup budget %v", cfg.ShutdownTimeout, budget)
|
|
}
|
|
}
|
|
|
|
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
|
|
for _, key := range []string{"DATABASE_URL_FILE", "SESSION_STORE_URL_FILE"} {
|
|
cases := map[string]func(map[string]string){
|
|
"unset": func(v map[string]string) { delete(v, key) },
|
|
"empty path": func(v map[string]string) { v[key] = "" },
|
|
"no file": func(v map[string]string) { v[key] = filepath.Join(t.TempDir(), "missing") },
|
|
"blank file": func(v map[string]string) { v[key] = secretFile(t, " \r\n") },
|
|
}
|
|
for name, breakIt := range cases {
|
|
values := required(t, nil)
|
|
breakIt(values)
|
|
_, err := config.Load(env(values))
|
|
if err == nil {
|
|
t.Errorf("%s %s: want error", key, name)
|
|
continue
|
|
}
|
|
if !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), "s3cret") {
|
|
t.Errorf("%s %s: error must name the variable and hide the secret: %v", key, name, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOverrides(t *testing.T) {
|
|
cfg, err := config.Load(env(required(t, map[string]string{
|
|
"APP_HOST": "127.0.0.1",
|
|
"APP_PORT": "9090",
|
|
"APP_ENV": "dev",
|
|
"APP_LOG_LEVEL": "debug",
|
|
"APP_SHUTDOWN_TIMEOUT": "3s",
|
|
"APP_MAX_BODY_BYTES": "1024",
|
|
"SESSION_TTL_SECONDS": "300",
|
|
"APP_DRAIN_DELAY": "0s",
|
|
"IDEMPOTENCY_TTL_SECONDS": "60",
|
|
"DB_MAX_CONNS": "25",
|
|
"APP_DEBUG_ADDR": ":9090",
|
|
})))
|
|
if err != nil {
|
|
t.Fatalf("Load error: %v", err)
|
|
}
|
|
if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.Level != slog.LevelDebug {
|
|
t.Fatalf("cfg = %+v", cfg)
|
|
}
|
|
if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 || cfg.SessionTTL != 5*time.Minute {
|
|
t.Fatalf("cfg = %+v", cfg)
|
|
}
|
|
if cfg.DrainDelay != 0 || cfg.IdempotencyTTL != time.Minute || cfg.DBMaxConns != 25 || cfg.DebugAddr != ":9090" {
|
|
t.Fatalf("lab3 overrides = drain %v, key ttl %v, pool %d, debug %q", cfg.DrainDelay, cfg.IdempotencyTTL, cfg.DBMaxConns, cfg.DebugAddr)
|
|
}
|
|
}
|
|
|
|
func TestInvalidValuesAreReported(t *testing.T) {
|
|
cases := map[string]map[string]string{
|
|
"port text": {"APP_PORT": "abc"},
|
|
"port zero": {"APP_PORT": "0"},
|
|
"port too big": {"APP_PORT": "70000"},
|
|
"level": {"APP_LOG_LEVEL": "loud"},
|
|
"timeout": {"APP_SHUTDOWN_TIMEOUT": "soon"},
|
|
"timeout negative": {"APP_SHUTDOWN_TIMEOUT": "-1s"},
|
|
"body zero": {"APP_MAX_BODY_BYTES": "0"},
|
|
"body text": {"APP_MAX_BODY_BYTES": "big"},
|
|
"empty host": {"APP_HOST": ""},
|
|
"ttl zero": {"SESSION_TTL_SECONDS": "0"},
|
|
"ttl negative": {"SESSION_TTL_SECONDS": "-5"},
|
|
"ttl text": {"SESSION_TTL_SECONDS": "hour"},
|
|
"ttl fraction": {"SESSION_TTL_SECONDS": "1.5"},
|
|
"drain negative": {"APP_DRAIN_DELAY": "-1s"},
|
|
"drain text": {"APP_DRAIN_DELAY": "abc"},
|
|
"key ttl zero": {"IDEMPOTENCY_TTL_SECONDS": "0"},
|
|
"key ttl text": {"IDEMPOTENCY_TTL_SECONDS": "day"},
|
|
"pool zero": {"DB_MAX_CONNS": "0"},
|
|
"pool too big": {"DB_MAX_CONNS": "1001"},
|
|
"pool text": {"DB_MAX_CONNS": "many"},
|
|
"debug addr": {"APP_DEBUG_ADDR": "nonsense"},
|
|
}
|
|
for name, values := range cases {
|
|
_, err := config.Load(env(required(t, values)))
|
|
if err == nil {
|
|
t.Errorf("%s: want error", name)
|
|
continue
|
|
}
|
|
for key := range values {
|
|
if !strings.Contains(err.Error(), key) {
|
|
t.Errorf("%s: error %q does not name %s", name, err, key)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadMigrateNeedsOnlyTheDatabaseURL(t *testing.T) {
|
|
cfg, err := config.LoadMigrate(env(map[string]string{
|
|
"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"),
|
|
"APP_ENV": "dev",
|
|
"APP_LOG_LEVEL": "warn",
|
|
}))
|
|
if err != nil {
|
|
t.Fatalf("LoadMigrate error: %v", err)
|
|
}
|
|
if cfg.DatabaseURL != databaseURL || cfg.Env != "dev" || cfg.Level != slog.LevelWarn {
|
|
t.Fatalf("cfg = %+v", cfg)
|
|
}
|
|
if _, err := config.LoadMigrate(env(map[string]string{})); err == nil || !strings.Contains(err.Error(), "DATABASE_URL_FILE") {
|
|
t.Fatalf("missing database url: err = %v", err)
|
|
}
|
|
}
|