- links: short names mapped to host selectors, validated against chains and static host names - dns.suggest / dns.save_link: ask, always or never - dns.domains: domain rules per name pattern (mx* -> easysahre.de) - the wizard edits the dns settings under Resolver & cache and keeps links; setup.SaveTo reports to a chosen writer Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.4 KiB
Go
121 lines
5.4 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDNSConfigValidation(t *testing.T) {
|
|
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ndns:\n suggest: Always\n domains:\n - match: \"mx*\"\n domain: easysahre.de\n"))
|
|
if cfg.DNS.SuggestMode() != DNSAlways || cfg.DNS.SaveLinkMode() != DNSAsk || len(cfg.DNS.Domains) != 1 {
|
|
t.Fatalf("dns: %+v", cfg.DNS)
|
|
}
|
|
for _, bad := range []DNSConfig{{Suggest: "sometimes"}, {SaveLink: "yes"}, {Domains: []DNSDomain{{Match: "[", Domain: "a.de"}}}, {Domains: []DNSDomain{{Domain: " "}}}, {Domains: []DNSDomain{{Domain: "a b.de"}}}} {
|
|
if err := (&Config{DNS: bad}).ValidateDNS(); err == nil {
|
|
t.Errorf("%+v accepted", bad)
|
|
}
|
|
}
|
|
}
|