Files
archie/internal/infrastructure/controlplanestore/audit_retention_test.go
T
sam dcc12582ce
deploy / plan (push) Successful in 35s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 25s
ci/woodpecker/push/deploy Pipeline was canceled
feat(audit): set per-kind event retention from the instance (#1856)
The retention sweep applied one window to a code-fixed allowlist, and the
largest event kind (agent transcripts) was outside it, so the events table
grew whatever the instance set. The audit-retention document now takes
per-kind overrides, the dashboard offers each prunable kind its own window
from the server's list, and a kind without an override follows the default.
A security kind is refused where the document is written, and the sweep cuts
each kind at its own cutoff in one bounded, locked transaction.
2026-11-11 02:31:46 +11:00

132 lines
4.8 KiB
Go

package controlplanestore
import (
"encoding/json"
"testing"
"time"
)
// TestAuditRetentionPerKindWindows pins the retention document: a per-kind
// window replaces the default, zero keeps a kind forever, and a kind the audit
// policy does not prune is refused where it is written rather than silently
// ignored by the sweep.
func TestAuditRetentionPerKindWindows(t *testing.T) {
tests := []struct {
name string
document string
wantErr bool
check func(*testing.T, RetentionPolicy)
}{
{
name: "the default window applies to every prunable kind",
document: `{"runtime_event_retention":"720h"}`,
check: func(t *testing.T, policy RetentionPolicy) {
if policy.Default != 720*time.Hour {
t.Fatalf("default = %v, want 720h", policy.Default)
}
if len(policy.Overrides) != 0 {
t.Fatalf("overrides = %v, want none", policy.Overrides)
}
},
},
{
name: "a per-kind window replaces the default",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"168h"}}`,
check: func(t *testing.T, policy RetentionPolicy) {
if policy.Default != 720*time.Hour || policy.Overrides["agent_transcript"] != 168*time.Hour {
t.Fatalf("policy = %+v, want 720h default and 168h transcripts", policy)
}
},
},
{
name: "zero keeps one kind forever",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"0"}}`,
check: func(t *testing.T, policy RetentionPolicy) {
if window, ok := policy.Overrides["agent_transcript"]; !ok || window != 0 {
t.Fatalf("transcript override = %v (present %v), want 0", window, ok)
}
},
},
{
name: "a security kind is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"task_queued":"168h"}}`,
wantErr: true,
},
{
name: "an unknown kind is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"not_a_kind":"168h"}}`,
wantErr: true,
},
{
name: "a window under a week is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"24h"}}`,
wantErr: true,
},
{
name: "a per-kind value that is not a duration is refused",
document: `{"runtime_event_retention":"720h","event_retention":{"agent_transcript":"week"}}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
document := []byte(tt.document)
if err := validateAuditRetention(document); (err != nil) != tt.wantErr {
t.Fatalf("validateAuditRetention() error = %v, wantErr %v", err, tt.wantErr)
}
policy, err := DecodeRetentionPolicy(document)
if tt.wantErr {
if err == nil {
t.Fatalf("DecodeRetentionPolicy() error = nil, want a refusal")
}
return
}
if err != nil {
t.Fatalf("DecodeRetentionPolicy() error = %v", err)
}
tt.check(t, policy)
})
}
}
// TestAuditRetentionNormalizeKeepsWholeHours pins that every stored window is
// rewritten in whole hours, so a value written in seconds is not read as zero
// by the dashboard and silently disable pruning.
func TestAuditRetentionNormalizeKeepsWholeHours(t *testing.T) {
document := []byte(`{"runtime_event_retention":"604800s","event_retention":{"agent_transcript":"168h"}}`)
normalized, err := normalizeAuditRetention(document)
if err != nil {
t.Fatalf("normalizeAuditRetention() error = %v", err)
}
var decoded auditRetention
if err := json.Unmarshal(normalized, &decoded); err != nil {
t.Fatalf("normalized document is not JSON: %v", err)
}
if decoded.RuntimeEventRetention != "168h" {
t.Fatalf("runtime_event_retention = %q, want 168h", decoded.RuntimeEventRetention)
}
if decoded.EventRetention["agent_transcript"] != "168h" {
t.Fatalf("agent_transcript = %q, want 168h", decoded.EventRetention["agent_transcript"])
}
}
// TestRetentionPolicyCutoffs pins the sweep's rule: each kind is cut at its own
// window, a kind kept forever is absent, and no window means no delete.
func TestRetentionPolicyCutoffs(t *testing.T) {
now := time.Date(2026, 11, 11, 2, 0, 0, 0, time.UTC)
policy := RetentionPolicy{
Default: 720 * time.Hour,
Overrides: map[string]time.Duration{"agent_transcript": 168 * time.Hour},
}
cutoffs := policy.Cutoffs(now, []string{"agent_transcript", "stage_start"})
if got := cutoffs["agent_transcript"]; !got.Equal(now.Add(-168 * time.Hour)) {
t.Fatalf("agent_transcript cutoff = %v, want %v", got, now.Add(-168*time.Hour))
}
if got := cutoffs["stage_start"]; !got.Equal(now.Add(-720 * time.Hour)) {
t.Fatalf("stage_start cutoff = %v, want %v", got, now.Add(-720*time.Hour))
}
if got := (RetentionPolicy{Overrides: map[string]time.Duration{"agent_transcript": 0}}).Cutoffs(now, []string{"agent_transcript"}); len(got) != 0 {
t.Fatalf("cutoffs for a kind kept forever = %v, want none", got)
}
}