feat: NetBox insights, host tools and configurable TUI
Test / test (pull_request) Successful in 4m15s
Test / test (pull_request) Successful in 4m15s
- 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>
This commit is contained in:
1 parent
e5bde9c61f
commit
1660bb3098
39 files changed
+4868
-96
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSSHRuleMatchesNetBoxAttributes(t *testing.T) {
|
||||
host := SSHHost{Name: "sw1", Kind: "device", Status: "active",
|
||||
Site: Ref{Name: "Falkenstein 1", Slug: "fsn1"}, Role: Ref{Name: "Core Switch", Slug: "core-switch"},
|
||||
Platform: Ref{Name: "Juniper Junos", Slug: "junos"}, Tenant: Ref{Name: "ACME"}}
|
||||
for _, tc := range []struct {
|
||||
match SSHMatch
|
||||
want bool
|
||||
}{
|
||||
{SSHMatch{Platform: "junos"}, true},
|
||||
{SSHMatch{Platform: "juniper junos"}, true},
|
||||
{SSHMatch{Platform: "eos"}, false},
|
||||
{SSHMatch{Site: "fsn1", Role: "core-switch"}, true},
|
||||
{SSHMatch{Role: "Core Switch"}, true},
|
||||
{SSHMatch{Site: "falkenstein-1"}, true},
|
||||
{SSHMatch{Tenant: "acme"}, true},
|
||||
{SSHMatch{Status: "ACTIVE"}, true},
|
||||
{SSHMatch{Status: "offline"}, false},
|
||||
{SSHMatch{Platform: "junos", Kind: "vm"}, false},
|
||||
} {
|
||||
if got := tc.match.matches(host); got != tc.want {
|
||||
t.Errorf("%+v: got %v, want %v", tc.match, got, tc.want)
|
||||
}
|
||||
}
|
||||
if err := (SSHMatch{Platform: "junos"}).Validate("rule"); err != nil {
|
||||
t.Fatalf("platform-only match rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHOptionsForPlatformRule(t *testing.T) {
|
||||
cfg := &Config{SSH: SSHConfig{Rules: []SSHRule{{Match: SSHMatch{Platform: "junos"}, SSHOptions: SSHOptions{Options: map[string]string{"KexAlgorithms": "+diffie-hellman-group14-sha1"}}}}}}
|
||||
opts := cfg.SSHOptionsFor(SSHHost{Name: "sw1", Kind: "device", Platform: Ref{Slug: "junos"}})
|
||||
if opts.Options["kexalgorithms"] != "+diffie-hellman-group14-sha1" {
|
||||
t.Fatalf("platform rule not applied: %+v", opts.Options)
|
||||
}
|
||||
if opts := cfg.SSHOptionsFor(SSHHost{Name: "web", Kind: "vm"}); len(opts.Options) != 0 {
|
||||
t.Fatalf("rule applied to other platform: %+v", opts.Options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarnStatusDefaultsAndOverrides(t *testing.T) {
|
||||
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n"))
|
||||
if cfg.SSH.WarnStatus != nil || !cfg.SSH.WarnsStatus("offline") || cfg.SSH.WarnsStatus("active") || cfg.SSH.WarnsStatus("") {
|
||||
t.Fatalf("default warn_status wrong: %#v", cfg.SSH.WarnStatus)
|
||||
}
|
||||
cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\nssh:\n warn_status: []\n"))
|
||||
if cfg.SSH.WarnStatus == nil || cfg.SSH.WarnsStatus("offline") {
|
||||
t.Fatalf("empty warn_status should disable the question: %#v", cfg.SSH.WarnStatus)
|
||||
}
|
||||
cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\nssh:\n warn_status: [planned]\n"))
|
||||
if !cfg.SSH.WarnsStatus("Planned") || cfg.SSH.WarnsStatus("offline") {
|
||||
t.Fatalf("custom warn_status wrong: %#v", cfg.SSH.WarnStatus)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReachabilityAndFallbackDefaults(t *testing.T) {
|
||||
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n"))
|
||||
if cfg.TUI.ReachabilityEnabled() || cfg.TUI.ReachabilityTimeoutDuration().Milliseconds() != 300 {
|
||||
t.Fatalf("reachability must be opt-in: %+v", cfg.TUI)
|
||||
}
|
||||
if strings.Join(cfg.TUI.EffectiveColumns(), ",") != "alias,ip,source,status" {
|
||||
t.Fatalf("default columns: %v", cfg.TUI.EffectiveColumns())
|
||||
}
|
||||
if cfg.Resolver.Fallback || cfg.Resolver.FallbackTimeoutDuration().Milliseconds() != 1000 {
|
||||
t.Fatalf("fallback defaults wrong: %+v", cfg.Resolver)
|
||||
}
|
||||
cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: [Reachability, ip, platform]\n reachability_timeout: 800\nresolver:\n fallback: true\n fallback_timeout: 250\n"))
|
||||
if !cfg.TUI.ReachabilityEnabled() || cfg.TUI.ReachabilityTimeoutDuration().Milliseconds() != 800 || strings.Join(cfg.TUI.Columns, ",") != "reachability,ip,platform" {
|
||||
t.Fatalf("reachability settings ignored: %+v", cfg.TUI)
|
||||
}
|
||||
if !cfg.Resolver.Fallback || cfg.Resolver.FallbackTimeoutDuration().Milliseconds() != 250 {
|
||||
t.Fatalf("fallback settings ignored: %+v", cfg.Resolver)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyringTokenKeepsVersionUndetected(t *testing.T) {
|
||||
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n token: keyring\nnetboxes: []\n"))
|
||||
if !IsKeyringToken(cfg.NetBox.Token) || cfg.NetBox.TokenVersion != 0 {
|
||||
t.Fatalf("keyring token treated as literal: %+v", cfg.NetBox)
|
||||
}
|
||||
if cfg.NetBox.KeyringAccount() != "default" || (NetBoxConfig{Name: "Prod"}).KeyringAccount() != "prod" {
|
||||
t.Fatal("unexpected keyring account names")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTUIColumnsValidation(t *testing.T) {
|
||||
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: []\n"))
|
||||
if cfg.TUI.Columns == nil || len(cfg.TUI.EffectiveColumns()) != 0 {
|
||||
t.Fatalf("empty columns should show only the hostname: %#v", cfg.TUI.Columns)
|
||||
}
|
||||
for _, bad := range []string{"[ip, colour]", "[ip, ip]"} {
|
||||
dir := writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: "+bad+"\n")
|
||||
orig := os.Getenv("XDG_CONFIG_HOME")
|
||||
os.Setenv("XDG_CONFIG_HOME", dir)
|
||||
_, err := Load()
|
||||
os.Setenv("XDG_CONFIG_HOME", orig)
|
||||
if err == nil || !strings.Contains(err.Error(), "tui.columns") {
|
||||
t.Errorf("%s accepted: %v", bad, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user