New starts a new agent for use in tests. The agent will use the provided coder URL and session token. The options passed to agent.New() can be modified by passing an optional variadic func(*agent.Options). Returns the agent. Closing the agent is handled by the test cleanup. It is the responsibility
(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent.Options))
| 20 | // It is the responsibility of the caller to call coderdtest.AwaitWorkspaceAgents |
| 21 | // to ensure agent is connected. |
| 22 | func New(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent.Options)) agent.Agent { |
| 23 | t.Helper() |
| 24 | |
| 25 | var o agent.Options |
| 26 | log := testutil.Logger(t).Named("agent") |
| 27 | o.Logger = log |
| 28 | o.SocketPath = testutil.AgentSocketPath(t) |
| 29 | |
| 30 | for _, opt := range opts { |
| 31 | opt(&o) |
| 32 | } |
| 33 | |
| 34 | if o.Client == nil { |
| 35 | agentClient := agentsdk.New(coderURL, agentsdk.WithFixedToken(agentToken)) |
| 36 | agentClient.SDK.SetLogger(log) |
| 37 | o.Client = agentClient |
| 38 | } |
| 39 | |
| 40 | if o.LogDir == "" { |
| 41 | o.LogDir = t.TempDir() |
| 42 | } |
| 43 | |
| 44 | agt := agent.New(o) |
| 45 | t.Cleanup(func() { |
| 46 | assert.NoError(t, agt.Close(), "failed to close agent during cleanup") |
| 47 | }) |
| 48 | |
| 49 | return agt |
| 50 | } |
| 51 | |
| 52 | // WithContextConfigFromEnv returns an agent option that |
| 53 | // populates ContextConfig from the current environment. |