Skip to content

Commit 413af83

Browse files
committed
Minor updates to traffic distribution unit tests
Merge TestReconcileHints_trafficDistribution_is_PreferClose and TestReconcileHints_trafficDistribution_is_nil_or_empty together. Change the `trafficDistribution: ""` test to `trafficDistribution: Unknown`, since `""` is not actually a possible value (but we should still test that unknown values are ignored, to prevent weird skew bugs). Fill in the NodeName field in the endpoints. It's not needed yet but it will be.
1 parent af35847 commit 413af83

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

staging/src/k8s.io/endpointslice/trafficdist/trafficdist_test.go

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"k8s.io/utils/ptr"
2727
)
2828

29-
func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
29+
func TestReconcileHints(t *testing.T) {
3030
testCases := []struct {
3131
name string
3232

@@ -40,19 +40,22 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
4040
wantSlicesUnchanged []*discoveryv1.EndpointSlice
4141
}{
4242
{
43-
name: "should set same zone hints",
43+
name: "should set zone hints with PreferClose",
44+
4445
trafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose),
4546
slicesToCreate: []*discoveryv1.EndpointSlice{
4647
{
4748
Endpoints: []discoveryv1.Endpoint{
4849
{
4950
Addresses: []string{"10.0.0.1"},
5051
Zone: ptr.To("zone-a"),
52+
NodeName: ptr.To("node-1"),
5153
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
5254
},
5355
{
5456
Addresses: []string{"10.0.0.2"},
5557
Zone: ptr.To("zone-b"),
58+
NodeName: ptr.To("node-2"),
5659
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
5760
},
5861
},
@@ -62,11 +65,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
6265
{
6366
Addresses: []string{"10.0.0.3"},
6467
Zone: ptr.To("zone-a"),
68+
NodeName: ptr.To("node-3"),
6569
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
6670
},
6771
{
6872
Addresses: []string{"10.0.0.4"},
6973
Zone: ptr.To("zone-b"),
74+
NodeName: ptr.To("node-4"),
7075
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
7176
},
7277
},
@@ -78,11 +83,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
7883
{
7984
Addresses: []string{"10.0.0.5"},
8085
Zone: ptr.To("zone-a"),
86+
NodeName: ptr.To("node-5"),
8187
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
8288
},
8389
{
8490
Addresses: []string{"10.0.0.6"},
8591
Zone: ptr.To("zone-a"),
92+
NodeName: ptr.To("node-6"),
8693
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
8794
},
8895
},
@@ -92,11 +99,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
9299
{
93100
Addresses: []string{"10.0.0.7"},
94101
Zone: ptr.To("zone-b"),
102+
NodeName: ptr.To("node-7"),
95103
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
96104
},
97105
{
98106
Addresses: []string{"10.0.0.8"},
99107
Zone: ptr.To("zone-c"),
108+
NodeName: ptr.To("node-8"),
100109
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
101110
},
102111
},
@@ -108,12 +117,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
108117
{
109118
Addresses: []string{"10.0.0.1"},
110119
Zone: ptr.To("zone-a"),
120+
NodeName: ptr.To("node-1"),
111121
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
112122
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
113123
},
114124
{
115125
Addresses: []string{"10.0.0.2"},
116126
Zone: ptr.To("zone-b"),
127+
NodeName: ptr.To("node-2"),
117128
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
118129
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
119130
},
@@ -124,12 +135,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
124135
{
125136
Addresses: []string{"10.0.0.3"},
126137
Zone: ptr.To("zone-a"),
138+
NodeName: ptr.To("node-3"),
127139
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
128140
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
129141
},
130142
{
131143
Addresses: []string{"10.0.0.4"},
132144
Zone: ptr.To("zone-b"),
145+
NodeName: ptr.To("node-4"),
133146
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
134147
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
135148
},
@@ -142,12 +155,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
142155
{
143156
Addresses: []string{"10.0.0.5"},
144157
Zone: ptr.To("zone-a"),
158+
NodeName: ptr.To("node-5"),
145159
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
146160
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
147161
},
148162
{
149163
Addresses: []string{"10.0.0.6"},
150164
Zone: ptr.To("zone-a"),
165+
NodeName: ptr.To("node-6"),
151166
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
152167
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
153168
},
@@ -158,12 +173,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
158173
{
159174
Addresses: []string{"10.0.0.7"},
160175
Zone: ptr.To("zone-b"),
176+
NodeName: ptr.To("node-7"),
161177
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
162178
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
163179
},
164180
{
165181
Addresses: []string{"10.0.0.8"},
166182
Zone: ptr.To("zone-c"),
183+
NodeName: ptr.To("node-8"),
167184
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
168185
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-c"}}},
169186
},
@@ -172,14 +189,16 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
172189
},
173190
},
174191
{
175-
name: "incorrect hints should be corrected",
192+
name: "should correct incorrect hints with PreferClose",
193+
176194
trafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose),
177195
slicesToUpdate: []*discoveryv1.EndpointSlice{
178196
{
179197
Endpoints: []discoveryv1.Endpoint{
180198
{
181199
Addresses: []string{"10.0.0.1"},
182200
Zone: ptr.To("zone-a"),
201+
NodeName: ptr.To("node-1"),
183202
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
184203
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}}, // incorrect hint as per new heuristic
185204
},
@@ -192,6 +211,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
192211
{
193212
Addresses: []string{"10.0.0.2"},
194213
Zone: ptr.To("zone-b"),
214+
NodeName: ptr.To("node-2"),
195215
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
196216
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-c"}}},
197217
},
@@ -202,6 +222,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
202222
{
203223
Addresses: []string{"10.0.0.3"},
204224
Zone: ptr.To("zone-c"),
225+
NodeName: ptr.To("node-3"),
205226
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
206227
},
207228
},
@@ -213,6 +234,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
213234
{
214235
Addresses: []string{"10.0.0.1"},
215236
Zone: ptr.To("zone-a"),
237+
NodeName: ptr.To("node-1"),
216238
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
217239
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
218240
},
@@ -223,6 +245,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
223245
{
224246
Addresses: []string{"10.0.0.2"},
225247
Zone: ptr.To("zone-b"),
248+
NodeName: ptr.To("node-2"),
226249
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
227250
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
228251
},
@@ -233,6 +256,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
233256
{
234257
Addresses: []string{"10.0.0.3"},
235258
Zone: ptr.To("zone-c"),
259+
NodeName: ptr.To("node-3"),
236260
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
237261
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-c"}}},
238262
},
@@ -241,14 +265,16 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
241265
},
242266
},
243267
{
244-
name: "unready endpoints should not trigger updates",
268+
name: "unready endpoints should not trigger updates",
269+
245270
trafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose),
246271
slicesUnchanged: []*discoveryv1.EndpointSlice{
247272
{
248273
Endpoints: []discoveryv1.Endpoint{
249274
{
250275
Addresses: []string{"10.0.0.2"},
251276
Zone: ptr.To("zone-b"),
277+
NodeName: ptr.To("node-2"),
252278
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(false)}, // endpoint is not ready
253279
},
254280
},
@@ -260,59 +286,31 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
260286
{
261287
Addresses: []string{"10.0.0.2"},
262288
Zone: ptr.To("zone-b"),
289+
NodeName: ptr.To("node-2"),
263290
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(false)},
264291
},
265292
},
266293
},
267294
},
268295
},
269-
}
270-
271-
for _, tc := range testCases {
272-
t.Run(tc.name, func(t *testing.T) {
273-
gotSlicesToCreate, gotSlicesToUpdate, gotSlicesUnchanged := ReconcileHints(tc.trafficDistribution, tc.slicesToCreate, tc.slicesToUpdate, tc.slicesUnchanged)
274-
275-
if diff := cmp.Diff(tc.wantSlicesToCreate, gotSlicesToCreate, cmpopts.EquateEmpty()); diff != "" {
276-
t.Errorf("ReconcileHints(...) returned unexpected diff in 'slicesToCreate': (-want, +got)\n%v", diff)
277-
}
278-
if diff := cmp.Diff(tc.wantSlicesToUpdate, gotSlicesToUpdate, cmpopts.EquateEmpty()); diff != "" {
279-
t.Errorf("ReconcileHints(...) returned unexpected diff in 'slicesToUpdate': (-want, +got)\n%v", diff)
280-
}
281-
if diff := cmp.Diff(tc.wantSlicesUnchanged, gotSlicesUnchanged, cmpopts.EquateEmpty()); diff != "" {
282-
t.Errorf("ReconcileHints(...) returned unexpected diff in 'slicesUnchanged': (-want, +got)\n%v", diff)
283-
}
284-
})
285-
}
286-
}
287-
288-
func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
289-
testCases := []struct {
290-
name string
291-
292-
trafficDistribution *string
293-
slicesToCreate []*discoveryv1.EndpointSlice
294-
slicesToUpdate []*discoveryv1.EndpointSlice
295-
slicesUnchanged []*discoveryv1.EndpointSlice
296-
297-
wantSlicesToCreate []*discoveryv1.EndpointSlice
298-
wantSlicesToUpdate []*discoveryv1.EndpointSlice
299-
wantSlicesUnchanged []*discoveryv1.EndpointSlice
300-
}{
301296
{
302-
name: "trafficDistribution='' should remove zone hints",
303-
trafficDistribution: ptr.To(""),
297+
name: "should remove hints when trafficDistribution is unrecognized",
298+
299+
trafficDistribution: ptr.To("Unknown"),
304300
slicesToCreate: []*discoveryv1.EndpointSlice{
305301
{
306302
Endpoints: []discoveryv1.Endpoint{
307303
{
308304
Addresses: []string{"10.0.0.1"},
309305
Zone: ptr.To("zone-a"),
306+
NodeName: ptr.To("node-1"),
310307
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
311308
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
312309
},
313310
{
314311
Addresses: []string{"10.0.0.2"},
315312
Zone: ptr.To("zone-b"),
313+
NodeName: ptr.To("node-2"),
316314
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
317315
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
318316
},
@@ -323,6 +321,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
323321
{
324322
Addresses: []string{"10.0.0.3"},
325323
Zone: ptr.To("zone-a"),
324+
NodeName: ptr.To("node-3"),
326325
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
327326
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
328327
},
@@ -335,6 +334,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
335334
{
336335
Addresses: []string{"10.0.0.5"},
337336
Zone: ptr.To("zone-a"),
337+
NodeName: ptr.To("node-5"),
338338
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
339339
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
340340
},
@@ -347,11 +347,13 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
347347
{
348348
Addresses: []string{"10.0.0.1"},
349349
Zone: ptr.To("zone-a"),
350+
NodeName: ptr.To("node-1"),
350351
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
351352
},
352353
{
353354
Addresses: []string{"10.0.0.2"},
354355
Zone: ptr.To("zone-b"),
356+
NodeName: ptr.To("node-2"),
355357
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
356358
},
357359
},
@@ -361,6 +363,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
361363
{
362364
Addresses: []string{"10.0.0.3"},
363365
Zone: ptr.To("zone-a"),
366+
NodeName: ptr.To("node-3"),
364367
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
365368
},
366369
},
@@ -372,21 +375,24 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
372375
{
373376
Addresses: []string{"10.0.0.5"},
374377
Zone: ptr.To("zone-a"),
378+
NodeName: ptr.To("node-5"),
375379
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
376380
},
377381
},
378382
},
379383
},
380384
},
381385
{
382-
name: "trafficDistribution=nil should remove zone hints",
386+
name: "should remove hints when trafficDistribution is unset",
387+
383388
trafficDistribution: nil,
384389
slicesToUpdate: []*discoveryv1.EndpointSlice{
385390
{
386391
Endpoints: []discoveryv1.Endpoint{
387392
{
388393
Addresses: []string{"10.0.0.5"},
389394
Zone: ptr.To("zone-a"),
395+
NodeName: ptr.To("node-5"),
390396
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
391397
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-a"}}},
392398
},
@@ -399,6 +405,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
399405
{
400406
Addresses: []string{"10.0.0.6"},
401407
Zone: ptr.To("zone-b"),
408+
NodeName: ptr.To("node-6"),
402409
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
403410
Hints: &discoveryv1.EndpointHints{ForZones: []discoveryv1.ForZone{{Name: "zone-b"}}},
404411
},
@@ -411,6 +418,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
411418
{
412419
Addresses: []string{"10.0.0.5"},
413420
Zone: ptr.To("zone-a"),
421+
NodeName: ptr.To("node-5"),
414422
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
415423
},
416424
},
@@ -420,6 +428,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
420428
{
421429
Addresses: []string{"10.0.0.6"},
422430
Zone: ptr.To("zone-b"),
431+
NodeName: ptr.To("node-6"),
423432
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
424433
},
425434
},
@@ -452,6 +461,7 @@ func TestReconcileHints_doesNotMutateUnchangedSlices(t *testing.T) {
452461
{
453462
Addresses: []string{"10.0.0.1"},
454463
Zone: ptr.To("zone-a"),
464+
NodeName: ptr.To("node-1"),
455465
Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)},
456466
},
457467
},

0 commit comments

Comments
 (0)