Test / test (pull_request) Successful in 4m27s
- ssh.pre_connect: local shell command run before every connection, layered like proxy_jump (global, instance/static host, rules; "none" disables). Host details are passed as NETSSH_* env vars, a non-zero exit aborts the connection, --dry-run only prints it. - shortcuts.show: show shortcut aliases in the TUI host list, an ALIAS column in `cache list` / `hosts list` (also via --aliases) and in Tab completion. - Both editable in `netssh configure`; README and CHANGELOG updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAuVpPncumMViLLc8N4
113 lines
4.2 KiB
Go
113 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh"
|
|
)
|
|
|
|
func TestRunPreConnectPassesHostAndStopsOnFailure(t *testing.T) {
|
|
host := resolvedHost{Name: "prod/web01", Kind: "vm", IP: "10.0.0.5", Tags: []string{"dmz", "web"}}
|
|
var gotCommand string
|
|
var gotEnv []string
|
|
fail := false
|
|
preConnectRunner = func(command string, env []string, _ io.Reader, _ io.Writer) error {
|
|
gotCommand, gotEnv = command, env
|
|
if fail {
|
|
return errors.New("exit status 1")
|
|
}
|
|
return nil
|
|
}
|
|
t.Cleanup(func() { preConnectRunner = runPreConnectCommand })
|
|
|
|
if err := runPreConnect("vpn-check", host, "sftp", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"NETSSH_HOST=web01", "NETSSH_TARGET=prod/web01", "NETSSH_INSTANCE=prod", "NETSSH_IP=10.0.0.5", "NETSSH_KIND=vm", "NETSSH_TAGS=dmz,web", "NETSSH_CLIENT=sftp"}
|
|
if gotCommand != "vpn-check" || !reflect.DeepEqual(gotEnv, want) {
|
|
t.Fatalf("got %q %v", gotCommand, gotEnv)
|
|
}
|
|
|
|
fail = true
|
|
if err := runPreConnect("vpn-check", host, "ssh", false); err == nil || !strings.Contains(err.Error(), "prod/web01") {
|
|
t.Fatalf("failure not reported: %v", err)
|
|
}
|
|
|
|
gotCommand = ""
|
|
if err := runPreConnect("vpn-check", host, "ssh", true); err != nil || gotCommand != "" {
|
|
t.Fatalf("dry-run ran the hook: %v %q", err, gotCommand)
|
|
}
|
|
if err := runPreConnect("", host, "ssh", false); err != nil || gotCommand != "" {
|
|
t.Fatalf("empty hook ran: %v %q", err, gotCommand)
|
|
}
|
|
}
|
|
|
|
func TestRunPreConnectCommandUsesShell(t *testing.T) {
|
|
var out bytes.Buffer
|
|
err := runPreConnectCommand(`echo "$NETSSH_HOST via $NETSSH_CLIENT"`, []string{"NETSSH_HOST=web01", "NETSSH_CLIENT=ssh"}, strings.NewReader(""), &out)
|
|
if err != nil || out.String() != "web01 via ssh\n" {
|
|
t.Fatalf("got %q, %v", out.String(), err)
|
|
}
|
|
if err := runPreConnectCommand("exit 3", nil, strings.NewReader(""), io.Discard); err == nil {
|
|
t.Fatal("non-zero exit not reported")
|
|
}
|
|
}
|
|
|
|
// --dry-run only prints the ssh command: it must not run the hook, which may
|
|
// have side effects (bringing up a VPN, logging).
|
|
func TestConnectHostDryRunSkipsPreConnect(t *testing.T) {
|
|
ran := 0
|
|
preConnectRunner = func(string, []string, io.Reader, io.Writer) error { ran++; return nil }
|
|
t.Cleanup(func() { preConnectRunner = runPreConnectCommand })
|
|
cfg := &config.Config{SSH: config.SSHConfig{SSHOptions: config.SSHOptions{PreConnect: "hook"}}}
|
|
stdout := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
connectHost(cfg, internalssh.SSH, internalssh.WrapperFlags{DryRun: true}, resolvedHost{Name: "web01", IP: "10.0.0.1"}, []int{0}, []string{"web01"})
|
|
w.Close()
|
|
os.Stdout = stdout
|
|
printed, _ := io.ReadAll(r)
|
|
if ran != 0 || !strings.Contains(string(printed), "10.0.0.1") {
|
|
t.Fatalf("dry-run: ran=%d printed=%q", ran, printed)
|
|
}
|
|
}
|
|
|
|
func TestShortcutAliasesOnlyWhenShortened(t *testing.T) {
|
|
cfg := config.Config{Shell: config.ShellConfig{Alias: "j"}, Shortcuts: fsnRewrite}
|
|
entries := []cache.Entry{{Name: "fsn1-bx1.hoster.de"}, {Name: "web01.other.de"}, {Name: "db01.hoster.de"}}
|
|
got := shortcutAliases(entries, cfg)
|
|
want := map[string]string{"fsn1-bx1.hoster.de": "fsnbx1", "db01.hoster.de": "db01"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
if got := shortcutAliases(entries, config.Config{}); len(got) != 0 {
|
|
t.Fatalf("aliases without shortcuts: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestHostCompletionsShowShortcutsForMatchingNames(t *testing.T) {
|
|
entries := []cache.Entry{{Name: "fsn1-bx1.hoster.de"}, {Name: "fsn1-db1.hoster.de"}}
|
|
if got := hostCompletions(entries, "fsn", fsnRewrite); !reflect.DeepEqual(got, []string{"fsn1-bx1.hoster.de", "fsn1-db1.hoster.de"}) {
|
|
t.Fatalf("without show: %v", got)
|
|
}
|
|
show := fsnRewrite
|
|
show.Show = true
|
|
want := []string{"fsn1-bx1.hoster.de", "fsn1-db1.hoster.de", "fsnbx1", "fsndb1"}
|
|
if got := hostCompletions(entries, "fsn", show); !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("with show: got %v, want %v", got, want)
|
|
}
|
|
// A shortcut equal to an offered name is not repeated.
|
|
plain := []cache.Entry{{Name: "web01"}}
|
|
if got := hostCompletions(plain, "web", show); !reflect.DeepEqual(got, []string{"web01"}) {
|
|
t.Fatalf("duplicate: %v", got)
|
|
}
|
|
}
|