TestConn_UpdateDERP tests that when update the DERP map we pick a new preferred DERP server and new connections can be made from clients.
(t *testing.T)
| 280 | // TestConn_UpdateDERP tests that when update the DERP map we pick a new |
| 281 | // preferred DERP server and new connections can be made from clients. |
| 282 | func TestConn_UpdateDERP(t *testing.T) { |
| 283 | t.Parallel() |
| 284 | logger := testutil.Logger(t) |
| 285 | |
| 286 | derpMap1, _ := tailnettest.RunDERPAndSTUN(t) |
| 287 | ip := tailnet.TailscaleServicePrefix.RandomAddr() |
| 288 | conn, err := tailnet.NewConn(&tailnet.Options{ |
| 289 | Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)}, |
| 290 | Logger: logger.Named("w1"), |
| 291 | DERPMap: derpMap1, |
| 292 | BlockEndpoints: true, |
| 293 | }) |
| 294 | require.NoError(t, err) |
| 295 | t.Cleanup(func() { |
| 296 | err := conn.Close() |
| 297 | assert.NoError(t, err) |
| 298 | }) |
| 299 | |
| 300 | // Buffer channel so callback doesn't block |
| 301 | nodes := make(chan *tailnet.Node, 50) |
| 302 | conn.SetNodeCallback(func(node *tailnet.Node) { |
| 303 | nodes <- node |
| 304 | }) |
| 305 | |
| 306 | ctx1, cancel1 := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 307 | t.Cleanup(cancel1) |
| 308 | select { |
| 309 | case node := <-nodes: |
| 310 | require.Equal(t, 1, node.PreferredDERP) |
| 311 | case <-ctx1.Done(): |
| 312 | t.Fatal("timed out waiting for node") |
| 313 | } |
| 314 | |
| 315 | // Connect from a different client. |
| 316 | client1, err := tailnet.NewConn(&tailnet.Options{ |
| 317 | Addresses: []netip.Prefix{tailnet.TailscaleServicePrefix.RandomPrefix()}, |
| 318 | Logger: logger.Named("client1"), |
| 319 | DERPMap: derpMap1, |
| 320 | BlockEndpoints: true, |
| 321 | }) |
| 322 | require.NoError(t, err) |
| 323 | t.Cleanup(func() { |
| 324 | err := client1.Close() |
| 325 | assert.NoError(t, err) |
| 326 | }) |
| 327 | stitch(t, conn, client1) |
| 328 | pn, err := tailnet.NodeToProto(conn.Node()) |
| 329 | require.NoError(t, err) |
| 330 | connID := uuid.New() |
| 331 | err = client1.UpdatePeers([]*proto.CoordinateResponse_PeerUpdate{{ |
| 332 | Id: connID[:], |
| 333 | Node: pn, |
| 334 | Kind: proto.CoordinateResponse_PeerUpdate_NODE, |
| 335 | }}) |
| 336 | require.NoError(t, err) |
| 337 | |
| 338 | awaitReachableCtx1, awaitReachableCancel1 := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 339 | t.Cleanup(awaitReachableCancel1) |
nothing calls this directly
no test coverage detected