44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package resolver
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
|
)
|
|
|
|
func TestHostnameStrategy(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, domain, want string
|
|
}{
|
|
{"Web01.Example.com", "", "web01.example.com"},
|
|
{"web01", "example.com", "web01.example.com"},
|
|
{"web01", ".example.com.", "web01.example.com"},
|
|
{"web01.lab", "example.com", "web01.lab"}, // already qualified
|
|
} {
|
|
got, err := (&HostnameStrategy{domain: tc.domain}).Resolve(context.Background(), &netbox.HostEntry{Name: tc.name}, nil)
|
|
if err != nil || got != tc.want {
|
|
t.Errorf("%q + %q: got %q, %v; want %q", tc.name, tc.domain, got, err, tc.want)
|
|
}
|
|
}
|
|
for _, name := range []string{"Core Switch 1", "-x", "a..b", "web_01"} {
|
|
if _, err := (&HostnameStrategy{}).Resolve(context.Background(), &netbox.HostEntry{Name: name}, nil); err != ErrNoIP {
|
|
t.Errorf("%q: got %v, want ErrNoIP", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHostnameAsFallback(t *testing.T) {
|
|
chain, err := New(config.ResolverConfig{Strategies: []string{"primary_ip", "hostname"}, HostnameDomain: "example.com"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ip, _ := chain.Resolve(context.Background(), &netbox.HostEntry{Name: "web01", PrimaryIP4: "192.0.2.1"}, nil); ip != "192.0.2.1" {
|
|
t.Fatalf("primary IP first: %q", ip)
|
|
}
|
|
if ip, _ := chain.Resolve(context.Background(), &netbox.HostEntry{Name: "web02"}, nil); ip != "web02.example.com" {
|
|
t.Fatalf("hostname fallback: %q", ip)
|
|
}
|
|
}
|