Skip to content

fix: Guard against CLI cmd running after test exit #1658

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 10 commits into from
May 23, 2022
Next Next commit
fix: Guard against CLI cmd running after test exit
  • Loading branch information
mafredri committed May 23, 2022
commit 97c3b066c3ca3ddeec7f754a0975cc1f9972453b
39 changes: 21 additions & 18 deletions cli/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ func TestWorkspaceAgent(t *testing.T) {
cmd, _ := clitest.New(t, "agent", "--auth", "azure-instance-identity", "--agent-url", client.URL.String())
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
errC := make(chan error)
go func() {
// A linting error occurs for weakly typing the context value here,
// but it seems reasonable for a one-off test.
// nolint
ctx = context.WithValue(ctx, "azure-client", metadataClient)
err := cmd.ExecuteContext(ctx)
require.NoError(t, err)
// A linting error occurs for weakly typing the context value here.
//nolint // The above seems reasonable for a one-off test.
ctx := context.WithValue(ctx, "azure-client", metadataClient)
errC <- cmd.ExecuteContext(ctx)
}()
coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
resources, err := client.WorkspaceResourcesByBuild(ctx, workspace.LatestBuild.ID)
Expand All @@ -66,6 +65,8 @@ func TestWorkspaceAgent(t *testing.T) {
_, err = dialer.Ping()
require.NoError(t, err)
cancelFunc()
err = <-errC
require.NoError(t, err)
})

t.Run("AWS", func(t *testing.T) {
Expand Down Expand Up @@ -103,13 +104,12 @@ func TestWorkspaceAgent(t *testing.T) {
cmd, _ := clitest.New(t, "agent", "--auth", "aws-instance-identity", "--agent-url", client.URL.String())
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
errC := make(chan error)
go func() {
// A linting error occurs for weakly typing the context value here,
// but it seems reasonable for a one-off test.
// nolint
ctx = context.WithValue(ctx, "aws-client", metadataClient)
err := cmd.ExecuteContext(ctx)
require.NoError(t, err)
// A linting error occurs for weakly typing the context value here.
//nolint // The above seems reasonable for a one-off test.
ctx := context.WithValue(ctx, "aws-client", metadataClient)
errC <- cmd.ExecuteContext(ctx)
}()
coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
resources, err := client.WorkspaceResourcesByBuild(ctx, workspace.LatestBuild.ID)
Expand All @@ -120,6 +120,8 @@ func TestWorkspaceAgent(t *testing.T) {
_, err = dialer.Ping()
require.NoError(t, err)
cancelFunc()
err = <-errC
require.NoError(t, err)
})

t.Run("GoogleCloud", func(t *testing.T) {
Expand Down Expand Up @@ -157,13 +159,12 @@ func TestWorkspaceAgent(t *testing.T) {
cmd, _ := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
errC := make(chan error)
go func() {
// A linting error occurs for weakly typing the context value here,
// but it seems reasonable for a one-off test.
// nolint
ctx = context.WithValue(ctx, "gcp-client", metadata)
err := cmd.ExecuteContext(ctx)
require.NoError(t, err)
// A linting error occurs for weakly typing the context value here.
//nolint // The above seems reasonable for a one-off test.
ctx := context.WithValue(ctx, "gcp-client", metadata)
errC <- cmd.ExecuteContext(ctx)
}()
coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
resources, err := client.WorkspaceResourcesByBuild(ctx, workspace.LatestBuild.ID)
Expand All @@ -174,5 +175,7 @@ func TestWorkspaceAgent(t *testing.T) {
_, err = dialer.Ping()
require.NoError(t, err)
cancelFunc()
err = <-errC
require.NoError(t, err)
})
}
7 changes: 5 additions & 2 deletions cli/gitssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func TestGitSSH(t *testing.T) {
clitest.SetupConfig(t, agentClient, root)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
errC := make(chan error)
go func() {
err := cmd.ExecuteContext(ctx)
require.NoError(t, err)
errC <- cmd.ExecuteContext(ctx)
}()

coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
Expand Down Expand Up @@ -103,5 +103,8 @@ func TestGitSSH(t *testing.T) {
err = cmd.ExecuteContext(context.Background())
require.NoError(t, err)
require.EqualValues(t, 1, inc)

err = <-errC
require.NoError(t, err)
})
}