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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package kubernetes
import "testing"
import "reflect"
// Test data for TestSymbolContainsWildcard cases.
var testdataSymbolContainsWildcard = []struct {
Symbol string
ExpectedResult bool
}{
{"mynamespace", false},
{"*", true},
{"any", true},
{"my*space", false},
{"*space", false},
{"myname*", false},
}
func TestSymbolContainsWildcard(t *testing.T) {
for _, example := range testdataSymbolContainsWildcard {
actualResult := symbolContainsWildcard(example.Symbol)
if actualResult != example.ExpectedResult {
t.Errorf("Expected SymbolContainsWildcard result '%v' for example string='%v'. Instead got result '%v'.", example.ExpectedResult, example.Symbol, actualResult)
}
}
}
func expectString(t *testing.T, function, qtype, query string, r *recordRequest, field, expected string) {
ref := reflect.ValueOf(r)
refField := reflect.Indirect(ref).FieldByName(field)
got := refField.String()
if got != expected {
t.Errorf("Expected %v(%v, \"%v\") to get %v == \"%v\". Instead got \"%v\".", function, query, qtype, field, expected, got)
}
}
func TestParseRequest(t *testing.T) {
var tcs map[string]string
k := Kubernetes{Zones: []string{"inter.webs.test"}}
f := "parseRequest"
// Test a valid SRV request
//
query := "_http._tcp.webs.mynamespace.svc.inter.webs.test."
r, e := k.parseRequest(query, "SRV")
if e != nil {
t.Errorf("Expected no error from parseRequest(%v, \"SRV\"). Instead got '%v'.", query, e)
}
tcs = map[string]string{
"port": "http",
"protocol": "tcp",
"endpoint": "",
"service": "webs",
"namespace": "mynamespace",
"typeName": "svc",
"zone": "inter.webs.test",
}
for field, expected := range tcs {
expectString(t, f, "SRV", query, &r, field, expected)
}
// Test wildcard acceptance
//
query = "*.any.*.any.svc.inter.webs.test."
r, e = k.parseRequest(query, "SRV")
if e != nil {
t.Errorf("Expected no error from parseRequest(\"%v\", \"SRV\"). Instead got '%v'.", query, e)
}
tcs = map[string]string{
"port": "*",
"protocol": "any",
"endpoint": "",
"service": "*",
"namespace": "any",
"typeName": "svc",
"zone": "inter.webs.test",
}
for field, expected := range tcs {
expectString(t, f, "SRV", query, &r, field, expected)
}
// Test A request of endpoint
query = "1-2-3-4.webs.mynamespace.svc.inter.webs.test."
r, e = k.parseRequest(query, "A")
if e != nil {
t.Errorf("Expected no error from parseRequest(\"%v\", \"A\"). Instead got '%v'.", query, e)
}
tcs = map[string]string{
"port": "",
"protocol": "",
"endpoint": "1-2-3-4",
"service": "webs",
"namespace": "mynamespace",
"typeName": "svc",
"zone": "inter.webs.test",
}
for field, expected := range tcs {
expectString(t, f, "A", query, &r, field, expected)
}
// Invalid query tests
invalidAQueries := []string{
"_http._tcp.webs.mynamespace.svc.inter.webs.test.", // A requests cannot have port or protocol
"servname.ns1.srv.inter.nets.test.", // A requests must have zone that matches corefile
}
for _, q := range invalidAQueries {
_, e = k.parseRequest(q, "A")
if e == nil {
t.Errorf("Expected error from %v(\"%v\", \"A\").", f, q)
}
}
invalidSRVQueries := []string{
"webs.mynamespace.svc.inter.webs.test.", // SRV requests must have port and protocol
"_http._pcp.webs.mynamespace.svc.inter.webs.test.", // SRV protocol must be tcp or udp
"_http._tcp.ep.webs.ns.svc.inter.webs.test.", // SRV requests cannot have an endpoint
"_*._*.webs.mynamespace.svc.inter.webs.test.", // SRV request with invalid wildcards
"_http._tcp",
"_tcp.test.",
".",
}
for _, q := range invalidSRVQueries {
_, e = k.parseRequest(q, "SRV")
if e == nil {
t.Errorf("Expected error from %v(\"%v\", \"SRV\").", f, q)
}
}
}
|