43 lines
1.6 KiB
Go
43 lines
1.6 KiB
Go
package rules
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/actions"
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/alerts"
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/analysis"
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/loki"
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/store"
|
|
)
|
|
|
|
func TestCooldownSuppressesDuplicateIncident(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"status":"success","data":{"result":[{"stream":{},"values":[["1700000000000000000","error"]]}]}}`))
|
|
}))
|
|
defer ts.Close()
|
|
|
|
s := store.NewFileStore(t.TempDir() + "/config.json")
|
|
if err := s.UpsertRule(store.Rule{Name: "Errors", Enabled: true, LogQL: `{service="api"}`, Threshold: 1, Window: "5m", Cooldown: "1h", Actions: []store.Action{{Type: "record_recommendation", Enabled: true}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine := NewEngine(s, loki.New(ts.URL, "", "", ""), analysis.NoopAnalyzer{}, actions.NewRunner(true), alerts.NewDispatcher())
|
|
engine.CheckAll(t.Context())
|
|
engine.CheckAll(t.Context())
|
|
data, err := s.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(data.Incidents) != 1 {
|
|
t.Fatalf("expected one incident, got %d", len(data.Incidents))
|
|
}
|
|
if data.Rules[0].SuppressedCount != 1 {
|
|
t.Fatalf("expected suppressed count 1, got %d", data.Rules[0].SuppressedCount)
|
|
}
|
|
if time.Since(data.Rules[0].LastAlertedAt) > time.Minute {
|
|
t.Fatalf("last alerted not set: %s", data.Rules[0].LastAlertedAt)
|
|
}
|
|
}
|