Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
139 lines
5.6 KiB
Go
139 lines
5.6 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/approvals"
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
"github.com/samcharles93/archie-core/modules/judgement"
|
|
"github.com/samcharles93/archie-core/modules/runs/internal/store"
|
|
)
|
|
|
|
// TestAfterApprovalDiscardsAnApprovalThatNoLongerMatchesTheAction checks that
|
|
// a changed target or effect, or a stricter verdict, invalidates the approval.
|
|
func TestAfterApprovalDiscardsAnApprovalThatNoLongerMatchesTheAction(t *testing.T) {
|
|
decided := func(level judgement.Level, mutate func(*judgement.Decision)) judgement.Decision {
|
|
d := judgement.Decision{Level: level, Action: judgement.Action{
|
|
Target: connectors.Target{Kind: "service", ID: "api", Environment: "prod"},
|
|
Effect: connectors.Effect{Summary: "restart"},
|
|
}}
|
|
if mutate != nil {
|
|
mutate(&d)
|
|
}
|
|
return d
|
|
}
|
|
approved := decided(judgement.ProposeAndWait, nil)
|
|
tests := []struct {
|
|
name string
|
|
fresh judgement.Decision
|
|
want afterwards
|
|
}{
|
|
{name: "an unchanged action uses the existing approval", fresh: decided(judgement.ProposeAndWait, nil), want: runApproval},
|
|
{name: "a stricter verdict requires new approval", fresh: decided(judgement.Escalate, nil), want: reaskPerson},
|
|
{name: "a changed target requires new approval", fresh: decided(judgement.ProposeAndWait, func(d *judgement.Decision) {
|
|
d.Action.Target.ID = "db"
|
|
}), want: reaskPerson},
|
|
{name: "a changed environment requires new approval", fresh: decided(judgement.ProposeAndWait, func(d *judgement.Decision) {
|
|
d.Action.Target.Environment = "staging"
|
|
}), want: reaskPerson},
|
|
{name: "a changed effect requires new approval", fresh: decided(judgement.ProposeAndWait, func(d *judgement.Decision) {
|
|
d.Action.Effect.Summary = "delete"
|
|
}), want: reaskPerson},
|
|
{name: "a refusal discards the approval", fresh: decided(judgement.Never, nil), want: refuseAction},
|
|
{name: "a verdict that lets it run needs no approval", fresh: decided(judgement.ActAndTell, nil), want: runFresh},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, why := afterApproval(approved, tt.fresh)
|
|
if got != tt.want {
|
|
t.Fatalf("afterApproval = %v (%s), want %v", got, why, tt.want)
|
|
}
|
|
if (got == reaskPerson || got == refuseAction) && why == "" {
|
|
t.Fatal("a discarded approval gives no reason")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// signalAfter is a worker store whose answer arrives once the run has waited
|
|
// that many times, as when a person answers during a later escalation step.
|
|
type signalAfter struct {
|
|
fakeWork
|
|
waits int
|
|
arrives int
|
|
}
|
|
|
|
func (s *signalAfter) Signalled(context.Context, uuid.UUID, string) (jsontext.Value, bool, error) {
|
|
s.waits++
|
|
if s.arrives > 0 && s.waits >= s.arrives {
|
|
return jsontext.Value(`{"approved":true,"by":"Second"}`), true, nil
|
|
}
|
|
return nil, false, nil
|
|
}
|
|
|
|
// escalations records what the run asked of approvals.
|
|
type escalations struct {
|
|
steps []int
|
|
expired int
|
|
}
|
|
|
|
func (e *escalations) Ask(context.Context, uuid.UUID, string, judgement.Decision, []governance.PathStep) (approvals.Request, error) {
|
|
return approvals.Request{}, nil
|
|
}
|
|
|
|
func (e *escalations) Escalate(_ context.Context, _ uuid.UUID, step int, _ governance.PathStep) error {
|
|
e.steps = append(e.steps, step)
|
|
return nil
|
|
}
|
|
|
|
func (e *escalations) Expire(context.Context, uuid.UUID) error { e.expired++; return nil }
|
|
|
|
func (e *escalations) Accept(context.Context, uuid.UUID) error { return nil }
|
|
|
|
func (e *escalations) Discard(context.Context, uuid.UUID, string) error { return nil }
|
|
|
|
// TestWaitThroughPathMovesOnAtEachDeadlineAndExpiresAtTheEnd guards the timed
|
|
// escalation path: an unanswered request passes to each next step in order,
|
|
// the last step's expiry settles it as expired with nothing acted on, and an
|
|
// answer at any step stops the escalation there.
|
|
func TestWaitThroughPathMovesOnAtEachDeadlineAndExpiresAtTheEnd(t *testing.T) {
|
|
// A zero deadline has passed by the time it is waited on.
|
|
step := func(person string) governance.PathStep { return governance.PathStep{Person: person} }
|
|
tests := []struct {
|
|
name string
|
|
path []governance.PathStep
|
|
arrives int
|
|
wantSteps []int
|
|
wantExpired int
|
|
wantAnswered bool
|
|
}{
|
|
{name: "one step with no answer expires without escalating", path: []governance.PathStep{step("a")}, wantExpired: 1},
|
|
{name: "every step runs out and then the request expires", path: []governance.PathStep{step("a"), step("b"), step("c")}, wantSteps: []int{1, 2}, wantExpired: 1},
|
|
{name: "an answer at the second step stops the escalation", path: []governance.PathStep{step("a"), step("b"), step("c")}, arrives: 2, wantSteps: []int{1}, wantAnswered: true},
|
|
{name: "an answer at the first step prevents escalation", path: []governance.PathStep{step("a"), step("b")}, arrives: 1, wantAnswered: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
work := &signalAfter{arrives: tt.arrives}
|
|
rc := newContext(context.Background(), work, store.Run{ID: uuid.New()}, uuid.New(), nil)
|
|
ask := &escalations{}
|
|
answer, expired, err := waitThroughPath(rc, ask, "k", approvals.Request{ID: uuid.New()}, tt.path)
|
|
if err != nil {
|
|
t.Fatalf("waitThroughPath: %v", err)
|
|
}
|
|
if !slices.Equal(ask.steps, tt.wantSteps) || ask.expired != tt.wantExpired {
|
|
t.Fatalf("escalated %v and expired %d times, want %v and %d", ask.steps, ask.expired, tt.wantSteps, tt.wantExpired)
|
|
}
|
|
if expired != (tt.wantExpired > 0) || answer.Approved != tt.wantAnswered {
|
|
t.Fatalf("expired = %v, approved = %v", expired, answer.Approved)
|
|
}
|
|
})
|
|
}
|
|
}
|