Files
felhom.eu/hub/internal/notify/r672_storage_cooldown_test.go
T
2026-09-24 16:26:17 +02:00

54 lines
2.2 KiB
Go

package notify
import (
"io"
"log"
"sync"
"testing"
"time"
)
// R-672 (hub v0.124.0): storage-fill alarms are "one operator event per POOL per 6 hours". The
// consequence asserted is the mail count.
//
// COMPANION RED-PROOF (REPORT): drop cooldownStorageSuffix from the key and the 6-hour window (hub
// v0.123.0: `customer:type`, 1 hour) → "a second pool filling in the same hour was silenced".
func TestR672_StorageFillIsPerPoolPerSixHours(t *testing.T) {
st := newDispStore(t)
d := NewDispatcher(st, "test-key", "from@felhom.eu", "op@felhom.eu", true, log.New(io.Discard, "", 0))
var mu sync.Mutex
sent := 0
d.sendEmailFn = func(string, string, string, map[string]string) error { mu.Lock(); sent++; mu.Unlock(); return nil }
count := func() int { mu.Lock(); defer mu.Unlock(); return sent }
pool := func(host, s string) string { return `{"host_id":"` + host + `","storage":"` + s + `","percent":91}` }
d.processOperator("c1", "storage_fill_critical", "critical", "pool A full", pool("h1", "local-lvm"), "hub")
d.processOperator("c1", "storage_fill_critical", "critical", "pool B full", pool("h1", "fast-lvm"), "hub")
if count() != 2 {
t.Fatalf("a second pool filling in the same hour was silenced (sent=%d)", count())
}
d.processOperator("c1", "storage_fill_critical", "critical", "pool A again", pool("h1", "local-lvm"), "hub")
if count() != 2 {
t.Fatalf("the same pool mailed twice at once (sent=%d)", count())
}
// Two hours later the SAME pool is still inside its 6-hour window (the old 1-hour window re-mailed it).
d.mu.Lock()
for k := range d.opCooldowns {
d.opCooldowns[k] = time.Now().Add(-2 * time.Hour)
}
d.mu.Unlock()
d.processOperator("c1", "storage_fill_critical", "critical", "pool A 2h later", pool("h1", "local-lvm"), "hub")
if count() != 2 {
t.Fatalf("the same pool was mailed again after 2 hours — want one per 6 hours (sent=%d)", count())
}
d.mu.Lock()
for k := range d.opCooldowns {
d.opCooldowns[k] = time.Now().Add(-7 * time.Hour)
}
d.mu.Unlock()
d.processOperator("c1", "storage_fill_critical", "critical", "pool A 7h later", pool("h1", "local-lvm"), "hub")
if count() != 3 {
t.Fatalf("after 6 hours the pool must alarm again (sent=%d)", count())
}
}