package judgement import ( "context" "encoding/json/jsontext" "errors" "log/slog" "sync/atomic" "testing" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/judgement/internal/store" ) type memoryPolicies struct{ sets map[string][]store.PolicySet } func (m *memoryPolicies) Policy(_ context.Context, _, kind string, _ int) (store.PolicySet, error) { v := m.sets[kind] if len(v) == 0 { return store.PolicySet{}, store.ErrNoPolicySet } return v[len(v)-1], nil } func (m *memoryPolicies) PolicyVersions(context.Context, string, string) ([]store.PolicySet, error) { return nil, nil } func (m *memoryPolicies) AddPolicy(_ context.Context, p store.PolicySet) (store.PolicySet, error) { p.Version = len(m.sets[p.Kind]) + 1 m.sets[p.Kind] = append(m.sets[p.Kind], p) return p, nil } // TestStarterPoliciesDecide guards the policy layer (spec section 5 rule 1, // R25): a hard rule refuses before any guard runs and a refusal names the // policy; the unknown-impact change-request rule flags a held action for a // change request, and only a held one. func TestStarterPoliciesDecide(t *testing.T) { prod := connectors.Target{Kind: "host", ID: "h1", Environment: "production"} tests := []struct { name string target connectors.Target effect connectors.Effect guard fixedGuard wantRefused string wantRule string wantLevel Level }{ { name: "irreversible in production is refused before any guard", target: prod, effect: connectors.Effect{Reversibility: connectors.Irreversible}, guard: fixedGuard{name: "g", ceiling: ActQuietly}, wantRefused: "no-irreversible-in-production", wantLevel: Never, }, { name: "irreversible in staging is judged", target: connectors.Target{Kind: "host", ID: "h1", Environment: "staging"}, effect: connectors.Effect{Reversibility: connectors.Irreversible}, guard: fixedGuard{name: "g", ceiling: ProposeAndWait}, wantLevel: ProposeAndWait, }, { name: "unknown impact held goes through a change request", target: prod, effect: connectors.Effect{}, guard: fixedGuard{name: "g", err: errors.New("no evidence")}, wantRule: "unknown-impact", wantLevel: ProposeAndWait, }, { name: "known impact held needs no change request", target: prod, effect: connectors.Effect{Reversibility: connectors.Recoverable}, guard: fixedGuard{name: "g", ceiling: ProposeAndWait}, wantLevel: ProposeAndWait, }, { name: "unknown impact that acts quietly needs no change request", target: prod, effect: connectors.Effect{}, guard: fixedGuard{name: "g", ceiling: ActQuietly}, wantLevel: ActQuietly, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var calls atomic.Int32 tt.guard.calls = &calls p := &Policies{store: &memoryPolicies{sets: map[string][]store.PolicySet{}}, log: slog.New(slog.DiscardHandler)} m := &Module{rules: []HardRule{p}, changeRules: p, guards: []Guard{tt.guard}} a := Action{Connector: "ssh", Name: "rm", Input: jsontext.Value(`{"path":"/data"}`), Target: tt.target, Effect: tt.effect} d := m.decide(withRun(t.Context(), RunContext{Workflow: "cleanup"}), a) if d.Level != tt.wantLevel || d.RefusedBy != tt.wantRefused || d.ChangeRequestRule != tt.wantRule || d.ChangeRequest != (tt.wantRule != "") { t.Fatalf("decision = level %s, refused by %q, change request %v by %q; want %s, %q, rule %q", d.Level, d.RefusedBy, d.ChangeRequest, d.ChangeRequestRule, tt.wantLevel, tt.wantRefused, tt.wantRule) } if tt.wantRefused != "" && calls.Load() != 0 { t.Fatalf("a guard ran %d times before the hard rule refused", calls.Load()) } }) } }