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

Function goEventuallyStartFakeAgent

scaletest/createworkspaces/run_test.go:537–573  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

535// finish, then start the agents. It is the caller's responsibility to
536// call the returned function to stop the agents.
537func 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}

Callers 1

Test_RunnerFunction · 0.85

Calls 11

NewFunction · 0.92
WithFixedTokenFunction · 0.92
NewFunction · 0.92
AwaitWorkspaceAgentsFunction · 0.92
WorkspacesMethod · 0.80
NamedMethod · 0.80
NotEmptyMethod · 0.80
HelperMethod · 0.65
LogfMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected