Skip to content

Commit 72f2efe

Browse files
authored
chore: implement easy NAT direct integration test (#13169)
1 parent 5e8f97d commit 72f2efe

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tailnet/test/integration/integration.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/coder/coder/v2/coderd/tracing"
3333
"github.com/coder/coder/v2/codersdk"
3434
"github.com/coder/coder/v2/tailnet"
35+
"github.com/coder/coder/v2/testutil"
3536
)
3637

3738
// IDs used in tests.
@@ -186,6 +187,42 @@ func StartClientDERPWebSockets(t *testing.T, logger slog.Logger, serverURL *url.
186187
})
187188
}
188189

190+
// StartClientDirect does the same thing as StartClientDERP but disables
191+
// BlockEndpoints (which enables Direct connections), and waits for a direct
192+
// connection to be established between the two peers.
193+
func StartClientDirect(t *testing.T, logger slog.Logger, serverURL *url.URL, myID, peerID uuid.UUID) *tailnet.Conn {
194+
conn := startClientOptions(t, logger, serverURL, myID, peerID, &tailnet.Options{
195+
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IPFromUUID(myID), 128)},
196+
DERPMap: basicDERPMap(t, serverURL),
197+
BlockEndpoints: false,
198+
Logger: logger,
199+
DERPForceWebSockets: true,
200+
// These tests don't have internet connection, so we need to force
201+
// magicsock to do anything.
202+
ForceNetworkUp: true,
203+
})
204+
205+
// Wait for direct connection to be established.
206+
peerIP := tailnet.IPFromUUID(peerID)
207+
require.Eventually(t, func() bool {
208+
t.Log("attempting ping to peer to judge direct connection")
209+
ctx := testutil.Context(t, testutil.WaitShort)
210+
_, p2p, pong, err := conn.Ping(ctx, peerIP)
211+
if err != nil {
212+
t.Logf("ping failed: %v", err)
213+
return false
214+
}
215+
if !p2p {
216+
t.Log("ping succeeded, but not direct yet")
217+
return false
218+
}
219+
t.Logf("ping succeeded, direct connection established via %s", pong.Endpoint)
220+
return true
221+
}, testutil.WaitLong, testutil.IntervalMedium)
222+
223+
return conn
224+
}
225+
189226
type ClientStarter struct {
190227
Options *tailnet.Options
191228
}

tailnet/test/integration/integration_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ var topologies = []integration.TestTopology{
8686
StartClient: integration.StartClientDERP,
8787
RunTests: integration.TestSuite,
8888
},
89+
{
90+
// Test that direct over "easy" NAT works. This should use local
91+
// endpoints to connect as routing is enabled between client 1 and
92+
// client 2.
93+
Name: "EasyNATDirect",
94+
SetupNetworking: integration.SetupNetworkingEasyNAT,
95+
ServerOptions: integration.ServerOptions{},
96+
StartClient: integration.StartClientDirect,
97+
RunTests: integration.TestSuite,
98+
},
8999
{
90100
// Test that DERP over WebSocket (as well as DERPForceWebSockets works).
91101
// This does not test the actual DERP failure detection code and

0 commit comments

Comments
 (0)