MCPcopy Index your code
hub / github.com/coder/coder / TestWorkspaceAgentRPCRole

Function TestWorkspaceAgentRPCRole

coderd/workspaceagentsrpc_test.go:173–252  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

171}
172
173func 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

Callers

nothing calls this directly

Calls 14

BatchCreateLogsMethod · 0.95
ContextFunction · 0.92
NewWithDatabaseFunction · 0.92
CreateFirstUserFunction · 0.92
WorkspaceBuildFunction · 0.92
NewFunction · 0.92
WithFixedTokenFunction · 0.92
AsSystemRestrictedFunction · 0.92
WithAgentMethod · 0.80
ConnectRPCWithRoleMethod · 0.80
RunMethod · 0.65
DoMethod · 0.65

Tested by

no test coverage detected