Files
ssh-netbox-wrapper/internal/netbox/details_test.go
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- info/open/whois: NetBox details (site, nested locations, rack units,
  role, platform, tenant, IPs per interface), web page, reverse IP lookup
- cache status, site, role, platform, tenant, location chain and rack;
  ask before connecting to hosts in ssh.warn_status (--force skips)
- ssh rules and command restrictions match site/role/platform/tenant/status
- tui.columns selects what the TUI shows; optional TCP reachability column;
  detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters
- resolver.fallback tries other strategies' addresses when unreachable
- copy-id, known-hosts reset and address-change notices
- export ansible (YAML/INI) and export ssh-config
- token: keyring stores NetBox tokens in the system keyring
- fix stale menu number in testenv configure test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:33:59 +01:00

102 lines
4.4 KiB
Go

package netbox
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func detailServer(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.URL.Path == "/api/dcim/devices/7/":
fmt.Fprint(w, `{"id":7,"name":"sw1","status":{"value":"offline","label":"Offline"},
"site":{"name":"Falkenstein 1","slug":"fsn1"},"rack":{"name":"R12"},"position":20.0,
"device_role":{"name":"Core Switch","slug":"core-switch"},"platform":{"name":"Junos","slug":"junos"},
"tenant":null,"serial":"ABC","asset_tag":null,"device_type":{"model":"QFX5120","manufacturer":{"name":"Juniper"}},
"primary_ip4":{"address":"10.0.0.7/24"},"tags":[{"name":"core"}]}`)
case r.URL.Path == "/api/ipam/ip-addresses/" && r.URL.Query().Get("device_id") == "7":
fmt.Fprint(w, `{"count":2,"results":[
{"address":"192.0.2.7/24","assigned_object":{"name":"xe-0/0/1"},"status":{"value":"active"}},
{"address":"10.0.0.7/24","assigned_object":{"name":"em0"},"dns_name":"sw1.example.com","status":{"value":"active"}}]}`)
case r.URL.Path == "/api/ipam/ip-addresses/" && r.URL.Query().Get("address") != "":
fmt.Fprint(w, `{"count":1,"results":[{"address":"10.0.0.7/24","status":{"value":"active"},
"assigned_object_type":"dcim.interface","assigned_object":{"name":"em0","device":{"id":7,"name":"sw1"}}}]}`)
case r.URL.Path == "/api/ipam/prefixes/":
if r.URL.Query().Get("contains") != "10.0.0.7" {
t.Errorf("unexpected contains %q", r.URL.Query().Get("contains"))
}
fmt.Fprint(w, `{"count":2,"results":[
{"prefix":"10.0.0.0/24","status":{"value":"active"},"vlan":{"vid":100,"name":"mgmt"},"site":{"name":"FSN1"}},
{"prefix":"10.0.0.0/8","description":"internal"}]}`)
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
return srv
}
func TestDeviceAttributesAndDetails(t *testing.T) {
srv := detailServer(t)
c := NewClient(srv.URL, "token", 0)
d, err := c.GetDetails(context.Background(), HostEntry{ID: 7, Name: "sw1", Kind: "device"})
if err != nil {
t.Fatal(err)
}
if d.Status != "offline" || d.StatusLabel != "Offline" || d.Site.Slug != "fsn1" || d.Role.Slug != "core-switch" || d.Platform.Slug != "junos" {
t.Fatalf("attributes not parsed: %+v", d.HostEntry)
}
if d.Rack != "R12" || d.Position != "20" || d.DeviceType != "Juniper QFX5120" || d.Serial != "ABC" {
t.Fatalf("details not parsed: %+v", d)
}
if d.URL != srv.URL+"/dcim/devices/7/" {
t.Fatalf("web URL %q", d.URL)
}
if len(d.IPs) != 2 || !d.IPs[0].Primary || d.IPs[0].Interface != "em0" || d.IPs[0].DNSName != "sw1.example.com" {
t.Fatalf("IPs not sorted primary first: %+v", d.IPs)
}
}
func TestWebURLForInstances(t *testing.T) {
c := NewMultiClient([]Source{{Name: "prod", URL: "https://nb.example/", Token: "t"}})
got, err := c.WebURL(HostEntry{Source: "prod", Name: "web", Kind: "vm", ID: 3})
if err != nil || got != "https://nb.example/virtualization/virtual-machines/3/" {
t.Fatalf("got %q, %v", got, err)
}
if _, err := c.WebURL(HostEntry{Source: "prod", Name: "web", Kind: "vm"}); err == nil {
t.Fatal("missing ID accepted")
}
}
func TestLookupIPAndPrefixes(t *testing.T) {
srv := detailServer(t)
c := NewMultiClient([]Source{{Name: "prod", URL: srv.URL, Token: "t"}})
owners, err := c.LookupIP(context.Background(), "10.0.0.7")
if err != nil || len(owners) != 1 || owners[0].Target() != "prod/sw1" || owners[0].Interface != "em0" || owners[0].HostKind != "device" {
t.Fatalf("owners %+v, %v", owners, err)
}
prefixes, err := c.PrefixesContaining(context.Background(), "10.0.0.7")
if err != nil || len(prefixes) != 2 || prefixes[0].Prefix != "10.0.0.0/8" || prefixes[1].VLAN != "100 (mgmt)" || prefixes[1].Source != "prod" {
t.Fatalf("prefixes %+v, %v", prefixes, err)
}
}
func TestLookupIPReportsFailingInstanceButKeepsOthers(t *testing.T) {
good := detailServer(t)
bad := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "down", http.StatusBadGateway)
}))
defer bad.Close()
c := NewMultiClient([]Source{{Name: "a", URL: bad.URL, Token: "t"}, {Name: "b", URL: good.URL, Token: "t"}})
owners, err := c.LookupIP(context.Background(), "10.0.0.7")
if err == nil || !strings.Contains(err.Error(), "NetBox a") || len(owners) != 1 {
t.Fatalf("owners %+v, err %v", owners, err)
}
}