143 lines
3.5 KiB
Go
143 lines
3.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDefault(t *testing.T) {
|
|
cfg := Default()
|
|
if cfg.Server.Address != ":4533" {
|
|
t.Errorf("default server address = %q, want :4533", cfg.Server.Address)
|
|
}
|
|
if cfg.Log.Level != "info" {
|
|
t.Errorf("default log level = %q, want info", cfg.Log.Level)
|
|
}
|
|
}
|
|
|
|
func TestLoadYAML(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
content := []byte(`server:
|
|
address: ":9000"
|
|
database:
|
|
url: "postgres://example"
|
|
log:
|
|
level: "debug"
|
|
format: "json"
|
|
`)
|
|
if err := os.WriteFile(path, content, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Address != ":9000" {
|
|
t.Errorf("server.address = %q", cfg.Server.Address)
|
|
}
|
|
if cfg.Database.URL != "postgres://example" {
|
|
t.Errorf("database.url = %q", cfg.Database.URL)
|
|
}
|
|
if cfg.Log.Level != "debug" || cfg.Log.Format != "json" {
|
|
t.Errorf("log = %+v", cfg.Log)
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFileReturnsDefaults(t *testing.T) {
|
|
cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.yaml"))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Address != ":4533" {
|
|
t.Errorf("expected defaults, got %+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestEnvOverrides(t *testing.T) {
|
|
t.Setenv("SMARTMUSIC_SERVER_ADDRESS", ":8080")
|
|
t.Setenv("SMARTMUSIC_DATABASE_URL", "postgres://env")
|
|
t.Setenv("SMARTMUSIC_LOG_LEVEL", "WARN")
|
|
t.Setenv("SMARTMUSIC_LOG_FORMAT", "JSON")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Address != ":8080" {
|
|
t.Errorf("server.address = %q", cfg.Server.Address)
|
|
}
|
|
if cfg.Database.URL != "postgres://env" {
|
|
t.Errorf("database.url = %q", cfg.Database.URL)
|
|
}
|
|
if cfg.Log.Level != "warn" {
|
|
t.Errorf("log.level = %q, want lowercased warn", cfg.Log.Level)
|
|
}
|
|
if cfg.Log.Format != "json" {
|
|
t.Errorf("log.format = %q, want lowercased json", cfg.Log.Format)
|
|
}
|
|
}
|
|
|
|
func TestAdminBootstrapEnvOverrides(t *testing.T) {
|
|
t.Setenv("SMARTMUSIC_AUTH_ADMIN_USERNAME", "root")
|
|
t.Setenv("SMARTMUSIC_AUTH_ADMIN_PASSWORD", "sekret")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Auth.AdminBootstrap.Username != "root" {
|
|
t.Errorf("admin username = %q", cfg.Auth.AdminBootstrap.Username)
|
|
}
|
|
if cfg.Auth.AdminBootstrap.Password != "sekret" {
|
|
t.Errorf("admin password = %q", cfg.Auth.AdminBootstrap.Password)
|
|
}
|
|
}
|
|
|
|
func TestLibraryEnvOverrides(t *testing.T) {
|
|
t.Setenv("SMARTMUSIC_LIBRARY_SCAN_PATHS", "/music:/other::/third")
|
|
t.Setenv("SMARTMUSIC_LIBRARY_SCAN_ON_STARTUP", "true")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
want := []string{"/music", "/other", "/third"}
|
|
if len(cfg.Library.ScanPaths) != len(want) {
|
|
t.Fatalf("scan_paths = %v, want %v", cfg.Library.ScanPaths, want)
|
|
}
|
|
for i, p := range want {
|
|
if cfg.Library.ScanPaths[i] != p {
|
|
t.Errorf("scan_paths[%d] = %q, want %q", i, cfg.Library.ScanPaths[i], p)
|
|
}
|
|
}
|
|
if !cfg.Library.ScanOnStartup {
|
|
t.Error("scan_on_startup = false, want true")
|
|
}
|
|
}
|
|
|
|
func TestLibraryYAMLLoads(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
content := []byte(`library:
|
|
scan_paths:
|
|
- /srv/music
|
|
- /mnt/archive
|
|
scan_on_startup: true
|
|
`)
|
|
if err := os.WriteFile(path, content, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if len(cfg.Library.ScanPaths) != 2 || cfg.Library.ScanPaths[0] != "/srv/music" {
|
|
t.Errorf("scan_paths = %v", cfg.Library.ScanPaths)
|
|
}
|
|
if !cfg.Library.ScanOnStartup {
|
|
t.Error("scan_on_startup = false")
|
|
}
|
|
}
|