mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
A failing modelBaseUrls upstream dropped its models from the metadata map. The registry replaced the discovered contexts, so those models lost their context length. The failure was also not visible. The adapter now keeps the last good metadata per upstream and reports metadata failures through upstreamErrors.
165 lines
5.3 KiB
Go
165 lines
5.3 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// flakyMetaServer serves ids with the given max_model_len on /v1/models,
|
|
// or a 500 while down is set.
|
|
func flakyMetaServer(t *testing.T, id string, n *atomic.Int32, down *atomic.Bool) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
if down.Load() {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = fmt.Fprintf(w, `{"data":[{"id":%q,"max_model_len":%d}]}`, id, n.Load())
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
type metaFixture struct {
|
|
ad *openaiAdapter
|
|
mainN, otherN *atomic.Int32
|
|
mainDown, otherDown *atomic.Bool
|
|
otherURL string
|
|
}
|
|
|
|
func newMetaFixture(t *testing.T) *metaFixture {
|
|
t.Helper()
|
|
f := &metaFixture{
|
|
mainN: new(atomic.Int32), otherN: new(atomic.Int32),
|
|
mainDown: new(atomic.Bool), otherDown: new(atomic.Bool),
|
|
}
|
|
f.mainN.Store(1000)
|
|
f.otherN.Store(2000)
|
|
mainSrv := flakyMetaServer(t, "main-model", f.mainN, f.mainDown)
|
|
otherSrv := flakyMetaServer(t, "glm", f.otherN, f.otherDown)
|
|
f.otherURL = otherSrv.URL
|
|
f.ad = newOpenAIAdapter("d1", mainSrv.URL, "")
|
|
f.ad.modelBases = map[string]string{"glm": otherSrv.URL}
|
|
f.ad.metadataPath = "/v1/models"
|
|
return f
|
|
}
|
|
|
|
func TestFetchModelMetadata_PartialFailureKeepsLastKnown(t *testing.T) {
|
|
f := newMetaFixture(t)
|
|
got, err := f.ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, map[string]int{"main-model": 1000, "glm": 2000}, got)
|
|
|
|
f.mainN.Store(1500)
|
|
f.otherDown.Store(true)
|
|
got, err = f.ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, map[string]int{"main-model": 1500, "glm": 2000}, got)
|
|
}
|
|
|
|
func TestFetchModelMetadata_PartialFailureReportedInUpstreamErrors(t *testing.T) {
|
|
f := newMetaFixture(t)
|
|
_, err := f.ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
assert.Empty(t, f.ad.upstreamErrors())
|
|
|
|
f.otherDown.Store(true)
|
|
_, err = f.ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
errs := f.ad.upstreamErrors()
|
|
require.Len(t, errs, 1)
|
|
assert.Equal(t, f.otherURL, errs[0].Upstream)
|
|
assert.Contains(t, errs[0].Err, "metadata")
|
|
}
|
|
|
|
func TestFetchModelMetadata_RecoveryClearsMetadataError(t *testing.T) {
|
|
f := newMetaFixture(t)
|
|
_, _ = f.ad.fetchModelMetadata(context.Background())
|
|
f.otherDown.Store(true)
|
|
_, _ = f.ad.fetchModelMetadata(context.Background())
|
|
require.NotEmpty(t, f.ad.upstreamErrors())
|
|
|
|
f.otherDown.Store(false)
|
|
f.otherN.Store(3000)
|
|
got, err := f.ad.fetchModelMetadata(context.Background())
|
|
require.NoError(t, err)
|
|
assert.Empty(t, f.ad.upstreamErrors())
|
|
assert.Equal(t, 3000, got["glm"])
|
|
}
|
|
|
|
func TestFetchModelMetadata_AllFailReturnsError(t *testing.T) {
|
|
f := newMetaFixture(t)
|
|
_, _ = f.ad.fetchModelMetadata(context.Background())
|
|
f.mainDown.Store(true)
|
|
f.otherDown.Store(true)
|
|
got, err := f.ad.fetchModelMetadata(context.Background())
|
|
assert.Error(t, err)
|
|
assert.Nil(t, got)
|
|
}
|
|
|
|
func TestRegistry_PartialMetadataFailureKeepsContextFor(t *testing.T) {
|
|
f := newMetaFixture(t)
|
|
cfg := &Config{Providers: map[string]*ProviderConfig{"d1": {Discovery: true, MetadataPath: "/v1/models"}}}
|
|
reg, err := newModelRegistry(map[string]providerAdapter{"d1": f.ad}, cfg, func(string, ...any) {})
|
|
require.NoError(t, err)
|
|
st := reg.states["d1"]
|
|
|
|
reg.captureModelMetadata(context.Background(), "d1", f.ad, st)
|
|
f.otherDown.Store(true)
|
|
reg.captureModelMetadata(context.Background(), "d1", f.ad, st)
|
|
|
|
n, ok := st.contextFor("glm")
|
|
assert.True(t, ok, "context of the failed upstream's model must survive")
|
|
assert.Equal(t, 2000, n)
|
|
}
|
|
|
|
func TestAdminOverview_UpstreamErrorsCarryMetadataFailure(t *testing.T) {
|
|
mainSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = fmt.Fprint(w, `{"data":[{"id":"main-model","max_model_len":1000}]}`)
|
|
}))
|
|
t.Cleanup(mainSrv.Close)
|
|
otherSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/meta" {
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = fmt.Fprint(w, `{"data":[{"id":"glm"}]}`)
|
|
}))
|
|
t.Cleanup(otherSrv.Close)
|
|
|
|
cfg := newAdminTestConfig()
|
|
cfg.Providers = map[string]*ProviderConfig{
|
|
"uni": {
|
|
Type: "openai", BaseURL: mainSrv.URL, APIKey: "sk", Discovery: true,
|
|
MetadataPath: "/meta",
|
|
Models: []string{"glm"},
|
|
ModelBaseURLs: map[string]string{"glm": otherSrv.URL},
|
|
},
|
|
}
|
|
h, _ := newAdminGatewayHandle(t, cfg)
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
var got adminOverviewResponse
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
|
|
require.Len(t, got.Providers, 1)
|
|
require.Len(t, got.Providers[0].UpstreamErrors, 1)
|
|
e := got.Providers[0].UpstreamErrors[0]
|
|
assert.Contains(t, e.Error, "metadata")
|
|
assert.Contains(t, e.Error, "502")
|
|
assert.True(t, strings.HasPrefix(e.Upstream, "http://"), e.Upstream)
|
|
}
|