Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f2d858

Browse files
committed
Remove socat requirement
1 parent fec214d commit 5f2d858

File tree

8 files changed

+35
-31
lines changed

8 files changed

+35
-31
lines changed

.github/workflows/coder.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,6 @@ jobs:
158158
terraform_version: 1.1.2
159159
terraform_wrapper: false
160160

161-
- name: Install socat
162-
if: runner.os == 'Linux'
163-
run: sudo apt-get install -y socat
164-
165-
- name: Install socat
166-
if: runner.os == 'macOS'
167-
run: brew install socat
168-
169161
- name: Test with Mock Database
170162
shell: bash
171163
env:

agent/agent_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net"
88
"os/exec"
9-
"path/filepath"
109
"runtime"
1110
"strconv"
1211
"strings"
@@ -118,8 +117,7 @@ func TestAgent(t *testing.T) {
118117

119118
func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exec.Cmd {
120119
agentConn := setupAgent(t)
121-
socket := filepath.Join(t.TempDir(), "ssh")
122-
listener, err := net.Listen("unix", socket)
120+
listener, err := net.Listen("tcp", "127.0.0.1:0")
123121
require.NoError(t, err)
124122
go func() {
125123
for {
@@ -136,7 +134,12 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
136134
t.Cleanup(func() {
137135
_ = listener.Close()
138136
})
139-
args := append(beforeArgs, "-o", "ProxyCommand socat - UNIX-CLIENT:"+socket, "-o", "StrictHostKeyChecking=no", "host")
137+
tcpAddr, valid := listener.Addr().(*net.TCPAddr)
138+
require.True(t, valid)
139+
args := append(beforeArgs,
140+
"-o", "HostName "+tcpAddr.IP.String(),
141+
"-o", "Port "+strconv.Itoa(tcpAddr.Port),
142+
"-o", "StrictHostKeyChecking=no", "host")
140143
args = append(args, afterArgs...)
141144
return exec.Command("ssh", args...)
142145
}

cli/cliui/prompt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ func promptJSON(reader *bufio.Reader, line string) (string, error) {
128128
continue
129129
}
130130
// Compacting the JSON makes it easier for parsing and testing.
131-
bytes := data.Bytes()
131+
rawJSON := data.Bytes()
132132
data.Reset()
133-
err = json.Compact(&data, bytes)
133+
err = json.Compact(&data, rawJSON)
134134
if err != nil {
135135
return line, xerrors.Errorf("compact json: %w", err)
136136
}

cli/cliui/resources_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"github.com/coder/coder/coderd/database"
99
"github.com/coder/coder/codersdk"
1010
"github.com/coder/coder/pty/ptytest"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestWorkspaceResources(t *testing.T) {
1415
t.Parallel()
1516
t.Run("SingleAgentSSH", func(t *testing.T) {
1617
t.Parallel()
1718
ptty := ptytest.New(t)
18-
cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{
19+
err := cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{
1920
Type: "google_compute_instance",
2021
Name: "dev",
2122
Transition: database.WorkspaceTransitionStart,
@@ -28,14 +29,15 @@ func TestWorkspaceResources(t *testing.T) {
2829
}}, cliui.WorkspaceResourcesOptions{
2930
WorkspaceName: "example",
3031
})
32+
require.NoError(t, err)
3133
ptty.ExpectMatch("coder ssh example")
3234
})
3335

3436
t.Run("MultipleStates", func(t *testing.T) {
3537
t.Parallel()
3638
ptty := ptytest.New(t)
3739
disconnected := database.Now().Add(-4 * time.Second)
38-
cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{
40+
err := cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{
3941
Address: "disk",
4042
Transition: database.WorkspaceTransitionStart,
4143
Type: "google_compute_disk",
@@ -78,6 +80,7 @@ func TestWorkspaceResources(t *testing.T) {
7880
HideAgentState: false,
7981
HideAccess: false,
8082
})
83+
require.NoError(t, err)
8184
ptty.ExpectMatch("google_compute_disk.root")
8285
ptty.ExpectMatch("google_compute_instance.dev")
8386
ptty.ExpectMatch("coder ssh dev.postgres")

cli/configssh.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const sshEndToken = "# ------------END-CODER------------"
3131

3232
func configSSH() *cobra.Command {
3333
var (
34-
sshConfigFile string
35-
sshOptions []string
34+
sshConfigFile string
35+
sshOptions []string
36+
skipProxyCommand bool
3637
)
3738
cmd := &cobra.Command{
3839
Use: "config-ssh",
@@ -90,16 +91,18 @@ func configSSH() *cobra.Command {
9091
}
9192
configOptions := []string{
9293
"Host coder." + hostname,
93-
"\tHostName coder." + hostname,
9494
}
9595
for _, option := range sshOptions {
9696
configOptions = append(configOptions, "\t"+option)
9797
}
9898
configOptions = append(configOptions,
99-
fmt.Sprintf("\tProxyCommand %q --global-config %q ssh --stdio %s", binaryFile, root, hostname),
99+
"\tHostName coder."+hostname,
100100
"\tConnectTimeout=0",
101101
"\tStrictHostKeyChecking=no",
102102
)
103+
if !skipProxyCommand {
104+
configOptions = append(configOptions, fmt.Sprintf("\tProxyCommand %q --global-config %q ssh --stdio %s", binaryFile, root, hostname))
105+
}
103106
sshConfigContent += strings.Join(configOptions, "\n") + "\n"
104107
sshConfigContentMutex.Unlock()
105108
}
@@ -128,6 +131,8 @@ func configSSH() *cobra.Command {
128131
}
129132
cliflag.StringVarP(cmd.Flags(), &sshConfigFile, "ssh-config-file", "", "CODER_SSH_CONFIG_FILE", "~/.ssh/config", "Specifies the path to an SSH config.")
130133
cmd.Flags().StringArrayVarP(&sshOptions, "ssh-option", "o", []string{}, "Specifies additional SSH options to embed in each host stanza.")
134+
cmd.Flags().BoolVarP(&skipProxyCommand, "skip-proxy-command", "", false, "Specifies whether the ProxyCommand option should be skipped. Useful for testing.")
135+
_ = cmd.Flags().MarkHidden("skip-proxy-command")
131136

132137
return cmd
133138
}

cli/configssh_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"os"
88
"os/exec"
9+
"strconv"
910
"strings"
1011
"testing"
1112

@@ -26,10 +27,6 @@ import (
2627

2728
func TestConfigSSH(t *testing.T) {
2829
t.Parallel()
29-
_, err := exec.LookPath("socat")
30-
if err != nil {
31-
t.Skip("You must have socat installed to run this test!")
32-
}
3330

3431
client := coderdtest.New(t, nil)
3532
user := coderdtest.CreateFirstUser(t, client)
@@ -89,10 +86,6 @@ func TestConfigSSH(t *testing.T) {
8986
require.NoError(t, err)
9087
defer agentConn.Close()
9188

92-
// Using socat we can force SSH to use a TCP port
93-
// created in this test. That way we still validate
94-
// our configuration, but use the native SSH command
95-
// line to interface.
9689
listener, err := net.Listen("tcp", "127.0.0.1:0")
9790
require.NoError(t, err)
9891
t.Cleanup(func() {
@@ -114,7 +107,13 @@ func TestConfigSSH(t *testing.T) {
114107
_ = listener.Close()
115108
})
116109

117-
cmd, root := clitest.New(t, "config-ssh", "--ssh-option", "ProxyCommand socat - TCP4:"+listener.Addr().String(), "--ssh-config-file", tempFile.Name())
110+
tcpAddr, valid := listener.Addr().(*net.TCPAddr)
111+
require.True(t, valid)
112+
cmd, root := clitest.New(t, "config-ssh",
113+
"--ssh-option", "HostName "+tcpAddr.IP.String(),
114+
"--ssh-option", "Port "+strconv.Itoa(tcpAddr.Port),
115+
"--ssh-config-file", tempFile.Name(),
116+
"--skip-proxy-command")
118117
clitest.SetupConfig(t, client, root)
119118
doneChan := make(chan struct{})
120119
pty := ptytest.New(t)

cli/workspaceautostart_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"context"
66
"testing"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/coder/coder/cli/clitest"
911
"github.com/coder/coder/coderd/coderdtest"
1012
"github.com/coder/coder/codersdk"
11-
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestWorkspaceAutostart(t *testing.T) {

cli/workspaceautostop_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"context"
66
"testing"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/coder/coder/cli/clitest"
911
"github.com/coder/coder/coderd/coderdtest"
1012
"github.com/coder/coder/codersdk"
11-
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestWorkspaceAutostop(t *testing.T) {

0 commit comments

Comments
 (0)