package runs import ( "context" "encoding/json/jsontext" "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" ) type decidingJudge struct { decision judgement.Decision executed int } func (j *decidingJudge) Assess(context.Context, judgement.Request) (judgement.Decision, error) { return j.decision, nil } func (j *decidingJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) { j.executed++ return "done", nil } func (j *decidingJudge) ExecuteApproved(context.Context, judgement.Decision, judgement.Approval, jsontext.Value, string) (string, error) { j.executed++ return "done", nil } func (j *decidingJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) { return nil, nil } type anyBox struct{} func (anyBox) Declarations() []connectors.Declaration { return nil } func (anyBox) Tool(string) (connectors.Tool, bool) { return nil, true } // TestDryRunNeverExecutesAChangingAction guards the dry run's promise: an // action that changes something is judged and never executed, however freely // judgement would let it run, while one that only reads runs for real. func TestDryRunNeverExecutesAChangingAction(t *testing.T) { decided := func(level judgement.Level, readOnly bool) judgement.Decision { d := judgement.Decision{Level: level} d.Action.Effect.ReadOnly = readOnly return d } tests := []struct { name string dry bool decision judgement.Decision wantExecuted int }{ {name: "dry: a changing action at act quietly is not executed", dry: true, decision: decided(judgement.ActQuietly, false)}, {name: "dry: a changing action at act and tell is not executed", dry: true, decision: decided(judgement.ActAndTell, false)}, {name: "dry: a read-only action runs for real", dry: true, decision: decided(judgement.ActQuietly, true), wantExecuted: 1}, {name: "live: a changing action at act quietly runs", decision: decided(judgement.ActQuietly, false), wantExecuted: 1}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { judge := &decidingJudge{decision: tt.decision} rc := newContext(t.Context(), &fakeWork{}, store.Run{ID: uuid.New(), Dry: tt.dry}, uuid.New(), nil) decl := connectors.Declaration{Connector: "c", Kind: connectors.KindAction, Name: "restart"} if _, err := call(t.Context(), anyBox{}, judge, rc, decl, jsontext.Value(`{}`)); err != nil { t.Fatalf("call: %v", err) } if judge.executed != tt.wantExecuted { t.Fatalf("executed %d times, want %d", judge.executed, tt.wantExecuted) } }) } }