shortcuts.rewrites applies regex replacements after the domain suffix is stripped, e.g. ^([a-z]+)[0-9]+- -> $1 turns fsn1-bx1.hoster.de into fsnbx1. The shortcut works for connecting (cached hosts), shell aliases, and Tab completion, where it is offered when the full name does not match the typed text. Rules are editable in netssh configure, and netssh alias --test HOSTNAME shows a name's shortcut and the cached hosts it matches. The shortcut lookup only ran when domains or strip_hyphens were set; it now also runs for rewrite-only configurations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
3.1 KiB
Go
118 lines
3.1 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)
|
|
}
|
|
}
|