Cancelling a run moved it and the runs it called to cancelled and wrote nothing to either trail, so a stopped run could not say when it stopped or who asked. The stop now returns the runs it stopped and records one cancel entry on each, in the same transaction, naming the principal that asked. The entry is a step named cancel with the caller's identity, so the run detail shows it with the rest of the trail.
336 lines
12 KiB
Go
336 lines
12 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/judgement"
|
|
"github.com/samcharles93/archie-core/modules/runs/internal/store"
|
|
)
|
|
|
|
// sequenceJudge decides each action at the level it lists, and can decide one
|
|
// action differently the second time, as a target or an effect that changed
|
|
// after the person approved it.
|
|
type sequenceJudge struct {
|
|
levels map[string]judgement.Level
|
|
again map[string]func(judgement.Decision) judgement.Decision
|
|
// judged counts how often each action was judged.
|
|
judged map[string]int
|
|
ran []string
|
|
decisions []judgement.Decision
|
|
}
|
|
|
|
func (j *sequenceJudge) Assess(_ context.Context, req judgement.Request) (judgement.Decision, error) {
|
|
if j.judged == nil {
|
|
j.judged = map[string]int{}
|
|
}
|
|
j.judged[req.Action]++
|
|
d := judgement.Decision{
|
|
ID: uuid.New(), Key: req.Key, Level: j.levels[req.Action],
|
|
Action: judgement.Action{
|
|
Connector: req.Connector, Name: req.Action, Input: req.Input,
|
|
Digest: connectors.Digest(req.Action, req.Input),
|
|
Target: connectors.Target{Kind: "container", ID: "web"},
|
|
},
|
|
}
|
|
if j.judged[req.Action] > 1 {
|
|
if change := j.again[req.Action]; change != nil {
|
|
d = change(d)
|
|
}
|
|
}
|
|
j.decisions = append(j.decisions, d)
|
|
return d, nil
|
|
}
|
|
|
|
func (j *sequenceJudge) Execute(_ context.Context, d judgement.Decision, _ jsontext.Value, _ string) (string, error) {
|
|
j.ran = append(j.ran, d.Action.Name)
|
|
return "done", nil
|
|
}
|
|
|
|
func (j *sequenceJudge) ExecuteApproved(_ context.Context, d judgement.Decision, a judgement.Approval, _ jsontext.Value, _ string) (string, error) {
|
|
if !a.Covers(d) {
|
|
return "", judgement.ErrUnapproved
|
|
}
|
|
j.ran = append(j.ran, d.Action.Name)
|
|
return "done", nil
|
|
}
|
|
|
|
func (j *sequenceJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) {
|
|
return j.decisions, nil
|
|
}
|
|
|
|
// answerWork answers the run's approval wait as the case says: approval, a
|
|
// denial, or nothing at all, so the request runs out unanswered.
|
|
type answerWork struct {
|
|
fakeWork
|
|
denied bool
|
|
silent bool
|
|
}
|
|
|
|
func (w *answerWork) Signalled(context.Context, uuid.UUID, string) (jsontext.Value, bool, error) {
|
|
switch {
|
|
case w.silent:
|
|
return nil, false, nil
|
|
case w.denied:
|
|
return jsontext.Value(`{"approved":false,"by":"Second"}`), true, nil
|
|
}
|
|
return jsontext.Value(`{"approved":true,"by":"Second"}`), true, nil
|
|
}
|
|
|
|
// TestASequenceStopsWhenAnActionChangesAfterItIsApproved guards the approval of
|
|
// a sequence: one request covers every action in the order the person saw
|
|
// them, each is judged again before it runs, and an action whose target changed,
|
|
// whose judgement is now stricter, or that is now refused does not run and
|
|
// leaves the rest of the sequence unrun and reported. A request nobody approves
|
|
// runs none of it, on any judgement of its own.
|
|
func TestASequenceStopsWhenAnActionChangesAfterItIsApproved(t *testing.T) {
|
|
const doc = `steps:
|
|
- id: approve
|
|
type: approval.wait
|
|
settings: {covers: [snapshot, clean, delete, verify]}
|
|
- id: snapshot
|
|
type: connector.action
|
|
settings: {connector: host, name: snapshot}
|
|
- id: clean
|
|
type: connector.action
|
|
settings: {connector: host, name: clean}
|
|
- id: delete
|
|
type: connector.action
|
|
settings: {connector: host, name: delete}
|
|
- id: verify
|
|
type: connector.action
|
|
settings: {connector: host, name: verify}
|
|
- type: run.notify
|
|
`
|
|
held := map[string]judgement.Level{
|
|
"snapshot": judgement.ProposeAndWait, "clean": judgement.ProposeAndWait,
|
|
"delete": judgement.ProposeAndWait, "verify": judgement.ProposeAndWait,
|
|
}
|
|
targetMoved := func(d judgement.Decision) judgement.Decision {
|
|
d.Action.Target.ID = "db-2"
|
|
return d
|
|
}
|
|
stricter := func(d judgement.Decision) judgement.Decision {
|
|
d.Level = judgement.Escalate
|
|
return d
|
|
}
|
|
refused := func(d judgement.Decision) judgement.Decision {
|
|
d.Level, d.Reason = judgement.Never, "a release is in flight"
|
|
return d
|
|
}
|
|
unattended := func(d judgement.Decision) judgement.Decision {
|
|
d.Level = judgement.ActQuietly
|
|
return d
|
|
}
|
|
// Judgement lets every action of the sequence run unattended, as it may once
|
|
// the reason it was held no longer applies.
|
|
runsAlone := map[string]func(judgement.Decision) judgement.Decision{
|
|
"snapshot": unattended, "clean": unattended, "delete": unattended, "verify": unattended,
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
// again is how an action is judged the second time.
|
|
again map[string]func(judgement.Decision) judgement.Decision
|
|
// denied and silent are how the person answers the request, if at all.
|
|
denied bool
|
|
silent bool
|
|
// wantRan are the actions that ran, in order.
|
|
wantRan []string
|
|
// wantBody is what the run reports of the actions after the one that
|
|
// did not run.
|
|
wantBody []string
|
|
}{
|
|
{
|
|
name: "every action of the sequence runs",
|
|
wantRan: []string{"snapshot", "clean", "delete", "verify"},
|
|
},
|
|
{
|
|
name: "an action whose target changed after the approval does not run",
|
|
again: map[string]func(judgement.Decision) judgement.Decision{"delete": targetMoved},
|
|
wantRan: []string{"snapshot", "clean"},
|
|
wantBody: []string{
|
|
"host delete on container:web: not run, because the target changed since it was approved",
|
|
"host verify on container:web: not run, because the target changed since it was approved",
|
|
},
|
|
},
|
|
{
|
|
name: "an action judged stricter after the approval stops the rest",
|
|
again: map[string]func(judgement.Decision) judgement.Decision{"delete": stricter},
|
|
wantRan: []string{"snapshot", "clean"},
|
|
wantBody: []string{
|
|
"host delete on container:web: not run, because judgement is stricter than when the action was approved: now escalate",
|
|
"host verify on container:web: not run, because judgement is stricter than when the action was approved: now escalate",
|
|
},
|
|
},
|
|
{
|
|
name: "an action refused after the approval stops the rest",
|
|
again: map[string]func(judgement.Decision) judgement.Decision{"delete": refused},
|
|
wantRan: []string{"snapshot", "clean"},
|
|
wantBody: []string{
|
|
"host delete on container:web: not run, because refused after approval: a release is in flight",
|
|
},
|
|
},
|
|
{
|
|
name: "a denied request runs none of the actions it covered",
|
|
again: runsAlone,
|
|
denied: true,
|
|
wantBody: []string{
|
|
"host snapshot on container:web: not run, because the request was denied",
|
|
"host verify on container:web: not run, because the request was denied",
|
|
},
|
|
},
|
|
{
|
|
name: "a request nobody answers in time runs none of the actions it covered",
|
|
again: runsAlone,
|
|
silent: true,
|
|
wantBody: []string{
|
|
"host snapshot on container:web: not run, because the request expired",
|
|
"host verify on container:web: not run, because the request expired",
|
|
},
|
|
},
|
|
{
|
|
name: "every action after the one that changed is reported unrun",
|
|
again: map[string]func(judgement.Decision) judgement.Decision{"clean": targetMoved},
|
|
wantRan: []string{"snapshot"},
|
|
wantBody: []string{
|
|
"host delete on container:web: not run, because the target changed since it was approved",
|
|
"host verify on container:web: not run, because the target changed since it was approved",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
parsed, err := ParseDocument(doc)
|
|
if err != nil {
|
|
t.Fatalf("ParseDocument: %v", err)
|
|
}
|
|
judge := &sequenceJudge{levels: held, again: tt.again}
|
|
ask := &askCounter{}
|
|
told := ¬ices{}
|
|
wf := Deps{Settings: onePersonPath{}, Judge: judge, Approver: ask, Notifier: told}.Compile(parsed)
|
|
work := &answerWork{fakeWork: fakeWork{}, denied: tt.denied, silent: tt.silent}
|
|
run := store.Run{ID: uuid.New()}
|
|
rc := newContext(t.Context(), work, run, uuid.New(), nil)
|
|
if _, err := wf(rc, jsontext.Value(`{}`)); err != nil {
|
|
t.Fatalf("run error = %v", err)
|
|
}
|
|
if rc.ending == nil || rc.ending.State != store.StateSucceeded {
|
|
t.Fatalf("ending = %+v, want the run succeeded", rc.ending)
|
|
}
|
|
if ask.asked != 1 {
|
|
t.Fatalf("asked %d times, want one approval for the whole sequence", ask.asked)
|
|
}
|
|
if !slices.Equal(judge.ran, tt.wantRan) {
|
|
t.Fatalf("ran %q, want %q", judge.ran, tt.wantRan)
|
|
}
|
|
if len(told.sent) != 1 {
|
|
t.Fatalf("notices = %+v, want one", told.sent)
|
|
}
|
|
for _, want := range tt.wantBody {
|
|
if !strings.Contains(told.sent[0].Body, want) {
|
|
t.Fatalf("report is %q, want it to contain %q", told.sent[0].Body, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestACoveredStepReportsTheSameResultAsAnUncoveredOne guards what a covered
|
|
// action hands the steps after the sequence: the state and the output an action
|
|
// step reports when no approval covers it.
|
|
func TestACoveredStepReportsTheSameResultAsAnUncoveredOne(t *testing.T) {
|
|
doc, err := ParseDocument(`steps:
|
|
- id: approve
|
|
type: approval.wait
|
|
settings: {covers: [snapshot]}
|
|
- id: snapshot
|
|
type: connector.action
|
|
settings: {connector: host, name: snapshot}
|
|
- type: message.send
|
|
settings: {channel: chat, chat_id: ops, text: "{{ steps.snapshot.result.state }} {{ steps.snapshot.result.result }}"}
|
|
`)
|
|
if err != nil {
|
|
t.Fatalf("ParseDocument: %v", err)
|
|
}
|
|
judge := &sequenceJudge{levels: map[string]judgement.Level{"snapshot": judgement.ProposeAndWait}}
|
|
sent := &chats{}
|
|
wf := Deps{Settings: onePersonPath{}, Judge: judge, Approver: &askCounter{}, Messenger: sent, Notifier: ¬ices{}}.Compile(doc)
|
|
rc := newContext(t.Context(), &answerWork{fakeWork: fakeWork{}}, store.Run{ID: uuid.New()}, uuid.New(), nil)
|
|
if _, err := wf(rc, jsontext.Value(`{}`)); err != nil {
|
|
t.Fatalf("run error = %v", err)
|
|
}
|
|
if want := []string{"chat/ops: ran done"}; !slices.Equal(sent.sent, want) {
|
|
t.Fatalf("sent %q, want %q", sent.sent, want)
|
|
}
|
|
}
|
|
|
|
// TestCoversNamesLaterActionSteps guards the save-time check on a sequence: an
|
|
// approval covers action steps after it, each one only once and none of them
|
|
// reading another step of the same approval, so every action it covers can be
|
|
// judged when it asks.
|
|
func TestCoversNamesLaterActionSteps(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
doc string
|
|
want string
|
|
}{
|
|
{
|
|
name: "an approval covering later action steps is accepted",
|
|
doc: `steps:
|
|
- {type: approval.wait, settings: {covers: [snapshot]}}
|
|
- {id: snapshot, type: connector.action, settings: {connector: host, name: snapshot}}
|
|
`,
|
|
},
|
|
{
|
|
name: "a covered step before the approval is refused",
|
|
doc: `steps:
|
|
- {id: snapshot, type: connector.action, settings: {connector: host, name: snapshot}}
|
|
- {type: approval.wait, settings: {covers: [snapshot]}}
|
|
`,
|
|
want: `steps[1].settings.covers[0]: no step after this one has id "snapshot"`,
|
|
},
|
|
{
|
|
name: "a covered step that is not an action is refused",
|
|
doc: `steps:
|
|
- {type: approval.wait, settings: {covers: [announce]}}
|
|
- {id: announce, type: message.send, settings: {channel: chat, chat_id: ops, text: hi}}
|
|
`,
|
|
want: `steps[0].settings.covers[0]: step "announce" is message.send, and an approval covers connector.action steps only`,
|
|
},
|
|
{
|
|
name: "a step named twice by one approval is refused",
|
|
doc: `steps:
|
|
- {type: approval.wait, settings: {covers: [snapshot, snapshot]}}
|
|
- {id: snapshot, type: connector.action, settings: {connector: host, name: snapshot}}
|
|
`,
|
|
want: `steps[0].settings: covers names step "snapshot" twice`,
|
|
},
|
|
{
|
|
name: "a covered step that reads another covered step is refused",
|
|
doc: `steps:
|
|
- {type: approval.wait, settings: {covers: [snapshot, delete]}}
|
|
- {id: snapshot, type: connector.action, settings: {connector: host, name: snapshot}}
|
|
- {id: delete, type: connector.action, settings: {connector: host, name: delete, input: {taken: "{{ steps.snapshot.result.id }}"}}}
|
|
`,
|
|
want: `steps[2].settings: step "delete" reads step "snapshot", which the same approval covers and which hasn't run yet when it asks`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := ParseDocument(tt.doc)
|
|
switch {
|
|
case tt.want == "" && err != nil:
|
|
t.Fatalf("ParseDocument: %v", err)
|
|
case tt.want != "" && (err == nil || !strings.Contains(err.Error(), tt.want)):
|
|
t.Fatalf("ParseDocument error = %v, want %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|