@@ -25,8 +25,10 @@ import (
25
25
"github.com/golang/glog"
26
26
27
27
"k8s.io/api/core/v1"
28
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
"k8s.io/apimachinery/pkg/runtime"
31
+ "k8s.io/apimachinery/pkg/runtime/schema"
30
32
"k8s.io/apimachinery/pkg/util/wait"
31
33
"k8s.io/apiserver/pkg/admission"
32
34
core "k8s.io/client-go/testing"
@@ -52,22 +54,34 @@ func newHandlerForTest(internalClient internalclientset.Interface) (admission.In
52
54
}
53
55
54
56
// newFakeServiceCatalogClientForTest creates a fake clientset that returns a
55
- // ClusterServiceClassList with the given ClusterServiceClass as the single list item.
56
- func newFakeServiceCatalogClientForTest (sc * servicecatalog.ClusterServiceClass , sps []* servicecatalog.ClusterServicePlan , classFilter string ) * fake.Clientset {
57
+ // ClusterServiceClassList with the given ClusterServiceClass as the single list item
58
+ // and list of ClusterServicePlan objects.
59
+ // If classFilter is provided (as in not ""), then only sps with the
60
+ // Spec.ClusterServiceClassRef.Name equaling that string will be added to the list.
61
+ func newFakeServiceCatalogClientForTest (sc * servicecatalog.ClusterServiceClass , sps []* servicecatalog.ClusterServicePlan , classFilter string , useGet bool ) * fake.Clientset {
57
62
fakeClient := & fake.Clientset {}
58
63
59
- // react to the given service classes
60
- scList := & servicecatalog.ClusterServiceClassList {
61
- ListMeta : metav1.ListMeta {
62
- ResourceVersion : "1" ,
63
- }}
64
- if sc != nil {
65
- scList .Items = append (scList .Items , * sc )
66
- }
64
+ if useGet {
65
+ fakeClient .AddReactor ("get" , "clusterserviceclasses" , func (action core.Action ) (bool , runtime.Object , error ) {
66
+ if sc != nil {
67
+ return true , sc , nil
68
+ }
69
+ return true , nil , apierrors .NewNotFound (schema.GroupResource {}, "" )
70
+ })
71
+ } else {
72
+ // react to the given service class list
73
+ scList := & servicecatalog.ClusterServiceClassList {
74
+ ListMeta : metav1.ListMeta {
75
+ ResourceVersion : "1" ,
76
+ }}
77
+ if sc != nil {
78
+ scList .Items = append (scList .Items , * sc )
79
+ }
67
80
68
- fakeClient .AddReactor ("list" , "clusterserviceclasses" , func (action core.Action ) (bool , runtime.Object , error ) {
69
- return true , scList , nil
70
- })
81
+ fakeClient .AddReactor ("list" , "clusterserviceclasses" , func (action core.Action ) (bool , runtime.Object , error ) {
82
+ return true , scList , nil
83
+ })
84
+ }
71
85
72
86
// react to the given plans
73
87
spList := & servicecatalog.ClusterServicePlanList {
@@ -166,10 +180,13 @@ func TestWithListFailure(t *testing.T) {
166
180
} else if ! strings .Contains (err .Error (), "simulated test failure" ) {
167
181
t .Errorf ("did not find expected error, got %q" , err )
168
182
}
183
+ assertPlanReference (t ,
184
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foo" },
185
+ instance .Spec .PlanReference )
169
186
}
170
187
171
188
func TestWithPlanWorks (t * testing.T ) {
172
- fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" )
189
+ fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" , false /* do not use get */ )
173
190
handler , informerFactory , err := newHandlerForTest (fakeClient )
174
191
if err != nil {
175
192
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -188,10 +205,13 @@ func TestWithPlanWorks(t *testing.T) {
188
205
}
189
206
t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
190
207
}
208
+ assertPlanReference (t ,
209
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foo" , ExternalClusterServicePlanName : "bar" },
210
+ instance .Spec .PlanReference )
191
211
}
192
212
193
213
func TestWithNoPlanFailsWithNoClusterServiceClass (t * testing.T ) {
194
- fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" )
214
+ fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" , false /* do not use get */ )
195
215
handler , informerFactory , err := newHandlerForTest (fakeClient )
196
216
if err != nil {
197
217
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -207,14 +227,17 @@ func TestWithNoPlanFailsWithNoClusterServiceClass(t *testing.T) {
207
227
} else if ! strings .Contains (err .Error (), "does not exist, can not figure" ) {
208
228
t .Errorf ("did not find expected error, got %q" , err )
209
229
}
230
+ assertPlanReference (t ,
231
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foobar" },
232
+ instance .Spec .PlanReference )
210
233
}
211
234
212
235
// checks that the defaulting action works when a service class only provides a single plan.
213
236
func TestWithNoPlanWorksWithSinglePlan (t * testing.T ) {
214
237
sc := newClusterServiceClass ("foo-id" , "foo" )
215
238
sps := newClusterServicePlans (1 , false )
216
239
glog .V (4 ).Infof ("Created Service as %+v" , sc )
217
- fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" )
240
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" , false /* do not use get */ )
218
241
219
242
handler , informerFactory , err := newHandlerForTest (fakeClient )
220
243
if err != nil {
@@ -233,18 +256,17 @@ func TestWithNoPlanWorksWithSinglePlan(t *testing.T) {
233
256
}
234
257
t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
235
258
}
236
- // Make sure the ServiceInstance has been mutated to include the service plan name
237
- if instance .Spec .ExternalClusterServicePlanName != "bar" {
238
- t .Errorf ("PlanName was not modified for the default plan" )
239
- }
259
+ assertPlanReference (t ,
260
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foo" , ExternalClusterServicePlanName : "bar" },
261
+ instance .Spec .PlanReference )
240
262
}
241
263
242
264
// checks that defaulting fails when there are multiple plans to choose from.
243
265
func TestWithNoPlanFailsWithMultiplePlans (t * testing.T ) {
244
266
sc := newClusterServiceClass ("foo-id" , "foo" )
245
267
sps := newClusterServicePlans (2 , false )
246
268
glog .V (4 ).Infof ("Created Service as %+v" , sc )
247
- fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" )
269
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" , false /* do not use get */ )
248
270
handler , informerFactory , err := newHandlerForTest (fakeClient )
249
271
if err != nil {
250
272
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -261,6 +283,9 @@ func TestWithNoPlanFailsWithMultiplePlans(t *testing.T) {
261
283
} else if ! strings .Contains (err .Error (), "has more than one plan, PlanName must be" ) {
262
284
t .Errorf ("did not find expected error, got %q" , err )
263
285
}
286
+ assertPlanReference (t ,
287
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foo" },
288
+ instance .Spec .PlanReference )
264
289
}
265
290
266
291
// checks that defaulting succeeds when there are multiple plans but only a
@@ -270,7 +295,7 @@ func TestWithNoPlanSucceedsWithMultiplePlansFromDifferentClasses(t *testing.T) {
270
295
sps := newClusterServicePlans (2 , true )
271
296
glog .V (4 ).Infof ("Created Service as %+v" , sc )
272
297
classFilter := "test-serviceclass"
273
- fakeClient := newFakeServiceCatalogClientForTest (sc , sps , classFilter )
298
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , classFilter , false /* do not use get */ )
274
299
handler , informerFactory , err := newHandlerForTest (fakeClient )
275
300
if err != nil {
276
301
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -288,8 +313,122 @@ func TestWithNoPlanSucceedsWithMultiplePlansFromDifferentClasses(t *testing.T) {
288
313
}
289
314
t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
290
315
}
291
- // Make sure the ServiceInstance has been mutated to include the service plan name
292
- if instance .Spec .ExternalClusterServicePlanName != "bar" {
293
- t .Errorf ("PlanName was not modified for the default plan" )
316
+ assertPlanReference (t ,
317
+ servicecatalog.PlanReference {ExternalClusterServiceClassName : "foo" , ExternalClusterServicePlanName : "bar" },
318
+ instance .Spec .PlanReference )
319
+ }
320
+
321
+ func TestWithNoPlanFailsWithNoClusterServiceClassK8SName (t * testing.T ) {
322
+ fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" , true /* use get */ )
323
+ handler , informerFactory , err := newHandlerForTest (fakeClient )
324
+ if err != nil {
325
+ t .Errorf ("unexpected error initializing handler: %v" , err )
326
+ }
327
+ informerFactory .Start (wait .NeverStop )
328
+
329
+ instance := newServiceInstance ("dummy" )
330
+ instance .Spec .ClusterServiceClassName = "foobar-id"
331
+
332
+ err = handler .Admit (admission .NewAttributesRecord (& instance , nil , servicecatalog .Kind ("ServiceInstance" ).WithVersion ("version" ), instance .Namespace , instance .Name , servicecatalog .Resource ("serviceinstances" ).WithVersion ("version" ), "" , admission .Create , nil ))
333
+ if err == nil {
334
+ t .Errorf ("unexpected success with no plan specified and no serviceclass existing" )
335
+ } else if ! strings .Contains (err .Error (), "does not exist, can not figure" ) {
336
+ t .Errorf ("did not find expected error, got %q" , err )
337
+ }
338
+ assertPlanReference (t ,
339
+ servicecatalog.PlanReference {ClusterServiceClassName : "foobar-id" },
340
+ instance .Spec .PlanReference )
341
+ }
342
+
343
+ // checks that the defaulting action works when a service class only provides a single plan.
344
+ func TestWithNoPlanWorksWithSinglePlanK8SName (t * testing.T ) {
345
+ sc := newClusterServiceClass ("foo-id" , "foo" )
346
+ sps := newClusterServicePlans (1 , false )
347
+ glog .V (4 ).Infof ("Created Service as %+v" , sc )
348
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" , true /* use get */ )
349
+
350
+ handler , informerFactory , err := newHandlerForTest (fakeClient )
351
+ if err != nil {
352
+ t .Errorf ("unexpected error initializing handler: %v" , err )
353
+ }
354
+ informerFactory .Start (wait .NeverStop )
355
+
356
+ instance := newServiceInstance ("dummy" )
357
+ instance .Spec .ClusterServiceClassName = "foo-id"
358
+
359
+ err = handler .Admit (admission .NewAttributesRecord (& instance , nil , servicecatalog .Kind ("ServiceInstance" ).WithVersion ("version" ), instance .Namespace , instance .Name , servicecatalog .Resource ("serviceinstances" ).WithVersion ("version" ), "" , admission .Create , nil ))
360
+ if err != nil {
361
+ actions := ""
362
+ for _ , action := range fakeClient .Actions () {
363
+ actions = actions + action .GetVerb () + ":" + action .GetResource ().Resource + ":" + action .GetSubresource () + ", "
364
+ }
365
+ t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
366
+ }
367
+ assertPlanReference (t ,
368
+ servicecatalog.PlanReference {ClusterServiceClassName : "foo-id" , ClusterServicePlanName : "bar-id" },
369
+ instance .Spec .PlanReference )
370
+ }
371
+
372
+ // checks that defaulting fails when there are multiple plans to choose from.
373
+ func TestWithNoPlanFailsWithMultiplePlansK8SName (t * testing.T ) {
374
+ sc := newClusterServiceClass ("foo-id" , "foo" )
375
+ sps := newClusterServicePlans (2 , false )
376
+ glog .V (4 ).Infof ("Created Service as %+v" , sc )
377
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" , true /* use get */ )
378
+ handler , informerFactory , err := newHandlerForTest (fakeClient )
379
+ if err != nil {
380
+ t .Errorf ("unexpected error initializing handler: %v" , err )
381
+ }
382
+ informerFactory .Start (wait .NeverStop )
383
+
384
+ instance := newServiceInstance ("dummy" )
385
+ instance .Spec .ClusterServiceClassName = "foo-id"
386
+
387
+ err = handler .Admit (admission .NewAttributesRecord (& instance , nil , servicecatalog .Kind ("ServiceInstance" ).WithVersion ("version" ), instance .Namespace , instance .Name , servicecatalog .Resource ("serviceinstances" ).WithVersion ("version" ), "" , admission .Create , nil ))
388
+ if err == nil {
389
+ t .Errorf ("unexpected success with no plan specified and no serviceclass existing" )
390
+ return
391
+ } else if ! strings .Contains (err .Error (), "has more than one plan, PlanName must be" ) {
392
+ t .Errorf ("did not find expected error, got %q" , err )
393
+ }
394
+ assertPlanReference (t ,
395
+ servicecatalog.PlanReference {ClusterServiceClassName : "foo-id" },
396
+ instance .Spec .PlanReference )
397
+ }
398
+
399
+ // checks that defaulting succeeds when there are multiple plans but only a
400
+ // single plan for the specified Service Class
401
+ func TestWithNoPlanSucceedsWithMultiplePlansFromDifferentClassesK8SName (t * testing.T ) {
402
+ sc := newClusterServiceClass ("foo-id" , "foo" )
403
+ sps := newClusterServicePlans (2 , true )
404
+ glog .V (4 ).Infof ("Created Service as %+v" , sc )
405
+ classFilter := "test-serviceclass"
406
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , classFilter , true /* use get */ )
407
+ handler , informerFactory , err := newHandlerForTest (fakeClient )
408
+ if err != nil {
409
+ t .Errorf ("unexpected error initializing handler: %v" , err )
410
+ }
411
+ informerFactory .Start (wait .NeverStop )
412
+
413
+ instance := newServiceInstance ("dummy" )
414
+ instance .Spec .ClusterServiceClassName = "foo-id"
415
+
416
+ err = handler .Admit (admission .NewAttributesRecord (& instance , nil , servicecatalog .Kind ("ServiceInstance" ).WithVersion ("version" ), instance .Namespace , instance .Name , servicecatalog .Resource ("serviceinstances" ).WithVersion ("version" ), "" , admission .Create , nil ))
417
+ if err != nil {
418
+ actions := ""
419
+ for _ , action := range fakeClient .Actions () {
420
+ actions = actions + action .GetVerb () + ":" + action .GetResource ().Resource + ":" + action .GetSubresource () + ", "
421
+ }
422
+ t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
423
+ }
424
+ assertPlanReference (t ,
425
+ servicecatalog.PlanReference {ClusterServiceClassName : "foo-id" , ClusterServicePlanName : "bar-id" },
426
+ instance .Spec .PlanReference )
427
+ }
428
+
429
+ // Compares expected and actual PlanReferences and reports with Errorf of any mismatch
430
+ func assertPlanReference (t * testing.T , expected servicecatalog.PlanReference , actual servicecatalog.PlanReference ) {
431
+ if expected != actual {
432
+ t .Errorf ("PlanReference was not as expected: %+v actual: %+v" , expected , actual )
294
433
}
295
434
}
0 commit comments