After the NetBox instances, netssh configure now opens a menu with all sections: SSH defaults, global SSH options (jump host, host key checking, target, address family, extra args, -o options), SSH rules, resolver & cache (including the cache file), shortcuts & command alias, and completion.max_results. SSH options can also be set per instance. The wizard rebuilt the config from its own fields, so re-running it dropped global SSH options, ssh.rules and completion.max_results from the file. It now edits a copy of the loaded config, and save validates SSH options and rules before writing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package setup
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
)
|
|
|
|
func TestParseLines(t *testing.T) {
|
|
got := parseLines(" -A \n\n\t-C\n \n")
|
|
if want := []string{"-A", "-C"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
}
|
|
if got := parseLines(" \n "); got != nil {
|
|
t.Fatalf("blank input: got %q, want nil", got)
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsRoundtrip(t *testing.T) {
|
|
text := "ServerAliveInterval = 60\n\nForwardAgent=yes\nLocalCommand=echo a=b\n"
|
|
got, err := parseOptions(text)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]string{"ServerAliveInterval": "60", "ForwardAgent": "yes", "LocalCommand": "echo a=b"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
again, err := parseOptions(formatOptions(got))
|
|
if err != nil || !reflect.DeepEqual(again, want) {
|
|
t.Fatalf("roundtrip: got %v (%v)", again, err)
|
|
}
|
|
if formatOptions(got) != "ForwardAgent=yes\nLocalCommand=echo a=b\nServerAliveInterval=60" {
|
|
t.Fatalf("formatOptions not sorted: %q", formatOptions(got))
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsEmpty(t *testing.T) {
|
|
got, err := parseOptions("\n \n")
|
|
if err != nil || got != nil {
|
|
t.Fatalf("got %v, %v; want nil, nil", got, err)
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsInvalid(t *testing.T) {
|
|
for _, text := range []string{"ServerAliveInterval", "=60", "Bad-Key=1", "1Key=1"} {
|
|
if _, err := parseOptions(text); err == nil {
|
|
t.Errorf("%q: expected error", text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDescribeMatch(t *testing.T) {
|
|
got := describeMatch(config.SSHMatch{Tag: "dmz", Name: "*.dmz"})
|
|
if got != "[tag=dmz name=*.dmz]" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestRuleValidation(t *testing.T) {
|
|
if err := (config.SSHMatch{}).Validate("rule"); err == nil || !strings.Contains(err.Error(), "at least one") {
|
|
t.Fatalf("empty match: got %v", err)
|
|
}
|
|
if err := (config.SSHMatch{Name: "[web"}).Validate("rule"); err == nil {
|
|
t.Fatal("invalid glob accepted")
|
|
}
|
|
if err := (config.SSHMatch{Kind: "vm"}).Validate("rule"); err != nil {
|
|
t.Fatalf("valid match rejected: %v", err)
|
|
}
|
|
}
|