-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathsync_test.go
303 lines (285 loc) · 6.99 KB
/
sync_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
package externaldns
import (
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
vsapi "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
extdnsapi "github.com/nginxinc/kubernetes-ingress/pkg/apis/externaldns/v1"
extdnsclient "github.com/nginxinc/kubernetes-ingress/pkg/client/listers/externaldns/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
// EventRecorder implements EventRecorder interface.
// It's dummy implementation purpose is for testing only.
type EventRecorder struct{}
func (EventRecorder) Event(runtime.Object, string, string, string) {}
func (EventRecorder) Eventf(runtime.Object, string, string, string, ...interface{}) {}
func (EventRecorder) AnnotatedEventf(runtime.Object, map[string]string, string, string, string, ...interface{}) {
}
func TestGetValidTargets(t *testing.T) {
t.Parallel()
tt := []struct {
name string
wantTargets extdnsapi.Targets
wantRecord string
endpoints []vsapi.ExternalEndpoint
chosenEndpoints []string
}{
{
name: "from external endpoint with IPv4",
wantTargets: extdnsapi.Targets{"10.23.4.5"},
wantRecord: "A",
endpoints: []vsapi.ExternalEndpoint{
{
IP: "10.23.4.5",
},
},
},
{
name: "from external endpoint with IPv6",
wantTargets: extdnsapi.Targets{"2001:db8:0:0:0:0:2:1"},
wantRecord: "AAAA",
endpoints: []vsapi.ExternalEndpoint{
{
IP: "2001:db8:0:0:0:0:2:1",
},
},
},
{
name: "from external endpoint with a hostname",
wantTargets: extdnsapi.Targets{"tea.com"},
wantRecord: "CNAME",
endpoints: []vsapi.ExternalEndpoint{
{
Hostname: "tea.com",
},
},
},
{
name: "from external endpoint with multiple targets",
wantTargets: extdnsapi.Targets{"10.2.3.4"},
wantRecord: "A",
endpoints: []vsapi.ExternalEndpoint{
{
IP: "2001:db8:0:0:0:0:2:1", // This IPv6 record will be ignored, as the priority is IPv4> IPv6> CNAME
},
{
IP: "10.2.3.4",
},
},
},
{
name: "from external endpoint with multiple targets",
wantTargets: extdnsapi.Targets{"1.2.3.4"},
wantRecord: "A",
endpoints: []vsapi.ExternalEndpoint{
{
IP: "10.2.3.4", // This extrenal IP will be ignored, as IPs have been chosen in the VS spec
},
},
chosenEndpoints: []string{
"1.2.3.4",
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
targets, recordType, err := getValidTargets(tc.endpoints, tc.chosenEndpoints)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(tc.wantTargets, targets) {
t.Error(cmp.Diff(tc.wantTargets, targets))
}
if recordType != tc.wantRecord {
t.Error(cmp.Diff(tc.wantRecord, recordType))
}
})
}
}
func TestSync_NotRunningOnExternalDNSDisabled(t *testing.T) {
t.Parallel()
vs := &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: false,
},
},
}
fn := SyncFnFor(nil, nil, nil)
err := fn(context.TODO(), vs)
if err != nil {
t.Errorf("want nil got %v", err)
}
}
func TestSync_ReturnsErrorOnNilExternalEndpoints(t *testing.T) {
t.Parallel()
vs := &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{},
}
rec := EventRecorder{}
fn := SyncFnFor(rec, nil, nil)
err := fn(context.TODO(), vs)
if err == nil {
t.Errorf("want error got nil")
}
}
func TestSync_ReturnsErrorOnInvalidTargetsInExternalEndpoints(t *testing.T) {
t.Parallel()
tt := []struct {
name string
input *vsapi.VirtualServer
}{
{
name: "when missing ip and Hostname",
input: &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{
ExternalEndpoints: []vsapi.ExternalEndpoint{
{
IP: "",
Hostname: "",
},
},
},
},
},
{
name: "when missing hostname",
input: &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{
ExternalEndpoints: []vsapi.ExternalEndpoint{
{
Hostname: "",
},
},
},
},
},
{
name: "when invalid ipv4 address",
input: &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{
ExternalEndpoints: []vsapi.ExternalEndpoint{
{
IP: "10.23.23..3",
Hostname: "",
},
},
},
},
},
{
name: "when invalid ipv6 address",
input: &vsapi.VirtualServer{
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{
ExternalEndpoints: []vsapi.ExternalEndpoint{
{
IP: "2001:::db8:0:0:0:0:2:1",
Hostname: "",
},
},
},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rec := EventRecorder{}
fn := SyncFnFor(rec, nil, nil)
err := fn(context.TODO(), tc.input)
if err == nil {
t.Error("want error, got nil")
}
})
}
}
type DNSEPListerExpansion struct{}
// EPNamespaceLister implements DNSEndpointNamespaceLister interface.
// It's dummy implementation of the interface to satisfy dependencies in tests.
type DNSEPNamespaceLister struct{}
func (DNSEPNamespaceLister) List(_ labels.Selector) (ret []*extdnsapi.DNSEndpoint, err error) {
return nil, nil
}
func (DNSEPNamespaceLister) Get(_ string) (*extdnsapi.DNSEndpoint, error) {
return nil, errors.New("test error")
}
// EPLister implements DNSEndpointLister interface. It's dummy
// implementation of the interface to satisfy dependencies in tests.
type DNSEPLister struct {
DNSEPListerExpansion
}
func (DNSEPLister) List(_ labels.Selector) (ret []*extdnsapi.DNSEndpoint, err error) {
return nil, nil
}
func (DNSEPLister) DNSEndpoints(_ string) extdnsclient.DNSEndpointNamespaceLister {
e := DNSEPNamespaceLister{}
return e
}
func TestSync_ReturnsErrorOnFailure(t *testing.T) {
t.Parallel()
tt := []struct {
name string
input *vsapi.VirtualServer
}{
{
name: "to retrieve host from namespace",
input: &vsapi.VirtualServer{
ObjectMeta: v1.ObjectMeta{
Namespace: "",
},
Spec: vsapi.VirtualServerSpec{
ExternalDNS: vsapi.ExternalDNS{
Enable: true,
},
},
Status: vsapi.VirtualServerStatus{
ExternalEndpoints: []vsapi.ExternalEndpoint{
{
IP: "10.10.10.20",
},
},
},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rec := EventRecorder{}
ig := make(map[string]*namespacedInformer)
nsi := namespacedInformer{extdnslister: DNSEPLister{}}
ig[""] = &nsi
fn := SyncFnFor(rec, nil, ig)
err := fn(context.TODO(), tc.input)
if err == nil {
t.Error("want error, got nil")
}
})
}
}