(t *testing.T)
| 171 | } |
| 172 | |
| 173 | func TestWorkspaceAgentRPCRole(t *testing.T) { |
| 174 | t.Parallel() |
| 175 | |
| 176 | t.Run("AgentRoleMonitorsConnection", func(t *testing.T) { |
| 177 | t.Parallel() |
| 178 | ctx := testutil.Context(t, testutil.WaitLong) |
| 179 | client, db := coderdtest.NewWithDatabase(t, nil) |
| 180 | user := coderdtest.CreateFirstUser(t, client) |
| 181 | r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ |
| 182 | OrganizationID: user.OrganizationID, |
| 183 | OwnerID: user.UserID, |
| 184 | }).WithAgent().Do() |
| 185 | |
| 186 | // Connect with role=agent using ConnectRPCWithRole. This is |
| 187 | // how the real workspace agent connects. |
| 188 | ac := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken)) |
| 189 | conn, err := ac.ConnectRPCWithRole(ctx, "agent") |
| 190 | require.NoError(t, err) |
| 191 | defer func() { |
| 192 | _ = conn.Close() |
| 193 | }() |
| 194 | |
| 195 | // The connection monitor updates the database asynchronously, |
| 196 | // so we need to wait for first_connected_at to be set. |
| 197 | var agent database.WorkspaceAgent |
| 198 | require.Eventually(t, func() bool { |
| 199 | agent, err = db.GetWorkspaceAgentByID(dbauthz.AsSystemRestricted(ctx), r.Agents[0].ID) |
| 200 | if err != nil { |
| 201 | return false |
| 202 | } |
| 203 | return agent.FirstConnectedAt.Valid |
| 204 | }, testutil.WaitShort, testutil.IntervalFast) |
| 205 | assert.True(t, agent.LastConnectedAt.Valid, |
| 206 | "last_connected_at should be set for agent role") |
| 207 | }) |
| 208 | |
| 209 | t.Run("NonAgentRoleSkipsMonitoring", func(t *testing.T) { |
| 210 | t.Parallel() |
| 211 | ctx := testutil.Context(t, testutil.WaitLong) |
| 212 | client, db := coderdtest.NewWithDatabase(t, nil) |
| 213 | user := coderdtest.CreateFirstUser(t, client) |
| 214 | r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ |
| 215 | OrganizationID: user.OrganizationID, |
| 216 | OwnerID: user.UserID, |
| 217 | }).WithAgent().Do() |
| 218 | |
| 219 | // Connect with a non-agent role using ConnectRPCWithRole. |
| 220 | // This is how coder-logstream-kube should connect. |
| 221 | ac := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken)) |
| 222 | conn, err := ac.ConnectRPCWithRole(ctx, "logstream-kube") |
| 223 | require.NoError(t, err) |
| 224 | |
| 225 | // Send a log to confirm the RPC connection is functional. |
| 226 | agentAPI := agentproto.NewDRPCAgentClient(conn) |
| 227 | _, err = agentAPI.BatchCreateLogs(ctx, &agentproto.BatchCreateLogsRequest{ |
| 228 | LogSourceId: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 229 | }) |
| 230 | // We don't care about the log source error, just that the |
nothing calls this directly
no test coverage detected