Files
HostKeeper/cmd/hostkeeper/connect_test.go
T
swanadiva 368b7cdadb feat: implement connect host command with native SSH
- Add connect command with native SSH (default) and direct Go SSH (--direct) modes
- Support host lookup by name or ID
- Build SSH arguments for system SSH client
- Include timeout configuration flag
- Add comprehensive tests for command and SSH arg building
- Update project state documentation
2026-06-23 13:48:05 +07:00

96 lines
1.9 KiB
Go

package main
import (
"testing"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
)
func TestConnectCommandExists(t *testing.T) {
if connectCmd == nil {
t.Fatal("connectCmd should not be nil")
}
if connectCmd.Use != "connect <host-name-or-id>" {
t.Errorf("expected Use 'connect <host-name-or-id>', got '%s'", connectCmd.Use)
}
if connectCmd.Short == "" {
t.Error("Short description should not be empty")
}
}
func TestConnectCommandFlags(t *testing.T) {
expectedFlags := []string{"timeout", "direct"}
for _, flagName := range expectedFlags {
if connectCmd.Flags().Lookup(flagName) == nil {
t.Errorf("flag '%s' should be defined", flagName)
}
}
}
func TestConnectArgs(t *testing.T) {
if connectCmd.Args == nil {
t.Error("Args validator should not be nil")
}
}
func TestBuildSSHArgs(t *testing.T) {
tests := []struct {
name string
host *models.Host
want []string
}{
{
name: "default port 22",
host: &models.Host{
Name: "test",
Hostname: "192.168.1.1",
Port: 22,
Username: "admin",
},
want: []string{"admin@192.168.1.1"},
},
{
name: "non-default port 2222",
host: &models.Host{
Name: "test",
Hostname: "192.168.1.1",
Port: 2222,
Username: "admin",
},
want: []string{"-p", "2222", "admin@192.168.1.1"},
},
{
name: "key auth with KeyID",
host: &models.Host{
Name: "test",
Hostname: "10.0.0.1",
Port: 22,
Username: "root",
Auth: models.AuthConfig{
Type: "key",
KeyID: "~/.ssh/id_rsa",
},
},
want: []string{"root@10.0.0.1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildSSHArgs(tt.host)
if len(got) != len(tt.want) {
t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want)
return
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want)
return
}
}
})
}
}