TestWorkspaceAgent_UpdatedDERP runs a real coderd server, with a real agent and a real client, and updates the DERP map live to ensure connections still work.
(t *testing.T)
| 2760 | // and a real client, and updates the DERP map live to ensure connections still |
| 2761 | // work. |
| 2762 | func TestWorkspaceAgent_UpdatedDERP(t *testing.T) { |
| 2763 | t.Parallel() |
| 2764 | |
| 2765 | logger := testutil.Logger(t) |
| 2766 | |
| 2767 | dv := coderdtest.DeploymentValues(t) |
| 2768 | err := dv.DERP.Config.BlockDirect.Set("true") |
| 2769 | require.NoError(t, err) |
| 2770 | |
| 2771 | client, closer, api := coderdtest.NewWithAPI(t, &coderdtest.Options{ |
| 2772 | DeploymentValues: dv, |
| 2773 | }) |
| 2774 | defer closer.Close() |
| 2775 | user := coderdtest.CreateFirstUser(t, client) |
| 2776 | |
| 2777 | // Change the DERP mapper to our custom one. |
| 2778 | var currentDerpMap atomic.Pointer[tailcfg.DERPMap] |
| 2779 | originalDerpMap, _ := tailnettest.RunDERPAndSTUN(t) |
| 2780 | currentDerpMap.Store(originalDerpMap) |
| 2781 | derpMapFn := func(_ *tailcfg.DERPMap) *tailcfg.DERPMap { |
| 2782 | return currentDerpMap.Load().Clone() |
| 2783 | } |
| 2784 | api.DERPMapper.Store(&derpMapFn) |
| 2785 | |
| 2786 | // Start workspace a workspace agent. |
| 2787 | r := dbfake.WorkspaceBuild(t, api.Database, database.WorkspaceTable{ |
| 2788 | OrganizationID: user.OrganizationID, |
| 2789 | OwnerID: user.UserID, |
| 2790 | }).WithAgent().Do() |
| 2791 | |
| 2792 | agentCloser := agenttest.New(t, client.URL, r.AgentToken) |
| 2793 | resources := coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID) |
| 2794 | agentID := resources[0].Agents[0].ID |
| 2795 | |
| 2796 | // Connect from a client. |
| 2797 | conn1, err := func() (workspacesdk.AgentConn, error) { |
| 2798 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 2799 | defer cancel() // Connection should remain open even if the dial context is canceled. |
| 2800 | |
| 2801 | return workspacesdk.New(client). |
| 2802 | DialAgent(ctx, agentID, &workspacesdk.DialAgentOptions{ |
| 2803 | Logger: logger.Named("client1"), |
| 2804 | }) |
| 2805 | }() |
| 2806 | require.NoError(t, err) |
| 2807 | defer conn1.Close() |
| 2808 | |
| 2809 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 2810 | defer cancel() |
| 2811 | |
| 2812 | ok := conn1.AwaitReachable(ctx) |
| 2813 | require.True(t, ok) |
| 2814 | |
| 2815 | // Change the DERP map and change the region ID. |
| 2816 | newDerpMap, _ := tailnettest.RunDERPAndSTUN(t) |
| 2817 | require.NotNil(t, newDerpMap) |
| 2818 | newDerpMap.Regions[2] = newDerpMap.Regions[1] |
| 2819 | delete(newDerpMap.Regions, 1) |
nothing calls this directly
no test coverage detected