mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type capturedLog struct {
|
|
lines []string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (c *capturedLog) logf(f string, a ...any) {
|
|
c.mu.Lock()
|
|
c.lines = append(c.lines, fmt.Sprintf(f, a...))
|
|
c.mu.Unlock()
|
|
}
|
|
|
|
func (c *capturedLog) count(sub string) int {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
n := 0
|
|
for _, l := range c.lines {
|
|
if strings.Contains(l, sub) {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
func newTwoProviderRegistry(t *testing.T, logf func(string, ...any)) *modelRegistry {
|
|
t.Helper()
|
|
adapters := map[string]providerAdapter{"alpha": newFakeAdapter("alpha"), "beta": newFakeAdapter("beta")}
|
|
cfg := &Config{Providers: map[string]*ProviderConfig{"alpha": {Models: []string{"a"}}, "beta": {Models: []string{"b"}}}}
|
|
reg, err := newModelRegistry(adapters, cfg, logf)
|
|
if err != nil {
|
|
t.Fatalf("newModelRegistry: %v", err)
|
|
}
|
|
return reg
|
|
}
|
|
|
|
func TestRegistry_DiscoveryDropLogNamesProvider(t *testing.T) {
|
|
var cl capturedLog
|
|
reg := newTwoProviderRegistry(t, cl.logf)
|
|
reg.finishRefresh(reg.states["alpha"], time.Now(), []string{""}, nil)
|
|
if cl.count(`provider "alpha" discovery dropped 1`) != 1 {
|
|
t.Fatalf("log does not name the provider: %v", cl.lines)
|
|
}
|
|
}
|
|
|
|
func TestRegistry_DiscoveryDropLogDebouncedPerProvider(t *testing.T) {
|
|
var cl capturedLog
|
|
reg := newTwoProviderRegistry(t, cl.logf)
|
|
now := time.Now()
|
|
reg.finishRefresh(reg.states["alpha"], now, []string{""}, nil)
|
|
reg.finishRefresh(reg.states["beta"], now.Add(time.Second), []string{""}, nil)
|
|
if cl.count(`provider "beta" discovery dropped`) != 1 || cl.count(`provider "alpha" discovery dropped`) != 1 {
|
|
t.Fatalf("each provider must log its own drop once: %v", cl.lines)
|
|
}
|
|
}
|
|
|
|
func TestRegistry_UpstreamErrorsLoggedWithoutSharedState(t *testing.T) {
|
|
var cl capturedLog
|
|
ad := newOpenAIAdapter("d1", "http://127.0.0.1:1", "")
|
|
ad.setUpstreamErrors([]upstreamFailure{{Upstream: "http://127.0.0.1:2", Err: "boom"}})
|
|
cfg := &Config{Providers: map[string]*ProviderConfig{"d1": {Models: []string{"m"}}}}
|
|
reg, err := newModelRegistry(map[string]providerAdapter{"d1": ad}, cfg, cl.logf)
|
|
if err != nil {
|
|
t.Fatalf("newModelRegistry: %v", err)
|
|
}
|
|
reg.finishRefresh(reg.states["d1"], time.Now(), []string{"m"}, nil)
|
|
if cl.count(`provider "d1" upstream`) != 1 {
|
|
t.Fatalf("tolerated upstream failure not logged: %v", cl.lines)
|
|
}
|
|
}
|