2026-04-04 06:29:16 -03:00
|
|
|
package cluster
|
|
|
|
|
|
2026-04-04 18:34:50 -03:00
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
2026-04-05 13:23:36 -03:00
|
|
|
"time"
|
2026-04-04 18:34:50 -03:00
|
|
|
|
|
|
|
|
"scm.bstein.dev/bstein/hecate/internal/config"
|
2026-04-05 13:23:36 -03:00
|
|
|
"scm.bstein.dev/bstein/hecate/internal/state"
|
2026-04-04 18:34:50 -03:00
|
|
|
)
|
2026-04-04 06:29:16 -03:00
|
|
|
|
|
|
|
|
func TestParseVaultSealed(t *testing.T) {
|
|
|
|
|
sealed, err := parseVaultSealed(`{"initialized":true,"sealed":true}`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse sealed=true: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !sealed {
|
|
|
|
|
t.Fatalf("expected sealed=true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed, err = parseVaultSealed(`{"initialized":true,"sealed":false}`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse sealed=false: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if sealed {
|
|
|
|
|
t.Fatalf("expected sealed=false")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseVaultSealedRejectsEmpty(t *testing.T) {
|
|
|
|
|
if _, err := parseVaultSealed(" "); err == nil {
|
|
|
|
|
t.Fatalf("expected parse error for empty status payload")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 06:44:31 -03:00
|
|
|
|
|
|
|
|
func TestParseVaultSealedWithKubectlPreamble(t *testing.T) {
|
|
|
|
|
raw := "Defaulted container \"vault\" out of: vault, setup-config (init)\n{\"sealed\":true,\"initialized\":true}\n"
|
|
|
|
|
sealed, err := parseVaultSealed(raw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("parse with preamble: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !sealed {
|
|
|
|
|
t.Fatalf("expected sealed=true from payload with preamble")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 18:34:50 -03:00
|
|
|
|
|
|
|
|
func TestFallbackWorkersFromInventoryUsesManagedNodes(t *testing.T) {
|
|
|
|
|
orch := &Orchestrator{
|
|
|
|
|
cfg: config.Config{
|
|
|
|
|
ControlPlanes: []string{"titan-0a", "titan-0b", "titan-0c"},
|
|
|
|
|
SSHManagedNodes: []string{
|
|
|
|
|
"titan-db",
|
|
|
|
|
"titan-0a",
|
|
|
|
|
"titan-15",
|
|
|
|
|
"titan-17",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
log: log.New(os.Stdout, "", 0),
|
|
|
|
|
}
|
|
|
|
|
got := orch.fallbackWorkersFromInventory()
|
|
|
|
|
want := []string{"titan-15", "titan-17", "titan-db"}
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Fatalf("fallback workers mismatch: got=%v want=%v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFallbackWorkersFromInventoryFallsBackToHosts(t *testing.T) {
|
|
|
|
|
orch := &Orchestrator{
|
|
|
|
|
cfg: config.Config{
|
|
|
|
|
ControlPlanes: []string{"titan-0a", "titan-0b", "titan-0c"},
|
|
|
|
|
SSHNodeHosts: map[string]string{
|
|
|
|
|
"titan-0a": "192.168.22.11",
|
|
|
|
|
"titan-22": "192.168.22.22",
|
|
|
|
|
"titan-24": "192.168.22.26",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
log: log.New(os.Stdout, "", 0),
|
|
|
|
|
}
|
|
|
|
|
got := orch.fallbackWorkersFromInventory()
|
|
|
|
|
want := []string{"titan-22", "titan-24"}
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Fatalf("fallback workers mismatch: got=%v want=%v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 13:23:36 -03:00
|
|
|
|
|
|
|
|
func TestIntentFreshTreatsZeroTimestampAsFresh(t *testing.T) {
|
|
|
|
|
if !intentFresh(state.Intent{}, 30*time.Second) {
|
|
|
|
|
t.Fatalf("zero updated_at intent should be treated as fresh")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIntentFreshRespectsAge(t *testing.T) {
|
|
|
|
|
stale := state.Intent{UpdatedAt: time.Now().Add(-2 * time.Minute)}
|
|
|
|
|
fresh := state.Intent{UpdatedAt: time.Now().Add(-20 * time.Second)}
|
|
|
|
|
if intentFresh(stale, 30*time.Second) {
|
|
|
|
|
t.Fatalf("expected stale intent to be considered not fresh")
|
|
|
|
|
}
|
|
|
|
|
if !intentFresh(fresh, 30*time.Second) {
|
|
|
|
|
t.Fatalf("expected recent intent to be considered fresh")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCoordinationPeersDedupesAndIncludesForwardHost(t *testing.T) {
|
|
|
|
|
orch := &Orchestrator{
|
|
|
|
|
cfg: config.Config{
|
|
|
|
|
Coordination: config.Coordination{
|
|
|
|
|
PeerHosts: []string{"titan-24", "titan-db", "titan-24", " "},
|
|
|
|
|
ForwardShutdownHost: "titan-db",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
got := orch.coordinationPeers()
|
|
|
|
|
want := []string{"titan-24", "titan-db"}
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
|
t.Fatalf("coordination peers mismatch: got=%v want=%v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|