fix(docs,integration): example env completeness, flaky reset, cache key assertion

Task 5 re-review fixes: 1 Important + 3 folded.

1. (Important) examples/kubernetes.yaml was construction-broken on
   copy-paste: the inline dashboard-admin user resolves env:LLMGW_ADMIN_KEY
   at construction time regardless of admin.enabled, but the Helm env
   snippet never listed it. Added it there, alongside a comment on the
   admin user explaining the env var is required either way. Fixed the
   two reversed above/below cross-references (kubernetes.yaml:90/:115)
   and reworded the misleading "self-contained" comment on the inline
   admin user. Verified with a throwaway round-trip test covering: no env
   vars set fails, every documented env var set succeeds (admin.enabled
   staying false as committed), and — the actual bug — every var set
   EXCEPT LLMGW_ADMIN_KEY still fails even with admin disabled.

2. README: Admin section's "(disabled still 404s)" now points at the
   same 404/passthroughUnknown wording the section already uses above it.
   Caching section's "block absent entirely is a construction error" now
   says "not enabled" — auth.go's actual check is !cfg.Cache.Enabled,
   which is identically true whether the block is omitted or present with
   enabled: false.

3. integration/mock/main.go: added POST /flaky/reset (handleFlakyReset),
   reachable through the "flaky" provider's own native passthrough route,
   resetting the atomic hit counter on demand. TestRetryFlakyRecovers now
   calls it at the start of the test, removing the make integration-keep
   re-run trap the old doc comment warned about instead of fixing.
   dynamic.yml.tmpl's stale "test 8" comment now reads "task 5".

