carlos / platform Public

db forward: T7 through the backhaul hop with the real CLI and console (plan 2, Task 11)

The edge test binary is built from the module root with TestMain's kept
build environment, as sharedCarlos builds the CLI.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 7b8720a2076a4f358fad490e0ff439065bfe719b parent 2a943e8
2 files changed, +151 −0
  • dbforward_e2e_test.go +84 −0
  • internal/edge/dbforward_backhaul_helper_test.go +67 −0
diff --git a/dbforward_e2e_test.go b/dbforward_e2e_test.go
index d039231e5..b57b98f16 100644
--- a/dbforward_e2e_test.go
+++ b/dbforward_e2e_test.go
@@ -699,3 +699,87 @@ func TestDBForwardRefusesSQLThatIsNotUTF8(t *testing.T) {
t.Fatalf("a query JSON would rewrite reached the bucket: %v", keys)
}
}
+
+// throughBackhaul serves the console's handler with console.NewHTTPServer
+// (its ReadTimeout set to readTimeout) on 127.0.0.1, behind two real edges
+// that package edge's test binary runs as a helper process: the CLI reaches
+// betty, which backhauls to annie, the console's home. The CLI finds betty
+// through a CONNECT proxy (a dbfTap, standing in for DNS) and trusts the
+// edges' test CA through SSL_CERT_FILE. Building the helper needs the go
+// command on PATH, as every CI step has.
+func throughBackhaul(readTimeout time.Duration, tap **dbfTap) func(t *testing.T, h http.Handler) (string, []string) {
+ return func(t *testing.T, h http.Handler) (string, []string) {
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ hs := console.NewHTTPServer(h)
+ hs.ReadTimeout = readTimeout
+ go hs.Serve(ln)
+ t.Cleanup(func() { hs.Close() })
+ dir := t.TempDir()
+ bin := filepath.Join(dir, "edge.test")
+ // From the module root with the environment TestMain kept for the
+ // carlos build (the real HOME and Go cache), as sharedCarlos builds.
+ build := exec.Command("go", "test", "-c", "-o", bin, "./internal/edge")
+ build.Dir, build.Env = moduleRoot, carlosBuildEnv
+ if out, err := build.CombinedOutput(); err != nil {
+ t.Fatalf("build the edge test binary: %v\n%s", err, out)
+ }
+ cmd := exec.Command(bin, "-test.run", "^TestDBForwardBackhaulPairHelper$", "-test.v")
+ cmd.Env = append(os.Environ(), "DBF_BACKHAUL_HELPER=1", "DBF_BACKHAUL_UPSTREAM="+ln.Addr().String(), "DBF_BACKHAUL_DIR="+dir)
+ stdin, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ pr, pw := io.Pipe()
+ cmd.Stdout = pw
+ var helperErr bytes.Buffer
+ cmd.Stderr = &helperErr
+ if err := cmd.Start(); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() {
+ stdin.Close()
+ cmd.Wait()
+ pw.Close()
+ if t.Failed() {
+ t.Logf("backhaul helper stderr:\n%s", helperErr.String())
+ }
+ })
+ lines := bufio.NewScanner(pr)
+ sender := ""
+ for lines.Scan() {
+ if addr, ok := strings.CutPrefix(lines.Text(), "DBF-BACKHAUL "); ok {
+ sender = addr
+ break
+ }
+ }
+ if sender == "" {
+ t.Fatalf("the backhaul helper never came up:\n%s", helperErr.String())
+ }
+ go func() {
+ for lines.Scan() {
+ }
+ }()
+ tp := newDBFTap(t, sender, true)
+ *tap = tp
+ return "https://console.backhaul.test", []string{"HTTPS_PROXY=http://" + tp.addr, "NO_PROXY=", "SSL_CERT_FILE=" + filepath.Join(dir, "ca.pem")}
+ }
+}
+
+// T7, the backhaul half, end to end (plan review finding 34): the real CLI
+// binary reaches the real console through two real edges, the second hop a
+// backhaul with its 30 s header bound, during a 40 s quiet restore. At the
+// CLI's own stderr and socket: DBF-11 within a second, no silence over 16 s,
+// and its rows after more than the hop's 30 s. The console's real
+// http.Server runs with ReadTimeout 2 s, as in
+// TestDBForwardLongRequestThroughTheEdge.
+// Mutations: drop the stream's flush (red: the wait lines sit in the
+// console's buffer and the CLI's socket is silent); stream nothing before
+// the box's first chunk (red: no headers inside the hop's 30 s).
+func TestDBForwardThroughTheBackhaulHop(t *testing.T) {
+ var tap *dbfTap
+ e := newDBFE2E(t, dbfE2EOptions{forward: true, serve: throughBackhaul(2*time.Second, &tap), worker: slowRestore(40 * time.Second)})
+ checkProgressAtTheCLI(t, e, tap, 40*time.Second)
+}
diff --git a/internal/edge/dbforward_backhaul_helper_test.go b/internal/edge/dbforward_backhaul_helper_test.go
new file mode 100644
index 000000000..f4aa1f09b
--- /dev/null
+++ b/internal/edge/dbforward_backhaul_helper_test.go
@@ -0,0 +1,67 @@
+package edge
+
+import (
+ "context"
+ "encoding/pem"
+ "fmt"
+ "io"
+ "net"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/release"
+)
+
+const dbfBackhaulHost = "console.backhaul.test"
+
+// TestDBForwardBackhaulPairHelper is not a test of its own: the root
+// package's TestDBForwardThroughTheBackhaulHop builds this package's test
+// binary and runs this as a helper process. It stands up two real edges
+// over one bucket, as TestBackhaulE2EAcrossTwoEdges does: annie, home of
+// console.backhaul.test, routed to DBF_BACKHAUL_UPSTREAM (the real console
+// the parent serves), and betty, which serves no host and so backhauls every
+// request for it to annie. It writes the test CA to DBF_BACKHAUL_DIR/ca.pem,
+// prints betty's TLS address on a line starting "DBF-BACKHAUL ", and serves
+// until its stdin closes.
+func TestDBForwardBackhaulPairHelper(t *testing.T) {
+ if os.Getenv("DBF_BACKHAUL_HELPER") != "1" {
+ t.Skip("a helper process for the root package's TestDBForwardThroughTheBackhaulHop")
+ }
+ upstream, dir := os.Getenv("DBF_BACKHAUL_UPSTREAM"), os.Getenv("DBF_BACKHAUL_DIR")
+ e2eQuietLogs(t)
+ bucket := t.TempDir()
+ seed := release.NewDirStore(bucket)
+ if err := MintMeshKey(context.Background(), seed); err != nil {
+ t.Fatal(err)
+ }
+ ca := newE2ECA(t)
+ ca.publish(t, seed, dbfBackhaulHost)
+ home := newBackhaulEdge(t, bucket, "annie", ca, nil, map[string]string{dbfBackhaulHost: upstream})
+ sender := newBackhaulEdge(t, bucket, "betty", ca, nil, nil)
+ eip, port, err := net.SplitHostPort(home.tlsAddr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ oldPort, oldCAs := backhaulPort, backhaulRootCAs
+ backhaulPort, backhaulRootCAs = port, ca.pool
+ t.Cleanup(func() { backhaulPort, backhaulRootCAs = oldPort, oldCAs })
+ putJSON(t, seed, "control/edges/annie.json", fmt.Sprintf(`{"v":1,"box":"annie","eip":%q,"hosts":{%q:{"role":"home"}}}`, eip, dbfBackhaulHost))
+ bEIP, _, err := net.SplitHostPort(sender.tlsAddr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ putJSON(t, seed, "control/edges/betty.json", fmt.Sprintf(`{"v":1,"box":"betty","eip":%q,"hosts":{}}`, bEIP))
+ for _, be := range []*backhaulEdge{home, sender} {
+ if err := be.mirror.Refresh(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ refreshHomeMap(t, be.homes)
+ }
+ caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.root.Raw})
+ if err := os.WriteFile(filepath.Join(dir, "ca.pem"), caPEM, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ fmt.Println("DBF-BACKHAUL " + sender.tlsAddr)
+ io.Copy(io.Discard, os.Stdin)
+}