-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_test.go
66 lines (59 loc) · 2.06 KB
/
dns_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"net"
"testing"
)
func TestParseZone(t *testing.T) {
tests := []struct {
full string
expected string
}{
{"_port._protocol.service.namespace.pod.zone", "zone"},
{"endpoint.service.namespace.pod.zone", "zone"},
{"service.namespace.pod.zone", "zone"},
{"_port._protocol.service.namespace.svc.zone", "zone"},
{"endpoint.service.namespace.svc.zone.a.a.a.a.a.a", "zone.a.a.a.a.a.a"},
{"service.namespace.svc.zone", "zone"},
{"asdasd", "cluster.local"},
}
for _, test := range tests {
result := parseZone(test.full)
if result != test.expected {
t.Errorf("parseZone(%q) = %q; want %q", test.full, result, test.expected)
}
}
}
func TestSplit(t *testing.T) {
tests := []struct {
name string
zone string
expected [6]string
}{
{"_port._protocol.service.namespace.pod.zone", "zone", [6]string{"pod", "_port", "_protocol", "service", "namespace", ""}},
{"endpoint.service.namespace.pod.zone", "zone", [6]string{"pod", "", "", "service", "namespace", "endpoint"}},
{"service.namespace.pod.zone", "zone", [6]string{"pod", "", "", "service", "namespace", ""}},
{"_port._protocol.service.namespace.svc.zone", "zone", [6]string{"svc", "_port", "_protocol", "service", "namespace", ""}},
{"endpoint.service.namespace.svc.zone", "zone", [6]string{"svc", "", "", "service", "namespace", "endpoint"}},
{"service.namespace.svc.zone", "zone", [6]string{"svc", "", "", "service", "namespace", ""}},
{"invalid.name", "zone", [6]string{"", "", "", "", "", ""}},
}
for _, test := range tests {
_type, port, protocol, service, namespace, endpoint := split(test.name, test.zone)
result := [6]string{_type, port, protocol, service, namespace, endpoint}
if result != test.expected {
t.Errorf("split(%q, %q) = %q; want %q", test.name, test.zone, result, test.expected)
}
}
}
func Test_rdns(t *testing.T) {
got, err := rdns(&net.UDPAddr{
IP: net.ParseIP("1.1.1.1"),
Port: 53,
}, "8.8.8.8")
if err != nil {
t.Fatalf("rdns() error = %v", err)
}
if got != "dns.google." {
t.Errorf("rdns() = %q; want not empty", got)
}
}