fix(workflow): apply step retry and on_failure to command.run
This commit is contained in:
2 files changed
+110
-9
No files matched your search
@@ -2,6 +2,7 @@ package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -95,15 +96,11 @@ func newCommandRunStage(settings yaml.Node) (Stage, error) {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Park rather than return an error: a returned error fails the
|
||||
// attempt, while a park leaves the run for an operator to retry
|
||||
// after fixing what the command found. An outcome stops the
|
||||
// engine at this step, so later steps do not run.
|
||||
tc.Outcome = Outcome{
|
||||
Status: StatusParked,
|
||||
Detail: fmt.Sprintf("%s: %s", CommandRunStepName, detail),
|
||||
}
|
||||
return nil
|
||||
// Return the failure, with the command's clipped output as its
|
||||
// text: the shared step wrapper applies retry and on_failure, and
|
||||
// the engine parks with this message. Setting a parked outcome
|
||||
// here instead would stop the step before either control ran.
|
||||
return errors.New(detail)
|
||||
}}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
|
||||
// settingsNode renders a command.run settings value the way a definition
|
||||
// carries it: a yaml.Node the factory decodes.
|
||||
func settingsNode(t *testing.T, s commandRunSettings) yaml.Node {
|
||||
t.Helper()
|
||||
raw, err := yaml.Marshal(s)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal settings: %v", err)
|
||||
}
|
||||
var n yaml.Node
|
||||
if err := yaml.Unmarshal(raw, &n); err != nil {
|
||||
t.Fatalf("settings node: %v", err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// TestCommandRunObeysStepControls pins that a failing command.run step is a
|
||||
// step failure like any other: retry re-runs it, on_failure: continue keeps
|
||||
// the run going, and only level: warn is advisory. Reporting the failure as
|
||||
// a parked outcome instead left all three controls unapplied.
|
||||
func TestCommandRunObeysStepControls(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
level string
|
||||
onFailure string
|
||||
attempts int
|
||||
wantRuns int
|
||||
wantErr bool
|
||||
wantContinues bool
|
||||
}{
|
||||
{
|
||||
name: "retry re-runs a failed command",
|
||||
attempts: 3,
|
||||
wantRuns: 3,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "on_failure continue keeps the run going",
|
||||
onFailure: onFailureContinue,
|
||||
wantRuns: 1,
|
||||
wantErr: true,
|
||||
wantContinues: true,
|
||||
},
|
||||
{
|
||||
name: "level warn is advisory",
|
||||
level: commandRunLevelWarn,
|
||||
wantRuns: 1,
|
||||
},
|
||||
}
|
||||
registry := StepRegistry{CommandRunStepName: newCommandRunStage}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
marker := filepath.Join(t.TempDir(), "runs")
|
||||
settings := commandRunSettings{
|
||||
Level: tt.level,
|
||||
Run: []commandRunCommand{{
|
||||
Name: "probe",
|
||||
Argv: []string{"sh", "-c", fmt.Sprintf("echo run >> %q; exit 1", marker)},
|
||||
}},
|
||||
}
|
||||
step := StepRecord{Type: CommandRunStepName, Settings: settingsNode(t, settings), OnFailure: tt.onFailure}
|
||||
if tt.attempts > 0 {
|
||||
step.Retry = &RetryPolicy{Attempts: tt.attempts}
|
||||
}
|
||||
stage, err := compileStep(step, registry)
|
||||
if err != nil {
|
||||
t.Fatalf("compile step: %v", err)
|
||||
}
|
||||
if stage.ContinueOnFailure != tt.wantContinues {
|
||||
t.Fatalf("ContinueOnFailure = %v, want %v", stage.ContinueOnFailure, tt.wantContinues)
|
||||
}
|
||||
tc := &TaskContext{Task: &task.Task{ID: 1}, Dir: t.TempDir(), Log: slog.Default()}
|
||||
runErr := stage.Run(context.Background(), tc)
|
||||
if (runErr != nil) != tt.wantErr {
|
||||
t.Fatalf("err = %v, want error %v", runErr, tt.wantErr)
|
||||
}
|
||||
if runErr != nil && strings.TrimSpace(runErr.Error()) == "" {
|
||||
t.Fatal("failure carried no detail for the park reason")
|
||||
}
|
||||
raw, err := os.ReadFile(marker)
|
||||
if err != nil {
|
||||
t.Fatalf("read marker: %v", err)
|
||||
}
|
||||
if got := len(strings.Fields(string(raw))); got != tt.wantRuns {
|
||||
t.Fatalf("command ran %d times, want %d", got, tt.wantRuns)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user