Init
This commit is contained in:
parent
c823102a63
commit
d37503c863
20 changed files with 4433 additions and 3 deletions
120
internal/checker/checker_test.go
Normal file
120
internal/checker/checker_test.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
package checker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/config"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/store"
|
||||
)
|
||||
|
||||
func TestHTTPChecker(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("X-Custom") != "expected-val" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(`{"service":"alive","code":42}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
def := config.DefaultsConfig{
|
||||
Timeout: config.NewDuration(2 * time.Second),
|
||||
Retries: 0,
|
||||
DegradedLatency: config.NewDuration(1 * time.Second),
|
||||
}
|
||||
|
||||
mon := config.MonitorConfig{
|
||||
ID: "http-1",
|
||||
Method: "http",
|
||||
URL: ts.URL,
|
||||
ExpectedStatus: []int{201},
|
||||
Headers: map[string]string{"X-Custom": "expected-val"},
|
||||
BodyRegex: `"service":\s*"alive"`,
|
||||
}
|
||||
|
||||
res := RunCheckWithRetries(context.Background(), mon, def)
|
||||
if res.State != store.StateUp {
|
||||
t.Fatalf("expected HTTP check up, got %s (%s)", res.State, res.Message)
|
||||
}
|
||||
|
||||
// Regex mismatch should mark down
|
||||
mon.BodyRegex = `"service":\s*"missing"`
|
||||
resFail := RunCheckWithRetries(context.Background(), mon, def)
|
||||
if resFail.State != store.StateDown {
|
||||
t.Fatalf("expected HTTP regex mismatch to mark down, got %s", resFail.State)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPChecker(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen failed: %v", err)
|
||||
}
|
||||
defer ln.Close()
|
||||
go func() {
|
||||
for {
|
||||
c, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = c.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
def := config.DefaultsConfig{Timeout: config.NewDuration(1 * time.Second)}
|
||||
mon := config.MonitorConfig{
|
||||
ID: "tcp-1",
|
||||
Method: "tcp",
|
||||
Target: ln.Addr().String(),
|
||||
}
|
||||
res := RunCheckWithRetries(context.Background(), mon, def)
|
||||
if res.State != store.StateUp {
|
||||
t.Fatalf("expected TCP check up, got %s (%s)", res.State, res.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSAndCmdAndPingCheckers(t *testing.T) {
|
||||
def := config.DefaultsConfig{Timeout: config.NewDuration(2 * time.Second)}
|
||||
|
||||
dnsMon := config.MonitorConfig{
|
||||
ID: "dns-1",
|
||||
Method: "dns",
|
||||
Host: "localhost",
|
||||
RecordType: "A",
|
||||
ExpectedRegex: `^127\.`,
|
||||
}
|
||||
dnsRes := RunCheckWithRetries(context.Background(), dnsMon, def)
|
||||
if dnsRes.State != store.StateUp {
|
||||
t.Fatalf("expected DNS check up, got %s (%s)", dnsRes.State, dnsRes.Message)
|
||||
}
|
||||
|
||||
cmdMon := config.MonitorConfig{
|
||||
ID: "cmd-1",
|
||||
Method: "cmd",
|
||||
Command: "printf 'rud-ok-123'",
|
||||
Shell: true,
|
||||
ExpectedExitCode: config.IntPtr(0),
|
||||
OutputRegex: `^rud-ok-[0-9]+$`,
|
||||
}
|
||||
cmdRes := RunCheckWithRetries(context.Background(), cmdMon, def)
|
||||
if cmdRes.State != store.StateUp {
|
||||
t.Fatalf("expected Cmd check up, got %s (%s)", cmdRes.State, cmdRes.Message)
|
||||
}
|
||||
|
||||
pingMon := config.MonitorConfig{
|
||||
ID: "ping-1",
|
||||
Method: "ping",
|
||||
Host: "127.0.0.1",
|
||||
Count: 1,
|
||||
}
|
||||
pingRes := RunCheckWithRetries(context.Background(), pingMon, def)
|
||||
if pingRes.State != store.StateUp {
|
||||
t.Fatalf("expected Ping check up, got %s (%s)", pingRes.State, pingRes.Message)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue