Skip to content

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 11 commits into from
Sep 26, 2023
Next Next commit
fix(cli): call agent directly in gitssh tests
  • Loading branch information
johnstcn committed Sep 21, 2023
commit af64f27e888b523dfb5cf42f050592d4eb8d6a33
31 changes: 23 additions & 8 deletions cli/gitssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"sync/atomic"
"testing"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"

"github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/stretchr/testify/assert"

"github.com/gliderlabs/ssh"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand All @@ -28,7 +35,7 @@ import (
"github.com/coder/coder/v2/testutil"
)

func prepareTestGitSSH(ctx context.Context, t *testing.T) (*codersdk.Client, string, gossh.PublicKey) {
func prepareTestGitSSH(ctx context.Context, t *testing.T) (*agentsdk.Client, string, gossh.PublicKey) {
t.Helper()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
Expand Down Expand Up @@ -57,12 +64,20 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*codersdk.Client, str
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

// start workspace agent
inv, root := clitest.New(t, "agent", "--agent-token", agentToken, "--agent-url", client.URL.String())
agentClient := codersdk.New(client.URL)
agentLog := slogtest.Make(t, nil).Leveled(slog.LevelDebug).Named("agent")
agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(agentToken)
clitest.SetupConfig(t, agentClient, root)
clitest.Start(t, inv)

agt := agent.New(agent.Options{
Client: agentClient,
Logger: agentLog,
LogDir: t.TempDir(),
ExchangeToken: func(_ context.Context) (string, error) {
return agentToken, nil
},
})
t.Cleanup(func() {
assert.NoError(t, agt.Close(), "failed to close agent in cleanup")
})
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
return agentClient, agentToken, pubkey
}
Expand Down Expand Up @@ -140,7 +155,7 @@ func TestGitSSH(t *testing.T) {
// set to agent config dir
inv, _ := clitest.New(t,
"gitssh",
"--agent-url", client.URL.String(),
"--agent-url", client.SDK.URL.String(),
"--agent-token", token,
"--",
fmt.Sprintf("-p%d", addr.Port),
Expand Down Expand Up @@ -203,7 +218,7 @@ func TestGitSSH(t *testing.T) {
pty := ptytest.New(t)
cmdArgs := []string{
"gitssh",
"--agent-url", client.URL.String(),
"--agent-url", client.SDK.URL.String(),
"--agent-token", token,
"--",
"-F", config,
Expand Down