Files
traefik-llmgateway/discovery_bounds_test.go
T
lukaszraczyloandGitHub f1b198bce4 fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
2026-11-29 10:50:36 +00:00

175 lines
5.1 KiB
Go

package traefikllmgateway
import (
"fmt"
"strings"
"sync"
"testing"
"time"
)
func newBoundsRegistry(t *testing.T, logf func(string, ...any)) (*modelRegistry, *providerState) {
t.Helper()
adapters := map[string]providerAdapter{"openai": newFakeAdapter("openai")}
cfg := &Config{Providers: map[string]*ProviderConfig{"openai": {Models: []string{"gpt-test"}}}}
reg, err := newModelRegistry(adapters, cfg, logf)
if err != nil {
t.Fatalf("newModelRegistry: %v", err)
}
return reg, reg.states["openai"]
}
func manyIDs(n int) []string {
ids := make([]string, n)
for i := range ids {
ids[i] = fmt.Sprintf("m-%d", i)
}
return ids
}
func discoveredCount(reg *modelRegistry, st *providerState) int {
st.mu.Lock()
defer st.mu.Unlock()
return len(st.discovered)
}
func TestDiscovery_DropsOverLengthAndInvalidIDs(t *testing.T) {
reg, st := newBoundsRegistry(t, func(string, ...any) {})
reg.finishRefresh(st, reg.now(), []string{strings.Repeat("a", 300), "", "bad\nid", "good-1", "good-2"}, nil)
if got := discoveredCount(reg, st); got != 2 {
t.Fatalf("discovered = %d, want 2", got)
}
if !st.discovered["good-1"] || !st.discovered["good-2"] {
t.Errorf("discovered = %v, want good-1 and good-2", st.discovered)
}
}
func TestDiscovery_AtCapAccepted(t *testing.T) {
reg, st := newBoundsRegistry(t, func(string, ...any) {})
reg.finishRefresh(st, reg.now(), manyIDs(maxDiscoveredModels), nil)
if got := discoveredCount(reg, st); got != maxDiscoveredModels {
t.Errorf("discovered = %d, want %d", got, maxDiscoveredModels)
}
}
func TestBoundDiscoveredIDs_RepeatedIDsCountOnce(t *testing.T) {
cases := map[string]struct {
explicit map[string]bool
ids []string
wantKept int
wantDropped int
}{
"copies of an explicit id cannot pass the cap": {
ids: append(repeatID("m-x", maxDiscoveredModels*2), "other"), explicit: map[string]bool{"m-x": true}, wantKept: 2,
},
"copies do not evict distinct ids": {
ids: append(repeatID("a", maxDiscoveredModels), manyIDs(10)...), wantKept: 11,
},
"copies are not reported as dropped": {
ids: append(repeatID("a", 3), "", "bad\nid"), wantKept: 1, wantDropped: 2,
},
"distinct ids past the cap are still dropped": {
ids: append(manyIDs(maxDiscoveredModels+5), manyIDs(7)...), wantKept: maxDiscoveredModels, wantDropped: 5,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
kept, dropped := boundDiscoveredIDs(c.ids, c.explicit)
if len(kept) != c.wantKept || dropped != c.wantDropped {
t.Errorf("kept %d dropped %d, want %d and %d", len(kept), dropped, c.wantKept, c.wantDropped)
}
})
}
}
func repeatID(id string, n int) []string {
ids := make([]string, n)
for i := range ids {
ids[i] = id
}
return ids
}
func TestDiscovery_OverCountKeepsCapAndLogsDebounced(t *testing.T) {
var mu sync.Mutex
var lines []string
reg, st := newBoundsRegistry(t, func(f string, a ...any) {
mu.Lock()
lines = append(lines, fmt.Sprintf(f, a...))
mu.Unlock()
})
now := time.Now()
reg.finishRefresh(st, now, manyIDs(maxDiscoveredModels+5), nil)
reg.finishRefresh(st, now.Add(time.Second), manyIDs(maxDiscoveredModels+5), nil)
if got := discoveredCount(reg, st); got != maxDiscoveredModels {
t.Errorf("discovered = %d, want %d", got, maxDiscoveredModels)
}
st.mu.Lock()
over := st.discovered["m-9999"] // lexicographically last, so the first surplus id dropped
st.mu.Unlock()
if over {
t.Error("an id past the cap was kept")
}
var n int
for _, l := range lines {
if strings.Contains(l, "dropped 5 model ids") {
n++
}
}
if n != 1 {
t.Errorf("drop log lines = %d, want 1 (debounced): %v", n, lines)
}
reg.finishRefresh(st, now.Add(upstreamErrLogEvery+time.Second), manyIDs(maxDiscoveredModels+5), nil)
n = 0
for _, l := range lines {
if strings.Contains(l, "dropped 5 model ids") {
n++
}
}
if n != 2 {
t.Errorf("drop log lines after the window = %d, want 2", n)
}
}
func TestDiscovery_OverCountKeepsExplicitIDsAndIsDeterministic(t *testing.T) {
adapters := map[string]providerAdapter{"openai": newFakeAdapter("openai")}
cfg := &Config{Providers: map[string]*ProviderConfig{"openai": {
Models: []string{"zz-model"},
ModelBaseURLs: map[string]string{"zz-routed": "http://other.invalid"},
}}}
run := func(ids []string) map[string]bool {
reg, err := newModelRegistry(adapters, cfg, func(string, ...any) {})
if err != nil {
t.Fatalf("newModelRegistry: %v", err)
}
st := reg.states["openai"]
reg.finishRefresh(st, reg.now(), ids, nil)
st.mu.Lock()
defer st.mu.Unlock()
got := make(map[string]bool, len(st.discovered))
for id := range st.discovered {
got[id] = true
}
return got
}
ids := append(manyIDs(maxDiscoveredModels+50), "zz-model", "zz-routed")
rev := make([]string, len(ids))
for i, id := range ids {
rev[len(ids)-1-i] = id
}
a, b := run(ids), run(rev)
for _, id := range []string{"zz-model", "zz-routed"} {
if !a[id] || !b[id] {
t.Errorf("explicit id %q dropped by the cap", id)
}
}
if len(a) != maxDiscoveredModels {
t.Errorf("kept %d ids, want %d", len(a), maxDiscoveredModels)
}
for id := range a {
if !b[id] {
t.Fatalf("kept set depends on listing order: %q only in the forward run", id)
}
}
}