gates / gates (push) Successful in 2m11s
The host report's new sent_at (the box clock at the moment of sending) is compared with the hub's clock at arrival. Both directions; told once; clears within 30 s. A report that waited on the box raises nothing (reported_at is not read). A second box off in the same direction points at the hub's own clock. Operator-only. Not deployed. Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
140 lines
5.9 KiB
Go
140 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
|
|
)
|
|
|
|
// clockReport is a host report of `host` whose agent stamped `sent_at` = the hub's now + skew, and whose collect
|
|
// started `collectAge` before that (reported_at) — a report that WAITED on the box before it was sent.
|
|
func clockReport(host string, skew, collectAge time.Duration, withSentAt bool) string {
|
|
b := validReportBody(host)
|
|
now := time.Now().Add(skew)
|
|
extra := fmt.Sprintf(`,"reported_at":%q`, now.Add(-collectAge).UTC().Format(time.RFC3339))
|
|
if withSentAt {
|
|
extra += fmt.Sprintf(`,"sent_at":%q`, now.UTC().Format(time.RFC3339Nano))
|
|
}
|
|
return b[:strings.LastIndex(b, "}")] + extra + "}"
|
|
}
|
|
|
|
func clockSetup(t *testing.T) (*Handler, *store.Store, func(host string, skew, age time.Duration, sent bool)) {
|
|
t.Helper()
|
|
h, st, _ := newTestHandler(t)
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "p"})
|
|
for _, id := range []string{"h1", "h2"} {
|
|
st.UpsertHost(&store.Host{HostID: id, CustomerID: "c1", APIKey: "KEY-" + id})
|
|
}
|
|
post := func(host string, skew, age time.Duration, sent bool) {
|
|
t.Helper()
|
|
if rr := do(h, http.MethodPost, "/host-report", "KEY-"+host, clockReport(host, skew, age, sent)); rr.Code != 200 {
|
|
t.Fatalf("report: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
return h, st, post
|
|
}
|
|
|
|
func clockEvents(t *testing.T, st *store.Store) (wrong, ok []store.Event) {
|
|
t.Helper()
|
|
since := time.Now().Add(-time.Hour)
|
|
wrong, _ = st.GetEventsByType("c1", eventBoxClockWrong, since)
|
|
ok, _ = st.GetEventsByType("c1", eventBoxClockOK, since)
|
|
return
|
|
}
|
|
|
|
// R-1384 (D79): a box more than one minute off, in EITHER direction, is told to the operator once; it clears once
|
|
// when the clock is back. COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub-box-clock.txt).
|
|
func TestBoxClock_BothDirectionsOnceThenClear(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
skew time.Duration
|
|
dir string
|
|
}{{"behind", -95 * time.Second, "behind"}, {"ahead", 95 * time.Second, "ahead"}} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
_, st, post := clockSetup(t)
|
|
post("h1", 20*time.Second, 0, true) // in step: nothing
|
|
post("h1", -45*time.Second, 0, true) // off, but under one minute: nothing
|
|
if w, o := clockEvents(t, st); len(w) != 0 || len(o) != 0 {
|
|
t.Fatalf("a clock under one minute off raised %d wrong, %d ok", len(w), len(o))
|
|
}
|
|
post("h1", c.skew, 0, true)
|
|
post("h1", c.skew, 0, true)
|
|
w, o := clockEvents(t, st)
|
|
if len(w) != 1 || len(o) != 0 {
|
|
t.Fatalf("wrong=%d ok=%d, want ONE alarm for two reports of the same wrong clock", len(w), len(o))
|
|
}
|
|
if w[0].Severity != "warning" || !strings.Contains(w[0].Message, "h1") || !strings.Contains(w[0].Message, "seconds "+c.dir+" ") ||
|
|
!strings.Contains(string(w[0].DetailsJSON), `"direction":"`+c.dir+`"`) {
|
|
t.Errorf("alarm = %+v, want a warning that names h1 and %q", w[0], c.dir)
|
|
}
|
|
if strings.Contains(w[0].Message, "HUB's own clock") {
|
|
t.Errorf("one box alone must not point at the hub's clock: %s", w[0].Message)
|
|
}
|
|
post("h1", c.skew/2, 0, true) // 47 s: under the alarm line, above the clear line → no word
|
|
if w, o := clockEvents(t, st); len(w) != 1 || len(o) != 0 {
|
|
t.Fatalf("between the two lines: wrong=%d ok=%d, want 1 and 0", len(w), len(o))
|
|
}
|
|
post("h1", 2*time.Second, 0, true)
|
|
post("h1", 2*time.Second, 0, true)
|
|
w, o = clockEvents(t, st)
|
|
if len(w) != 1 || len(o) != 1 || !strings.Contains(o[0].Message, "was "+c.dir) {
|
|
t.Fatalf("after the clock came back: wrong=%d ok=%d (%v), want one recovery", len(w), len(o), o)
|
|
}
|
|
post("h1", c.skew, 0, true) // and a second time it goes wrong, it is told again
|
|
if w, _ := clockEvents(t, st); len(w) != 2 {
|
|
t.Fatalf("a second wrong clock after a recovery: wrong=%d, want 2", len(w))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The trap the ruling names: a report that WAITED on the box (a long collect) carries an old reported_at. That is not
|
|
// a wrong clock, and it raises nothing — the hub reads the time stamped at SENDING. And an agent that sends no
|
|
// sent_at is never judged, however old its reported_at is.
|
|
func TestBoxClock_AQueuedReportRaisesNothing(t *testing.T) {
|
|
_, st, post := clockSetup(t)
|
|
post("h1", 0, 10*time.Minute, true) // collected 10 minutes ago, sent now, clock right
|
|
post("h1", 0, 10*time.Minute, false) // an older agent: no sent_at at all
|
|
post("h1", 0, -10*time.Minute, false)
|
|
if w, o := clockEvents(t, st); len(w) != 0 || len(o) != 0 {
|
|
t.Fatalf("a report that waited on the box raised %d wrong, %d ok — an old reported_at is not a wrong clock", len(w), len(o))
|
|
}
|
|
// positive control: the same handler, the same box, does alarm on the send time
|
|
post("h1", -5*time.Minute, 0, true)
|
|
if w, _ := clockEvents(t, st); len(w) != 1 {
|
|
t.Fatalf("control: a send time 5 minutes behind raised %d alarm(s), want 1", len(w))
|
|
}
|
|
}
|
|
|
|
// When the HUB's clock is wrong, every healthy box looks wrong the same way. The second box's alarm says so. A box
|
|
// off in the OTHER direction does not.
|
|
func TestBoxClock_SecondBoxSameDirectionPointsAtTheHub(t *testing.T) {
|
|
_, st, post := clockSetup(t)
|
|
post("h1", 3*time.Minute, 0, true)
|
|
post("h2", -3*time.Minute, 0, true)
|
|
w, _ := clockEvents(t, st)
|
|
if len(w) != 2 {
|
|
t.Fatalf("wrong=%d, want 2", len(w))
|
|
}
|
|
for _, e := range w {
|
|
if strings.Contains(e.Message, "HUB's own clock") {
|
|
t.Errorf("boxes off in OPPOSITE directions must not point at the hub: %s", e.Message)
|
|
}
|
|
}
|
|
post("h2", 3*time.Minute, 0, true) // h2 flips: now both are ahead
|
|
w, _ = clockEvents(t, st)
|
|
var last store.Event
|
|
for _, e := range w {
|
|
if strings.Contains(e.Message, "of h2 ") && strings.Contains(e.Message, "ahead") {
|
|
last = e
|
|
}
|
|
}
|
|
if len(w) != 3 || !strings.Contains(last.Message, "HUB's own clock") || !strings.Contains(last.Message, "h1") {
|
|
t.Fatalf("wrong=%d; the second box ahead must name the hub's clock and h1: %q", len(w), last.Message)
|
|
}
|
|
}
|