Since the runner creates the workspace on it's own, we have to keep listing workspaces until we find it, then wait for the build to finish, then start the agents. It is the caller's responsibility to call the returned function to stop the agents.
(ctx context.Context, t *testing.T, client *codersdk.Client, agentToken string)
| 535 | // finish, then start the agents. It is the caller's responsibility to |
| 536 | // call the returned function to stop the agents. |
| 537 | func goEventuallyStartFakeAgent(ctx context.Context, t *testing.T, client *codersdk.Client, agentToken string) chan io.Closer { |
| 538 | t.Helper() |
| 539 | ch := make(chan io.Closer, 1) // Don't block. |
| 540 | go func() { |
| 541 | defer close(ch) |
| 542 | var workspace codersdk.Workspace |
| 543 | if !assert.Eventually(t, func() bool { |
| 544 | res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{}) |
| 545 | if err != nil { |
| 546 | return false |
| 547 | } |
| 548 | if len(res.Workspaces) == 1 { |
| 549 | workspace = res.Workspaces[0] |
| 550 | return true |
| 551 | } |
| 552 | return false |
| 553 | }, testutil.WaitShort, testutil.IntervalMedium) { |
| 554 | return |
| 555 | } |
| 556 | |
| 557 | coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) |
| 558 | |
| 559 | agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(agentToken)) |
| 560 | agentCloser := agent.New(agent.Options{ |
| 561 | Client: agentClient, |
| 562 | Logger: slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}). |
| 563 | Named("agent").Leveled(slog.LevelWarn), |
| 564 | }) |
| 565 | resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID) |
| 566 | assert.GreaterOrEqual(t, len(resources), 1, "workspace %s has no resources", workspace.ID.String()) |
| 567 | assert.NotEmpty(t, resources[0].Agents, "workspace %s has no agents", workspace.ID.String()) |
| 568 | agentID := resources[0].Agents[0].ID |
| 569 | t.Logf("agent %s is running for workspace %s", agentID.String(), workspace.ID.String()) |
| 570 | ch <- agentCloser |
| 571 | }() |
| 572 | return ch |
| 573 | } |
no test coverage detected