@@ -74,12 +74,14 @@ func TestSyncEndpoints(t *testing.T) {
74
74
75
75
testCases := []struct {
76
76
testName string
77
+ service * v1.Service
77
78
endpoints * v1.Endpoints
78
79
endpointSlices []* discovery.EndpointSlice
79
80
expectedNumActions int
80
81
expectedNumSlices int
81
82
}{{
82
83
testName : "Endpoints with no addresses" ,
84
+ service : & v1.Service {},
83
85
endpoints : & v1.Endpoints {
84
86
Subsets : []v1.EndpointSubset {{
85
87
Ports : []v1.EndpointPort {{Port : 80 }},
@@ -90,6 +92,7 @@ func TestSyncEndpoints(t *testing.T) {
90
92
expectedNumSlices : 0 ,
91
93
}, {
92
94
testName : "Endpoints with skip label true" ,
95
+ service : & v1.Service {},
93
96
endpoints : & v1.Endpoints {
94
97
ObjectMeta : metav1.ObjectMeta {
95
98
Labels : map [string ]string {discovery .LabelSkipMirror : "true" },
@@ -104,6 +107,7 @@ func TestSyncEndpoints(t *testing.T) {
104
107
expectedNumSlices : 0 ,
105
108
}, {
106
109
testName : "Endpoints with skip label false" ,
110
+ service : & v1.Service {},
107
111
endpoints : & v1.Endpoints {
108
112
ObjectMeta : metav1.ObjectMeta {
109
113
Labels : map [string ]string {discovery .LabelSkipMirror : "false" },
@@ -116,8 +120,37 @@ func TestSyncEndpoints(t *testing.T) {
116
120
endpointSlices : []* discovery.EndpointSlice {},
117
121
expectedNumActions : 1 ,
118
122
expectedNumSlices : 1 ,
123
+ }, {
124
+ testName : "Endpoints with missing Service" ,
125
+ service : nil ,
126
+ endpoints : & v1.Endpoints {
127
+ Subsets : []v1.EndpointSubset {{
128
+ Ports : []v1.EndpointPort {{Port : 80 }},
129
+ Addresses : []v1.EndpointAddress {{IP : "10.0.0.1" }},
130
+ }},
131
+ },
132
+ endpointSlices : []* discovery.EndpointSlice {},
133
+ expectedNumActions : 0 ,
134
+ expectedNumSlices : 0 ,
135
+ }, {
136
+ testName : "Endpoints with Service with selector specified" ,
137
+ service : & v1.Service {
138
+ Spec : v1.ServiceSpec {
139
+ Selector : map [string ]string {"foo" : "bar" },
140
+ },
141
+ },
142
+ endpoints : & v1.Endpoints {
143
+ Subsets : []v1.EndpointSubset {{
144
+ Ports : []v1.EndpointPort {{Port : 80 }},
145
+ Addresses : []v1.EndpointAddress {{IP : "10.0.0.1" }},
146
+ }},
147
+ },
148
+ endpointSlices : []* discovery.EndpointSlice {},
149
+ expectedNumActions : 0 ,
150
+ expectedNumSlices : 0 ,
119
151
}, {
120
152
testName : "Existing EndpointSlices that need to be cleaned up" ,
153
+ service : & v1.Service {},
121
154
endpoints : & v1.Endpoints {
122
155
Subsets : []v1.EndpointSubset {{
123
156
Ports : []v1.EndpointPort {{Port : 80 }},
@@ -136,6 +169,7 @@ func TestSyncEndpoints(t *testing.T) {
136
169
expectedNumSlices : 0 ,
137
170
}, {
138
171
testName : "Existing EndpointSlices managed by a different controller, no addresses to sync" ,
172
+ service : & v1.Service {},
139
173
endpoints : & v1.Endpoints {
140
174
Subsets : []v1.EndpointSubset {{
141
175
Ports : []v1.EndpointPort {{Port : 80 }},
@@ -154,6 +188,7 @@ func TestSyncEndpoints(t *testing.T) {
154
188
expectedNumSlices : 0 ,
155
189
}, {
156
190
testName : "Endpoints with 1000 addresses" ,
191
+ service : & v1.Service {},
157
192
endpoints : & v1.Endpoints {
158
193
Subsets : []v1.EndpointSubset {{
159
194
Ports : []v1.EndpointPort {{Port : 80 }},
@@ -165,6 +200,7 @@ func TestSyncEndpoints(t *testing.T) {
165
200
expectedNumSlices : 1 ,
166
201
}, {
167
202
testName : "Endpoints with 1001 addresses - 1 should not be mirrored" ,
203
+ service : & v1.Service {},
168
204
endpoints : & v1.Endpoints {
169
205
Subsets : []v1.EndpointSubset {{
170
206
Ports : []v1.EndpointPort {{Port : 80 }},
@@ -182,10 +218,11 @@ func TestSyncEndpoints(t *testing.T) {
182
218
tc .endpoints .Name = endpointsName
183
219
tc .endpoints .Namespace = namespace
184
220
esController .endpointsStore .Add (tc .endpoints )
185
- esController .serviceStore .Add (& v1.Service {ObjectMeta : metav1.ObjectMeta {
186
- Name : endpointsName ,
187
- Namespace : namespace ,
188
- }})
221
+ if tc .service != nil {
222
+ tc .service .Name = endpointsName
223
+ tc .service .Namespace = namespace
224
+ esController .serviceStore .Add (tc .service )
225
+ }
189
226
190
227
for _ , epSlice := range tc .endpointSlices {
191
228
epSlice .Namespace = namespace
@@ -214,102 +251,51 @@ func TestSyncEndpoints(t *testing.T) {
214
251
}
215
252
216
253
func TestShouldMirror (t * testing.T ) {
217
- svcWithSelector := & v1.Service {
218
- ObjectMeta : metav1.ObjectMeta {
219
- Name : "with-selector" ,
220
- Namespace : "example1" ,
221
- },
222
- Spec : v1.ServiceSpec {
223
- Selector : map [string ]string {"with" : "selector" },
224
- },
225
- }
226
- svcWithoutSelector := & v1.Service {
227
- ObjectMeta : metav1.ObjectMeta {
228
- Name : "without-selector" ,
229
- Namespace : "example1" ,
230
- },
231
- Spec : v1.ServiceSpec {},
232
- }
233
-
234
254
testCases := []struct {
235
255
testName string
236
256
endpoints * v1.Endpoints
237
- service * v1.Service
238
257
shouldMirror bool
239
258
}{{
240
- testName : "Service without selector with matching endpoints" ,
241
- service : svcWithoutSelector ,
259
+ testName : "Standard Endpoints" ,
242
260
endpoints : & v1.Endpoints {
243
261
ObjectMeta : metav1.ObjectMeta {
244
- Name : svcWithoutSelector .Name ,
245
- Namespace : svcWithoutSelector .Namespace ,
262
+ Name : "test-endpoints" ,
246
263
},
247
264
},
248
265
shouldMirror : true ,
249
266
}, {
250
- testName : "Service without selector, matching Endpoints with skip-mirror=true" ,
251
- service : svcWithoutSelector ,
267
+ testName : "Endpoints with skip-mirror=true" ,
252
268
endpoints : & v1.Endpoints {
253
269
ObjectMeta : metav1.ObjectMeta {
254
- Name : svcWithSelector .Name ,
255
- Namespace : svcWithSelector .Namespace ,
270
+ Name : "test-endpoints" ,
256
271
Labels : map [string ]string {
257
272
discovery .LabelSkipMirror : "true" ,
258
273
},
259
274
},
260
275
},
261
276
shouldMirror : false ,
262
277
}, {
263
- testName : "Service without selector, matching Endpoints with skip-mirror=invalid" ,
264
- service : svcWithoutSelector ,
278
+ testName : "Endpoints with skip-mirror=invalid" ,
265
279
endpoints : & v1.Endpoints {
266
280
ObjectMeta : metav1.ObjectMeta {
267
- Name : svcWithoutSelector .Name ,
268
- Namespace : svcWithoutSelector .Namespace ,
281
+ Name : "test-endpoints" ,
269
282
Labels : map [string ]string {
270
283
discovery .LabelSkipMirror : "invalid" ,
271
284
},
272
285
},
273
286
},
274
287
shouldMirror : true ,
275
288
}, {
276
- testName : "Service without selector, matching Endpoints with leader election annotation" ,
277
- service : svcWithoutSelector ,
289
+ testName : "Endpoints with leader election annotation" ,
278
290
endpoints : & v1.Endpoints {
279
291
ObjectMeta : metav1.ObjectMeta {
280
- Name : svcWithSelector .Name ,
281
- Namespace : svcWithSelector .Namespace ,
292
+ Name : "test-endpoints" ,
282
293
Annotations : map [string ]string {
283
294
resourcelock .LeaderElectionRecordAnnotationKey : "" ,
284
295
},
285
296
},
286
297
},
287
298
shouldMirror : false ,
288
- }, {
289
- testName : "Service without selector, matching Endpoints without skip label in different namespace" ,
290
- service : svcWithSelector ,
291
- endpoints : & v1.Endpoints {
292
- ObjectMeta : metav1.ObjectMeta {
293
- Name : svcWithSelector .Name ,
294
- Namespace : svcWithSelector .Namespace + "different" ,
295
- },
296
- },
297
- shouldMirror : false ,
298
- }, {
299
- testName : "Service without selector or matching endpoints" ,
300
- service : svcWithoutSelector ,
301
- endpoints : nil ,
302
- shouldMirror : false ,
303
- }, {
304
- testName : "Endpoints without matching Service" ,
305
- service : nil ,
306
- endpoints : & v1.Endpoints {
307
- ObjectMeta : metav1.ObjectMeta {
308
- Name : svcWithoutSelector .Name ,
309
- Namespace : svcWithoutSelector .Namespace ,
310
- },
311
- },
312
- shouldMirror : false ,
313
299
}}
314
300
315
301
for _ , tc := range testCases {
@@ -323,13 +309,6 @@ func TestShouldMirror(t *testing.T) {
323
309
}
324
310
}
325
311
326
- if tc .service != nil {
327
- err := c .serviceStore .Add (tc .service )
328
- if err != nil {
329
- t .Fatalf ("Error adding Service to store: %v" , err )
330
- }
331
- }
332
-
333
312
shouldMirror := c .shouldMirror (tc .endpoints )
334
313
335
314
if shouldMirror != tc .shouldMirror {
0 commit comments