Files
ssh-netbox-wrapper/internal/runner/runner_test.go
Sebastian Unterschütz e5bde9c61f
Test / test (push) Successful in 4m4s
Release / release (push) Successful in 7m39s
feat: add host actions, lifecycle hooks and operational tools
2026-11-18 10:41:36 +01:00

44 lines
1.4 KiB
Go

package runner
import (
"bytes"
"context"
"io"
"strings"
"testing"
"time"
)
func TestExitAndTimeout(t *testing.T) {
var out bytes.Buffer
r := Run(context.Background(), []string{"sh", "-c", "printf result; exit 7"}, Options{Stdout: &out, Stderr: io.Discard})
if !r.Started || r.Code != 7 || out.String() != "result" {
t.Fatalf("%+v %q", r, out.String())
}
start := time.Now()
r = Run(context.Background(), []string{"sh", "-c", "trap '' TERM; sleep 30 & wait"}, Options{Stdout: io.Discard, Stderr: io.Discard, Timeout: 50 * time.Millisecond})
if r.Code != 124 || time.Since(start) > 4*time.Second {
t.Fatalf("timeout: %+v", r)
}
}
func TestCancellationAndNoStart(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
r := Run(ctx, []string{"sh", "-c", "exit 0"}, Options{Stderr: io.Discard})
if r.Started || r.Code != 130 {
t.Fatalf("%+v", r)
}
var errOut bytes.Buffer
r = Run(context.Background(), []string{"/no/such/netssh-test-executable"}, Options{Stderr: &errOut})
if r.Started || r.Code == 0 || !strings.Contains(errOut.String(), "no such") {
t.Fatalf("%+v %s", r, errOut.String())
}
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
time.AfterFunc(50*time.Millisecond, cancel)
r = Run(ctx, []string{"sh", "-c", "sleep 30"}, Options{Stdout: io.Discard, Stderr: io.Discard})
if r.Code != 130 {
t.Fatalf("%+v", r)
}
}