4. integration_test.go: added redisKeys (docker compose exec redis-cli
   KEYS, mirroring flushRedis's own pattern) and a
   TestResponseCache assertion that redis holds at least one
   llmgw:cache:* key immediately after the miss request.

go test ./... -count=1, gofmt, vet, make yaegi-check all pass; make
integration exits 0 across two fresh full runs.
This commit is contained in:
2026-08-20 20:30:08 +01:00
parent 2fe71c25eb
commit f49c51b341
5 changed files with 108 additions and 42 deletions
+35 -5
View File
@@ -339,6 +339,26 @@ func flushRedis(t *testing.T) {
}
}
// redisKeys returns every key in the shared Redis matching pattern (a
// redis-cli KEYS glob), via the same docker compose exec pattern
// flushRedis uses above. KEYS is O(n) and blocks Redis briefly — fine
// against this suite's tiny keyspace, and used only for a one-off test
// assertion, never on a hot path.
func redisKeys(t *testing.T, pattern string) []string {
t.Helper()
out, err := exec.Command("docker", "compose", "-f", composeFile(), "exec", "-T", "redis", "redis-cli", "KEYS", pattern).CombinedOutput()
if err != nil {
t.Fatalf("redis-cli KEYS %s: %v\n%s", pattern, err, out)
}
var keys []string
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if line != "" {
keys = append(keys, line)
}
}
return keys
}
// TestCrossReplicaLimits covers integration test 4: bob's limited group
// (requestsPerMinute: 3) is enforced globally across both Traefik
// "replicas" via the Redis they share, regardless of which one a given
@@ -533,12 +553,19 @@ func TestRealUpstreamSmoke(t *testing.T) {
// retry made exactly two upstream tries, not merely that the mock
// happened to succeed on a fresh process.
//
// This test is NOT safe to re-run against a `make integration-keep`
// stack without restarting mockopenai first: handleFlakyOpenAIChat's own
// hit counter is process-lifetime state with no reset endpoint (see its
// doc comment, integration/mock/main.go) — a second run against the same
// container never observes the forced-503-then-success sequence again.
// Resets the mock's hit counter first via POST /flaky/reset — reachable
// through the "flaky" provider's own native passthrough route
// (routes_passthrough.go), which reaches handleFlakyReset
// (integration/mock/main.go) — so this test reproduces the same
// forced-503-then-success sequence on every run, including a re-run
// against an already-up `make integration-keep` stack with no
// mockopenai restart.
func TestRetryFlakyRecovers(t *testing.T) {
resetResp, resetBody := doJSON(t, http.MethodPost, traefik1URL+"/flaky/reset", aliceKey, nil)
if resetResp.StatusCode != http.StatusOK {
t.Fatalf("reset flaky counter: status = %d, body=%#v", resetResp.StatusCode, resetBody)
}
reqBody := map[string]any{
"model": "flaky/flaky-mock",
"messages": []map[string]any{{"role": "user", "content": "hi"}},
@@ -605,6 +632,9 @@ func TestResponseCache(t *testing.T) {
if content := firstChoiceContent(t, body1); content != "mock openai response" {
t.Errorf("first request: content = %q, want %q", content, "mock openai response")
}
if keys := redisKeys(t, "llmgw:cache:*"); len(keys) == 0 {
t.Error("redis holds no llmgw:cache:* key after a cache miss, want the entry the miss's SET (cache.go) should have written")
}
tokensAfterMiss, _ := adminUsageEntry(t, "users", "alice")["tokensPerDay"].(float64)
if tokensAfterMiss <= 0 {
+34 -16
View File
@@ -76,7 +76,12 @@ func newServerMux(mode string) (*http.ServeMux, error) {
// 5): the plugin's "flaky" provider points its baseUrl at this
// mode's own address plus "/flaky", so a.baseURL+"/v1/chat/
// completions" (provider_openai.go) resolves to exactly this path.
// flaky/reset is reachable the same way, through the "flaky"
// provider's native passthrough route (adapter.base()+"/"+rest,
// routes_passthrough.go) — the test calls it directly, never this
// mock process's own network address.
mux.HandleFunc("/flaky/v1/chat/completions", handleFlakyOpenAIChat)
mux.HandleFunc("/flaky/reset", handleFlakyReset)
mux.HandleFunc("/v1/images/generations", handleOpenAIImages)
mux.HandleFunc("/v1/audio/speech", handleOpenAIAudioSpeech)
case "anthropic":
@@ -208,24 +213,37 @@ func handleOpenAIChat(w http.ResponseWriter, r *http.Request) {
sw.write("", "[DONE]")
}
// flakyHits counts every request handleFlakyOpenAIChat has served across
// this process's lifetime. It is package-level, process-lifetime state
// deliberately, not reset per request: the retry integration test relies
// on exactly the FIRST request this mock process ever receives on
// /flaky/v1/chat/completions failing, and every one after succeeding, so
// a fresh mockopenai container (one per `make integration` run) always
// starts the scenario from a clean slate. Re-running the integration
// suite against an already-up stack (`make integration-keep`, without
// restarting mockopenai) will NOT reproduce the retry test a second time
// — the same non-idempotence flushRedis exists to paper over for the
// rate-limit counters has no equivalent reset here, since there is no
// admin endpoint on this fixture server to clear it.
// flakyHits counts every request handleFlakyOpenAIChat has served since
// the last reset (handleFlakyReset, below), or since process start if
// never reset. It is package-level state deliberately, not reset per
// request: the retry integration test relies on exactly the FIRST
// request counted this way failing, and every one after succeeding.
// TestRetryFlakyRecovers calls handleFlakyReset (via the gateway's own
// "flaky" passthrough provider) at its own start, so a re-run against an
// already-up stack (`make integration-keep`, no mockopenai restart)
// reproduces the same forced-503-then-success scenario every time — the
// same idempotence flushRedis gives the rate-limit counters via
// TestCrossReplicaLimits.
var flakyHits int32
// handleFlakyOpenAIChat answers the first request this process ever
// receives with HTTP 503 (a transient failure retryPolicy.isTransient
// classifies as retryable, retry.go) and every request after that with a
// normal chat completion response carrying an extra top-level
// handleFlakyReset resets flakyHits to 0, letting the retry integration
// test reproduce its forced-503-then-success scenario on demand rather
// than only on this process's true first request. POST only — a GET (or
// any other method) must never mutate state; it answers 405 instead.
func handleFlakyReset(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
atomic.StoreInt32(&flakyHits, 0)
writeJSON(w, map[string]any{"reset": true})
}
// handleFlakyOpenAIChat answers the first request since the last reset
// (handleFlakyReset, or process start if never reset) with HTTP 503 (a
// transient failure retryPolicy.isTransient classifies as retryable,
// retry.go) and every request after that with a normal chat completion
// response carrying an extra top-level
// "mock_attempt" field set to this handler's own call count — the
// unified route's openai-type adapter forwards a JSON response body
// verbatim (forwardJSON, provider_openai.go), so this field survives
+7 -5
View File
@@ -44,14 +44,16 @@ http:
baseUrl: "http://mockgemini:8080"
apiKey: "mock-gemini-key"
models: ["gemini-mock"]
# Backs test 8's retry coverage only: baseUrl points at the
# Backs task 5's retry coverage only: baseUrl points at the
# SAME mockopenai container, under its dedicated /flaky prefix
# (see integration/mock/main.go's handleFlakyOpenAIChat), so
# a.baseURL+"/v1/chat/completions" (provider_openai.go)
# resolves to /flaky/v1/chat/completions. discovery is left
# off — GET /flaky/v1/models isn't served — so the explicit
# "flaky-mock" model id below is the only way this provider's
# model is known.
# resolves to /flaky/v1/chat/completions, and this provider's
# own native passthrough route (/flaky/reset) reaches
# handleFlakyReset the same way. discovery is left off — GET
# /flaky/v1/models isn't served — so the explicit "flaky-mock"
# model id below is the only way this provider's model is
# known.
flaky:
type: openai
baseUrl: "http://mockopenai:8080/flaky"