- 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
196 lines
6.7 KiB
Go
196 lines
6.7 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
tea "charm.land/bubbletea/v2"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
|
)
|
|
|
|
func tuiSourceServer(t *testing.T, fail bool) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if fail {
|
|
http.Error(w, "offline", 503)
|
|
return
|
|
}
|
|
if r.URL.Path == "/api/dcim/devices/" {
|
|
fmt.Fprint(w, `{"count":1,"results":[{"id":1,"name":"web","primary_ip4":{"address":"10.0.0.1/24"}}]}`)
|
|
} else {
|
|
fmt.Fprint(w, `{"count":0,"results":[]}`)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestCombinedSearchDoesNotStopAtFirstCachedInstance(t *testing.T) {
|
|
office, lab := tuiSourceServer(t, false), tuiSourceServer(t, false)
|
|
client := netbox.NewMultiClient([]netbox.Source{{Name: "office", URL: office.URL}, {Name: "lab", URL: lab.URL}})
|
|
c := cache.New("", 3600)
|
|
c.Upsert(cache.Entry{Name: "office/web", Kind: "device", IP: "10.0.0.9"})
|
|
m := New(client, c, "")
|
|
msg := m.doSearch("web", 1)().(searchResultMsg)
|
|
if msg.err != nil || len(msg.entries) != 2 {
|
|
t.Fatalf("cache hid another instance: %+v, %v", msg.entries, msg.err)
|
|
}
|
|
m.input.SetValue("web")
|
|
m.seq = 1
|
|
m.Update(msg)
|
|
if len(m.list.Items()) != 2 {
|
|
t.Fatal("combined results not displayed")
|
|
}
|
|
for _, item := range m.list.Items() {
|
|
if !strings.Contains(item.(hostItem).name, "/") {
|
|
t.Fatal("instance not visible in results")
|
|
}
|
|
}
|
|
m.list.Select(0)
|
|
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
|
if m.selected == nil || m.selected.Name != "lab/web" {
|
|
t.Fatalf("selection lost source: %+v", m.selected)
|
|
}
|
|
}
|
|
|
|
func TestPartialOutageKeepsLiveAndCachedResultsVisible(t *testing.T) {
|
|
office, lab := tuiSourceServer(t, false), tuiSourceServer(t, true)
|
|
client := netbox.NewMultiClient([]netbox.Source{{Name: "office", URL: office.URL}, {Name: "lab", URL: lab.URL}})
|
|
c := cache.New("", 3600)
|
|
c.Upsert(cache.Entry{Name: "lab/web", Kind: "device", IP: "10.0.0.2"})
|
|
m := New(client, c, "")
|
|
m.input.SetValue("web")
|
|
m.seq = 1
|
|
m.Update(tea.WindowSizeMsg{Width: 100, Height: 24})
|
|
msg := m.doSearch("web", 1)().(searchResultMsg)
|
|
if len(msg.entries) != 2 || msg.err == nil {
|
|
t.Fatalf("partial outage lost cache or warning: %+v, %v", msg.entries, msg.err)
|
|
}
|
|
m.Update(msg)
|
|
view := m.View().Content
|
|
if !strings.Contains(view, "warning:") || !strings.Contains(view, "lab/web") || !strings.Contains(view, "office/web") {
|
|
t.Fatalf("partial results hidden: %s", view)
|
|
}
|
|
if rows := strings.Count(view, "\n"); rows > 24 {
|
|
t.Fatalf("outage warning overflowed view: %d", rows)
|
|
}
|
|
}
|
|
|
|
func TestHiddenPrefixKeepsCanonicalTargets(t *testing.T) {
|
|
cfg := config.Config{NetBoxes: []config.NetBoxConfig{
|
|
{Name: "OFFICE", DisablePrefix: true}, {Name: "lab"},
|
|
}}
|
|
for _, cached := range []bool{false, true} {
|
|
for _, action := range []string{"ssh", "sftp", "multi", "autocomplete", "edit", "tunnel"} {
|
|
t.Run(fmt.Sprintf("cached=%v/%s", cached, action), func(t *testing.T) {
|
|
m := New(nil, nil, "")
|
|
m.SetHostLabel(cfg.HostLabel)
|
|
m.Update(tea.WindowSizeMsg{Width: 100, Height: 24})
|
|
entries := []netbox.HostEntry{
|
|
{Source: "office", Name: "web01", PrimaryIP4: "10.0.0.1"},
|
|
{Source: "lab", Name: "web01", PrimaryIP4: "10.0.0.2"},
|
|
}
|
|
if cached {
|
|
for i := range entries {
|
|
entries[i].Name = entries[i].Target()
|
|
entries[i].Source = ""
|
|
}
|
|
}
|
|
m.updateListItems(entries)
|
|
item := m.list.Items()[0].(hostItem)
|
|
if item.Title() != "web01" || item.name != "office/web01" {
|
|
t.Fatalf("display changed identity: %+v", item)
|
|
}
|
|
view := m.View().Content
|
|
if strings.Contains(view, "office/web01") || !strings.Contains(view, "lab/web01") {
|
|
t.Fatalf("wrong rendered prefixes: %s", view)
|
|
}
|
|
switch action {
|
|
case "ssh", "sftp":
|
|
key := tea.KeyPressMsg{Code: tea.KeyEnter}
|
|
if action == "sftp" {
|
|
key = tea.KeyPressMsg{Code: 'o', Mod: tea.ModCtrl}
|
|
}
|
|
m.Update(key)
|
|
if m.selected == nil || m.selected.Name != "office/web01" {
|
|
t.Fatalf("lost instance: %+v", m.selected)
|
|
}
|
|
case "multi":
|
|
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
|
|
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
|
|
if m.selected == nil || len(m.selected.Targets) != 1 || m.selected.Targets[0] != "office/web01" {
|
|
t.Fatalf("multi lost instance: %+v", m.selected)
|
|
}
|
|
case "autocomplete":
|
|
m.Update(tea.KeyPressMsg{Code: tea.KeyTab})
|
|
if m.input.Value() != "office/web01" {
|
|
t.Fatal("autocomplete lost instance")
|
|
}
|
|
case "edit":
|
|
m.Update(tea.KeyPressMsg{Code: 'e', Mod: tea.ModCtrl})
|
|
if m.editHost == nil || m.editHost.name != "office/web01" {
|
|
t.Fatal("editor lost instance")
|
|
}
|
|
case "tunnel":
|
|
m.Update(tea.KeyPressMsg{Code: 't', Mod: tea.ModCtrl})
|
|
if m.forwardHost == nil || m.forwardHost.name != "office/web01" {
|
|
t.Fatal("tunnel lost instance")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHiddenPrefixDuplicateLabels(t *testing.T) {
|
|
cfg := config.Config{NetBoxes: []config.NetBoxConfig{
|
|
{Name: "office", DisablePrefix: true}, {Name: "lab", DisablePrefix: true},
|
|
}}
|
|
for _, other := range []netbox.HostEntry{
|
|
{Source: "lab", Name: "WEB"},
|
|
{Name: "web", Kind: "static"},
|
|
} {
|
|
m := New(nil, nil, "")
|
|
m.SetHostLabel(cfg.HostLabel)
|
|
m.updateListItems([]netbox.HostEntry{{Source: "office", Name: "web"}, other})
|
|
for _, entry := range m.list.Items() {
|
|
item := entry.(hostItem)
|
|
if item.Title() != item.name {
|
|
t.Fatalf("ambiguous label: %+v", item)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAliasesShownNextToHostName(t *testing.T) {
|
|
m := New(nil, cache.New("", 3600), "")
|
|
m.SetAliases(func(target string) string {
|
|
return map[string]string{"fsn1-bx1.hoster.de": "fsnbx1", "web01": "web01"}[target]
|
|
})
|
|
m.updateListItems([]netbox.HostEntry{{Name: "fsn1-bx1.hoster.de", PrimaryIP4: "10.0.0.1"}, {Name: "web01", PrimaryIP4: "10.0.0.2"}})
|
|
items := m.list.Items()
|
|
if got := items[0].(hostItem).alias; got != "fsnbx1" {
|
|
t.Fatalf("alias = %q", got)
|
|
}
|
|
// An alias equal to the shown name is not repeated.
|
|
if got := items[1].(hostItem).alias; got != "" {
|
|
t.Fatalf("redundant alias %q", got)
|
|
}
|
|
var b strings.Builder
|
|
compactDelegate{marked: map[string]bool{}}.Render(&b, m.list, 1, items[0])
|
|
if !strings.Contains(b.String(), "fsn1-bx1.hoster.de") || !strings.Contains(b.String(), "→ fsnbx1") {
|
|
t.Fatalf("row %q", b.String())
|
|
}
|
|
// Without SetAliases nothing changes.
|
|
plain := New(nil, cache.New("", 3600), "")
|
|
plain.updateListItems([]netbox.HostEntry{{Name: "fsn1-bx1.hoster.de"}})
|
|
if got := plain.list.Items()[0].(hostItem).alias; got != "" {
|
|
t.Fatalf("alias without SetAliases: %q", got)
|
|
}
|
|
}
|