Files
Havline/internal/config/config_test.go
T

72 lines
1.9 KiB
Go
Raw Normal View History

2026-09-23 09:40:59 +08:00
package config
import (
"os"
2026-10-07 19:26:02 +08:00
"path/filepath"
2026-09-23 09:40:59 +08:00
"testing"
)
func TestLoadNginxDefaultPorts(t *testing.T) {
2026-10-01 16:54:35 +08:00
t.Setenv("HAVLINE_NGINX_HTTP_PORT", "18080")
t.Setenv("HAVLINE_NGINX_HTTPS_PORT", "9443")
2026-09-23 09:40:59 +08:00
cfg := Load()
if cfg.NginxDefaultHTTPPort != 18080 {
t.Fatalf("http port %d", cfg.NginxDefaultHTTPPort)
}
if cfg.NginxDefaultHTTPSPort != 9443 {
t.Fatalf("https port %d", cfg.NginxDefaultHTTPSPort)
}
if cfg.DefaultListenPort(false) != 18080 {
t.Fatalf("default http listen %d", cfg.DefaultListenPort(false))
}
if cfg.DefaultListenPort(true) != 9443 {
t.Fatalf("default https listen %d", cfg.DefaultListenPort(true))
}
}
func TestLoadNginxDefaultPortsFallback(t *testing.T) {
2026-10-01 16:54:35 +08:00
os.Unsetenv("HAVLINE_NGINX_HTTP_PORT")
os.Unsetenv("HAVLINE_NGINX_HTTPS_PORT")
2026-09-23 09:40:59 +08:00
cfg := Load()
if cfg.NginxDefaultHTTPPort != 80 || cfg.NginxDefaultHTTPSPort != 443 {
t.Fatalf("ports %d %d", cfg.NginxDefaultHTTPPort, cfg.NginxDefaultHTTPSPort)
}
}
func TestLoadUpdaterTokenFile(t *testing.T) {
t.Setenv("HAVLINE_UPDATER_TOKEN_FILE", "/tmp/havline-updater.token")
cfg := Load()
if cfg.UpdaterTokenFile != "/tmp/havline-updater.token" {
t.Fatalf("updater token file %q", cfg.UpdaterTokenFile)
}
}
2026-10-07 19:26:02 +08:00
2026-10-16 20:18:04 +08:00
func TestLoadListenAddrAllowsExplicitEmpty(t *testing.T) {
t.Setenv("HAVLINE_LISTEN", "")
cfg := Load()
if cfg.ListenAddr != "" {
t.Fatalf("listen addr %q, want empty for unix-socket-only mode", cfg.ListenAddr)
}
}
func TestLoadUpdaterTokenFileAllowsExplicitEmpty(t *testing.T) {
t.Setenv("HAVLINE_UPDATER_TOKEN_FILE", "")
cfg := Load()
if cfg.UpdaterTokenFile != "" {
t.Fatalf("updater token file %q, want empty", cfg.UpdaterTokenFile)
}
}
2026-10-07 19:26:02 +08:00
func TestSSHKnownHostsPathForServer(t *testing.T) {
cfg := Config{DataDir: "/data"}
want := filepath.Join("/data", "ssh_known_hosts", "server-3")
if got := cfg.SSHKnownHostsPathForServer(3); got != want {
t.Fatalf("per-server known_hosts path %q", got)
}
}