Files
osmedeus/test/e2e/sudo_test.go
T
j3ssie bd1434739e feat: add sudo authentication support with keepalive and variable renames
- Add sudo_auth() function for TTY prompts and credential management with optional 4-minute keepalive loop
- Add --sudo-aware CLI flag to detect workflows with sudo commands and offer authentication guidance
- Add sudo step scanner to detect sudo usage across all step types (bash, parallel, foreach) and nested structures
- Add parse_url_file() function to batch-process URLs with format directives, supporting bare IPs and CIDR notation
- Add portscan test data with realistic nmap JSONL samples
- Rename {{Workspace}} to {{TargetSpace}} in function examples for clarity
- Add sudo E2E tests covering tip message, flag acceptance, and non-sudo workflows
2026-02-16 23:46:33 +07:00

75 lines
2.4 KiB
Go

package e2e
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSudo_TipMessage verifies that running a workflow containing sudo commands
// as a non-root user without --sudo-aware prints the tip message.
func TestSudo_TipMessage(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("test must run as non-root user")
}
log := NewTestLogger(t)
log.Step("Testing sudo tip message (non-root, no --sudo-aware)")
workflowPath := getTestdataPath(t)
stdout, _, err := runCLIWithLog(t, log, "run", "-m", "test-sudo", "-t", "localhost", "--dry-run", "-F", workflowPath)
require.NoError(t, err)
log.Info("Asserting stdout contains sudo-aware tip")
assert.Contains(t, stdout, "sudo")
assert.Contains(t, stdout, "--sudo-aware")
log.Success("sudo tip message displayed correctly")
}
// TestSudo_NoTipWhenNoSudo verifies that workflows without sudo commands
// do not print the sudo tip.
func TestSudo_NoTipWhenNoSudo(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("test must run as non-root user")
}
log := NewTestLogger(t)
log.Step("Testing no sudo tip for workflow without sudo")
workflowPath := getTestdataPath(t)
stdout, _, err := runCLIWithLog(t, log, "run", "-m", "test-bash", "-t", "localhost", "--dry-run", "-F", workflowPath)
require.NoError(t, err)
log.Info("Asserting stdout does NOT contain sudo-aware tip")
assert.NotContains(t, stdout, "--sudo-aware")
log.Success("no false sudo tip for non-sudo workflow")
}
// TestSudo_FlagAccepted verifies that --sudo-aware flag is accepted by the CLI.
// NOTE: This test requires an interactive sudo prompt and is intended for
// `make test-sudo`. It authenticates sudo once and keeps credentials alive.
func TestSudo_FlagAccepted(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("test must run as non-root user")
}
if os.Getenv("OSM_TEST_SUDO") == "" {
t.Skip("set OSM_TEST_SUDO=1 to run interactive sudo tests")
}
log := NewTestLogger(t)
log.Step("Testing --sudo-aware flag with sudo workflow")
workflowPath := getTestdataPath(t)
stdout, _, err := runCLIWithLog(t, log, "run", "-m", "test-sudo", "-t", "localhost", "--sudo-aware", "--dry-run", "-F", workflowPath)
require.NoError(t, err)
log.Info("Asserting stdout contains authentication message")
assert.Contains(t, stdout, "Sudo commands detected")
log.Success("--sudo-aware flag triggers authentication")
}