@@ -25,11 +25,9 @@ import (
25
25
"testing"
26
26
"time"
27
27
28
- availabilitymetrics "k8s.io/kube-aggregator/pkg/controllers/status/metrics"
29
- "k8s.io/utils/pointer"
30
-
31
28
v1 "k8s.io/api/core/v1"
32
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "k8s.io/apimachinery/pkg/runtime"
33
31
"k8s.io/apimachinery/pkg/util/dump"
34
32
v1listers "k8s.io/client-go/listers/core/v1"
35
33
clienttesting "k8s.io/client-go/testing"
@@ -39,11 +37,13 @@ import (
39
37
"k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/fake"
40
38
apiregistrationclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1"
41
39
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/v1"
40
+ availabilitymetrics "k8s.io/kube-aggregator/pkg/controllers/status/metrics"
41
+ "k8s.io/utils/ptr"
42
42
)
43
43
44
44
const (
45
- testServicePort = 1234
46
- testServicePortName = "testPort"
45
+ testServicePort int32 = 1234
46
+ testServicePortName = "testPort"
47
47
)
48
48
49
49
func newEndpoints (namespace , name string ) * v1.Endpoints {
@@ -100,13 +100,18 @@ func newRemoteAPIService(name string) *apiregistration.APIService {
100
100
Service : & apiregistration.ServiceReference {
101
101
Namespace : "foo" ,
102
102
Name : "bar" ,
103
- Port : pointer . Int32Ptr (testServicePort ),
103
+ Port : ptr . To (testServicePort ),
104
104
},
105
105
},
106
106
}
107
107
}
108
108
109
- func setupAPIServices (apiServices []* apiregistration.APIService ) (* AvailableConditionController , * fake.Clientset ) {
109
+ type T interface {
110
+ Fatalf (format string , args ... interface {})
111
+ Errorf (format string , args ... interface {})
112
+ }
113
+
114
+ func setupAPIServices (t T , apiServices []runtime.Object ) (* AvailableConditionController , * fake.Clientset ) {
110
115
fakeClient := fake .NewSimpleClientset ()
111
116
apiServiceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
112
117
serviceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
@@ -118,7 +123,9 @@ func setupAPIServices(apiServices []*apiregistration.APIService) (*AvailableCond
118
123
defer testServer .Close ()
119
124
120
125
for _ , o := range apiServices {
121
- apiServiceIndexer .Add (o )
126
+ if err := apiServiceIndexer .Add (o ); err != nil {
127
+ t .Fatalf ("failed to add APIService: %v" , err )
128
+ }
122
129
}
123
130
124
131
c := AvailableConditionController {
@@ -145,7 +152,7 @@ func setupAPIServices(apiServices []*apiregistration.APIService) (*AvailableCond
145
152
func BenchmarkBuildCache (b * testing.B ) {
146
153
apiServiceName := "remote.group"
147
154
// model 1 APIService pointing at a given service, and 30 pointing at local group/versions
148
- apiServices := []* apiregistration. APIService {newRemoteAPIService (apiServiceName )}
155
+ apiServices := []runtime. Object {newRemoteAPIService (apiServiceName )}
149
156
for i := 0 ; i < 30 ; i ++ {
150
157
apiServices = append (apiServices , newLocalAPIService (fmt .Sprintf ("local.group%d" , i )))
151
158
}
@@ -154,7 +161,7 @@ func BenchmarkBuildCache(b *testing.B) {
154
161
for i := 0 ; i < 100 ; i ++ {
155
162
services = append (services , newService ("foo" , fmt .Sprintf ("bar%d" , i ), testServicePort , testServicePortName ))
156
163
}
157
- c , _ := setupAPIServices (apiServices )
164
+ c , _ := setupAPIServices (b , apiServices )
158
165
b .ReportAllocs ()
159
166
b .ResetTimer ()
160
167
for n := 1 ; n <= b .N ; n ++ {
@@ -175,7 +182,7 @@ func TestBuildCache(t *testing.T) {
175
182
name string
176
183
177
184
apiServiceName string
178
- apiServices []* apiregistration. APIService
185
+ apiServices []runtime. Object
179
186
services []* v1.Service
180
187
endpoints []* v1.Endpoints
181
188
@@ -184,13 +191,13 @@ func TestBuildCache(t *testing.T) {
184
191
{
185
192
name : "api service" ,
186
193
apiServiceName : "remote.group" ,
187
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
194
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
188
195
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
189
196
},
190
197
}
191
198
for _ , tc := range tests {
192
199
t .Run (tc .name , func (t * testing.T ) {
193
- c , fakeClient := setupAPIServices (tc .apiServices )
200
+ c , fakeClient := setupAPIServices (t , tc .apiServices )
194
201
for _ , svc := range tc .services {
195
202
c .addService (svc )
196
203
}
@@ -210,18 +217,19 @@ func TestSync(t *testing.T) {
210
217
name string
211
218
212
219
apiServiceName string
213
- apiServices []* apiregistration. APIService
220
+ apiServices []runtime. Object
214
221
services []* v1.Service
215
222
endpoints []* v1.Endpoints
216
223
backendStatus int
217
224
backendLocation string
218
225
219
226
expectedAvailability apiregistration.APIServiceCondition
227
+ expectedSyncError string
220
228
}{
221
229
{
222
230
name : "local" ,
223
231
apiServiceName : "local.group" ,
224
- apiServices : []* apiregistration. APIService {newLocalAPIService ("local.group" )},
232
+ apiServices : []runtime. Object {newLocalAPIService ("local.group" )},
225
233
backendStatus : http .StatusOK ,
226
234
expectedAvailability : apiregistration.APIServiceCondition {
227
235
Type : apiregistration .Available ,
@@ -233,7 +241,7 @@ func TestSync(t *testing.T) {
233
241
{
234
242
name : "no service" ,
235
243
apiServiceName : "remote.group" ,
236
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
244
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
237
245
services : []* v1.Service {newService ("foo" , "not-bar" , testServicePort , testServicePortName )},
238
246
backendStatus : http .StatusOK ,
239
247
expectedAvailability : apiregistration.APIServiceCondition {
@@ -246,7 +254,7 @@ func TestSync(t *testing.T) {
246
254
{
247
255
name : "service on bad port" ,
248
256
apiServiceName : "remote.group" ,
249
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
257
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
250
258
services : []* v1.Service {{
251
259
ObjectMeta : metav1.ObjectMeta {Namespace : "foo" , Name : "bar" },
252
260
Spec : v1.ServiceSpec {
@@ -268,7 +276,7 @@ func TestSync(t *testing.T) {
268
276
{
269
277
name : "no endpoints" ,
270
278
apiServiceName : "remote.group" ,
271
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
279
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
272
280
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
273
281
backendStatus : http .StatusOK ,
274
282
expectedAvailability : apiregistration.APIServiceCondition {
@@ -281,7 +289,7 @@ func TestSync(t *testing.T) {
281
289
{
282
290
name : "missing endpoints" ,
283
291
apiServiceName : "remote.group" ,
284
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
292
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
285
293
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
286
294
endpoints : []* v1.Endpoints {newEndpoints ("foo" , "bar" )},
287
295
backendStatus : http .StatusOK ,
@@ -295,7 +303,7 @@ func TestSync(t *testing.T) {
295
303
{
296
304
name : "wrong endpoint port name" ,
297
305
apiServiceName : "remote.group" ,
298
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
306
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
299
307
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
300
308
endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , "wrongName" )},
301
309
backendStatus : http .StatusOK ,
@@ -309,7 +317,7 @@ func TestSync(t *testing.T) {
309
317
{
310
318
name : "remote" ,
311
319
apiServiceName : "remote.group" ,
312
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
320
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
313
321
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
314
322
endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
315
323
backendStatus : http .StatusOK ,
@@ -323,7 +331,7 @@ func TestSync(t *testing.T) {
323
331
{
324
332
name : "remote-bad-return" ,
325
333
apiServiceName : "remote.group" ,
326
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
334
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
327
335
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
328
336
endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
329
337
backendStatus : http .StatusForbidden ,
@@ -333,11 +341,12 @@ func TestSync(t *testing.T) {
333
341
Reason : "FailedDiscoveryCheck" ,
334
342
Message : `failing or missing response from` ,
335
343
},
344
+ expectedSyncError : "failing or missing response from" ,
336
345
},
337
346
{
338
347
name : "remote-redirect" ,
339
348
apiServiceName : "remote.group" ,
340
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
349
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
341
350
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
342
351
endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
343
352
backendStatus : http .StatusFound ,
@@ -348,11 +357,12 @@ func TestSync(t *testing.T) {
348
357
Reason : "FailedDiscoveryCheck" ,
349
358
Message : `failing or missing response from` ,
350
359
},
360
+ expectedSyncError : "failing or missing response from" ,
351
361
},
352
362
{
353
363
name : "remote-304" ,
354
364
apiServiceName : "remote.group" ,
355
- apiServices : []* apiregistration. APIService {newRemoteAPIService ("remote.group" )},
365
+ apiServices : []runtime. Object {newRemoteAPIService ("remote.group" )},
356
366
services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
357
367
endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
358
368
backendStatus : http .StatusNotModified ,
@@ -362,12 +372,13 @@ func TestSync(t *testing.T) {
362
372
Reason : "FailedDiscoveryCheck" ,
363
373
Message : `failing or missing response from` ,
364
374
},
375
+ expectedSyncError : "failing or missing response from" ,
365
376
},
366
377
}
367
378
368
379
for _ , tc := range tests {
369
380
t .Run (tc .name , func (t * testing.T ) {
370
- fakeClient := fake .NewSimpleClientset ()
381
+ fakeClient := fake .NewSimpleClientset (tc . apiServices ... )
371
382
apiServiceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
372
383
serviceIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
373
384
endpointsIndexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
@@ -398,7 +409,16 @@ func TestSync(t *testing.T) {
398
409
proxyCurrentCertKeyContent : func () ([]byte , []byte ) { return emptyCert (), emptyCert () },
399
410
metrics : availabilitymetrics .New (),
400
411
}
401
- c .sync (tc .apiServiceName )
412
+ err := c .sync (tc .apiServiceName )
413
+ if tc .expectedSyncError != "" {
414
+ if err == nil {
415
+ t .Fatalf ("%v expected error with %q, got none" , tc .name , tc .expectedSyncError )
416
+ } else if ! strings .Contains (err .Error (), tc .expectedSyncError ) {
417
+ t .Fatalf ("%v expected error with %q, got %q" , tc .name , tc .expectedSyncError , err .Error ())
418
+ }
419
+ } else if err != nil {
420
+ t .Fatalf ("%v unexpected sync error: %v" , tc .name , err )
421
+ }
402
422
403
423
// ought to have one action writing status
404
424
if e , a := 1 , len (fakeClient .Actions ()); e != a {
@@ -445,19 +465,23 @@ func TestUpdateAPIServiceStatus(t *testing.T) {
445
465
foo := & apiregistration.APIService {Status : apiregistration.APIServiceStatus {Conditions : []apiregistration.APIServiceCondition {{Type : "foo" }}}}
446
466
bar := & apiregistration.APIService {Status : apiregistration.APIServiceStatus {Conditions : []apiregistration.APIServiceCondition {{Type : "bar" }}}}
447
467
448
- fakeClient := fake .NewSimpleClientset ()
468
+ fakeClient := fake .NewSimpleClientset (foo )
449
469
c := AvailableConditionController {
450
470
apiServiceClient : fakeClient .ApiregistrationV1 ().(apiregistrationclient.APIServicesGetter ),
451
471
metrics : availabilitymetrics .New (),
452
472
}
453
473
454
- c .updateAPIServiceStatus (foo , foo )
474
+ if _ , err := c .updateAPIServiceStatus (foo , foo ); err != nil {
475
+ t .Fatalf ("unexpected error: %v" , err )
476
+ }
455
477
if e , a := 0 , len (fakeClient .Actions ()); e != a {
456
478
t .Error (dump .Pretty (fakeClient .Actions ()))
457
479
}
458
480
459
481
fakeClient .ClearActions ()
460
- c .updateAPIServiceStatus (foo , bar )
482
+ if _ , err := c .updateAPIServiceStatus (foo , bar ); err != nil {
483
+ t .Fatalf ("unexpected error: %v" , err )
484
+ }
461
485
if e , a := 1 , len (fakeClient .Actions ()); e != a {
462
486
t .Error (dump .Pretty (fakeClient .Actions ()))
463
487
}
0 commit comments