@@ -32,6 +32,7 @@ import (
32
32
"github.com/coder/coder/v2/coderd/tracing"
33
33
"github.com/coder/coder/v2/codersdk"
34
34
"github.com/coder/coder/v2/tailnet"
35
+ "github.com/coder/coder/v2/testutil"
35
36
)
36
37
37
38
// IDs used in tests.
@@ -186,6 +187,42 @@ func StartClientDERPWebSockets(t *testing.T, logger slog.Logger, serverURL *url.
186
187
})
187
188
}
188
189
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
+
189
226
type ClientStarter struct {
190
227
Options * tailnet.Options
191
228
}
0 commit comments