Found by the new Docker test environment against a real NetBox 4.7: - The interface_name strategy filtered IP addresses with interface_name and vminterface_name, which NetBox ignores, so it returned the first address of the host instead of the named interface's. Use the real filters interface and vminterface; a missing interface (HTTP 400) falls through to the next strategy. - sftp and scp took instance-qualified hosts (prod/web01:/path) for local paths, because a "/" comes before the ":". Such arguments are remote when the prefix is a configured NetBox instance. - Shell aliases of instance-qualified hosts ran the shortcut rules on "instance/host", so anchored rewrite rules never matched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package shortcuts_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/shortcuts"
|
|
)
|
|
|
|
func TestNormalize(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".example.com", ".example.de"},
|
|
StripHyphens: true,
|
|
}
|
|
|
|
cases := []struct {
|
|
input string
|
|
want string
|
|
}{
|
|
{"fsn1-web01.example.com", "fsn1web01"},
|
|
{"fsn1-web01.example.de", "fsn1web01"},
|
|
{"FSN1-WEB01.EXAMPLE.COM", "fsn1web01"},
|
|
{"fsn1-web01", "fsn1web01"},
|
|
{"web01.example.com", "web01"},
|
|
{"web01.other.com", "web01.other.com"}, // unknown domain not stripped
|
|
}
|
|
|
|
for _, c := range cases {
|
|
got := shortcuts.Normalize(c.input, cfg)
|
|
if got != c.want {
|
|
t.Errorf("Normalize(%q) = %q, want %q", c.input, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeNoHyphenStrip(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".example.com"},
|
|
StripHyphens: false,
|
|
}
|
|
|
|
got := shortcuts.Normalize("fsn1-web01.example.com", cfg)
|
|
want := "fsn1-web01"
|
|
if got != want {
|
|
t.Errorf("Normalize = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestAliasName_WithDomainAndHyphens(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".example.com"},
|
|
StripHyphens: true,
|
|
}
|
|
got := shortcuts.AliasName("fsn1-web01.example.com", cfg)
|
|
want := "fsn1web01"
|
|
if got != want {
|
|
t.Errorf("AliasName = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestAliasName_DotReplacement(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".example.com"},
|
|
}
|
|
// "web01.other.com" — domain not stripped, remaining dots → underscores
|
|
got := shortcuts.AliasName("web01.other.com", cfg)
|
|
want := "web01_other_com"
|
|
if got != want {
|
|
t.Errorf("AliasName = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestAliasName_NoConfig(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{}
|
|
got := shortcuts.AliasName("web01.example.com", cfg)
|
|
want := "web01_example_com"
|
|
if got != want {
|
|
t.Errorf("AliasName = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRewrites(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".hoster.de"},
|
|
Rewrites: []config.ShortcutRewrite{{Pattern: `^([a-z]+)[0-9]+-`, Replace: "$1"}},
|
|
}
|
|
for input, want := range map[string]string{
|
|
"fsn1-bx1.hoster.de": "fsnbx1",
|
|
"FSN1-BX1.hoster.de": "fsnbx1",
|
|
"fsn1-bx1": "fsnbx1",
|
|
"fsnbx1": "fsnbx1", // the shortcut itself is left alone
|
|
"web01.hoster.de": "web01",
|
|
} {
|
|
if got := shortcuts.Normalize(input, cfg); got != want {
|
|
t.Errorf("Normalize(%q) = %q, want %q", input, got, want)
|
|
}
|
|
}
|
|
if got := shortcuts.AliasName("fsn1-bx1.hoster.de", cfg); got != "fsnbx1" {
|
|
t.Errorf("AliasName = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeRewritesOrder(t *testing.T) {
|
|
// Rules run in order after domain stripping, before hyphen stripping.
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".hoster.de"},
|
|
StripHyphens: true,
|
|
Rewrites: []config.ShortcutRewrite{
|
|
{Pattern: `\.hoster\.de$`, Replace: "-never"},
|
|
{Pattern: `^fsn`, Replace: "f"},
|
|
{Pattern: `^f1`, Replace: "falkenstein"},
|
|
},
|
|
}
|
|
if got := shortcuts.Normalize("fsn1-bx-1.hoster.de", cfg); got != "falkensteinbx1" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAliasNameRewritesInstanceQualifiedHosts(t *testing.T) {
|
|
cfg := config.ShortcutsConfig{
|
|
Domains: []string{".hoster.test"},
|
|
Rewrites: []config.ShortcutRewrite{{Pattern: `^([a-z]+)[0-9]+-`, Replace: "$1"}},
|
|
}
|
|
if got := shortcuts.AliasName("fsn/fsn1-pve1.hoster.test", cfg); got != "fsn_fsnpve1" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|