Skip to content

test(agent): Improve use of inv.Context() #7696

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 2 commits into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions cli/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func TestWorkspaceAgent(t *testing.T) {
pty := ptytest.New(t).Attach(inv)

clitest.Start(t, inv)
pty.ExpectMatch("starting agent")
ctx := inv.Context()
pty.ExpectMatchContext(ctx, "starting agent")

coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

Expand Down Expand Up @@ -99,8 +100,7 @@ func TestWorkspaceAgent(t *testing.T) {
//nolint:revive,staticcheck
context.WithValue(inv.Context(), "azure-client", metadataClient),
)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
ctx := inv.Context()
clitest.Start(t, inv)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
workspace, err := client.Workspace(ctx, workspace.ID)
Expand All @@ -112,7 +112,7 @@ func TestWorkspaceAgent(t *testing.T) {
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
require.NoError(t, err)
defer dialer.Close()
require.True(t, dialer.AwaitReachable(context.Background()))
require.True(t, dialer.AwaitReachable(ctx))
})

t.Run("AWS", func(t *testing.T) {
Expand Down Expand Up @@ -153,17 +153,18 @@ func TestWorkspaceAgent(t *testing.T) {
context.WithValue(inv.Context(), "aws-client", metadataClient),
)
clitest.Start(t, inv)
ctx := inv.Context()
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
workspace, err := client.Workspace(inv.Context(), workspace.ID)
workspace, err := client.Workspace(ctx, workspace.ID)
require.NoError(t, err)
resources := workspace.LatestBuild.Resources
if assert.NotEmpty(t, resources) && assert.NotEmpty(t, resources[0].Agents) {
assert.NotEmpty(t, resources[0].Agents[0].Version)
}
dialer, err := client.DialWorkspaceAgent(inv.Context(), resources[0].Agents[0].ID, nil)
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
require.NoError(t, err)
defer dialer.Close()
require.True(t, dialer.AwaitReachable(context.Background()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is a second PR fixing context.Background(). I'm curious if there is a lint-way to prevent this dev mistake.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be unfortunate to ban context.Background() entirely since there are valid use-cases. But most of our tests should try to have a timeout so maybe? Ruleguard (scripts/rules.go) would probably be the place to put it, but it's a bit finicky to get right.

In theory, we have a lot of "debt" here:

❯ rg 'context\.Background\(\)' **/*_test.go | grep -E -v 'context\.With(Timeout|Deadline|Value)\(' | wc -l
451

require.True(t, dialer.AwaitReachable(ctx))
})

t.Run("GoogleCloud", func(t *testing.T) {
Expand Down Expand Up @@ -204,7 +205,7 @@ func TestWorkspaceAgent(t *testing.T) {
clitest.Start(t,
inv.WithContext(
//nolint:revive,staticcheck
context.WithValue(context.Background(), "gcp-client", metadataClient),
context.WithValue(inv.Context(), "gcp-client", metadataClient),
),
)

Expand All @@ -220,7 +221,7 @@ func TestWorkspaceAgent(t *testing.T) {
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
require.NoError(t, err)
defer dialer.Close()
require.True(t, dialer.AwaitReachable(context.Background()))
require.True(t, dialer.AwaitReachable(ctx))
sshClient, err := dialer.SSHClient(ctx)
require.NoError(t, err)
defer sshClient.Close()
Expand Down Expand Up @@ -269,7 +270,7 @@ func TestWorkspaceAgent(t *testing.T) {
pty := ptytest.New(t).Attach(inv)

clitest.Start(t, inv)
pty.ExpectMatch("starting agent")
pty.ExpectMatchContext(inv.Context(), "starting agent")

resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
require.Len(t, resources, 1)
Expand Down