-
Notifications
You must be signed in to change notification settings - Fork 894
refactor(agent): add agenttest.New helper function #9812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
af64f27
fix(cli): call agent directly in gitssh tests
johnstcn d99e29c
add agenttest.New test helper
johnstcn 3d2788e
return wrapper instead of func
johnstcn 5b4cad9
refactor existing unit tests
johnstcn 81a3a78
ignore duplicated code in unit tests
johnstcn 3316b99
address code review comments
johnstcn c2cba9d
fixup! address code review comments
johnstcn 6aa4bbe
Merge remote-tracking branch 'origin/main' into cj/agenttest
johnstcn d55c797
address PR comments
johnstcn 903fe3e
skip flaky test
johnstcn 9f3f6ff
Merge remote-tracking branch 'origin/main' into cj/agenttest
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add agenttest.New test helper
- Loading branch information
commit d99e29c5157facbda4a202288cdbf6c625b8606f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package agenttest | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/coder/coder/v2/agent" | ||
"github.com/coder/coder/v2/coderd/coderdtest" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
) | ||
|
||
// Options are options for creating a new test agent. | ||
type Options struct { | ||
// AgentOptions are the options to use for the agent. | ||
AgentOptions agent.Options | ||
|
||
// AgentToken is the token to use for the agent. | ||
AgentToken string | ||
// URL is the URL to which the agent should connect. | ||
URL *url.URL | ||
// WorkspaceID is the ID of the workspace to which the agent should connect. | ||
WorkspaceID uuid.UUID | ||
// Logger is the logger to use for the agent. | ||
// Defaults to a new test logger if not specified. | ||
Logger *slog.Logger | ||
} | ||
|
||
// OptFunc is a function that modifies the given options. | ||
type OptFunc func(*Options) | ||
|
||
func WithAgentToken(token string) OptFunc { | ||
return func(o *Options) { | ||
o.AgentToken = token | ||
} | ||
} | ||
|
||
func WithURL(u *url.URL) OptFunc { | ||
return func(o *Options) { | ||
o.URL = u | ||
} | ||
} | ||
|
||
func WithWorkspaceID(id uuid.UUID) OptFunc { | ||
return func(o *Options) { | ||
o.WorkspaceID = id | ||
} | ||
} | ||
|
||
// New starts a new agent for use in tests. | ||
// Returns the agent client and a function that will block until the agent is | ||
// connected to the workspace. | ||
// Closing the agent is handled by the test cleanup. | ||
func New(t testing.TB, opts ...OptFunc) (agentClient *agentsdk.Client, awaitAgent func(*codersdk.Client)) { | ||
t.Helper() | ||
|
||
var o Options | ||
for _, opt := range opts { | ||
opt(&o) | ||
} | ||
|
||
if o.URL == nil { | ||
require.Fail(t, "must specify URL for agent") | ||
} | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
agentClient = agentsdk.New(o.URL) | ||
|
||
if o.AgentToken == "" { | ||
o.AgentToken = uuid.NewString() | ||
} | ||
agentClient.SetSessionToken(o.AgentToken) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if o.AgentOptions.Client == nil { | ||
o.AgentOptions.Client = agentClient | ||
} | ||
|
||
if o.AgentOptions.ExchangeToken == nil { | ||
o.AgentOptions.ExchangeToken = func(_ context.Context) (string, error) { | ||
return o.AgentToken, nil | ||
} | ||
} | ||
|
||
if o.AgentOptions.LogDir == "" { | ||
o.AgentOptions.LogDir = t.TempDir() | ||
} | ||
|
||
if o.Logger == nil { | ||
log := slogtest.Make(t, nil).Leveled(slog.LevelDebug).Named("agent") | ||
o.Logger = &log | ||
} | ||
|
||
o.AgentOptions.Logger = *o.Logger | ||
|
||
agentCloser := agent.New(o.AgentOptions) | ||
t.Cleanup(func() { | ||
assert.NoError(t, agentCloser.Close(), "failed to close agent during cleanup") | ||
}) | ||
|
||
awaitAgent = func(c *codersdk.Client) { | ||
if o.WorkspaceID == uuid.Nil { | ||
require.FailNow(t, "must specify workspace ID for agent in order to wait") | ||
} | ||
coderdtest.AwaitWorkspaceAgents(t, c, o.WorkspaceID) | ||
} | ||
|
||
return agentClient, awaitAgent | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.