-
Notifications
You must be signed in to change notification settings - Fork 34
/
configmap_test.go
384 lines (328 loc) · 8.63 KB
/
configmap_test.go
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package configmap
import (
"errors"
"testing"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sfake "k8s.io/client-go/kubernetes/fake"
)
func TestNewBuilder(t *testing.T) {
testCases := []struct {
name string
nsname string
expectedCM *corev1.ConfigMap
expectedErr string
}{
{
name: "test",
nsname: "testns",
expectedCM: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "testns",
},
},
expectedErr: "",
},
{
name: "",
nsname: "testns",
expectedCM: nil,
expectedErr: "configmap 'name' cannot be empty",
},
{
name: "test",
nsname: "",
expectedCM: nil,
expectedErr: "configmap 'nsname' cannot be empty",
},
}
for _, testCase := range testCases {
testSettings := clients.GetTestClients(clients.TestClientParams{})
testBuilder := NewBuilder(testSettings, testCase.name, testCase.nsname)
if testCase.expectedErr == "" {
assert.NotNil(t, testBuilder)
assert.Equal(t, testCase.expectedCM.Name, testBuilder.Definition.Name)
assert.Equal(t, testCase.expectedCM.Namespace, testBuilder.Definition.Namespace)
} else {
assert.Equal(t, testCase.expectedErr, testBuilder.errorMsg)
}
}
}
func TestPull(t *testing.T) {
testCases := []struct {
name string
nsname string
expectedError bool
addToRuntimeObjects bool
expectedErrorText string
}{
{
name: "test",
nsname: "testns",
expectedError: false,
addToRuntimeObjects: true,
},
{
name: "",
nsname: "testns",
expectedError: true,
addToRuntimeObjects: false,
expectedErrorText: "configmap 'name' cannot be empty",
},
{
name: "test",
nsname: "",
expectedError: true,
addToRuntimeObjects: false,
expectedErrorText: "configmap 'nsname' cannot be empty",
},
{
name: "test",
nsname: "testns",
expectedError: true,
addToRuntimeObjects: false,
expectedErrorText: "configmap object test does not exist in namespace testns",
},
}
for _, testCase := range testCases {
var (
runtimeObjects []runtime.Object
testSettings *clients.Settings
)
testCM := generateConfigMap(testCase.name, testCase.nsname)
if testCase.addToRuntimeObjects {
runtimeObjects = append(runtimeObjects, testCM)
}
testSettings = clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: runtimeObjects,
})
// Test the Pull function
builderResult, err := Pull(testSettings, testCase.name, testCase.nsname)
// Check the error
if testCase.expectedError {
assert.NotNil(t, err)
if testCase.expectedErrorText != "" {
assert.Equal(t, testCase.expectedErrorText, err.Error())
}
} else {
assert.Nil(t, err)
assert.NotNil(t, builderResult)
assert.Equal(t, testCase.name, builderResult.Definition.Name)
assert.Equal(t, testCase.nsname, builderResult.Definition.Namespace)
}
}
}
func TestCreate(t *testing.T) {
testCases := []struct {
addToRuntimeObjects bool
}{
{
addToRuntimeObjects: true,
},
{
addToRuntimeObjects: false,
},
}
for _, testCase := range testCases {
var (
runtimeObjects []runtime.Object
)
testCM := generateConfigMap("test-name", "test-namespace")
if testCase.addToRuntimeObjects {
runtimeObjects = append(runtimeObjects, testCM)
}
testBuilder := buildTestBuilderWithFakeObjects(runtimeObjects)
// Test the Create function
builderResult, err := testBuilder.Create()
assert.Nil(t, err)
assert.NotNil(t, builderResult)
assert.Equal(t, "test-name", builderResult.Definition.Name)
assert.Equal(t, "test-namespace", builderResult.Definition.Namespace)
}
}
func TestDelete(t *testing.T) {
testCases := []struct {
addToRuntimeObjects bool
}{
{
addToRuntimeObjects: true,
},
{
addToRuntimeObjects: false,
},
}
for _, testCase := range testCases {
var (
runtimeObjects []runtime.Object
)
testCM := generateConfigMap("test-name", "test-namespace")
if testCase.addToRuntimeObjects {
runtimeObjects = append(runtimeObjects, testCM)
}
testBuilder := buildTestBuilderWithFakeObjects(runtimeObjects)
// Test the Delete function
err := testBuilder.Delete()
assert.Nil(t, err)
}
}
func TestUpdate(t *testing.T) {
generateTestConfigMap := func() *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
}
}
testCases := []struct {
configMapExistsAlready bool
}{
{
configMapExistsAlready: false,
},
{
configMapExistsAlready: true,
},
}
for _, testCase := range testCases {
var runtimeObjects []runtime.Object
if testCase.configMapExistsAlready {
runtimeObjects = append(runtimeObjects, generateTestConfigMap())
}
testBuilder := buildTestBuilderWithFakeObjects(runtimeObjects)
// Assert the deployment before the update
assert.NotNil(t, testBuilder.Definition)
assert.Nil(t, testBuilder.Definition.Data)
// Set a value in the definition to test the update
testBuilder.Definition.Data = map[string]string{"key1": "value1", "key2": "value2"}
// Perform the update
result, err := testBuilder.Update()
// Assert the result
assert.NotNil(t, testBuilder.Definition)
if !testCase.configMapExistsAlready {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, testBuilder.Definition.Name, result.Definition.Name)
assert.Equal(t, testBuilder.Definition.Data, result.Definition.Data)
}
}
}
func TestGetGVR(t *testing.T) {
testGVR := GetGVR()
assert.Equal(t, "configmaps", testGVR.Resource)
assert.Equal(t, "v1", testGVR.Version)
assert.Equal(t, "", testGVR.Group)
}
func TestValidate(t *testing.T) {
testCases := []struct {
builderNil bool
definitionNil bool
apiClientNil bool
expectedError string
}{
{
builderNil: true,
definitionNil: false,
apiClientNil: false,
expectedError: "error: received nil ConfigMap builder",
},
{
builderNil: false,
definitionNil: true,
apiClientNil: false,
expectedError: "can not redefine the undefined ConfigMap",
},
{
builderNil: false,
definitionNil: false,
apiClientNil: true,
expectedError: "ConfigMap builder cannot have nil apiClient",
},
{
builderNil: false,
definitionNil: false,
apiClientNil: false,
expectedError: "",
},
}
for _, testCase := range testCases {
testBuilder := buildTestBuilderWithFakeObjects([]runtime.Object{})
if testCase.builderNil {
testBuilder = nil
}
if testCase.definitionNil {
testBuilder.Definition = nil
}
if testCase.apiClientNil {
testBuilder.apiClient = nil
}
result, err := testBuilder.validate()
if testCase.expectedError != "" {
assert.NotNil(t, err)
assert.Equal(t, testCase.expectedError, err.Error())
assert.False(t, result)
} else {
assert.Nil(t, err)
assert.True(t, result)
}
}
}
func TestWithOptions(t *testing.T) {
testBuilder := buildTestBuilderWithFakeObjects([]runtime.Object{})
testBuilder.WithOptions(func(builder *Builder) (*Builder, error) {
return builder, nil
})
assert.Equal(t, "", testBuilder.errorMsg)
testBuilder.WithOptions(func(builder *Builder) (*Builder, error) {
return builder, errors.New("error")
})
assert.Equal(t, "error", testBuilder.errorMsg)
}
func TestWithData(t *testing.T) {
testCases := []struct {
key string
value string
expectedErr string
}{
{
key: "key",
value: "value",
expectedErr: "",
},
{
key: "",
value: "",
expectedErr: "'data' cannot be empty",
},
}
for _, testCase := range testCases {
testBuilder := buildTestBuilderWithFakeObjects([]runtime.Object{})
if testCase.expectedErr == "" {
testBuilder.WithData(map[string]string{testCase.key: testCase.value})
assert.Equal(t, testCase.value, testBuilder.Definition.Data[testCase.key])
} else {
testBuilder.WithData(map[string]string{})
assert.Equal(t, testCase.expectedErr, testBuilder.errorMsg)
}
}
}
func buildTestBuilderWithFakeObjects(objects []runtime.Object) *Builder {
fakeClient := k8sfake.NewSimpleClientset(objects...)
return NewBuilder(&clients.Settings{
CoreV1Interface: fakeClient.CoreV1(),
K8sClient: fakeClient,
}, "test-name", "test-namespace")
}
func generateConfigMap(name, nsname string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
}
}