edge: the backhaul scene takes whole route rows, an activator and edges-file entries (asset cache plan 3 Task 9)
newBackhaulEdgeWith takes registry rows (Account, App, AssetGen, socket instances) and the activator New is handed; newBackhaulEdge keeps its host-to-address form over it. newBackhaulSceneWith adds routes, the home's activator, certificates and home edges-file entries in edgesHostView's shape (publishEdgesEntries, publishHomeEdgesFile). A box's 443 door can carry a tap recording each request's inbound headers and the final response headers. The zero extra is the scene exactly as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+140
−17
internal/edge/backhaul_e2e_test.go+140 −17
diff --git a/internal/edge/backhaul_e2e_test.go b/internal/edge/backhaul_e2e_test.go| index b841e47f9..3008e6f61 100644 |
| --- a/internal/edge/backhaul_e2e_test.go |
| +++ b/internal/edge/backhaul_e2e_test.go |
| @@ -55,7 +55,9 @@ import ( |
| "net" |
| "net/http" |
| "net/http/httptest" |
| + "sort" |
| "strings" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| @@ -253,22 +255,91 @@ type backhaulEdge struct { |
| // 127.0.0.1, so a test that needs the peer to be that address says so |
| // here. Unset, the peer is the real one. |
| peerAs atomic.Value // string |
| + // tap, when set, sees every request at this box's 443 door as it |
| + // arrived (before ServeHTTP strips anything) and the final response |
| + // headers this box wrote for it. The asset e2e tests read the wire |
| + // between the two edges from it; nil, the door is exactly as before. |
| + tap atomic.Pointer[e2eTap] |
| client *http.Client |
| } |
| -// newBackhaulEdge builds one box. routes are host → upstream address; anything |
| -// not listed is a host this box genuinely does not serve. |
| +// e2eTap records one box's 443 door: each request's headers as they arrived |
| +// and the final response headers written for it, in arrival order. |
| +type e2eTap struct { |
| + mu sync.Mutex |
| + reqs []http.Header |
| + resps []http.Header |
| +} |
| + |
| +// last returns the most recent request's inbound and response headers. |
| +func (t *e2eTap) last() (req, resp http.Header) { |
| + t.mu.Lock() |
| + defer t.mu.Unlock() |
| + if n := len(t.reqs); n > 0 { |
| + req = t.reqs[n-1] |
| + } |
| + if n := len(t.resps); n > 0 { |
| + resp = t.resps[n-1] |
| + } |
| + return req, resp |
| +} |
| + |
| +// e2eTapWriter snapshots the final response headers at the first final |
| +// status. Unwrap keeps flushing and hijacking working through it. |
| +type e2eTapWriter struct { |
| + http.ResponseWriter |
| + tap *e2eTap |
| + wrote bool |
| +} |
| + |
| +func (w *e2eTapWriter) WriteHeader(code int) { |
| + if code >= 200 && !w.wrote { |
| + w.wrote = true |
| + w.tap.mu.Lock() |
| + w.tap.resps = append(w.tap.resps, w.Header().Clone()) |
| + w.tap.mu.Unlock() |
| + } |
| + w.ResponseWriter.WriteHeader(code) |
| +} |
| + |
| +func (w *e2eTapWriter) Write(p []byte) (int, error) { |
| + if !w.wrote { |
| + w.WriteHeader(http.StatusOK) |
| + } |
| + return w.ResponseWriter.Write(p) |
| +} |
| + |
| +func (w *e2eTapWriter) Flush() { _ = http.NewResponseController(w.ResponseWriter).Flush() } |
| + |
| +func (w *e2eTapWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter } |
| + |
| +// newBackhaulEdge builds one box. routes are host → upstream address, each a |
| +// TCP service route; anything not listed is a host this box genuinely does |
| +// not serve. |
| func newBackhaulEdge(t *testing.T, bucketDir, box string, ca *e2eCA, from *net.TCPAddr, routes map[string]string) *backhaulEdge { |
| + t.Helper() |
| + rts := make([]registry.Route, 0, len(routes)) |
| + for host, addr := range routes { |
| + rts = append(rts, registry.Route{Host: host, Kind: "service", Addr: addr, Status: "ready"}) |
| + } |
| + return newBackhaulEdgeWith(t, bucketDir, box, ca, from, nil, rts) |
| +} |
| + |
| +// newBackhaulEdgeWith is newBackhaulEdge with whole route rows (so a test |
| +// can give a route its Account, App, AssetGen, or make it an activator- |
| +// managed instance on a unix socket) and the activator New is handed (nil |
| +// for none, as for `carlos edge`). |
| +func newBackhaulEdgeWith(t *testing.T, bucketDir, box string, ca *e2eCA, from *net.TCPAddr, act Activator, routes []registry.Route) *backhaulEdge { |
| t.Helper() |
| be := &backhaulEdge{store: release.NewDirStore(bucketDir)} |
| reg := openReg(t) |
| - for host, addr := range routes { |
| - if err := reg.Upsert(registry.Route{Host: host, Kind: "service", Addr: addr, Status: "ready"}); err != nil { |
| - t.Fatalf("upsert %s: %v", host, err) |
| + for _, rt := range routes { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatalf("upsert %s: %v", rt.Host, err) |
| } |
| } |
| - e, err := New(reg, nil) |
| + e, err := New(reg, act) |
| if err != nil { |
| t.Fatalf("New: %v", err) |
| } |
| @@ -305,6 +376,12 @@ func newBackhaulEdge(t *testing.T, bucketDir, box string, ca *e2eCA, from *net.T |
| _, port, _ := net.SplitHostPort(r.RemoteAddr) |
| r.RemoteAddr = net.JoinHostPort(p, port) |
| } |
| + if tap := be.tap.Load(); tap != nil { |
| + tap.mu.Lock() |
| + tap.reqs = append(tap.reqs, r.Header.Clone()) |
| + tap.mu.Unlock() |
| + w = &e2eTapWriter{ResponseWriter: w, tap: tap} |
| + } |
| e.ServeHTTP(w, r) |
| })) |
| https.TLS = tlsCfg |
| @@ -375,9 +452,29 @@ type backhaulScene struct { |
| // platform would not give us a second loopback address. |
| clientIP string |
| deadEIP string |
| + // aEIP and bEIP are the two edges' addresses as their edges files |
| + // publish them. |
| + aEIP, bEIP string |
| +} |
| + |
| +// backhaulSceneExtra is what a scene adds to the standard two edges: more |
| +// route rows on either edge, the home edge's activator, more certificates in |
| +// the bucket, and more entries in the home edge's edges file (host → the |
| +// JSON of its entry, edgesHostView's shape). The zero value is the standard |
| +// scene. |
| +type backhaulSceneExtra struct { |
| + homeRoutes, farRoutes []registry.Route |
| + homeAct Activator |
| + certs []string |
| + homeEntries map[string]string |
| } |
| func newBackhaulScene(t *testing.T) *backhaulScene { |
| + t.Helper() |
| + return newBackhaulSceneWith(t, backhaulSceneExtra{}) |
| +} |
| + |
| +func newBackhaulSceneWith(t *testing.T, x backhaulSceneExtra) *backhaulScene { |
| t.Helper() |
| e2eQuietLogs(t) |
| bucket := t.TempDir() |
| @@ -393,7 +490,7 @@ func newBackhaulScene(t *testing.T) *backhaulScene { |
| if err := MintMeshKey(context.Background(), s.seed); err != nil { |
| t.Fatalf("mint mesh key: %v", err) |
| } |
| - for _, host := range []string{e2eHomedHost, e2eMovedHost, e2eDeadHost, e2eUnhomedHost, e2eSenderHost} { |
| + for _, host := range append([]string{e2eHomedHost, e2eMovedHost, e2eDeadHost, e2eUnhomedHost, e2eSenderHost}, x.certs...) { |
| s.leaf[host] = s.ca.publish(t, s.seed, host) |
| } |
| @@ -401,12 +498,12 @@ func newBackhaulScene(t *testing.T) *backhaulScene { |
| if from != nil { |
| s.clientIP = from.IP.String() |
| } |
| - s.a = newBackhaulEdge(t, bucket, "annie", s.ca, from, map[string]string{ |
| - e2eHomedHost: e2eApp(t, e2eHomeMarker).Listener.Addr().String(), |
| - }) |
| - s.b = newBackhaulEdge(t, bucket, "betty", s.ca, from, map[string]string{ |
| - e2eSenderHost: e2eApp(t, e2eSenderMarker).Listener.Addr().String(), |
| - }) |
| + s.a = newBackhaulEdgeWith(t, bucket, "annie", s.ca, from, x.homeAct, append([]registry.Route{{ |
| + Host: e2eHomedHost, Kind: "service", Addr: e2eApp(t, e2eHomeMarker).Listener.Addr().String(), Status: "ready", |
| + }}, x.homeRoutes...)) |
| + s.b = newBackhaulEdgeWith(t, bucket, "betty", s.ca, from, nil, append([]registry.Route{{ |
| + Host: e2eSenderHost, Kind: "service", Addr: e2eApp(t, e2eSenderMarker).Listener.Addr().String(), Status: "ready", |
| + }}, x.farRoutes...)) |
| // The dial seams, armed for the test's lifetime: 443 is an ephemeral port |
| // here, and the home edge's certificate is signed by the test CA rather |
| @@ -431,24 +528,50 @@ func newBackhaulScene(t *testing.T) *backhaulScene { |
| if err != nil { |
| t.Fatalf("split sending edge address: %v", err) |
| } |
| - s.publishEdgesFile(t, "annie", eip, e2eHomedHost, e2eMovedHost) |
| + s.aEIP, s.bEIP = eip, bEIP |
| + s.publishHomeEdgesFile(t, x.homeEntries) |
| s.publishEdgesFile(t, "betty", bEIP, e2eSenderHost) |
| s.publishEdgesFile(t, "carol", s.deadEIP, e2eDeadHost) |
| return s |
| } |
| +// publishHomeEdgesFile (re)writes the home edge's file: the standard homed |
| +// and moved hosts, plus extra (host → entry JSON), such as an asset host's |
| +// entry read from the writer-pinned golden. |
| +func (s *backhaulScene) publishHomeEdgesFile(t *testing.T, extra map[string]string) { |
| + t.Helper() |
| + entries := map[string]string{e2eHomedHost: `{"role":"home"}`, e2eMovedHost: `{"role":"home"}`} |
| + for h, entry := range extra { |
| + entries[h] = entry |
| + } |
| + s.publishEdgesEntries(t, "annie", s.aEIP, entries) |
| +} |
| + |
| // publishEdgesFile writes one box's control/edges/<box>.json — homedOn's shape, |
| // written through the seed handle so neither edge is the publisher of its own |
| // map (which is the production writer arrangement: the box's own host loop |
| // writes it, and every edge only ever READS the prefix). |
| func (s *backhaulScene) publishEdgesFile(t *testing.T, box, eip string, hosts ...string) { |
| t.Helper() |
| - entries := make([]string, 0, len(hosts)) |
| + entries := make(map[string]string, len(hosts)) |
| for _, h := range hosts { |
| - entries = append(entries, fmt.Sprintf("%q:{\"role\":\"home\"}", h)) |
| + entries[h] = `{"role":"home"}` |
| + } |
| + s.publishEdgesEntries(t, box, eip, entries) |
| +} |
| + |
| +// publishEdgesEntries is publishEdgesFile with each host's whole entry given |
| +// as JSON in edgesHostView's shape, so an entry can carry an owner and an |
| +// assets block. |
| +func (s *backhaulScene) publishEdgesEntries(t *testing.T, box, eip string, entries map[string]string) { |
| + t.Helper() |
| + parts := make([]string, 0, len(entries)) |
| + for h, entry := range entries { |
| + parts = append(parts, fmt.Sprintf("%q:%s", h, entry)) |
| } |
| + sort.Strings(parts) |
| putJSON(t, s.seed, "control/edges/"+box+".json", fmt.Sprintf( |
| - `{"v":1,"box":%q,"eip":%q,"hosts":{%s}}`, box, eip, strings.Join(entries, ","))) |
| + `{"v":1,"box":%q,"eip":%q,"hosts":{%s}}`, box, eip, strings.Join(parts, ","))) |
| } |
| // converge drives every mirror and every map ONE poll forward. Explicit, never |