mirror of
https://github.com/go-gost/x.git
synced 2026-10-20 00:17:32 +00:00
- Add CmdProber to x/internal/probe/cmd.go with shellCmd() for Windows/Unix - Add cmd probe dispatch in x/chain/node.go (early-return, no transport needed) - Add Command field to config.ProbeConfig and parseProbeConfig mapping - Add 5 CmdProber unit tests (success, failure, timeout, not-found, pipeline) - Add 2 chain integration tests (cmd probe + cmd disabled)
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package probe
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCmdProber(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
p := &CmdProber{Command: "true"}
|
|
if err := p.Probe(); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
p := &CmdProber{Command: "false"}
|
|
if err := p.Probe(); err == nil {
|
|
t.Fatal("expected error for exit 1, got nil")
|
|
}
|
|
})
|
|
|
|
t.Run("timeout", func(t *testing.T) {
|
|
p := &CmdProber{
|
|
Command: "sleep 10",
|
|
Timeout: 50 * time.Millisecond,
|
|
}
|
|
start := time.Now()
|
|
err := p.Probe()
|
|
elapsed := time.Since(start)
|
|
if err == nil {
|
|
t.Fatal("expected timeout error, got nil")
|
|
}
|
|
if elapsed > 2*time.Second {
|
|
t.Fatalf("timeout took too long: %v", elapsed)
|
|
}
|
|
})
|
|
|
|
t.Run("command not found", func(t *testing.T) {
|
|
p := &CmdProber{Command: "nonexistent-command-xyz"}
|
|
if err := p.Probe(); err == nil {
|
|
t.Fatal("expected error for unknown command, got nil")
|
|
}
|
|
})
|
|
|
|
t.Run("shell pipeline", func(t *testing.T) {
|
|
p := &CmdProber{Command: "echo hello | grep hello"}
|
|
if err := p.Probe(); err != nil {
|
|
t.Fatalf("shell pipeline failed: %v", err)
|
|
}
|
|
})
|
|
}
|