style(alerting): align quiet hours code and tests with Go style guide
ci / image (pull_request) Successful in 4m28s
ci / build (pull_request) Successful in 8m16s
ci / lint (pull_request) Successful in 12m11s
ci / test (pull_request) Successful in 15m40s

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 19:43:17 +01:00
1 parent ee3769f578
commit 1c31003baf
4 files changed
+162 -94

No files matched your search

+24 -13
View File
@@ -159,9 +159,15 @@ func TestAlertGroupQuietHours(t *testing.T) {
owner.signup("q@"+uniq("agq")+".test", uniq("agq-"))
channel := func(name string) string {
t.Helper()
st, body := owner.do("POST", "/api/v1/org/channels", map[string]any{"name": name, "type": "webhook", "url": "https://example.test/hook", "enabled": true})
req := map[string]any{
"name": name,
"type": "webhook",
"url": "https://example.test/hook",
"enabled": true,
}
st, body := owner.do("POST", "/api/v1/org/channels", req)
if st != http.StatusCreated {
t.Fatalf("create channel = %d %v", st, body)
t.Fatalf("POST /api/v1/org/channels = %d %v, want %d", st, body, http.StatusCreated)
}
return body["channel"].(map[string]any)["id"].(string)
}
@@ -245,30 +251,35 @@ func TestAlertGroupQuietHours(t *testing.T) {
}
}
count := func(ch string) int {
return e.count(t, `SELECT count(*) FROM notifications n JOIN incidents i ON i.id=n.incident_id WHERE i.monitor_id=$1 AND n.channel_id=$2`, monID, ch)
const q = `SELECT count(*) FROM notifications n
JOIN incidents i ON i.id = n.incident_id
WHERE i.monitor_id = $1 AND n.channel_id = $2`
return e.count(t, q, monID, ch)
}
ping("/fail")
ping("")
if inc := e.count(t, `SELECT count(*) FROM incidents WHERE monitor_id=$1`, monID); inc != 1 {
t.Fatalf("incidents = %d, want 1", inc)
}
if count(quietCh) != 0 || count(sharedCh) != 0 {
t.Fatalf("muted group sent notifications: quiet=%d shared=%d", count(quietCh), count(sharedCh))
if q, s := count(quietCh), count(sharedCh); q != 0 || s != 0 {
t.Fatalf("muted notifications quiet/shared = %d/%d, want 0/0", q, s)
}
// Assign both Muted and AlwaysOn: only AlwaysOn's channels (activeCh + sharedCh) notify,
// while quietCh stays silent.
st, body = owner.do("PUT", "/api/v1/org/monitors/"+monID, map[string]any{
"name": "SilentBackup", "type": "heartbeat", "interval_seconds": 3600,
"alert_group_ids": []string{mutedGroup, activeGroup},
"name": "SilentBackup",
"type": "heartbeat",
"interval_seconds": 3600,
"alert_group_ids": []string{mutedGroup, activeGroup},
})
if st != http.StatusOK {
t.Fatalf("update monitor = %d %v", st, body)
t.Fatalf("PUT /api/v1/org/monitors/%s = %d %v, want %d", monID, st, body, http.StatusOK)
}
ping("/fail")
ping("")
if count(quietCh) != 0 || count(activeCh) != 2 || count(sharedCh) != 2 {
t.Fatalf("mixed quiet/active deliveries quiet/active/shared = %d/%d/%d, want 0/2/2", count(quietCh), count(activeCh), count(sharedCh))
if q, a, s := count(quietCh), count(activeCh), count(sharedCh); q != 0 || a != 2 || s != 2 {
t.Fatalf("mixed deliveries quiet/active/shared = %d/%d/%d, want 0/2/2", q, a, s)
}
// Explicitly clearing quiet_hours re-enables notifications on quietCh.
@@ -278,11 +289,11 @@ func TestAlertGroupQuietHours(t *testing.T) {
"quiet_hours": []map[string]any{},
})
if st != http.StatusOK || len(body["quiet_hours"].([]any)) != 0 {
t.Fatalf("clear quiet_hours = %d %v", st, body)
t.Fatalf("clear quiet_hours = %d %v, want 200 with empty quiet_hours", st, body)
}
ping("/fail")
ping("")
if count(quietCh) != 2 || count(activeCh) != 4 || count(sharedCh) != 4 {
t.Fatalf("after clearing quiet hours quiet/active/shared = %d/%d/%d, want 2/4/4", count(quietCh), count(activeCh), count(sharedCh))
if q, a, s := count(quietCh), count(activeCh), count(sharedCh); q != 2 || a != 4 || s != 4 {
t.Fatalf("after clearing quiet hours quiet/active/shared = %d/%d/%d, want 2/4/4", q, a, s)
}
}