From 940732e91c9ce110ba331dcc379b28aedb157ef7 Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Sat, 14 Mar 2026 15:04:50 +1000 Subject: [PATCH] fix: sshcli unit test and lint --- internal/sshexec/sshcli_test.go | 106 ++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 39 deletions(-) diff --git a/internal/sshexec/sshcli_test.go b/internal/sshexec/sshcli_test.go index 073a90c0..de535a8e 100644 --- a/internal/sshexec/sshcli_test.go +++ b/internal/sshexec/sshcli_test.go @@ -1,12 +1,13 @@ package sshexec import ( + "context" "testing" "github.com/stretchr/testify/assert" ) -func TestSSHCLIRemote_buildSSHArgs(t *testing.T) { +func TestSSHCLIRemote_newSSHCommand(t *testing.T) { t.Parallel() tests := []struct { @@ -15,59 +16,85 @@ func TestSSHCLIRemote_buildSSHArgs(t *testing.T) { host string port int keyPath string + cmd string expected []string }{ { - name: "host only", - host: "example.com", - expected: []string{"-o", "ConnectTimeout=5", "example.com"}, + name: "host only", + host: "example.com", + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "example.com", "whoami", + }, }, { - name: "with user", - user: "root", - host: "example.com", - expected: []string{"-o", "ConnectTimeout=5", "root@example.com"}, + name: "with user", + user: "root", + host: "example.com", + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "root@example.com", "whoami", + }, }, { - name: "with port", - host: "example.com", - port: 2222, - expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "example.com"}, + name: "with port", + host: "example.com", + port: 2222, + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-p", "2222", "example.com", "whoami", + }, }, { - name: "with key", - host: "example.com", - keyPath: "/path/to/key", - expected: []string{"-o", "ConnectTimeout=5", "-i", "/path/to/key", "example.com"}, + name: "with key", + host: "example.com", + keyPath: "/path/to/key", + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-i", "/path/to/key", "example.com", "whoami", + }, }, { - name: "user and port", - user: "ubuntu", - host: "192.168.1.10", - port: 2222, - expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "ubuntu@192.168.1.10"}, + name: "user and port", + user: "ubuntu", + host: "192.168.1.10", + port: 2222, + cmd: "ls -la", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-p", "2222", "ubuntu@192.168.1.10", "ls -la", + }, }, { - name: "user and key", - user: "root", - host: "example.com", - keyPath: "/path/to/key", - expected: []string{"-o", "ConnectTimeout=5", "-i", "/path/to/key", "root@example.com"}, + name: "user and key", + user: "root", + host: "example.com", + keyPath: "/path/to/key", + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-i", "/path/to/key", "root@example.com", "whoami", + }, }, { - name: "port and key", - host: "example.com", - port: 22, - keyPath: "~/.ssh/id_rsa", - expected: []string{"-o", "ConnectTimeout=5", "-p", "22", "-i", "~/.ssh/id_rsa", "example.com"}, + name: "port and key", + host: "example.com", + port: 22, + keyPath: "~/.ssh/id_rsa", + cmd: "whoami", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-p", "22", "-i", "~/.ssh/id_rsa", "example.com", "whoami", + }, }, { - name: "all options", - user: "admin", - host: "server.local", - port: 2222, - keyPath: "~/.ssh/id_rsa", - expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "-i", "~/.ssh/id_rsa", "admin@server.local"}, + name: "all options", + user: "admin", + host: "server.local", + port: 2222, + keyPath: "~/.ssh/id_rsa", + cmd: "sudo bash -c 'echo hello'", + expected: []string{ + "ssh", "-o", "ConnectTimeout=5", "-T", "-p", "2222", "-i", "~/.ssh/id_rsa", + "admin@server.local", "sudo bash -c 'echo hello'", + }, }, } @@ -81,8 +108,9 @@ func TestSSHCLIRemote_buildSSHArgs(t *testing.T) { port: tt.port, keyPath: tt.keyPath, } - result := remote.buildSSHArgs() - assert.Equal(t, tt.expected, result) + execCmd := remote.newSSHCommand(context.Background(), tt.cmd) + assert.Equal(t, tt.expected, execCmd.Args) + assert.NotNil(t, execCmd.Cancel, "Cancel should be set for graceful shutdown") }) } }