Files
ClusterCanvas/service/internal/runner/executor_sudo_test.go
T
Squid 0e06063c1d Clarify run-if skip reasons with the resolved variable value.
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.
2026-07-19 21:59:35 +02:00

100 lines
2.7 KiB
Go

package runner
import (
"testing"
"codeberg.org/SquidSE/ClusterCanvas/service/internal/settings"
)
func TestWrapShellWithSudo(t *testing.T) {
if got := WrapShellWithSudo("/usr/bin/systemctl restart myservice", false); got != "/usr/bin/systemctl restart myservice" {
t.Fatalf("without sudo = %q", got)
}
if got := WrapShellWithSudo("/usr/bin/systemctl restart myservice", true); got != "sudo -n /usr/bin/systemctl restart myservice" {
t.Fatalf("with sudo = %q", got)
}
}
func TestScriptRemoteCommand(t *testing.T) {
if got := ScriptRemoteCommand(false); got != "bash -s" {
t.Fatalf("without sudo = %q", got)
}
if got := ScriptRemoteCommand(true); got != "sudo -n bash -s" {
t.Fatalf("with sudo = %q", got)
}
}
func TestBuildRemoteCommand(t *testing.T) {
shellCmd := BuildRemoteCommand(settings.ActionKindShell, "apt update", true, nil)
if shellCmd != "sudo -n apt update" {
t.Fatalf("shell = %q", shellCmd)
}
scriptCmd := BuildRemoteCommand(settings.ActionKindScript, "apt update", true, nil)
if scriptCmd != "sudo -n bash -s" {
t.Fatalf("script = %q", scriptCmd)
}
withEnv := BuildRemoteCommand(
settings.ActionKindShell,
"apt update",
true,
map[string]string{"FOO": "bar"},
)
if withEnv != "export 'FOO'='bar'; sudo -n apt update" {
t.Fatalf("with env = %q", withEnv)
}
}
func TestResolveItemRequiresSudo(t *testing.T) {
library := []settings.Action{
{
ID: "lib-1",
Name: "Restart",
Kind: settings.ActionKindShell,
Body: "/usr/bin/systemctl restart myservice",
RequiresSudo: true,
Env: []settings.ActionEnvVar{},
},
}
resolved, err := ResolveItem(settings.NodeActionItem{
ID: "i1",
Source: settings.ActionItemSourceLibrary,
LibraryActionID: "lib-1",
}, library)
if err != nil {
t.Fatalf("resolve library: %v", err)
}
if !resolved.RequiresSudo || resolved.Body != "/usr/bin/systemctl restart myservice" {
t.Fatalf("library resolved = %#v", resolved)
}
local, err := ResolveItem(settings.NodeActionItem{
ID: "i2",
Source: settings.ActionItemSourceLocal,
Name: "Local restart",
Kind: settings.ActionKindShell,
Body: "/usr/bin/true",
RequiresSudo: true,
}, library)
if err != nil {
t.Fatalf("resolve local: %v", err)
}
if !local.RequiresSudo {
t.Fatalf("local resolved = %#v", local)
}
noSudo, err := ResolveItem(settings.NodeActionItem{
ID: "i3",
Source: settings.ActionItemSourceLocal,
Name: "Uptime",
Kind: settings.ActionKindShell,
Body: "uptime",
}, library)
if err != nil {
t.Fatalf("resolve no-sudo: %v", err)
}
if noSudo.RequiresSudo {
t.Fatalf("expected RequiresSudo false, got %#v", noSudo)
}
}