Failed conditions now explain skips in plain language (e.g. UpdateCount was 0) and truncate long captures so misconfigured apt listings cannot flood the UI.
85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package runner
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"codeberg.org/SquidSE/ClusterCanvas/service/internal/settings"
|
|
)
|
|
|
|
func TestRunScopedVariableSequence(t *testing.T) {
|
|
vars := map[string]string{}
|
|
|
|
// Action 1 captures trimmed stdout into UpdateCount.
|
|
captured := strings.TrimSpace("3\n")
|
|
vars["UpdateCount"] = captured
|
|
if vars["UpdateCount"] != "3" {
|
|
t.Fatalf("captured = %q", vars["UpdateCount"])
|
|
}
|
|
|
|
// Action 2 condition passes and body expands.
|
|
ok, detail, err := EvaluateCondition("if $UpdateCount >= 1", vars)
|
|
if err != nil || !ok {
|
|
t.Fatalf("condition should pass: ok=%v err=%v", ok, err)
|
|
}
|
|
if detail != "$UpdateCount=3 >= 1" {
|
|
t.Fatalf("detail = %q", detail)
|
|
}
|
|
body := ExpandDollarVars("echo upgrades=$UpdateCount", vars)
|
|
if body != "echo upgrades=3" {
|
|
t.Fatalf("body = %q", body)
|
|
}
|
|
|
|
// Action 3 condition fails → skipped.
|
|
ok, detail, err = EvaluateCondition("if $UpdateCount >= 10", vars)
|
|
if err != nil || ok {
|
|
t.Fatalf("condition should fail: ok=%v err=%v", ok, err)
|
|
}
|
|
wantFail := "$UpdateCount was 3, which does not meet run condition (>= 10)"
|
|
if detail != wantFail {
|
|
t.Fatalf("detail = %q, want %q", detail, wantFail)
|
|
}
|
|
|
|
// A fresh run starts with an empty map (no leak).
|
|
nextRun := map[string]string{}
|
|
ok, detail, err = EvaluateCondition("if $UpdateCount >= 1", nextRun)
|
|
if err != nil || ok {
|
|
t.Fatalf("unset in new run should be false: ok=%v err=%v", ok, err)
|
|
}
|
|
wantUnset := "$UpdateCount was unset, which does not meet run condition (>= 1)"
|
|
if detail != wantUnset {
|
|
t.Fatalf("detail = %q, want %q", detail, wantUnset)
|
|
}
|
|
}
|
|
|
|
func TestSkipResultShape(t *testing.T) {
|
|
item := settings.NodeActionItem{
|
|
ID: "i1",
|
|
Name: "Upgrade",
|
|
RunIf: "if $UpdateCount >= 1",
|
|
}
|
|
vars := map[string]string{"UpdateCount": "0"}
|
|
shouldRun, detail, condErr := EvaluateCondition(item.RunIf, vars)
|
|
if condErr != nil {
|
|
t.Fatalf("eval: %v", condErr)
|
|
}
|
|
if shouldRun {
|
|
t.Fatal("expected skip")
|
|
}
|
|
skipReason := item.Name + " skipped: run condition was not met"
|
|
if detail != "" {
|
|
skipReason = item.Name + " skipped: " + detail
|
|
}
|
|
result := settings.ActionItemRunResult{
|
|
ItemID: item.ID,
|
|
ActionName: item.Name,
|
|
Skipped: true,
|
|
SkipReason: skipReason,
|
|
ExitCode: 0,
|
|
}
|
|
want := "Upgrade skipped: $UpdateCount was 0, which does not meet run condition (>= 1)"
|
|
if !result.Skipped || result.SkipReason != want {
|
|
t.Fatalf("result = %#v, want SkipReason %q", result, want)
|
|
}
|
|
}
|