[client] Make Test_ConnectPeers handshake over loopback

The previous busy-loop fix did not stop the flake: the test still timed
out after 30s waiting for the peer handshake. Root cause is the endpoint
address. The peers pointed at each other via the host's routable NIC IP
(getLocalIP), so the handshake had to hairpin through the host/container
network stack. Under CI load that drops packets, and WireGuard only
retries a lost handshake initiation every REKEY_TIMEOUT (5s), stacking up
to the ~30s timeout.

Point both endpoints at 127.0.0.1 instead. The userspace WireGuard bind
listens on 0.0.0.0, so loopback reaches it over a path that never drops.
Remove the now-unused getLocalIP helper.
This commit is contained in:
riccardom
2026-07-23 18:27:20 +02:00
parent 3358138ccc
commit 34d640dfc9

View File

@@ -505,10 +505,12 @@ func Test_ConnectPeers(t *testing.T) {
t.Fatal(err)
}
localIP, err := getLocalIP()
if err != nil {
t.Fatal(err)
}
// Peers talk to each other over loopback. The userspace WireGuard bind
// listens on 0.0.0.0, so 127.0.0.1 reaches it. Using the routable NIC IP
// instead makes the handshake hairpin through the host/container network
// stack, which drops packets under CI load and leaves WireGuard retrying
// only every REKEY_TIMEOUT (5s) — the root of the 30s flake.
localIP := "127.0.0.1"
peer1endpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", localIP, peer1wgPort))
if err != nil {
@@ -622,27 +624,3 @@ func getPeer(ifaceName, peerPubKey string) (wgtypes.Peer, error) {
return wgtypes.Peer{}, fmt.Errorf("peer not found")
}
func getLocalIP() (string, error) {
// Get all interfaces
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ipNet.IP.IsLoopback() {
continue
}
if ipNet.IP.To4() == nil {
continue
}
return ipNet.IP.String(), nil
}
return "", fmt.Errorf("no local IP found")
}