| new file mode 100644 |
| index 000000000..3d474195f |
| --- /dev/null |
| +++ b/internal/edge/path_across_cache_test.go |
| @@ -0,0 +1,211 @@ |
| +package edge |
| + |
| +// Path routes across boxes (#186, spec §7.2, rev 5.1): a case 3 request (a |
| +// prefix of a front this box does not serve selects a target here) neither |
| +// looks up nor stores in the response LRU, and a target's Carlos-Purge-Host |
| +// naming that front is skipped as unserved, as it always was. Case 1 (front |
| +// and target both here) is cached as before. |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// cacheAcrossBackend answers a GET with a cacheable body naming itself and |
| +// the request, counting them, and a POST with the purge declaration set on |
| +// it. |
| +type cacheAcrossBackend struct { |
| + name string |
| + gets atomic.Int64 |
| + purgeHost, purge string |
| +} |
| + |
| +func (b *cacheAcrossBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| + if r.Method == http.MethodGet { |
| + b.gets.Add(1) |
| + w.Header().Set("Cache-Control", "public, max-age=600") |
| + w.Write([]byte(b.name + "|" + r.Host + "|" + r.URL.Path)) |
| + return |
| + } |
| + if b.purgeHost != "" { |
| + w.Header().Set("Carlos-Purge-Host", b.purgeHost) |
| + } |
| + if b.purge != "" { |
| + w.Header().Set("Carlos-Purge", b.purge) |
| + } |
| + w.WriteHeader(http.StatusOK) |
| +} |
| + |
| +// backedBy makes b the backend of a route (its Addr). |
| +func backedBy(t *testing.T, b http.Handler) func(*registry.Route) { |
| + t.Helper() |
| + srv := httptest.NewServer(b) |
| + t.Cleanup(srv.Close) |
| + return func(r *registry.Route) { r.Addr = srv.Listener.Addr().String() } |
| +} |
| + |
| +// doAcross sends method host+urlPath through ServeHTTP and wants a 200. |
| +func doAcross(t *testing.T, e *Edge, method, host, urlPath string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + rec := httptest.NewRecorder() |
| + e.ServeHTTP(rec, httptest.NewRequest(method, "http://"+host+urlPath, nil)) |
| + if rec.Code != http.StatusOK { |
| + t.Fatalf("%s %s%s = %d, want 200; body %q", method, host, urlPath, rec.Code, rec.Body.String()) |
| + } |
| + return rec |
| +} |
| + |
| +// case3Scene is a box serving the target tenant.example.com (account bae) |
| +// from tenant, with the claim's row acme -> tenant.example.com for |
| +// hub.example.com, which betty publishes as a front route of bae/hub. The |
| +// cache is armed. extra runs before the Edge is built. |
| +func case3Scene(t *testing.T, tenant http.Handler, extra func(*selScene)) (*selScene, *Edge) { |
| + t.Helper() |
| + s := newSelScene(t) |
| + s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant)) |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + s.publish("betty", selFront, frontRow(selAcct, "hub")) |
| + if extra != nil { |
| + extra(s) |
| + } |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + got := s.sel(selFront, "/acme/page") |
| + wantLocal(t, got, selTarget, "acme", selTarget) |
| + if got.Up.front == nil { |
| + t.Fatalf("fixture: %s is not a case 3 selection", describeSel(got)) |
| + } |
| + return s, e |
| +} |
| + |
| +func TestPathAcrossCase3NotCached(t *testing.T) { |
| + tenant := &cacheAcrossBackend{name: "tenant"} |
| + _, e := case3Scene(t, tenant, nil) |
| + |
| + for range 2 { |
| + doAcross(t, e, http.MethodGet, selFront, "/acme/page") |
| + } |
| + if n := tenant.gets.Load(); n != 2 { |
| + t.Fatalf("the target saw %d GETs for two identical case 3 requests, want 2", n) |
| + } |
| + if cached(e.cache, selFront, "/acme/page", "") { |
| + t.Fatal("a cacheable case 3 response was stored under the front") |
| + } |
| + // Control: the target's own host is cached on the same box. |
| + for range 2 { |
| + doAcross(t, e, http.MethodGet, selTarget, "/page") |
| + } |
| + if n := tenant.gets.Load(); n != 3 { |
| + t.Fatalf("control: the target saw %d GETs, want 3 (its own host's second GET a hit)", n) |
| + } |
| +} |
| + |
| +// A catch-all here (*.example.com, another account) answered hub.example.com |
| +// before the claim's path rows arrived, and its page is cached under that |
| +// name. Once case 3 selects the target for the prefix, the request reaches |
| +// the target, never the catch-all's cached page. |
| +func TestPathAcrossCase3NeverServedFromCatchAllCache(t *testing.T) { |
| + tenant := &cacheAcrossBackend{name: "tenant"} |
| + wild := &cacheAcrossBackend{name: "wild"} |
| + s := newSelScene(t) |
| + s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant)) |
| + s.instance("wild.example.net", selOther, "wild", backedBy(t, wild)) |
| + if err := s.reg.UpsertAlias(registry.RouteAlias{Alias: "*.example.com", Host: "wild.example.net", Account: selOther, App: "wild"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + s.publish("betty", selFront, frontRow(selAcct, "hub")) |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + |
| + for range 2 { |
| + doAcross(t, e, http.MethodGet, selFront, "/acme/page") |
| + } |
| + if n := wild.gets.Load(); n != 1 || !cached(e.cache, selFront, "/acme/page", "") { |
| + t.Fatalf("fixture: the catch-all saw %d GETs (want 1) and its page cached = %v (want true)", n, cached(e.cache, selFront, "/acme/page", "")) |
| + } |
| + |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + got := s.sel(selFront, "/acme/page") |
| + wantLocal(t, got, selTarget, "acme", selTarget) |
| + if got.Up.front == nil { |
| + t.Fatalf("fixture: %s is not a case 3 selection", describeSel(got)) |
| + } |
| + for i := range 2 { |
| + rec := doAcross(t, e, http.MethodGet, selFront, "/acme/page") |
| + if want := "tenant|" + selFront + "|/acme/page"; rec.Body.String() != want { |
| + t.Fatalf("request %d: body %q, want the target's %q (never the catch-all's cached page)", i, rec.Body.String(), want) |
| + } |
| + } |
| + if n := tenant.gets.Load(); n != 2 { |
| + t.Fatalf("the target saw %d GETs, want 2", n) |
| + } |
| +} |
| + |
| +// Case 1: the front and the target both here. The front's prefix is cached |
| +// under the front's name as it always was. |
| +func TestPathAcrossSameBoxStillCached(t *testing.T) { |
| + tenant := &cacheAcrossBackend{name: "tenant"} |
| + s := newSelScene(t) |
| + s.instance(selFront, selAcct, "hub") |
| + s.instance(selTarget, selAcct, "tenant", backedBy(t, tenant)) |
| + s.path(selFront, "acme", selTarget, selAcct, "hub") |
| + e := s.build() |
| + e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + got := s.sel(selFront, "/acme/page") |
| + wantLocal(t, got, selTarget, "acme", selTarget) |
| + if got.Up.front != nil { |
| + t.Fatalf("fixture: a same-box target carries a front mark: %s", describeSel(got)) |
| + } |
| + |
| + for range 2 { |
| + doAcross(t, e, http.MethodGet, selFront, "/acme/page") |
| + } |
| + if n := tenant.gets.Load(); n != 1 { |
| + t.Fatalf("the target saw %d GETs, want 1 (the second answered from the LRU)", n) |
| + } |
| + if !cached(e.cache, selFront, "/acme/page", "") { |
| + t.Fatal("a case 1 response was not stored under the front") |
| + } |
| +} |
| + |
| +// A target naming a front this box does not serve in Carlos-Purge-Host: the |
| +// name is skipped as unserved, nothing under it is evicted, the rest of the |
| +// declaration (the target's own host) is honoured, and the write itself |
| +// succeeds, sent on the front (case 3) or on the target's own host. |
| +func TestPurgeAcrossSkipped(t *testing.T) { |
| + for _, on := range []struct{ host, path string }{ |
| + {selFront, "/acme/admin"}, |
| + {selTarget, "/admin"}, |
| + } { |
| + t.Run(on.host, func(t *testing.T) { |
| + logs := captureLog(t) |
| + tenant := &cacheAcrossBackend{name: "tenant", purgeHost: selFront + ", " + selTarget, purge: "*"} |
| + _, e := case3Scene(t, tenant, nil) |
| + seed(t, e.cache, selFront, "/acme/events/42", "", "under the front") |
| + seed(t, e.cache, selTarget, "/events/42", "", "the target's own") |
| + |
| + rec := doAcross(t, e, http.MethodPost, on.host, on.path) |
| + |
| + if got := rec.Header().Get("Carlos-Purge-Host"); got != "" { |
| + t.Fatalf("Carlos-Purge-Host reached the client: %q", got) |
| + } |
| + if !cached(e.cache, selFront, "/acme/events/42", "") { |
| + t.Fatal("a purge naming a front this box does not serve evicted an entry under it") |
| + } |
| + if cached(e.cache, selTarget, "/events/42", "") { |
| + t.Fatal("the rest of the declaration (the target's own host) was not honoured") |
| + } |
| + if out := logs.String(); !strings.Contains(out, "does not serve") { |
| + t.Errorf("no unserved line for the skipped front; log:\n%s", out) |
| + } |
| + }) |
| + } |
| +} |