Files
traefik-llmgateway/admin_catalog_test.go
lukaszraczyloandGitHub bfdffc2ab0 fix(admin): show aliases, guard the provider filter, tolerate failing upstreams (#23)
* fix(admin): attach model aliases to the model their target resolves to

The catalog grouped aliases by the raw target string and looked them up by
canonical provider/model id. A bare target never matched, so no model card
listed its aliases. Each alias now keys on the canonical id that the
registry resolves its target to. The alias view gains an additive
resolvedTarget field.

Model-id cells in the overview, usage and panels now show an alias badge
with the target, using the catalog alias list.

* fix(webui): omit the provider filter select until providers load

* fix(discovery): tolerate failing modelBaseUrls upstreams

A modelBaseUrls upstream that fails /v1/models must not fail the provider.
Discovery now fails only when baseUrl fails. The explicit models of the
failing upstream stay listed, and the other upstreams still merge.

The admin overview reports each failed upstream in the additive
upstreamErrors field. The registry logs it at most once per 10 minutes
per provider. The README states the new semantics.

* chore(webui): regenerate admin assets

Rebuilt with make admin-ui for the alias badge and the provider filter changes.

* style(auth): order authStore fields for fieldalignment

golangci-lint's fieldalignment check reported 240 leading pointer bytes on authStore where 232 is optimal. The bool anonymousAnyKey moves next to the mutexes. No behaviour change.
2026-11-28 02:13:22 +00:00

517 lines
17 KiB
Go

package traefikllmgateway
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
)
// --- GET /admin/api/catalog ---
func TestAdminCatalog_GateMatrix(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
h, _ := newAdminGatewayHandle(t, cfg)
cases := []struct {
name string
apiKey string
want int
}{
{"unauthenticated", "", http.StatusUnauthorized},
{"non-admin", "sk-alice", http.StatusForbidden},
{"admin", "sk-admin1", http.StatusOK},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, c.apiKey))
if rec.Code != c.want {
t.Errorf("status = %d, want %d, body=%s", rec.Code, c.want, rec.Body.String())
}
})
}
}
// TestAdminCatalog_PriceSourceAndDisplayFree exercises the five billing
// price sources plus the ':free'-suffix display-only case (DECISIONS Q3)
// end to end through GET /admin/api/catalog — including N1's own
// regression (verify-redesign-final.md): "alpha/a-model-1" carries a
// `pricing:` override and NO modelMeta/discovery entry at all, so an
// earlier version (InputPerMTokUSD/OutputPerMTokUSD filled from
// buildAdminModelMetaView's own display-layer resolution instead of
// billingPriceSource's) reported PriceSource: "override" alongside nil
// prices — this table now pins both price fields to the ACTUAL billed
// rate for every priced source, and nil for free/unpriced.
func TestAdminCatalog_PriceSourceAndDisplayFree(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Pricing = map[string]*ModelPricing{
"alpha/a-model-1": {InputPerM: 5, OutputPerM: 5}, // override on canonical, no modelMeta/discovery price at all
}
cfg.ModelMeta = map[string]*ModelMetaConfig{
"alpha/a-model-2": {Free: true}, // modelMeta free
}
cfg.Providers["zeta"].Models = []string{"z-model", "gpt-5", "some-model:free"}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200, body=%s", rec.Code, rec.Body.String())
}
var got adminCatalogResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
byID := map[string]adminCatalogModel{}
for _, p := range got.Providers {
for _, m := range p.Models {
byID[m.ID] = m
}
}
builtinGPT5 := builtinPricing["gpt-5"]
cases := []struct {
wantInputPerM *float64 // nil means "must be omitted/nil"
wantOutputPerM *float64
id string
wantSource string
wantDisplayFree bool
}{
{ptrFloat64(5), ptrFloat64(5), "alpha/a-model-1", priceSourceOverride, false},
{nil, nil, "alpha/a-model-2", priceSourceFree, false},
{ptrFloat64(builtinGPT5.InputPerM), ptrFloat64(builtinGPT5.OutputPerM), "zeta/gpt-5", priceSourceBuiltin, false},
{nil, nil, "zeta/some-model:free", priceSourceUnpriced, true},
{nil, nil, "zeta/z-model", priceSourceUnpriced, false},
}
for _, c := range cases {
got, ok := byID[c.id]
if !ok {
t.Errorf("model %q missing from catalog", c.id)
continue
}
if got.PriceSource != c.wantSource {
t.Errorf("model %q PriceSource = %q, want %q", c.id, got.PriceSource, c.wantSource)
}
if got.DisplayFree != c.wantDisplayFree {
t.Errorf("model %q DisplayFree = %v, want %v", c.id, got.DisplayFree, c.wantDisplayFree)
}
if !ptrFloat64Equal(got.InputPerMTokUSD, c.wantInputPerM) {
t.Errorf("model %q InputPerMTokUSD = %v, want %v", c.id, derefFloat64(got.InputPerMTokUSD), derefFloat64(c.wantInputPerM))
}
if !ptrFloat64Equal(got.OutputPerMTokUSD, c.wantOutputPerM) {
t.Errorf("model %q OutputPerMTokUSD = %v, want %v", c.id, derefFloat64(got.OutputPerMTokUSD), derefFloat64(c.wantOutputPerM))
}
}
}
// ptrFloat64/derefFloat64/ptrFloat64Equal are small nil-safe *float64
// helpers for TestAdminCatalog_PriceSourceAndDisplayFree's own table —
// adminCatalogModel's price fields are `*float64` (nil = "no billed
// rate", never a fabricated 0), so the table needs to express and
// compare that nil-ness directly rather than comparing bare float64s.
func ptrFloat64(v float64) *float64 { return &v }
func derefFloat64(p *float64) float64 {
if p == nil {
return 0
}
return *p
}
func ptrFloat64Equal(a, b *float64) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
return *a == *b
}
// TestAdminCatalog_AliasesGroupedByTarget pins each model's Aliases field
// to exactly the configured aliases whose Target names it, sorted.
func TestAdminCatalog_AliasesGroupedByTarget(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.ModelAliases = map[string]string{
"z-model-b-alias": "zeta/z-model",
"z-model-a-alias": "zeta/z-model",
}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, "sk-admin1"))
var got adminCatalogResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
var zModel *adminCatalogModel
for _, p := range got.Providers {
for i := range p.Models {
if p.Models[i].ID == "zeta/z-model" {
zModel = &p.Models[i]
}
}
}
if zModel == nil {
t.Fatal("zeta/z-model missing from catalog")
}
want := []string{"z-model-a-alias", "z-model-b-alias"}
if len(zModel.Aliases) != len(want) || zModel.Aliases[0] != want[0] || zModel.Aliases[1] != want[1] {
t.Errorf("Aliases = %v, want %v (sorted)", zModel.Aliases, want)
}
if len(got.Aliases) != 2 {
t.Errorf("top-level Aliases = %d entries, want 2", len(got.Aliases))
}
}
// TestAdminCatalog_AliasTargetForms pins that a bare target, a
// provider-prefixed target and an unresolvable target behave as the
// registry's own resolution does: the first two attach to the canonical
// model, the last attaches nowhere but still lists in catalog.aliases.
func TestAdminCatalog_AliasTargetForms(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.ModelAliases = map[string]string{
"fast-bare": "z-model",
"fast-prefixed": "zeta/z-model",
"fast-missing": "no-such-model",
}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, "sk-admin1"))
var got adminCatalogResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
attached := map[string][]string{}
for _, p := range got.Providers {
for _, m := range p.Models {
attached[m.ID] = m.Aliases
}
}
want := []string{"fast-bare", "fast-prefixed"}
if g := attached["zeta/z-model"]; len(g) != 2 || g[0] != want[0] || g[1] != want[1] {
t.Errorf("zeta/z-model Aliases = %v, want %v", g, want)
}
for id, names := range attached {
for _, n := range names {
if n == "fast-missing" {
t.Errorf("unresolvable entry attached to %s", id)
}
}
}
resolved := map[string]string{}
for _, a := range got.Aliases {
resolved[a.Alias] = a.ResolvedTarget
}
if len(resolved) != 3 {
t.Fatalf("top-level Aliases = %v, want 3 entries", resolved)
}
if resolved["fast-bare"] != "zeta/z-model" || resolved["fast-prefixed"] != "zeta/z-model" || resolved["fast-missing"] != "" {
t.Errorf("ResolvedTarget = %v", resolved)
}
}
// --- GET /admin/api/consumers ---
func TestAdminConsumers_GateMatrix(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
h, _ := newAdminGatewayHandle(t, cfg)
cases := []struct {
name string
apiKey string
want int
}{
{"unauthenticated", "", http.StatusUnauthorized},
{"non-admin", "sk-alice", http.StatusForbidden},
{"admin", "sk-admin1", http.StatusOK},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminConsumersPath, c.apiKey))
if rec.Code != c.want {
t.Errorf("status = %d, want %d, body=%s", rec.Code, c.want, rec.Body.String())
}
})
}
}
// TestAdminConsumers_UserSourceAndPersonalGrant exercises source
// (inline/file) and the personal-grant Providers/Models fields end to
// end through GET /admin/api/consumers.
func TestAdminConsumers_UserSourceAndPersonalGrant(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Users.Inline = append(cfg.Users.Inline, &UserConfig{
Name: "grantee", Group: "agroup", APIKey: "sk-grantee",
Providers: []string{"alpha"}, Models: []string{"a-model-1"},
})
h, gw := newAdminGatewayHandle(t, cfg)
if err := gw.auth.replaceFileUsers([]*UserConfig{
{Name: "filed", Group: "agroup", APIKey: "sk-filed"},
}); err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminConsumersPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200, body=%s", rec.Code, rec.Body.String())
}
var got adminConsumersResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
byName := map[string]adminConsumerUser{}
for _, u := range got.Users {
byName[u.Name] = u
}
if byName["alice"].Source != "inline" {
t.Errorf(`alice.Source = %q, want "inline"`, byName["alice"].Source)
}
if byName["filed"].Source != "file" {
t.Errorf(`filed.Source = %q, want "file"`, byName["filed"].Source)
}
if !byName["admin1"].Admin {
t.Errorf("admin1.Admin = false, want true")
}
g := byName["grantee"]
if len(g.Providers) != 1 || g.Providers[0] != "alpha" {
t.Errorf("grantee.Providers = %v, want [alpha]", g.Providers)
}
if len(g.Models) != 1 || g.Models[0] != "a-model-1" {
t.Errorf("grantee.Models = %v, want [a-model-1]", g.Models)
}
}
// TestAdminConsumers_ModelAccessCounts pins adminConsumerGroup's
// ModelAccess/AllowedProviders against the live catalog for a group with
// an explicit provider+model restriction.
func TestAdminConsumers_ModelAccessCounts(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Groups["agroup"] = &GroupConfig{Providers: []string{"alpha"}, Models: []string{"a-model-1"}}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminConsumersPath, "sk-admin1"))
var got adminConsumersResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
var agroup *adminConsumerGroup
for i := range got.Groups {
if got.Groups[i].Name == "agroup" {
agroup = &got.Groups[i]
}
}
if agroup == nil {
t.Fatal("agroup missing from consumers response")
}
if agroup.AllowedProviders == nil {
t.Error("AllowedProviders = nil, want non-nil")
}
if len(agroup.AllowedProviders) != 1 || agroup.AllowedProviders[0] != "alpha" {
t.Errorf("AllowedProviders = %v, want [alpha]", agroup.AllowedProviders)
}
alpha, ok := agroup.ModelAccess["alpha"]
if !ok {
t.Fatal("ModelAccess[alpha] missing")
}
if alpha.Allowed != 1 || alpha.Total != 2 {
t.Errorf("ModelAccess[alpha] = %+v, want {Allowed:1 Total:2}", alpha)
}
zeta, ok := agroup.ModelAccess["zeta"]
if !ok {
t.Fatal("ModelAccess[zeta] missing")
}
if zeta.Allowed != 0 || zeta.Total != 1 {
t.Errorf("ModelAccess[zeta] = %+v, want {Allowed:0 Total:1}", zeta)
}
// zgroup has no restriction: unrestricted access to every provider.
var zgroup *adminConsumerGroup
for i := range got.Groups {
if got.Groups[i].Name == "zgroup" {
zgroup = &got.Groups[i]
}
}
if zgroup == nil {
t.Fatal("zgroup missing")
}
if len(zgroup.AllowedProviders) != 2 {
t.Errorf("zgroup.AllowedProviders = %v, want both providers", zgroup.AllowedProviders)
}
}
// TestAdminConsumers_ModelAccessPrefixedGlobs pins that model globs written
// as "provider/model" count, as every request path also tries the prefixed id.
func TestAdminConsumers_ModelAccessPrefixedGlobs(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Groups["prefixed"] = &GroupConfig{Providers: []string{"alpha"}, Models: []string{"alpha/*"}}
cfg.Groups["exact"] = &GroupConfig{Providers: []string{"alpha"}, Models: []string{"alpha/a-model-1"}}
cfg.Groups["bare"] = &GroupConfig{Providers: []string{"alpha"}, Models: []string{"a-model-*"}}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminConsumersPath, "sk-admin1"))
var got adminConsumersResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
want := map[string]adminModelAccessCount{
"prefixed": {Allowed: 2, Total: 2},
"exact": {Allowed: 1, Total: 2},
"bare": {Allowed: 2, Total: 2},
}
for _, grp := range got.Groups {
w, ok := want[grp.Name]
if !ok {
continue
}
if grp.ModelAccess["alpha"] != w {
t.Errorf("%s ModelAccess[alpha] = %+v, want %+v", grp.Name, grp.ModelAccess["alpha"], w)
}
delete(want, grp.Name)
}
if len(want) != 0 {
t.Errorf("groups missing from response: %v", want)
}
}
// TestAdminConsumers_UsersFilePathOnly pins the "usersFile path shown,
// contents never" risk note (plan §6): only the configured path appears,
// never any per-user data beyond what userSummary/groupSummary already
// carry.
func TestAdminConsumers_UsersFilePathOnly(t *testing.T) {
t.Parallel()
fp := filepath.Join(t.TempDir(), "users.json")
writeUsersDoc(t, fp, []*UserConfig{{Name: "filed", Group: "agroup", APIKey: "sk-filed"}}, time.Time{})
cfg := newAdminTestConfig()
cfg.Users.File = fp
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminConsumersPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200, body=%s", rec.Code, rec.Body.String())
}
var got adminConsumersResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
if got.UsersFile != fp {
t.Errorf("UsersFile = %q, want %q", got.UsersFile, fp)
}
for _, u := range got.Users {
if u.Name == "filed" && u.Source != "file" {
t.Errorf("filed.Source = %q, want file", u.Source)
}
}
}
func TestAdminCatalog_KindsAndEndpointRows(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Providers["zeta"].ModelBaseURLs = map[string]string{"bge-reranker": "http://rerank.invalid"}
cfg.Providers["zeta"].PathBaseURLs = map[string]string{"/v1/decide": "http://thx.invalid", "/v1/other": "http://o.invalid"}
cfg.Providers["zeta"].Kinds = map[string]string{"bge-reranker": "reranking", "/v1/decide": "decision"}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
}
var got adminCatalogResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
byID := map[string]adminCatalogModel{}
for _, p := range got.Providers {
if p.Name != "zeta" {
for _, m := range p.Models {
if m.Kind != "" || m.Endpoint {
t.Errorf("provider %s model %s carries kind/endpoint without config", p.Name, m.ID)
}
}
continue
}
for _, m := range p.Models {
byID[m.ID] = m
}
}
cases := []struct {
id string
kind string
endpoint bool
}{
{"zeta/z-model", "", false},
{"zeta/bge-reranker", "reranking", false},
{"zeta/v1/decide", "decision", true},
{"zeta/v1/other", "", true},
}
for _, c := range cases {
m, ok := byID[c.id]
if !ok {
t.Errorf("%s missing from catalog", c.id)
continue
}
if m.Kind != c.kind || m.Endpoint != c.endpoint {
t.Errorf("%s: kind=%q endpoint=%v, want %q %v", c.id, m.Kind, m.Endpoint, c.kind, c.endpoint)
}
}
}
func TestKindsValidation(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Providers["zeta"].Kinds = map[string]string{"z-model": ""}
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
if _, err := New(context.Background(), next, cfg, "llmgw"); err == nil {
t.Fatal("empty kind accepted")
}
}
func TestAdminOverview_KindsAndEndpoints(t *testing.T) {
t.Parallel()
cfg := newAdminTestConfig()
cfg.Providers["zeta"].PathBaseURLs = map[string]string{"/v1/decide": "http://thx.invalid"}
cfg.Providers["zeta"].Kinds = map[string]string{"/v1/decide": "decision"}
h, _ := newAdminGatewayHandle(t, cfg)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
}
var got adminOverviewResponse
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
for _, p := range got.Providers {
switch p.Name {
case "zeta":
if len(p.Endpoints) != 1 || p.Endpoints[0] != "/v1/decide" || p.Kinds["/v1/decide"] != "decision" {
t.Errorf("zeta endpoints=%v kinds=%v", p.Endpoints, p.Kinds)
}
default:
if len(p.Endpoints) != 0 || len(p.Kinds) != 0 {
t.Errorf("%s carries endpoints/kinds without config", p.Name)
}
}
}
}