- pluggable per-domain Repository interfaces; PostgreSQL and SQLite impls - SQLite on modernc.org/sqlite (WAL, busy_timeout, foreign_keys, immediate tx) - separate migrations/sqlite and db.MigrateSQLite; store.NewPostgres/NewSQLite - cmd/reconcile compares object storage with the journal (report, then --delete) - storage.Backend.List and photos.Repository.AllStorageKeys - DB_BACKEND/SQLITE_PATH and STORAGE_BACKEND/FS_ROOT selectable; local defaults (sqlite ./galo.db, fs ./data) - Makefile reconcile/reconcile-delete; docs and .env.example updated
135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func setRequiredEnv(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("JWT_SECRET", "test-secret")
|
|
t.Setenv("PG_PASSWORD", "test-pass")
|
|
}
|
|
|
|
func TestLoadStorageBackend(t *testing.T) {
|
|
t.Run("defaults to fs", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("STORAGE_BACKEND", "")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Storage.Backend != "fs" || cfg.Storage.FSRoot != "./data" {
|
|
t.Fatalf("storage = %+v", cfg.Storage)
|
|
}
|
|
})
|
|
|
|
t.Run("fs with root", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("STORAGE_BACKEND", "fs")
|
|
t.Setenv("FS_ROOT", "/var/lib/galo/objects")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Storage.Backend != "fs" || cfg.Storage.FSRoot != "/var/lib/galo/objects" {
|
|
t.Fatalf("storage = %+v", cfg.Storage)
|
|
}
|
|
})
|
|
|
|
t.Run("fs empty root falls back to default", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("STORAGE_BACKEND", "fs")
|
|
t.Setenv("FS_ROOT", "")
|
|
t.Setenv("URL_SECRET", "")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Storage.FSRoot != "./data" {
|
|
t.Fatalf("FSRoot = %q, want ./data", cfg.Storage.FSRoot)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid backend", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("STORAGE_BACKEND", "gcs")
|
|
if _, err := Load(); err == nil {
|
|
t.Fatal("expected error for invalid backend")
|
|
}
|
|
})
|
|
|
|
t.Run("object url ttl", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("STORAGE_BACKEND", "")
|
|
t.Setenv("OBJECT_URL_TTL", "30m")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Storage.URLTTL != 30*time.Minute {
|
|
t.Fatalf("URLTTL = %v, want 30m", cfg.Storage.URLTTL)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadDatabaseBackend(t *testing.T) {
|
|
t.Run("defaults to sqlite", func(t *testing.T) {
|
|
setRequiredEnv(t)
|
|
t.Setenv("DB_BACKEND", "")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Database.Backend != "sqlite" || cfg.Database.SQLitePath != "./galo.db" {
|
|
t.Fatalf("database = %+v", cfg.Database)
|
|
}
|
|
})
|
|
|
|
t.Run("postgres requires password", func(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", "s")
|
|
t.Setenv("PG_PASSWORD", "")
|
|
t.Setenv("DB_BACKEND", "postgres")
|
|
if _, err := Load(); err == nil {
|
|
t.Fatal("expected error for postgres without PG_PASSWORD")
|
|
}
|
|
})
|
|
|
|
t.Run("sqlite with path", func(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", "s")
|
|
t.Setenv("PG_PASSWORD", "")
|
|
t.Setenv("DB_BACKEND", "sqlite")
|
|
t.Setenv("SQLITE_PATH", "/data/galo.db")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Database.Backend != "sqlite" || cfg.Database.SQLitePath != "/data/galo.db" {
|
|
t.Fatalf("database = %+v", cfg.Database)
|
|
}
|
|
})
|
|
|
|
t.Run("sqlite empty path falls back to default", func(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", "s")
|
|
t.Setenv("PG_PASSWORD", "")
|
|
t.Setenv("DB_BACKEND", "sqlite")
|
|
t.Setenv("SQLITE_PATH", "")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Database.SQLitePath != "./galo.db" {
|
|
t.Fatalf("SQLitePath = %q, want ./galo.db", cfg.Database.SQLitePath)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid backend", func(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", "s")
|
|
t.Setenv("PG_PASSWORD", "p")
|
|
t.Setenv("DB_BACKEND", "mysql")
|
|
if _, err := Load(); err == nil {
|
|
t.Fatal("expected error for invalid backend")
|
|
}
|
|
})
|
|
}
|