ce7cb702218693d08ea6d83464d2457c9250027c
This commit is contained in:
commit
34c5f8963c
1 changed files with 145336 additions and 0 deletions
75
packages/grafana-rcon/backend/main_test.go
Normal file
75
packages/grafana-rcon/backend/main_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCommandBoundary(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, method, body string
|
||||
allow bool
|
||||
authErr error
|
||||
status, calls int
|
||||
}{
|
||||
{"viewer", "POST", `{"command":"stop"}`, false, nil, 403, 0},
|
||||
{"auth failure", "POST", `{"command":"stop"}`, false, errors.New("offline"), 503, 0},
|
||||
{"query refresh", "GET", `{"command":"stop"}`, true, nil, 405, 0},
|
||||
{"empty", "POST", `{"command":" "}`, true, nil, 400, 0},
|
||||
{"newline", "POST", `{"command":"list\nstop"}`, true, nil, 400, 0},
|
||||
{"nul", "POST", `{"command":"list\u0000stop"}`, true, nil, 400, 0},
|
||||
{"oversized", "POST", `{"command":"` + strings.Repeat("x", 1001) + `"}`, true, nil, 400, 0},
|
||||
{"admin", "POST", `{"command":"list"}`, true, nil, 200, 1},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
calls := 0
|
||||
c := &console{allowed: func(*http.Request) (bool, error) { return tc.allow, tc.authErr }, execute: func(context.Context, string) (string, error) { calls++; return "Nobody online", nil }}
|
||||
w := httptest.NewRecorder()
|
||||
c.routes().ServeHTTP(w, httptest.NewRequest(tc.method, "/command", strings.NewReader(tc.body)))
|
||||
if w.Code != tc.status || calls != tc.calls {
|
||||
t.Fatalf("status=%d calls=%d", w.Code, calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoRetry(t *testing.T) {
|
||||
calls := 0
|
||||
c := &console{allowed: func(*http.Request) (bool, error) { return true, nil }, execute: func(context.Context, string) (string, error) { calls++; return "", errors.New("lost response") }}
|
||||
w := httptest.NewRecorder()
|
||||
c.routes().ServeHTTP(w, httptest.NewRequest("POST", "/command", strings.NewReader(`{"command":"say once"}`)))
|
||||
if w.Code != 502 || calls != 1 {
|
||||
t.Fatalf("status=%d calls=%d", w.Code, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
allow bool
|
||||
err error
|
||||
status, calls int
|
||||
}{
|
||||
{"viewer", false, nil, 403, 0},
|
||||
{"admin", true, nil, 200, 1},
|
||||
{"unavailable RCON", true, errors.New("offline"), 502, 1},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
checks, commands := 0, 0
|
||||
c := &console{
|
||||
allowed: func(*http.Request) (bool, error) { return tc.allow, nil },
|
||||
check: func(context.Context) error { checks++; return tc.err },
|
||||
execute: func(context.Context, string) (string, error) { commands++; return "", nil },
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
c.routes().ServeHTTP(w, httptest.NewRequest("GET", "/health", nil))
|
||||
if w.Code != tc.status || checks != tc.calls || commands != 0 {
|
||||
t.Fatalf("status=%d checks=%d commands=%d", w.Code, checks, commands)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue