feat(memory,runs,governance): remember each target's incidents, cite them in a run, and propose a root-cause fix after K with one cause (archie #1993)
This commit is contained in:
14 files changed
+774
-26
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/modules/memory/internal/store"
|
||||
)
|
||||
|
||||
// fakeLedger holds one target's incidents, newest first, and the proposals made.
|
||||
type fakeLedger struct {
|
||||
rows []store.Incident
|
||||
proposals []store.Proposal
|
||||
}
|
||||
|
||||
func (f *fakeLedger) Upsert(context.Context, string, store.Incident) error { return nil }
|
||||
|
||||
func (f *fakeLedger) SetActions(context.Context, string, uuid.UUID, []byte) error { return nil }
|
||||
|
||||
func (f *fakeLedger) Incident(_ context.Context, _ string, runID uuid.UUID) (store.Incident, bool, error) {
|
||||
for _, r := range f.rows {
|
||||
if r.RunID == runID {
|
||||
return r, true, nil
|
||||
}
|
||||
}
|
||||
return store.Incident{}, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeLedger) ByTarget(context.Context, string, string, string, int) ([]store.Incident, error) {
|
||||
return f.rows, nil
|
||||
}
|
||||
|
||||
func (f *fakeLedger) Propose(_ context.Context, _ string, p store.Proposal) (store.Proposal, error) {
|
||||
f.proposals = append(f.proposals, p)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// TestFinishProposesOnRecurrence guards the rule that a cause seen threshold
|
||||
// times on a target raises a root-cause fix, whatever happened to the actions
|
||||
// taken in each incident, and that other causes don't count towards it.
|
||||
func TestFinishProposesOnRecurrence(t *testing.T) {
|
||||
now := time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC)
|
||||
run := uuid.New()
|
||||
incidents := func(causes ...string) []store.Incident {
|
||||
rows := []store.Incident{{RunID: run, Cause: causes[0], At: now}}
|
||||
for i, c := range causes[1:] {
|
||||
rows = append(rows, store.Incident{RunID: uuid.New(), Cause: c, At: now.AddDate(0, 0, -i-1)})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
rows []store.Incident
|
||||
threshold int
|
||||
want bool
|
||||
}{
|
||||
{"one short of the threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, false},
|
||||
{"the threshold is met by this incident", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
|
||||
{"other causes are not counted", incidents("disk-pressure", "oom", "oom", "oom", "oom", "disk-pressure"), 5, false},
|
||||
{"the threshold is the organisation's", incidents("disk-pressure", "disk-pressure", "disk-pressure"), 3, true},
|
||||
{"past the threshold still proposes", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ledger := &fakeLedger{rows: tt.rows}
|
||||
m := &Module{orgID: "org", ledger: ledger}
|
||||
review, err := m.Finish(t.Context(), run, []Action{{Action: "restart", Target: "host:web-1", State: "denied"}}, tt.threshold)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := review.Proposal != ""; got != tt.want || (len(ledger.proposals) == 1) != tt.want {
|
||||
t.Fatalf("proposal = %q, recorded %d; want proposal %v", review.Proposal, len(ledger.proposals), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user