coordinationTest tests that a coordination behaves correctly
( ctx context.Context, t *testing.T, uut tailnet.CloserWaiter, fConn *fakeCoordinatee, reqs chan *proto.CoordinateRequest, resps chan *proto.CoordinateResponse, agentID uuid.UUID, )
| 487 | |
| 488 | // coordinationTest tests that a coordination behaves correctly |
| 489 | func coordinationTest( |
| 490 | ctx context.Context, t *testing.T, |
| 491 | uut tailnet.CloserWaiter, fConn *fakeCoordinatee, |
| 492 | reqs chan *proto.CoordinateRequest, resps chan *proto.CoordinateResponse, |
| 493 | agentID uuid.UUID, |
| 494 | ) { |
| 495 | // It should add the tunnel, since we configured as a client |
| 496 | req := testutil.TryReceive(ctx, t, reqs) |
| 497 | require.Equal(t, agentID[:], req.GetAddTunnel().GetId()) |
| 498 | |
| 499 | // when we call the callback, it should send a node update |
| 500 | require.NotNil(t, fConn.callback) |
| 501 | fConn.callback(&tailnet.Node{PreferredDERP: 1}) |
| 502 | |
| 503 | req = testutil.TryReceive(ctx, t, reqs) |
| 504 | require.Equal(t, int32(1), req.GetUpdateSelf().GetNode().GetPreferredDerp()) |
| 505 | |
| 506 | // When we send a peer update, it should update the coordinatee |
| 507 | nk, err := key.NewNode().Public().MarshalBinary() |
| 508 | require.NoError(t, err) |
| 509 | dk, err := key.NewDisco().Public().MarshalText() |
| 510 | require.NoError(t, err) |
| 511 | updates := []*proto.CoordinateResponse_PeerUpdate{ |
| 512 | { |
| 513 | Id: agentID[:], |
| 514 | Kind: proto.CoordinateResponse_PeerUpdate_NODE, |
| 515 | Node: &proto.Node{ |
| 516 | Id: 2, |
| 517 | Key: nk, |
| 518 | Disco: string(dk), |
| 519 | }, |
| 520 | }, |
| 521 | } |
| 522 | testutil.RequireSend(ctx, t, resps, &proto.CoordinateResponse{PeerUpdates: updates}) |
| 523 | require.Eventually(t, func() bool { |
| 524 | fConn.Lock() |
| 525 | defer fConn.Unlock() |
| 526 | return len(fConn.updates) > 0 |
| 527 | }, testutil.WaitShort, testutil.IntervalFast) |
| 528 | require.Len(t, fConn.updates[0], 1) |
| 529 | require.Equal(t, agentID[:], fConn.updates[0][0].Id) |
| 530 | |
| 531 | errCh := make(chan error, 1) |
| 532 | go func() { |
| 533 | errCh <- uut.Close(ctx) |
| 534 | }() |
| 535 | |
| 536 | // When we close, it should gracefully disconnect |
| 537 | req = testutil.TryReceive(ctx, t, reqs) |
| 538 | require.NotNil(t, req.Disconnect) |
| 539 | close(resps) |
| 540 | |
| 541 | err = testutil.TryReceive(ctx, t, errCh) |
| 542 | require.NoError(t, err) |
| 543 | |
| 544 | // It should set all peers lost on the coordinatee |
| 545 | require.Equal(t, 1, fConn.setAllPeersLostCalls) |
| 546 | } |
no test coverage detected