package tui import ( "errors" "net" "strconv" "strings" "testing" "time" tea "charm.land/bubbletea/v2" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" ) func TestFilterByNetBoxAttributes(t *testing.T) { entries := []netbox.HostEntry{ {Name: "sw1", Kind: "device", Status: "active", Site: config.Ref{Name: "FSN 1", Slug: "fsn1"}, Platform: config.Ref{Slug: "junos"}, Role: config.Ref{Slug: "core"}}, {Name: "sw2", Kind: "device", Status: "offline", Site: config.Ref{Slug: "nbg1"}, Platform: config.Ref{Slug: "junos"}}, {Name: "web", Kind: "vm", Status: "active", Site: config.Ref{Slug: "fsn1"}, Tenant: config.Ref{Name: "ACME"}}, } for filter, want := range map[string]string{ "status:active": "sw1 web", "site:fsn1": "sw1 web", "site:FSN-1": "sw1", "platform:junos status:active": "sw1", "role:core": "sw1", "tenant:acme kind:vm": "web", "bogus": "sw1 sw2 web", } { var names []string for _, e := range applyFilter(entries, parseFilter(filter)) { names = append(names, e.Name) } if got := strings.Join(names, " "); got != want { t.Errorf("%q: got %q, want %q", filter, got, want) } } } func TestStatusBadgeShownForInactiveHosts(t *testing.T) { m := New(nil, nil, "") m.updateListItems([]netbox.HostEntry{{Name: "a", Status: "active"}, {Name: "b", Status: "offline"}}) var out strings.Builder for i, it := range m.list.Items() { compactDelegate{marked: map[string]bool{}}.Render(&out, m.list, i, it) out.WriteString("\n") } if strings.Contains(out.String(), "[active]") || !strings.Contains(out.String(), "[offline]") { t.Fatalf("unexpected badges:\n%s", out.String()) } } func TestReachabilityProbesListedHosts(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Skip(err) } defer ln.Close() port := ln.Addr().(*net.TCPAddr).Port m := New(nil, nil, "") m.SetReachability(time.Second, func(name, kind string, tags []string) (int, bool) { return port, name != "behind-jump" }) _, cmd := m.Update(searchResultMsg{query: "", recent: true, entries: []netbox.HostEntry{ {Name: "up", PrimaryIP4: "127.0.0.1"}, {Name: "behind-jump", PrimaryIP4: "127.0.0.1"}, }}) if cmd == nil { t.Fatal("no probe started") } for _, msg := range runBatch(cmd) { m.Update(msg) } up := hostItem{name: "up"}.key() skipped := hostItem{name: "behind-jump"}.key() if m.reach[up] != reachUp || m.reach[skipped] != reachSkipped { t.Fatalf("states: %v", m.reach) } var out strings.Builder m.list.Select(0) compactDelegate{marked: m.marked, reach: m.reach}.Render(&out, m.list, 0, m.list.Items()[0]) if !strings.Contains(out.String(), "✓") { t.Fatalf("reachable mark missing: %q", out.String()) } // A second result list does not probe hosts that already have a state. _, cmd = m.Update(searchResultMsg{query: "", recent: true, entries: []netbox.HostEntry{{Name: "up", PrimaryIP4: "127.0.0.1"}}}) if len(runBatch(cmd)) != 0 { t.Fatal("host probed twice") } } func TestUnreachableHostMarked(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Skip(err) } port := ln.Addr().(*net.TCPAddr).Port ln.Close() m := New(nil, nil, "") m.SetReachability(200*time.Millisecond, func(string, string, []string) (int, bool) { return port, true }) _, cmd := m.Update(searchResultMsg{recent: true, entries: []netbox.HostEntry{{Name: "down", PrimaryIP4: "127.0.0.1"}}}) for _, msg := range runBatch(cmd) { m.Update(msg) } if m.reach[hostItem{name: "down"}.key()] != reachDown { t.Fatalf("states: %v", m.reach) } } // runBatch executes a command and the commands of a batch it returns. func runBatch(cmd tea.Cmd) []tea.Msg { if cmd == nil { return nil } msg := cmd() if batch, ok := msg.(tea.BatchMsg); ok { var out []tea.Msg for _, c := range batch { out = append(out, runBatch(c)...) } return out } if msg == nil { return nil } return []tea.Msg{msg} } func TestDetailViewLoadsAndConnects(t *testing.T) { m := New(nil, nil, "") var asked string m.SetDetails(func(name, kind string, id int) (string, error) { asked = name + "/" + strconv.Itoa(id) return "sw1 (device)\n Site FSN1\n", nil }) m.updateListItems([]netbox.HostEntry{{Name: "sw1", ID: 7, Kind: "device", PrimaryIP4: "10.0.0.7"}}) _, cmd := m.Update(tea.KeyPressMsg{Code: 'l', Mod: tea.ModCtrl}) if m.detailHost == nil || !strings.Contains(m.viewContent(), "Loading") { t.Fatal("detail view not opened") } m.Update(cmd()) if asked != "sw1/7" || !strings.Contains(m.viewContent(), "Site FSN1") { t.Fatalf("details not shown (asked %q):\n%s", asked, m.viewContent()) } m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) if m.selected == nil || m.selected.Name != "sw1" { t.Fatalf("enter in detail view should connect: %+v", m.selected) } } func TestDetailViewShowsErrorAndEscCloses(t *testing.T) { m := New(nil, nil, "") m.SetDetails(func(string, string, int) (string, error) { return "", errors.New("boom") }) m.updateListItems([]netbox.HostEntry{{Name: "sw1"}}) _, cmd := m.Update(tea.KeyPressMsg{Code: 'l', Mod: tea.ModCtrl}) m.Update(cmd()) if !strings.Contains(m.viewContent(), "Error: boom") { t.Fatalf("error not shown:\n%s", m.viewContent()) } m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) if m.detailHost != nil || m.selected != nil { t.Fatal("esc should close the detail view without selecting") } } func TestCtrlGOpensNetBoxPage(t *testing.T) { m := New(nil, nil, "") var opened string m.SetOpener(func(name, kind string, id int) error { opened = name; return nil }) m.updateListItems([]netbox.HostEntry{{Name: "web", Kind: "vm"}}) _, cmd := m.Update(tea.KeyPressMsg{Code: 'g', Mod: tea.ModCtrl}) if cmd == nil || m.selected != nil { t.Fatal("ctrl+g must not quit the TUI") } m.Update(cmd()) if opened != "web" || !strings.Contains(m.viewContent(), "opened web in NetBox") { t.Fatalf("opened %q:\n%s", opened, m.viewContent()) } } func TestConfiguredColumns(t *testing.T) { m := New(nil, nil, "") m.SetColumns([]string{"platform", "site", "tags", "kind"}) m.updateListItems([]netbox.HostEntry{{Name: "sw1", Kind: "device", PrimaryIP4: "10.0.0.7", Status: "offline", Tags: []string{"core", "dc"}, Site: config.Ref{Name: "FSN 1", Slug: "fsn1"}, Platform: config.Ref{Slug: "junos"}}}) var out strings.Builder m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) got := out.String() for _, want := range []string{"platform:junos", "site:fsn1", "#core #dc", "[device]"} { if !strings.Contains(got, want) { t.Errorf("missing %q in %q", want, got) } } if strings.Contains(got, "10.0.0.7") || strings.Contains(got, "[offline]") || strings.Contains(got, "[live]") { t.Fatalf("columns not configured must be hidden: %q", got) } if strings.Index(got, "platform:") > strings.Index(got, "site:") { t.Fatalf("configured order not kept: %q", got) } m.SetColumns(nil) out.Reset() m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) if got := out.String(); !strings.HasSuffix(strings.TrimSpace(got), "sw1") { t.Fatalf("empty column list should show only the name: %q", got) } } func TestLocationColumnAndFilter(t *testing.T) { entries := []netbox.HostEntry{ {Name: "srv1", Site: config.Ref{Name: "FSN1"}, Location: []config.Ref{{Name: "Building A", Slug: "building-a"}, {Name: "Room 2.04", Slug: "room-2-04"}}, Rack: "R12", Position: "20"}, {Name: "srv2", Site: config.Ref{Name: "NBG1"}, Location: []config.Ref{{Name: "Hall 1", Slug: "hall-1"}}, Rack: "B03"}, } for filter, want := range map[string]string{"location:building-a": "srv1", "location:room-2-04": "srv1", "rack:b03": "srv2", "location:hall-1": "srv2"} { var names []string for _, e := range applyFilter(entries, parseFilter(filter)) { names = append(names, e.Name) } if got := strings.Join(names, " "); got != want { t.Errorf("%q: got %q, want %q", filter, got, want) } } m := New(nil, nil, "") m.SetColumns([]string{"location"}) m.updateListItems(entries[:1]) var out strings.Builder m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) if !strings.Contains(out.String(), "FSN1 › Building A › Room 2.04 › R12 › U20") { t.Fatalf("location column: %q", out.String()) } }