mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
Refactors our use of `slogtest` to instantiate a "standard logger" across most of our tests. This standard logger incorporates https://github.com/coder/slog/pull/217 to also ignore database query canceled errors by default, which are a source of low-severity flakes. Any test that has set non-default `slogtest.Options` is left alone. In particular, `coderdtest` defaults to ignoring all errors. We might consider revisiting that decision now that we have better tools to target the really common flaky Error logs on shutdown.
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package agenttest
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/coder/coder/v2/agent"
|
|
"github.com/coder/coder/v2/codersdk/agentsdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
// 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 of the caller to call coderdtest.AwaitWorkspaceAgents
|
|
// to ensure agent is connected.
|
|
func New(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent.Options)) agent.Agent {
|
|
t.Helper()
|
|
|
|
var o agent.Options
|
|
log := testutil.Logger(t).Named("agent")
|
|
o.Logger = log
|
|
|
|
for _, opt := range opts {
|
|
opt(&o)
|
|
}
|
|
|
|
if o.Client == nil {
|
|
agentClient := agentsdk.New(coderURL)
|
|
agentClient.SetSessionToken(agentToken)
|
|
agentClient.SDK.SetLogger(log)
|
|
o.Client = agentClient
|
|
}
|
|
|
|
if o.ExchangeToken == nil {
|
|
o.ExchangeToken = func(_ context.Context) (string, error) {
|
|
return agentToken, nil
|
|
}
|
|
}
|
|
|
|
if o.LogDir == "" {
|
|
o.LogDir = t.TempDir()
|
|
}
|
|
|
|
agt := agent.New(o)
|
|
t.Cleanup(func() {
|
|
assert.NoError(t, agt.Close(), "failed to close agent during cleanup")
|
|
})
|
|
|
|
return agt
|
|
}
|