Add portable shared WireGuard profiles while retaining Tailscale and Worker boundaries. Bound rover command/telemetry delivery, recovery and diagnostic capture across Core and Node.
142 lines
4.6 KiB
Go
142 lines
4.6 KiB
Go
package node
|
|
|
|
// Bounded connection establishment regression inside the existing isolated
|
|
// Worker netem namespace. No motor, host networking, or board interface exists.
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWorkerConnectFirstSYN(t *testing.T) {
|
|
for _, legacy := range []bool{false, true} {
|
|
name := "production"
|
|
if legacy {
|
|
name = "legacy-one-second"
|
|
}
|
|
t.Run(name, func(t *testing.T) { connectFirstSYN(t, legacy) })
|
|
}
|
|
}
|
|
|
|
func connectFirstSYN(t *testing.T, legacy bool) {
|
|
if os.Getenv("MISSION_CORE_ISOLATED_NETEM") != "1" {
|
|
t.Fatal("isolated netem only")
|
|
}
|
|
if _, err := os.Stat("/.dockerenv"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
interfaces, err := net.Interfaces()
|
|
if err != nil || len(interfaces) != 1 || interfaces[0].Name != "lo" {
|
|
t.Fatal("loopback-only namespace required")
|
|
}
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }))
|
|
server.Listener.Close()
|
|
server.Listener, err = net.Listen("tcp4", "127.0.0.2:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.StartTLS()
|
|
defer server.Close()
|
|
tr := newRoverHTTPTransport(server.Client().Transport.(*http.Transport).TLSClientConfig.Clone())
|
|
tr.TLSClientConfig.ServerName = "127.0.0.1"
|
|
if legacy {
|
|
tr.DialContext = (&net.Dialer{Timeout: time.Second}).DialContext
|
|
}
|
|
defer tr.CloseIdleConnections()
|
|
tc(t, "qdisc", "add", "dev", "lo", "root", "handle", "1:", "prio", "bands", "3")
|
|
defer tc(t, "qdisc", "del", "dev", "lo", "root")
|
|
tc(t, "filter", "add", "dev", "lo", "protocol", "ip", "parent", "1:", "pref", "7", "u32",
|
|
"match", "ip", "dst", "127.0.0.2/32", "match", "ip", "protocol", "6", "0xff",
|
|
"match", "u8", "0x02", "0x12", "at", "33", "action", "drop")
|
|
// The path is healthy again before the 1 s initial TCP retransmission.
|
|
// Only the first SYN is intentionally discarded, never every retry.
|
|
var workers sync.WaitGroup
|
|
var counters string
|
|
workers.Add(1)
|
|
go func() {
|
|
defer workers.Done()
|
|
time.Sleep(200 * time.Millisecond)
|
|
counters = string(tc(t, "-s", "filter", "show", "dev", "lo", "parent", "1:", "pref", "7"))
|
|
tc(t, "filter", "del", "dev", "lo", "parent", "1:", "pref", "7")
|
|
}()
|
|
started := time.Now()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2500*time.Millisecond)
|
|
defer cancel()
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", server.URL, nil)
|
|
response, requestErr := (&http.Client{Transport: tr}).Do(req)
|
|
elapsed := time.Since(started)
|
|
status := 0
|
|
if response != nil {
|
|
status = response.StatusCode
|
|
response.Body.Close()
|
|
}
|
|
workers.Wait()
|
|
data, _ := json.Marshal(map[string]any{"case": t.Name(), "elapsed_ms": float64(elapsed.Microseconds()) / 1000,
|
|
"status": status, "error": roverErrorKind(requestErr), "filter_counters": counters, "actuator_output": false})
|
|
t.Log("CONNECT_EVIDENCE " + string(data))
|
|
if !strings.Contains(counters, "dropped 1,") {
|
|
t.Fatal("Expected exactly one dropped SYN")
|
|
}
|
|
if legacy && requestErr == nil {
|
|
t.Fatal("Legacy deadline unexpectedly recovered SYN loss")
|
|
}
|
|
if !legacy && (requestErr != nil || status != 200) {
|
|
t.Fatal("Production transport could not recover first SYN loss")
|
|
}
|
|
}
|
|
|
|
// A longer TCP establishment allowance must still obey its owning context.
|
|
func TestWorkerConnectCancellation(t *testing.T) {
|
|
if os.Getenv("MISSION_CORE_ISOLATED_NETEM") != "1" {
|
|
t.Fatal("isolated Worker only")
|
|
}
|
|
listener, err := net.Listen("tcp4", "127.0.0.2:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
// Accept TCP but withhold TLS; cancellation must interrupt the handshake.
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
c, e := listener.Accept()
|
|
if e == nil {
|
|
defer c.Close()
|
|
b := make([]byte, 4096)
|
|
for {
|
|
if _, e = c.Read(b); e != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
tr := newRoverHTTPTransport(nil)
|
|
defer tr.CloseIdleConnections()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 75*time.Millisecond)
|
|
defer cancel()
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", "https://"+listener.Addr().String(), nil)
|
|
started := time.Now()
|
|
response, err := (&http.Client{Transport: tr}).Do(req)
|
|
requestElapsed := time.Since(started)
|
|
if response != nil {
|
|
response.Body.Close()
|
|
}
|
|
if err == nil || requestElapsed > 500*time.Millisecond {
|
|
t.Fatal("Owning cancellation did not bound handshake")
|
|
}
|
|
listener.Close()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Handshake worker remained")
|
|
}
|
|
t.Logf("CONNECT_CANCEL request_elapsed_ms=%d cleanup_elapsed_ms=%d actuator_output=false", requestElapsed.Milliseconds(), time.Since(started).Milliseconds())
|
|
}
|