Replay every unanimous upstream status, not just 4xx
openvoxdb does not reserve 5xx for its own faults: the same malformed query is a 400 on /nodes and a 500 on /facts, and /metrics answers a flat 403, so a 4xx-only replay rule made pdbmux's behaviour depend on the route. The meta and metrics handlers held their own copy of the gateway error and bypassed the replay entirely. - Replay any status from 400 up that every backend agreed on, with the backend's own body and content type. - Keep 502 for backends disagreeing on the status, or a backend that answered nothing at all. - Route /pdb/meta, /metrics and the pass-through path through the same rule as the merged query handlers. - Count a unanimous 5xx as a failed round and let it fall back to a stale cache entry; only a unanimous 4xx stays exempt from both. - Answer successful queries with openvoxdb's application/json;charset=utf-8.
This commit is contained in:
+67
-1
@@ -4,6 +4,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -22,7 +23,7 @@ func TestRejectedQuerySurfacesUpstreamStatus(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
params := query(tc.q)
|
||||
status, upstream := h.a.queryRaw(ctx, t, tc.path, params)
|
||||
if !clientShaped(status) {
|
||||
if !replayableStatus(status) || status >= 500 {
|
||||
t.Fatalf("backend %s answered HTTP %d for %s, which is not the client error this test needs: %s",
|
||||
h.a.name, status, tc.q, upstream)
|
||||
}
|
||||
@@ -56,3 +57,68 @@ func TestRejectedQuerySurfacesUpstreamStatus(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// openvoxdb answers the same malformed clause with 400 on /nodes and 500 on
|
||||
// /facts — engine.clj's rewrite-fact-query runs an unguarded nth only for the
|
||||
// facts entity. Both are the backends' real answer, so both have to reach the
|
||||
// client rather than one of them becoming a 502.
|
||||
func TestUnanimousServerErrorSurfacesUpstreamStatus(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
params := query(`["=","name"]`)
|
||||
|
||||
status, upstream := h.a.queryRaw(ctx, t, factsPath, params)
|
||||
if status < 500 {
|
||||
t.Skipf("backend %s answered HTTP %d for the arity bug this test needs", h.a.name, status)
|
||||
}
|
||||
if other, _ := h.b.queryRaw(ctx, t, factsPath, params); other != status {
|
||||
t.Fatalf("backends answered %d and %d, so the failure is not unanimous", status, other)
|
||||
}
|
||||
|
||||
resp := rawGet(t, factsPath, params)
|
||||
if resp.status != status {
|
||||
t.Fatalf("pdbmux answered HTTP %d, want the upstream %d: %s", resp.status, status, resp.body)
|
||||
}
|
||||
if got, want := strings.TrimSpace(string(resp.body)), strings.TrimSpace(string(upstream)); want != "" && got != want {
|
||||
t.Errorf("pdbmux body = %q, want openvoxdb's own explanation %q", got, want)
|
||||
}
|
||||
for _, b := range []*backend{h.a, h.b} {
|
||||
if strings.Contains(string(resp.body), b.url) {
|
||||
t.Errorf("replayed body names backend %s: %q", b.name, resp.body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /metrics and /pdb/meta had their own copy of the gateway error, so a status
|
||||
// both backends agreed on never reached the client on those routes.
|
||||
func TestUnanimousMetricsStatusIsReplayed(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, path := range []string{"/metrics/v1/mbeans", "/metrics/v2/list"} {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
status, _ := h.a.queryRaw(ctx, t, path, nil)
|
||||
if status < 400 {
|
||||
t.Skipf("%s answers HTTP %d on this estate, so there is nothing to replay", path, status)
|
||||
}
|
||||
if other, _ := h.b.queryRaw(ctx, t, path, nil); other != status {
|
||||
t.Fatalf("backends answered %d and %d, so the status is not unanimous", status, other)
|
||||
}
|
||||
if resp := rawGet(t, path, nil); resp.status != status {
|
||||
t.Fatalf("pdbmux answered HTTP %d, want the upstream %d: %s", resp.status, status, resp.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A client cannot tell pdbmux from a PuppetDB by the content type either.
|
||||
func TestSuccessContentTypeMatchesUpstream(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
params := url.Values{"limit": {"1"}}
|
||||
_, _ = h.a.queryRaw(ctx, t, nodesPath, params)
|
||||
|
||||
resp := rawGet(t, nodesPath, params)
|
||||
if resp.status != 200 {
|
||||
t.Fatalf("status %d: %s", resp.status, resp.body)
|
||||
}
|
||||
if got := resp.header.Get("Content-Type"); got != "application/json;charset=utf-8" {
|
||||
t.Errorf("Content-Type = %q, want openvoxdb's own %q", got, "application/json;charset=utf-8")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user