-
Notifications
You must be signed in to change notification settings - Fork 65
/
gateway_test.go
341 lines (309 loc) · 10 KB
/
gateway_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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package gateway
import (
"context"
"errors"
"net/netip"
"strings"
"testing"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
type FallthroughCase struct {
test.Case
FallthroughZones []string
FallthroughExpected bool
}
type Fallen struct {
error
}
func TestLookup(t *testing.T) {
real := []string{"Ingress", "Service", "HTTPRoute", "TLSRoute", "GRPCRoute", "VirtualServer"}
fake := []string{"Pod", "Gateway"}
for _, resource := range real {
if found := lookupResource(resource); found == nil {
t.Errorf("Could not lookup supported resource %s", resource)
}
}
for _, resource := range fake {
if found := lookupResource(resource); found != nil {
t.Errorf("Located unsupported resource %s", resource)
}
}
}
func TestPlugin(t *testing.T) {
ctrl := &KubeController{hasSynced: true}
gw := newGateway()
gw.Zones = []string{"example.com."}
gw.Next = test.NextHandler(dns.RcodeSuccess, nil)
gw.ExternalAddrFunc = gw.SelfAddress
gw.Controller = ctrl
setupLookupFuncs()
ctx := context.TODO()
for i, tc := range tests {
r := tc.Msg()
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := gw.ServeDNS(ctx, w, r)
if err != tc.Error {
t.Errorf("Test %d expected no error, got %v", i, err)
return
}
if tc.Error != nil {
continue
}
resp := w.Msg
if resp == nil {
t.Fatalf("Test %d, got nil message and no error for %q", i, r.Question[0].Name)
}
if err = test.SortAndCheck(resp, tc); err != nil {
t.Errorf("Test %d failed with error: %v", i, err)
}
}
}
func TestPluginFallthrough(t *testing.T) {
ctrl := &KubeController{hasSynced: true}
gw := newGateway()
gw.Zones = []string{"example.com."}
gw.Next = test.NextHandler(dns.RcodeSuccess, Fallen{})
gw.ExternalAddrFunc = gw.SelfAddress
gw.Controller = ctrl
setupLookupFuncs()
ctx := context.TODO()
for i, tc := range testsFallthrough {
r := tc.Msg()
w := dnstest.NewRecorder(&test.ResponseWriter{})
gw.Fall = fall.F{Zones: tc.FallthroughZones}
_, err := gw.ServeDNS(ctx, w, r)
if errors.As(err, &Fallen{}) && !tc.FallthroughExpected {
t.Fatalf("Test %d query resulted unexpectedly in a fall through instead of a response", i)
}
if err == nil && tc.FallthroughExpected {
t.Fatalf("Test %d query resulted unexpectedly in a response instead of a fall through", i)
}
}
}
var tests = []test.Case{
// Existing Service IPv4 | Test 0
{
Qname: "svc1.ns1.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svc1.ns1.example.com. 60 IN A 192.0.1.1"),
},
},
// Existing Ingress | Test 1
{
Qname: "domain.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("domain.example.com. 60 IN A 192.0.0.1"),
},
},
// Ingress takes precedence over services | Test 2
{
Qname: "svc2.ns1.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svc2.ns1.example.com. 60 IN A 192.0.0.2"),
},
},
// Non-existing Service | Test 3
{
Qname: "svcX.ns1.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// Non-existing Ingress | Test 4
{
Qname: "d0main.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// SOA for the existing domain | Test 5
{
Qname: "domain.example.com.", Qtype: dns.TypeSOA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// Service with no public addresses | Test 6
{
Qname: "svc3.ns1.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// Real service, wrong query type | Test 7
{
Qname: "svc3.ns1.example.com.", Qtype: dns.TypeCNAME, Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// Ingress FQDN == zone | Test 8
{
Qname: "example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("example.com. 60 IN A 192.0.0.3"),
},
},
// Existing Ingress with a mix of lower and upper case letters | Test 9
{
Qname: "dOmAiN.eXamPLe.cOm.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("domain.example.com. 60 IN A 192.0.0.1"),
},
},
// Existing Service with a mix of lower and upper case letters | Test 10
{
Qname: "svC1.Ns1.exAmplE.Com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svc1.ns1.example.com. 60 IN A 192.0.1.1"),
},
},
// Existing VirtualServer | Test 11
{
Qname: "vs1.example.com", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("vs1.example.com. 60 IN A 192.0.3.1"),
},
},
// VirtualServer lookup priority over Ingress | Test 12
{
Qname: "shadow-vs.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("shadow-vs.example.com. 60 IN A 192.0.3.5"),
},
},
// basic gateway API lookup | Test 13
{
Qname: "domain.gw.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("domain.gw.example.com. 60 IN A 192.0.2.1"),
},
},
// gateway API lookup priority over Ingress and VirtualServers | Test 14
{
Qname: "shadow.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("shadow.example.com. 60 IN A 192.0.2.4"),
},
},
// Existing Service A record, but no AAAA record | Test 15
{
Qname: "svc2.ns1.example.com.", Qtype: dns.TypeAAAA, Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("example.com. 60 IN SOA dns1.kube-system.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
},
},
// Existing Service IPv6 | Test 16
{
Qname: "svc1.ns1.example.com.", Qtype: dns.TypeAAAA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.AAAA("svc1.ns1.example.com. 60 IN AAAA fd12:3456:789a:1::"),
},
},
// lookup apex NS record | Test 17
{
Qname: "example.com.", Qtype: dns.TypeNS, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.NS("example.com. 60 IN NS dns1.kube-system.example.com"),
},
Extra: []dns.RR{
test.A("dns1.kube-system.example.com. 60 IN A 192.0.1.53"),
},
},
}
var testsFallthrough = []FallthroughCase{
// Match found, fallthrough enabled | Test 0
{
Case: test.Case{Qname: "example.com.", Qtype: dns.TypeA},
FallthroughZones: []string{"."}, FallthroughExpected: false,
},
// No match found, fallthrough enabled | Test 1
{
Case: test.Case{Qname: "non-existent.example.com.", Qtype: dns.TypeA},
FallthroughZones: []string{"."}, FallthroughExpected: true,
},
// Match found, fallthrough for different zone | Test 2
{
Case: test.Case{Qname: "example.com.", Qtype: dns.TypeA},
FallthroughZones: []string{"not-example.com."}, FallthroughExpected: false,
},
// No match found, fallthrough for different zone | Test 3
{
Case: test.Case{Qname: "non-existent.example.com.", Qtype: dns.TypeA},
FallthroughZones: []string{"not-example.com."}, FallthroughExpected: false,
},
// No fallthrough on gw apex | Test 4
{
Case: test.Case{Qname: "dns1.kube-system.example.com.", Qtype: dns.TypeA},
FallthroughZones: []string{"."}, FallthroughExpected: false,
},
}
var testServiceIndexes = map[string][]netip.Addr{
"svc1.ns1": {netip.MustParseAddr("192.0.1.1"), netip.MustParseAddr("fd12:3456:789a:1::")},
"svc2.ns1": {netip.MustParseAddr("192.0.1.2")},
"svc3.ns1": {},
"dns1.kube-system": {netip.MustParseAddr("192.0.1.53")},
}
func testServiceLookup(keys []string) (results []netip.Addr) {
for _, key := range keys {
results = append(results, testServiceIndexes[strings.ToLower(key)]...)
}
return results
}
var testIngressIndexes = map[string][]netip.Addr{
"domain.example.com": {netip.MustParseAddr("192.0.0.1")},
"svc2.ns1.example.com": {netip.MustParseAddr("192.0.0.2")},
"example.com": {netip.MustParseAddr("192.0.0.3")},
"shadow.example.com": {netip.MustParseAddr("192.0.0.4")},
"shadow-vs.example.com": {netip.MustParseAddr("192.0.0.5")},
}
func testIngressLookup(keys []string) (results []netip.Addr) {
for _, key := range keys {
results = append(results, testIngressIndexes[strings.ToLower(key)]...)
}
return results
}
var testVirtualServerIndexes = map[string][]netip.Addr{
"vs1.example.com": {netip.MustParseAddr("192.0.3.1")},
"shadow.example.com": {netip.MustParseAddr("192.0.3.4")},
"shadow-vs.example.com": {netip.MustParseAddr("192.0.3.5")},
}
func testVirtualServerLookup(keys []string) (results []netip.Addr) {
for _, key := range keys {
results = append(results, testVirtualServerIndexes[strings.ToLower(key)]...)
}
return results
}
var testRouteIndexes = map[string][]netip.Addr{
"domain.gw.example.com": {netip.MustParseAddr("192.0.2.1")},
"shadow.example.com": {netip.MustParseAddr("192.0.2.4")},
}
func testRouteLookup(keys []string) (results []netip.Addr) {
for _, key := range keys {
results = append(results, testRouteIndexes[strings.ToLower(key)]...)
}
return results
}
func setupLookupFuncs() {
if resource := lookupResource("Ingress"); resource != nil {
resource.lookup = testIngressLookup
}
if resource := lookupResource("Service"); resource != nil {
resource.lookup = testServiceLookup
}
if resource := lookupResource("VirtualServer"); resource != nil {
resource.lookup = testVirtualServerLookup
}
if resource := lookupResource("HTTPRoute"); resource != nil {
resource.lookup = testRouteLookup
}
if resource := lookupResource("TLSRoute"); resource != nil {
resource.lookup = testRouteLookup
}
if resource := lookupResource("GRPCRoute"); resource != nil {
resource.lookup = testRouteLookup
}
}