ananke/internal/config/config_test.go

127 lines
3.5 KiB
Go

package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestLoadAcceptsUPSTargets(t *testing.T) {
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "hecate.yaml")
raw := `
control_planes: [titan-0a, titan-0b, titan-0c]
expected_flux_branch: main
iac_repo_path: /opt/titan-iac
ups:
enabled: true
provider: nut
targets:
- name: pyrphoros
target: pyrphoros@localhost
shutdown:
default_budget_seconds: 300
state:
run_history_path: /tmp/runs.json
lock_path: /tmp/hecate.lock
`
if err := os.WriteFile(cfgPath, []byte(strings.TrimSpace(raw)), 0o644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := Load(cfgPath)
if err != nil {
t.Fatalf("load config: %v", err)
}
if len(cfg.UPS.Targets) != 1 || cfg.UPS.Targets[0].Target != "pyrphoros@localhost" {
t.Fatalf("unexpected UPS targets: %#v", cfg.UPS.Targets)
}
}
func TestValidateForwardShutdownRequiresConfigPath(t *testing.T) {
cfg := defaults()
cfg.Coordination.ForwardShutdownHost = "titan-db"
cfg.Coordination.ForwardShutdownConfig = ""
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error for missing forward_shutdown_config")
}
}
func TestValidateRejectsUnknownRole(t *testing.T) {
cfg := defaults()
cfg.Coordination.Role = "unknown"
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error for unknown coordination role")
}
}
func TestValidateRejectsUnknownEtcdRestoreControlPlane(t *testing.T) {
cfg := defaults()
cfg.Startup.EtcdRestoreControlPlane = "titan-missing"
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error for unknown etcd restore control plane")
}
}
func TestLoadSetsCoordinationGuardDefaults(t *testing.T) {
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "hecate.yaml")
raw := `
control_planes: [titan-0a, titan-0b, titan-0c]
expected_flux_branch: main
iac_repo_path: /opt/titan-iac
coordination:
role: coordinator
ups:
enabled: false
state:
run_history_path: /tmp/runs.json
lock_path: /tmp/hecate.lock
`
if err := os.WriteFile(cfgPath, []byte(strings.TrimSpace(raw)), 0o644); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := Load(cfgPath)
if err != nil {
t.Fatalf("load config: %v", err)
}
if cfg.Coordination.StartupGuardMaxAgeSec <= 0 {
t.Fatalf("expected startup guard max age default > 0, got %d", cfg.Coordination.StartupGuardMaxAgeSec)
}
if cfg.Startup.EtcdRestoreControlPlane == "" {
t.Fatalf("expected startup etcd restore control plane default to be set")
}
if cfg.Startup.TimeSyncMode == "" {
t.Fatalf("expected startup time sync mode default to be set")
}
if cfg.Startup.VaultUnsealKeyFile == "" {
t.Fatalf("expected startup vault unseal key file default to be set")
}
}
func TestValidateRejectsInvalidTimeSyncMode(t *testing.T) {
cfg := defaults()
cfg.Startup.TimeSyncMode = "invalid"
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error for invalid time_sync_mode")
}
}
func TestValidateRejectsBadStoragePVCFormat(t *testing.T) {
cfg := defaults()
cfg.Startup.StorageCriticalPVCs = []string{"vault-data-vault-0"}
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error for invalid storage_critical_pvcs entry")
}
}
func TestValidateRejectsMissingPostStartProbesWhenRequired(t *testing.T) {
cfg := defaults()
cfg.Startup.RequirePostStartProbes = true
cfg.Startup.PostStartProbes = nil
if err := cfg.Validate(); err == nil {
t.Fatalf("expected validation error when post start probes are required but empty")
}
}