fix: sshcli unit test and lint

This commit is contained in:
Pasha Sviderski
2026-03-14 15:04:50 +10:00
parent 9e4c759ffc
commit 940732e91c
+67 -39
View File
@@ -1,12 +1,13 @@
package sshexec package sshexec
import ( import (
"context"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSSHCLIRemote_buildSSHArgs(t *testing.T) { func TestSSHCLIRemote_newSSHCommand(t *testing.T) {
t.Parallel() t.Parallel()
tests := []struct { tests := []struct {
@@ -15,59 +16,85 @@ func TestSSHCLIRemote_buildSSHArgs(t *testing.T) {
host string host string
port int port int
keyPath string keyPath string
cmd string
expected []string expected []string
}{ }{
{ {
name: "host only", name: "host only",
host: "example.com", host: "example.com",
expected: []string{"-o", "ConnectTimeout=5", "example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "example.com", "whoami",
},
}, },
{ {
name: "with user", name: "with user",
user: "root", user: "root",
host: "example.com", host: "example.com",
expected: []string{"-o", "ConnectTimeout=5", "root@example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "root@example.com", "whoami",
},
}, },
{ {
name: "with port", name: "with port",
host: "example.com", host: "example.com",
port: 2222, port: 2222,
expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "-p", "2222", "example.com", "whoami",
},
}, },
{ {
name: "with key", name: "with key",
host: "example.com", host: "example.com",
keyPath: "/path/to/key", keyPath: "/path/to/key",
expected: []string{"-o", "ConnectTimeout=5", "-i", "/path/to/key", "example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "-i", "/path/to/key", "example.com", "whoami",
},
}, },
{ {
name: "user and port", name: "user and port",
user: "ubuntu", user: "ubuntu",
host: "192.168.1.10", host: "192.168.1.10",
port: 2222, port: 2222,
expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "ubuntu@192.168.1.10"}, cmd: "ls -la",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "-p", "2222", "ubuntu@192.168.1.10", "ls -la",
},
}, },
{ {
name: "user and key", name: "user and key",
user: "root", user: "root",
host: "example.com", host: "example.com",
keyPath: "/path/to/key", keyPath: "/path/to/key",
expected: []string{"-o", "ConnectTimeout=5", "-i", "/path/to/key", "root@example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "-i", "/path/to/key", "root@example.com", "whoami",
},
}, },
{ {
name: "port and key", name: "port and key",
host: "example.com", host: "example.com",
port: 22, port: 22,
keyPath: "~/.ssh/id_rsa", keyPath: "~/.ssh/id_rsa",
expected: []string{"-o", "ConnectTimeout=5", "-p", "22", "-i", "~/.ssh/id_rsa", "example.com"}, cmd: "whoami",
expected: []string{
"ssh", "-o", "ConnectTimeout=5", "-T", "-p", "22", "-i", "~/.ssh/id_rsa", "example.com", "whoami",
},
}, },
{ {
name: "all options", name: "all options",
user: "admin", user: "admin",
host: "server.local", host: "server.local",
port: 2222, port: 2222,
keyPath: "~/.ssh/id_rsa", keyPath: "~/.ssh/id_rsa",
expected: []string{"-o", "ConnectTimeout=5", "-p", "2222", "-i", "~/.ssh/id_rsa", "admin@server.local"}, 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, port: tt.port,
keyPath: tt.keyPath, keyPath: tt.keyPath,
} }
result := remote.buildSSHArgs() execCmd := remote.newSSHCommand(context.Background(), tt.cmd)
assert.Equal(t, tt.expected, result) assert.Equal(t, tt.expected, execCmd.Args)
assert.NotNil(t, execCmd.Cancel, "Cancel should be set for graceful shutdown")
}) })
} }
} }