@@ -24,6 +24,7 @@ import (
24
24
25
25
"github.com/golang/glog"
26
26
27
+ "k8s.io/api/core/v1"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
"k8s.io/apimachinery/pkg/util/wait"
@@ -52,7 +53,7 @@ func newHandlerForTest(internalClient internalclientset.Interface) (admission.In
52
53
53
54
// newFakeServiceCatalogClientForTest creates a fake clientset that returns a
54
55
// ClusterServiceClassList with the given ClusterServiceClass as the single list item.
55
- func newFakeServiceCatalogClientForTest (sc * servicecatalog.ClusterServiceClass , sps []* servicecatalog.ClusterServicePlan ) * fake.Clientset {
56
+ func newFakeServiceCatalogClientForTest (sc * servicecatalog.ClusterServiceClass , sps []* servicecatalog.ClusterServicePlan , classFilter string ) * fake.Clientset {
56
57
fakeClient := & fake.Clientset {}
57
58
58
59
// react to the given service classes
@@ -74,7 +75,9 @@ func newFakeServiceCatalogClientForTest(sc *servicecatalog.ClusterServiceClass,
74
75
ResourceVersion : "1" ,
75
76
}}
76
77
for _ , sp := range sps {
77
- spList .Items = append (spList .Items , * sp )
78
+ if classFilter == "" || classFilter == sp .Spec .ClusterServiceClassRef .Name {
79
+ spList .Items = append (spList .Items , * sp )
80
+ }
78
81
}
79
82
fakeClient .AddReactor ("list" , "clusterserviceplans" , func (action core.Action ) (bool , runtime.Object , error ) {
80
83
return true , spList , nil
@@ -104,21 +107,30 @@ func newClusterServiceClass(id string, name string) *servicecatalog.ClusterServi
104
107
return sc
105
108
}
106
109
107
- // newClusterServiceClass returns a new serviceclass.
108
- func newClusterServicePlans (count uint ) []* servicecatalog.ClusterServicePlan {
110
+ // newClusterServicePlans returns new serviceplans.
111
+ func newClusterServicePlans (count uint , useDifferentClasses bool ) []* servicecatalog.ClusterServicePlan {
112
+ classname := "test-serviceclass"
109
113
sp1 := & servicecatalog.ClusterServicePlan {
110
114
ObjectMeta : metav1.ObjectMeta {Name : "bar-id" },
111
115
Spec : servicecatalog.ClusterServicePlanSpec {
112
116
ExternalName : "bar" ,
113
117
ExternalID : "12345" ,
118
+ ClusterServiceClassRef : v1.LocalObjectReference {
119
+ Name : classname ,
120
+ },
114
121
},
115
122
}
116
-
123
+ if useDifferentClasses {
124
+ classname = "different-serviceclass"
125
+ }
117
126
sp2 := & servicecatalog.ClusterServicePlan {
118
127
ObjectMeta : metav1.ObjectMeta {Name : "baz-id" },
119
128
Spec : servicecatalog.ClusterServicePlanSpec {
120
129
ExternalName : "baz" ,
121
130
ExternalID : "23456" ,
131
+ ClusterServiceClassRef : v1.LocalObjectReference {
132
+ Name : classname ,
133
+ },
122
134
},
123
135
}
124
136
@@ -157,7 +169,7 @@ func TestWithListFailure(t *testing.T) {
157
169
}
158
170
159
171
func TestWithPlanWorks (t * testing.T ) {
160
- fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 ) )
172
+ fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" )
161
173
handler , informerFactory , err := newHandlerForTest (fakeClient )
162
174
if err != nil {
163
175
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -179,7 +191,7 @@ func TestWithPlanWorks(t *testing.T) {
179
191
}
180
192
181
193
func TestWithNoPlanFailsWithNoClusterServiceClass (t * testing.T ) {
182
- fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 ) )
194
+ fakeClient := newFakeServiceCatalogClientForTest (nil , newClusterServicePlans (1 , false ), "" )
183
195
handler , informerFactory , err := newHandlerForTest (fakeClient )
184
196
if err != nil {
185
197
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -200,9 +212,9 @@ func TestWithNoPlanFailsWithNoClusterServiceClass(t *testing.T) {
200
212
// checks that the defaulting action works when a service class only provides a single plan.
201
213
func TestWithNoPlanWorksWithSinglePlan (t * testing.T ) {
202
214
sc := newClusterServiceClass ("foo-id" , "foo" )
203
- sps := newClusterServicePlans (1 )
215
+ sps := newClusterServicePlans (1 , false )
204
216
glog .V (4 ).Infof ("Created Service as %+v" , sc )
205
- fakeClient := newFakeServiceCatalogClientForTest (sc , sps )
217
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" )
206
218
207
219
handler , informerFactory , err := newHandlerForTest (fakeClient )
208
220
if err != nil {
@@ -230,9 +242,9 @@ func TestWithNoPlanWorksWithSinglePlan(t *testing.T) {
230
242
// checks that defaulting fails when there are multiple plans to choose from.
231
243
func TestWithNoPlanFailsWithMultiplePlans (t * testing.T ) {
232
244
sc := newClusterServiceClass ("foo-id" , "foo" )
233
- sps := newClusterServicePlans (2 )
245
+ sps := newClusterServicePlans (2 , false )
234
246
glog .V (4 ).Infof ("Created Service as %+v" , sc )
235
- fakeClient := newFakeServiceCatalogClientForTest (sc , sps )
247
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , "" )
236
248
handler , informerFactory , err := newHandlerForTest (fakeClient )
237
249
if err != nil {
238
250
t .Errorf ("unexpected error initializing handler: %v" , err )
@@ -250,3 +262,34 @@ func TestWithNoPlanFailsWithMultiplePlans(t *testing.T) {
250
262
t .Errorf ("did not find expected error, got %q" , err )
251
263
}
252
264
}
265
+
266
+ // checks that defaulting succeeds when there are multiple plans but only a
267
+ // single plan for the specified Service Class
268
+ func TestWithNoPlanSucceedsWithMultiplePlansFromDifferentClasses (t * testing.T ) {
269
+ sc := newClusterServiceClass ("foo-id" , "foo" )
270
+ sps := newClusterServicePlans (2 , true )
271
+ glog .V (4 ).Infof ("Created Service as %+v" , sc )
272
+ classFilter := "test-serviceclass"
273
+ fakeClient := newFakeServiceCatalogClientForTest (sc , sps , classFilter )
274
+ handler , informerFactory , err := newHandlerForTest (fakeClient )
275
+ if err != nil {
276
+ t .Errorf ("unexpected error initializing handler: %v" , err )
277
+ }
278
+ informerFactory .Start (wait .NeverStop )
279
+
280
+ instance := newServiceInstance ("dummy" )
281
+ instance .Spec .ExternalClusterServiceClassName = "foo"
282
+
283
+ err = handler .Admit (admission .NewAttributesRecord (& instance , nil , servicecatalog .Kind ("ServiceInstance" ).WithVersion ("version" ), instance .Namespace , instance .Name , servicecatalog .Resource ("serviceinstances" ).WithVersion ("version" ), "" , admission .Create , nil ))
284
+ if err != nil {
285
+ actions := ""
286
+ for _ , action := range fakeClient .Actions () {
287
+ actions = actions + action .GetVerb () + ":" + action .GetResource ().Resource + ":" + action .GetSubresource () + ", "
288
+ }
289
+ t .Errorf ("unexpected error %q returned from admission handler: %v" , err , actions )
290
+ }
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" )
294
+ }
295
+ }
0 commit comments