8000 feat(cli): use coder connect in `coder ssh --stdio`, if available by ethanndickson · Pull Request #17572 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content
/ coder Public

feat(cli): use coder connect in coder ssh --stdio, if available #17572

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
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename flag, extra test
  • Loading branch information
ethanndickson committed Apr 29, 2025
commit 578ebb0b744d5db9cde72101412a4686dfcd931a
4 changes: 2 additions & 2 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ func (r *RootCmd) ssh() *serpent.Command {
Hidden: true, // Hidden until this features is at least in beta.
},
{
Flag: "force-tunnel",
Description: "Force the use of a new tunnel to the workspace, even if the Coder Connect tunnel is available.",
Flag: "force-new-tunnel",
Description: "Force the creation of a new tunnel to the workspace, even if the Coder Connect tunnel is available.",
Value: serpent.BoolOf(&forceTunnel),
},
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)),
Expand Down
88 changes: 59 additions & 29 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,41 +2114,71 @@ func TestSSH_Container(t *testing.T) {
func TestSSH_CoderConnect(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()
t.Run("Enabled", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

fs := afero.NewMemMapFs()
//nolint:revive,staticcheck
ctx = context.WithValue(ctx, "fs", fs)
fs := afero.NewMemMapFs()
//nolint:revive,staticcheck
ctx = context.WithValue(ctx, "fs", fs)

client, workspace, agentToken := setupWorkspaceForAgent(t)
inv, root := clitest.New(t, "ssh", workspace.Name, "--network-info-dir", "/net", "--stdio")
clitest.SetupConfig(t, client, root)
_ = ptytest.New(t).Attach(inv)
client, workspace, agentToken := setupWorkspaceForAgent(t)
inv, root := clitest.New(t, "ssh", workspace.Name, "--network-info-dir", "/net", "--stdio")
clitest.SetupConfig(t, client, root)
_ = ptytest.New(t).Attach(inv)

errCh := make(chan error, 1)
tGo(t, func() {
err := inv.WithContext(withCoderConnectRunning(ctx)).Run()
errCh <- err
errCh := make(chan error, 1)
tGo(t, func() {
err := inv.WithContext(withCoderConnectRunning(ctx)).Run()
errCh <- err
})

_ = agenttest.New(t, client.URL, agentToken)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

err := testutil.TryReceive(ctx, t, errCh)
// Making an SSH server available here is difficult, so we'll just check
// the command attempts to dial it.
require.ErrorContains(t, err, "dial coder connect host")
require.ErrorContains(t, err, "dev.myworkspace.myuser.coder")

// The network info file should be created since we passed `--stdio`
assert.Eventually(t, func() bool {
entries, err := afero.ReadDir(fs, "/net")
if err != nil {
return false
}
return len(entries) > 0
}, testutil.WaitLong, testutil.IntervalFast)
})

_ = agenttest.New(t, client.URL, agentToken)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
t.Run("Disabled", func(t *testing.T) {
t.Parallel()

err := testutil.TryReceive(ctx, t, errCh)
// Making an SSH server available here is difficult, so we'll just check
// the command attempts to dial it.
require.ErrorContains(t, err, "dial coder connect host")
require.ErrorContains(t, err, "dev.myworkspace.myuser.coder")

// The network info file should be created since we passed `--stdio`
assert.Eventually(t, func() bool {
entries, err := afero.ReadDir(fs, "/net")
if err != nil {
return false
}
return len(entries) > 0
}, testutil.WaitLong, testutil.IntervalFast)
client, workspace, agentToken := setupWorkspaceForAgent(t)
inv, root := clitest.New(t, "ssh", workspace.Name, "--force-new-tunnel")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

cmdDone := tGo(t, func() {
err := inv.WithContext(withCoderConnectRunning(ctx)).Run()
assert.NoError(t, err)
})
// Shouldn't fail to dial the coder connect host `--force-new-tunnel`
// is passed.
pty.ExpectMatch("Waiting")

_ = agenttest.New(t, client.URL, agentToken)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

// Shells on Mac, Windows, and Linux all exit shells with the "exit" command.
pty.WriteLine("exit")
<-cmdDone
})
}

// tGoContext runs fn in a goroutine passing a context that will be
Expand Down
4 changes: 2 additions & 2 deletions cli/testdata/coder_ssh_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ OPTIONS:
-e, --env string-array, $CODER_SSH_ENV
Set environment variable(s) for session (key1=value1,key2=value2,...).

--force-tunnel bool
Force the use of a new tunnel to the workspace, even if the Coder
--force-new-tunnel bool
Force the creation of a new tunnel to the workspace, even if the Coder
Connect tunnel is available.

-A, --forward-agent bool, $CODER_SSH_FORWARD_AGENT
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/cli/ssh.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0