Harden core packages and add interactive CLI config builder (rud config/validate/test)

This commit is contained in:
tyuXX 2026-11-19 13:05:26 +00:00
parent d37503c863
commit 99e24be6c9
29 changed files with 6763 additions and 165 deletions

View file

@ -5,6 +5,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
@ -118,3 +119,88 @@ func TestDNSAndCmdAndPingCheckers(t *testing.T) {
t.Fatalf("expected Ping check up, got %s (%s)", pingRes.State, pingRes.Message)
}
}
func TestConcurrentLoopbackPings(t *testing.T) {
def := config.DefaultsConfig{Timeout: config.NewDuration(2 * time.Second)}
const workers = 6
var wg sync.WaitGroup
errs := make(chan string, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
mon := config.MonitorConfig{
ID: "ping-concurrent",
Method: "ping",
Host: "127.0.0.1",
Count: 2,
}
res := RunCheckWithRetries(context.Background(), mon, def)
if res.State != store.StateUp {
errs <- res.Message
}
}()
}
wg.Wait()
close(errs)
for msg := range errs {
t.Fatalf("concurrent ping failed: %s", msg)
}
}
func TestCmdBoundedOutputAndTimeoutCleanup(t *testing.T) {
// Command writing > 1.5 MiB should remain bounded and still check regex
def := config.DefaultsConfig{Timeout: config.NewDuration(3 * time.Second)}
bigCmd := config.MonitorConfig{
ID: "cmd-big",
Method: "cmd",
Command: "printf 'HEAD:'; head -c 1500000 /dev/zero | tr '\\0' 'A'",
Shell: true,
OutputRegex: `^HEAD:A+$`,
}
res := RunCheckWithRetries(context.Background(), bigCmd, def)
if res.State != store.StateUp {
t.Fatalf("expected bounded big output cmd to pass, got %s (%s)", res.State, res.Message)
}
// Command spawning a sleep child should terminate quickly on timeout without hanging pipes
start := time.Now()
timeoutDef := config.DefaultsConfig{Timeout: config.NewDuration(150 * time.Millisecond)}
sleepCmd := config.MonitorConfig{
ID: "cmd-timeout",
Method: "cmd",
Command: "sleep 10 & wait",
Shell: true,
}
resTimeout := RunCheckWithRetries(context.Background(), sleepCmd, timeoutDef)
elapsed := time.Since(start)
if resTimeout.State != store.StateDown {
t.Fatalf("expected timed out cmd to mark down, got %s", resTimeout.State)
}
if elapsed > 2*time.Second {
t.Fatalf("timed out cmd took too long to clean up process group: %v", elapsed)
}
}
func TestSubMsDegradedLatencyThreshold(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
def := config.DefaultsConfig{
Timeout: config.NewDuration(2 * time.Second),
DegradedLatency: config.NewDuration(500 * time.Microsecond), // 0.5ms (< 1ms!)
}
mon := config.MonitorConfig{
ID: "http-subms",
Method: "http",
URL: ts.URL,
}
res := RunCheckWithRetries(context.Background(), mon, def)
if res.State != store.StateDegraded {
t.Fatalf("expected sub-ms degraded threshold (500us) to trigger degraded state, got %s (latency=%.3fms)", res.State, res.LatencyMs)
}
}