Files
felhom.eu/hub/internal/monitor/host_capability_test.go
T
admin b7b165bff5 hub: HostCapabilityChecker — operator alert on agent capability-degraded (v0.19.0)
Companion to felhom-agent v0.44.0. New monitor.HostCapabilityChecker (sibling of
HostStalenessChecker) reads the capabilities snapshot from the latest host report and emits
agent_capability_degraded/recovered (operator-only, 1h cooldown) on ok<->degraded transitions
for any Critical capability. store.GetHostCapabilities (MAX(id), no migration). Goldens mirror
the new capabilities field.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EPZ4GJ8L5Jqf8UiPwbn1kt
2026-06-29 18:50:50 +02:00

108 lines
4.1 KiB
Go

package monitor
import (
"io"
"log"
"path/filepath"
"testing"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
_ "modernc.org/sqlite"
)
// reportWith builds a host-report body carrying the given capability snapshot (the only field the
// capability checker reads). One critical-degraded entry makes the host "degraded".
func reportWith(caps string) []byte {
return []byte(`{"host_id":"h1","capabilities":` + caps + `}`)
}
const capAllOK = `[{"name":"guest-init-pid","feature":"drive-gate","critical":true,"status":"ok"}]`
const capCritDegraded = `[{"name":"guest-init-pid","feature":"drive-gate guest-sees","critical":true,"status":"degraded","reason":"sudo policy denied"}]`
const capNonCritDegraded = `[{"name":"disk-smart","feature":"SMART","critical":false,"status":"degraded","reason":"binary not found"}]`
func newCapStore(t *testing.T) *store.Store {
t.Helper()
st, err := store.New(filepath.Join(t.TempDir(), "test.db"), log.New(io.Discard, "", 0))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ck", RetrievalPassword: "p"})
st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: "k1"})
return st
}
// TestHostCapabilityChecker covers the full transition contract (§10): seed (no event), ok→degraded
// (one event), steady degraded (none), degraded→ok (recovered), and that a non-critical degradation
// never trips the host state.
func TestHostCapabilityChecker(t *testing.T) {
st := newCapStore(t)
st.SaveHostReport("h1", "c1", reportWith(capAllOK), store.HostReportDenorm{})
var events []string
onEvent := func(_, eventType, _, _, _, _ string) { events = append(events, eventType) }
// Seed ok → no event on init.
cc := NewHostCapabilityChecker(st, onEvent, log.New(io.Discard, "", 0))
if cc.GetState("h1") != "ok" {
t.Fatalf("seed state = %s, want ok", cc.GetState("h1"))
}
if len(events) != 0 {
t.Fatalf("seed must not emit, got %v", events)
}
// ok → degraded: exactly ONE operator event.
st.SaveHostReport("h1", "c1", reportWith(capCritDegraded), store.HostReportDenorm{})
cc.Check()
if cc.GetState("h1") != "degraded" {
t.Fatalf("state = %s, want degraded", cc.GetState("h1"))
}
if len(events) != 1 || events[0] != "agent_capability_degraded" {
t.Fatalf("want one agent_capability_degraded, got %v", events)
}
// Steady degraded: NO duplicate event.
st.SaveHostReport("h1", "c1", reportWith(capCritDegraded), store.HostReportDenorm{})
cc.Check()
if len(events) != 1 {
t.Fatalf("steady degraded must not re-emit, got %v", events)
}
// degraded → ok: recovered event.
st.SaveHostReport("h1", "c1", reportWith(capAllOK), store.HostReportDenorm{})
cc.Check()
if cc.GetState("h1") != "ok" {
t.Fatalf("state = %s, want ok", cc.GetState("h1"))
}
if len(events) != 2 || events[1] != "agent_capability_recovered" {
t.Fatalf("want recovered event, got %v", events)
}
}
// A NON-critical degradation must NOT flip the host to degraded (no operator noise).
func TestHostCapabilityChecker_NonCriticalIgnored(t *testing.T) {
st := newCapStore(t)
st.SaveHostReport("h1", "c1", reportWith(capAllOK), store.HostReportDenorm{})
var events []string
cc := NewHostCapabilityChecker(st, func(_, et, _, _, _, _ string) { events = append(events, et) }, log.New(io.Discard, "", 0))
st.SaveHostReport("h1", "c1", reportWith(capNonCritDegraded), store.HostReportDenorm{})
cc.Check()
if cc.GetState("h1") != "ok" {
t.Fatalf("non-critical degradation flipped state to %s, want ok", cc.GetState("h1"))
}
if len(events) != 0 {
t.Fatalf("non-critical degradation must not alert, got %v", events)
}
}
// A pre-v0.44.0 agent (no capabilities array) is "ok" — an old agent can't trip a false alert.
func TestHostCapabilityChecker_OldAgentNoCaps(t *testing.T) {
st := newCapStore(t)
st.SaveHostReport("h1", "c1", []byte(`{"host_id":"h1"}`), store.HostReportDenorm{})
cc := NewHostCapabilityChecker(st, func(_, _, _, _, _, _ string) {}, log.New(io.Discard, "", 0))
if cc.GetState("h1") != "ok" {
t.Fatalf("old agent (no caps) state = %s, want ok", cc.GetState("h1"))
}
}