forked from kubernetes-retired/service-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion_test.go
More file actions
368 lines (340 loc) · 10.1 KB
/
conversion_test.go
File metadata and controls
368 lines (340 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"encoding/json"
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
)
type conversionFunc func(string, string) (string, string, error)
type testcase struct {
name string
inLabel string
inValue string
outLabel string
outValue string
success bool
expectedError string
}
func TestClusterServicePlanFieldLabelConversionFunc(t *testing.T) {
cases := []testcase{
{
name: "spec.externalName works",
inLabel: "spec.externalName",
inValue: "somenamehere",
outLabel: "spec.externalName",
outValue: "somenamehere",
success: true,
},
{
name: "spec.clusterServiceClassRef.name works",
inLabel: "spec.clusterServiceClassRef.name",
inValue: "someref",
outLabel: "spec.clusterServiceClassRef.name",
outValue: "someref",
success: true,
},
{
name: "spec.clusterServiceBrokerName works",
inLabel: "spec.clusterServiceBrokerName",
inValue: "somebroker",
outLabel: "spec.clusterServiceBrokerName",
outValue: "somebroker",
success: true,
},
{
name: "spec.externalID works",
inLabel: "spec.externalID",
inValue: "externalid",
outLabel: "spec.externalID",
outValue: "externalid",
success: true,
},
{
name: "random fails",
inLabel: "spec.random",
inValue: "randomvalue",
outLabel: "",
outValue: "",
success: false,
expectedError: "field label not supported: spec.random",
},
}
runTestCases(t, cases, "ClusterServicePlanFieldLabelConversionFunc", ClusterServicePlanFieldLabelConversionFunc)
}
func TestServicePlanFieldLabelConversionFunc(t *testing.T) {
cases := []testcase{
{
name: "spec.externalName works",
inLabel: "spec.externalName",
inValue: "somenamehere",
outLabel: "spec.externalName",
outValue: "somenamehere",
success: true,
},
{
name: "spec.serviceClassRef.name works",
inLabel: "spec.serviceClassRef.name",
inValue: "someref",
outLabel: "spec.serviceClassRef.name",
outValue: "someref",
success: true,
},
{
name: "spec.serviceBrokerName works",
inLabel: "spec.serviceBrokerName",
inValue: "somebroker",
outLabel: "spec.serviceBrokerName",
outValue: "somebroker",
success: true,
},
{
name: "spec.externalID works",
inLabel: "spec.externalID",
inValue: "externalid",
outLabel: "spec.externalID",
outValue: "externalid",
success: true,
},
{
name: "random fails",
inLabel: "spec.random",
inValue: "randomvalue",
outLabel: "",
outValue: "",
success: false,
expectedError: "field label not supported: spec.random",
},
}
runTestCases(t, cases, "ServicePlanFieldLabelConversionFunc", ServicePlanFieldLabelConversionFunc)
}
func TestClusterServiceClassFieldLabelConversionFunc(t *testing.T) {
cases := []testcase{
{
name: "spec.externalName works",
inLabel: "spec.externalName",
inValue: "somenamehere",
outLabel: "spec.externalName",
outValue: "somenamehere",
success: true,
},
{
name: "spec.clusterServiceClassRef.name fails",
inLabel: "spec.clusterServiceClassRef.name",
inValue: "someref",
outLabel: "",
outValue: "",
success: false,
expectedError: "field label not supported: spec.clusterServiceClassRef.name",
},
{
name: "spec.clusterServiceBrokerName works",
inLabel: "spec.clusterServiceBrokerName",
inValue: "somebroker",
outLabel: "spec.clusterServiceBrokerName",
outValue: "somebroker",
success: true,
},
{
name: "spec.externalID works",
inLabel: "spec.externalID",
inValue: "externalid",
outLabel: "spec.externalID",
outValue: "externalid",
success: true,
},
{
name: "random fails",
inLabel: "spec.random",
inValue: "randomvalue",
outLabel: "",
outValue: "",
success: false,
expectedError: "field label not supported: spec.random",
},
}
runTestCases(t, cases, "ClusterServiceClassFieldLabelConversionFunc", ClusterServiceClassFieldLabelConversionFunc)
}
func TestServiceInstanceFieldLabelConversionFunc(t *testing.T) {
cases := []testcase{
{
name: "spec.clusterServiceClassRef.name works",
inLabel: "spec.clusterServiceClassRef.name",
inValue: "someref",
outLabel: "spec.clusterServiceClassRef.name",
outValue: "someref",
success: true,
},
{
name: "spec.clusterServicePlanRef.name works",
inLabel: "spec.clusterServicePlanRef.name",
inValue: "someref",
outLabel: "spec.clusterServicePlanRef.name",
outValue: "someref",
success: true,
},
{
name: "random fails",
inLabel: "spec.random",
inValue: "randomvalue",
outLabel: "",
outValue: "",
success: false,
expectedError: "field label not supported: spec.random",
},
}
runTestCases(t, cases, "ServiceInstanceFieldLabelConversionFunc", ServiceInstanceFieldLabelConversionFunc)
}
func runTestCases(t *testing.T, cases []testcase, testFuncName string, testFunc conversionFunc) {
for _, tc := range cases {
outLabel, outValue, err := testFunc(tc.inLabel, tc.inValue)
if tc.success {
if err != nil {
t.Errorf("%s:%s -- unexpected failure : %q", testFuncName, tc.name, err.Error())
} else {
if a, e := outLabel, tc.outLabel; a != e {
t.Errorf("%s:%s -- label mismatch, expected %q got %q", testFuncName, tc.name, e, a)
}
if a, e := outValue, tc.outValue; a != e {
t.Errorf("%s:%s -- value mismatch, expected %q got %q", testFuncName, tc.name, e, a)
}
}
} else {
if err == nil {
t.Errorf("%s:%s -- unexpected success, expected: %q", testFuncName, tc.name, tc.expectedError)
} else {
if !strings.Contains(err.Error(), tc.expectedError) {
t.Errorf("%s:%s -- did not find expected error %q got %q", testFuncName, tc.name, tc.expectedError, err)
}
}
}
}
}
func TestConvert_v1beta1_CatalogRestrictions_To_servicecatalog_CatalogRestrictions_AndBack(t *testing.T) {
originalIn := CatalogRestrictions{
ServiceClass: []string{"name not in (foo)"},
ServicePlan: []string{"name == bar", "externalName==baz"},
}
var originalOut servicecatalog.CatalogRestrictions
Convert_v1beta1_CatalogRestrictions_To_servicecatalog_CatalogRestrictions(&originalIn, &originalOut, nil)
var convertedOut CatalogRestrictions
Convert_servicecatalog_CatalogRestrictions_To_v1beta1_CatalogRestrictions(&originalOut, &convertedOut, nil)
// original in and converted out should match, but string formatting and order modifications are allowed.
for _, r := range convertedOut.ServiceClass {
if !findInRequirementsIgnoreSpaces(r, originalIn.ServiceClass) {
t.Fail()
}
}
for _, r := range convertedOut.ServicePlan {
if !findInRequirementsIgnoreSpaces(r, originalIn.ServicePlan) {
t.Fail()
}
}
}
func findInRequirementsIgnoreSpaces(requirement string, requirements []string) bool {
find := strings.Replace(requirement, " ", "", -1)
for _, r := range requirements {
found := strings.Replace(r, " ", "", -1)
if find == found {
return true
}
}
return false
}
func TestConvertClusterServiceClassToProperties(t *testing.T) {
cases := []struct {
name string
sc *ClusterServiceClass
json string
}{
{
name: "nil object",
json: "{}",
},
{
name: "normal object",
sc: &ClusterServiceClass{
ObjectMeta: metav1.ObjectMeta{Name: "service-class"},
Spec: ClusterServiceClassSpec{
CommonServiceClassSpec: CommonServiceClassSpec{
ExternalName: "external-class-name",
ExternalID: "external-id",
},
},
},
json: "{\"name\":\"service-class\",\"spec.externalID\":\"external-id\",\"spec.externalName\":\"external-class-name\"}",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := ConvertClusterServiceClassToProperties(tc.sc)
if p == nil {
t.Fatalf("Failed to create Properties object from %+v", tc.sc)
}
b, err := json.Marshal(p)
if err != nil {
t.Fatalf("Unexpected error with json marchal, %v", err)
}
js := string(b)
if js != tc.json {
t.Fatalf("Failed to create expected Properties object,\n\texpected: \t%q,\n \tgot: \t\t%q", tc.json, js)
}
})
}
}
func TestConvertClusterServicePlanToProperties(t *testing.T) {
cases := []struct {
name string
sp *ClusterServicePlan
json string
}{
{
name: "nil object",
json: "{}",
},
{
name: "normal object",
sp: &ClusterServicePlan{
ObjectMeta: metav1.ObjectMeta{Name: "service-plan"},
Spec: ClusterServicePlanSpec{
CommonServicePlanSpec: CommonServicePlanSpec{
ExternalName: "external-plan-name",
ExternalID: "external-id",
},
ClusterServiceClassRef: ClusterObjectReference{
Name: "cluster-service-class-name",
},
},
},
json: "{\"name\":\"service-plan\",\"spec.clusterServiceClass.name\":\"cluster-service-class-name\",\"spec.externalID\":\"external-id\",\"spec.externalName\":\"external-plan-name\"}",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := ConvertClusterServicePlanToProperties(tc.sp)
if p == nil {
t.Fatalf("Failed to create Properties object from %+v", tc.sp)
}
b, err := json.Marshal(p)
if err != nil {
t.Fatalf("Unexpected error with json marchal, %v", err)
}
js := string(b)
if js != tc.json {
t.Fatalf("Failed to create expected Properties object,\n\texpected: \t%q,\n \tgot: \t\t%q", tc.json, js)
}
})
}
}