Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/common/customize_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package common_test
6
7
import (
8
"reflect"
9
"testing"
10
11
"github.com/stretchr/testify/require"
12
corev1 "k8s.io/api/core/v1"
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
15
"github.com/gitpod-io/gitpod/installer/pkg/common"
16
"github.com/gitpod-io/gitpod/installer/pkg/components/proxy"
17
config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"
18
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
19
)
20
21
func TestCustomizeAnnotation(t *testing.T) {
22
testCases := []struct {
23
Name string
24
Customization []config.Customization
25
Component string
26
TypeMeta metav1.TypeMeta
27
ExistingAnnotations []func() map[string]string
28
Expect map[string]string
29
}{
30
{
31
Name: "no customization",
32
Customization: nil,
33
Component: "component",
34
TypeMeta: common.TypeMetaDeployment,
35
Expect: map[string]string{},
36
},
37
{
38
Customization: []config.Customization{},
39
Name: "empty customization",
40
Component: "component",
41
TypeMeta: common.TypeMetaDeployment,
42
Expect: map[string]string{},
43
},
44
{
45
Customization: []config.Customization{
46
{
47
TypeMeta: common.TypeMetaBatchJob,
48
Metadata: metav1.ObjectMeta{
49
Name: "component",
50
Annotations: map[string]string{
51
"key1": "value1",
52
},
53
},
54
},
55
},
56
Name: "ignore different typeMeta annotations",
57
Component: "component",
58
TypeMeta: common.TypeMetaDeployment,
59
Expect: map[string]string{},
60
},
61
{
62
Customization: []config.Customization{
63
{
64
TypeMeta: common.TypeMetaDeployment,
65
Metadata: metav1.ObjectMeta{
66
Name: "component2",
67
Annotations: map[string]string{
68
"key1": "value1",
69
},
70
},
71
},
72
},
73
Name: "ignore same typeMeta, different name annotations",
74
Component: "component",
75
TypeMeta: common.TypeMetaDeployment,
76
Expect: map[string]string{},
77
},
78
{
79
Customization: []config.Customization{
80
{
81
TypeMeta: common.TypeMetaDeployment,
82
Metadata: metav1.ObjectMeta{
83
Name: "component",
84
Annotations: map[string]string{
85
"key1": "value1",
86
},
87
},
88
},
89
},
90
Name: "single component annotations",
91
Component: "component",
92
TypeMeta: common.TypeMetaDeployment,
93
Expect: map[string]string{
94
"key1": "value1",
95
},
96
},
97
{
98
Customization: []config.Customization{
99
{
100
TypeMeta: common.TypeMetaDeployment,
101
Metadata: metav1.ObjectMeta{
102
Name: "component",
103
Annotations: map[string]string{
104
"key1": "value1",
105
"key2": "value2",
106
},
107
},
108
},
109
{
110
TypeMeta: common.TypeMetaDeployment,
111
Metadata: metav1.ObjectMeta{
112
Name: "component",
113
Annotations: map[string]string{
114
"key3": "value3",
115
},
116
},
117
},
118
},
119
Name: "multiple component annotations",
120
Component: "component",
121
TypeMeta: common.TypeMetaDeployment,
122
Expect: map[string]string{
123
"key1": "value1",
124
"key2": "value2",
125
"key3": "value3",
126
},
127
},
128
{
129
Customization: []config.Customization{
130
{
131
TypeMeta: metav1.TypeMeta{
132
APIVersion: "*",
133
Kind: "*",
134
},
135
Metadata: metav1.ObjectMeta{
136
Name: "*",
137
Annotations: map[string]string{
138
"key1": "value1",
139
},
140
},
141
},
142
},
143
Name: "wildcard annotations",
144
Component: "component",
145
TypeMeta: common.TypeMetaDeployment,
146
Expect: map[string]string{
147
"key1": "value1",
148
},
149
},
150
{
151
Customization: []config.Customization{
152
{
153
TypeMeta: metav1.TypeMeta{
154
APIVersion: "*",
155
Kind: "*",
156
},
157
Metadata: metav1.ObjectMeta{
158
Name: "*",
159
Annotations: map[string]string{
160
"key1": "value1",
161
"key2": "override",
162
"key3": "",
163
},
164
},
165
},
166
},
167
Name: "override input, do not override existing",
168
Component: "component",
169
TypeMeta: common.TypeMetaDeployment,
170
ExistingAnnotations: []func() map[string]string{
171
func() map[string]string {
172
return map[string]string{
173
"key2": "original",
174
}
175
},
176
},
177
Expect: map[string]string{
178
"key1": "value1",
179
"key2": "original",
180
"key3": "",
181
},
182
},
183
{
184
Customization: []config.Customization{
185
{
186
TypeMeta: metav1.TypeMeta{
187
APIVersion: "v1",
188
Kind: "Service",
189
},
190
Metadata: metav1.ObjectMeta{
191
Name: "proxy",
192
Annotations: map[string]string{
193
"service.beta.kubernetes.io/aws-load-balancer-ip-address-type": "ipv4",
194
},
195
},
196
},
197
},
198
Name: "Test service #11106",
199
TypeMeta: common.TypeMetaService,
200
Component: proxy.Component,
201
Expect: map[string]string{
202
"service.beta.kubernetes.io/aws-load-balancer-ip-address-type": "ipv4",
203
},
204
},
205
}
206
207
for _, testCase := range testCases {
208
t.Run(testCase.Name, func(t *testing.T) {
209
ctx, err := common.NewRenderContext(config.Config{
210
Customization: &testCase.Customization,
211
}, versions.Manifest{}, "test_namespace")
212
require.NoError(t, err)
213
214
result := common.CustomizeAnnotation(ctx, testCase.Component, testCase.TypeMeta, testCase.ExistingAnnotations...)
215
216
if !reflect.DeepEqual(testCase.Expect, result) {
217
t.Errorf("expected %v but got %v", testCase.Expect, result)
218
}
219
})
220
}
221
}
222
223
func TestCustomizeLabel(t *testing.T) {
224
testCases := []struct {
225
Name string
226
Customization []config.Customization
227
Component string
228
TypeMeta metav1.TypeMeta
229
ExistingLabels []func() map[string]string
230
Expect map[string]string
231
}{
232
{
233
Name: "no customization",
234
Customization: nil,
235
Component: "component",
236
TypeMeta: common.TypeMetaDeployment,
237
Expect: map[string]string{},
238
},
239
{
240
Customization: []config.Customization{},
241
Name: "empty customization",
242
Component: "component",
243
TypeMeta: common.TypeMetaDeployment,
244
Expect: map[string]string{},
245
},
246
{
247
Customization: []config.Customization{
248
{
249
TypeMeta: common.TypeMetaBatchJob,
250
Metadata: metav1.ObjectMeta{
251
Name: "component",
252
Labels: map[string]string{
253
"key1": "value1",
254
},
255
},
256
},
257
},
258
Name: "ignore different typeMeta labels",
259
Component: "component",
260
TypeMeta: common.TypeMetaDeployment,
261
Expect: map[string]string{},
262
},
263
{
264
Customization: []config.Customization{
265
{
266
TypeMeta: common.TypeMetaDeployment,
267
Metadata: metav1.ObjectMeta{
268
Name: "component2",
269
Labels: map[string]string{
270
"key1": "value1",
271
},
272
},
273
},
274
},
275
Name: "ignore same typeMeta, different name labels",
276
Component: "component",
277
TypeMeta: common.TypeMetaDeployment,
278
Expect: map[string]string{},
279
},
280
{
281
Customization: []config.Customization{
282
{
283
TypeMeta: common.TypeMetaDeployment,
284
Metadata: metav1.ObjectMeta{
285
Name: "component",
286
Labels: map[string]string{
287
"key1": "value1",
288
},
289
},
290
},
291
},
292
Name: "single component labels",
293
Component: "component",
294
TypeMeta: common.TypeMetaDeployment,
295
Expect: map[string]string{
296
"key1": "value1",
297
},
298
},
299
{
300
Customization: []config.Customization{
301
{
302
TypeMeta: common.TypeMetaDeployment,
303
Metadata: metav1.ObjectMeta{
304
Name: "component",
305
Labels: map[string]string{
306
"key1": "value1",
307
"key2": "value2",
308
},
309
},
310
},
311
{
312
TypeMeta: common.TypeMetaDeployment,
313
Metadata: metav1.ObjectMeta{
314
Name: "component",
315
Labels: map[string]string{
316
"key3": "value3",
317
},
318
},
319
},
320
},
321
Name: "multiple component labels",
322
Component: "component",
323
TypeMeta: common.TypeMetaDeployment,
324
Expect: map[string]string{
325
"key1": "value1",
326
"key2": "value2",
327
"key3": "value3",
328
},
329
},
330
{
331
Customization: []config.Customization{
332
{
333
TypeMeta: metav1.TypeMeta{
334
APIVersion: "*",
335
Kind: "*",
336
},
337
Metadata: metav1.ObjectMeta{
338
Name: "*",
339
Labels: map[string]string{
340
"key1": "value1",
341
},
342
},
343
},
344
},
345
Name: "wildcard labels",
346
Component: "component",
347
TypeMeta: common.TypeMetaDeployment,
348
Expect: map[string]string{
349
"key1": "value1",
350
},
351
},
352
{
353
Customization: []config.Customization{
354
{
355
TypeMeta: metav1.TypeMeta{
356
APIVersion: "*",
357
Kind: "*",
358
},
359
Metadata: metav1.ObjectMeta{
360
Name: "*",
361
Labels: map[string]string{
362
"key1": "value1",
363
"key2": "override",
364
"key3": "",
365
},
366
},
367
},
368
},
369
Name: "override input, do not override existing",
370
Component: "component",
371
TypeMeta: common.TypeMetaDeployment,
372
ExistingLabels: []func() map[string]string{
373
func() map[string]string {
374
return map[string]string{
375
"key2": "original",
376
}
377
},
378
},
379
Expect: map[string]string{
380
"key1": "value1",
381
"key2": "original",
382
"key3": "",
383
},
384
},
385
}
386
387
for _, testCase := range testCases {
388
t.Run(testCase.Name, func(t *testing.T) {
389
ctx, err := common.NewRenderContext(config.Config{
390
Customization: &testCase.Customization,
391
}, versions.Manifest{}, "test_namespace")
392
require.NoError(t, err)
393
394
result := common.CustomizeLabel(ctx, testCase.Component, testCase.TypeMeta, testCase.ExistingLabels...)
395
396
// These all have the default labels - add these in
397
expectation := common.DefaultLabels(testCase.Component)
398
for k, v := range testCase.Expect {
399
expectation[k] = v
400
}
401
402
if !reflect.DeepEqual(expectation, result) {
403
t.Errorf("expected %v but got %v", expectation, result)
404
}
405
})
406
}
407
}
408
409
func TestCustomizeEnvvar(t *testing.T) {
410
testCases := []struct {
411
Name string
412
Customization []config.Customization
413
Component string
414
ExistingEnnvars []corev1.EnvVar
415
Expect []corev1.EnvVar
416
}{
417
{
418
Name: "no customization",
419
Customization: nil,
420
Component: "component",
421
Expect: []corev1.EnvVar{},
422
},
423
{
424
Customization: []config.Customization{},
425
Name: "empty customization",
426
Component: "component",
427
Expect: []corev1.EnvVar{},
428
},
429
{
430
Customization: []config.Customization{
431
{
432
TypeMeta: common.TypeMetaDeployment,
433
Metadata: metav1.ObjectMeta{
434
Name: "component2",
435
},
436
Spec: config.CustomizationSpec{
437
Env: []corev1.EnvVar{
438
{
439
Name: "key1",
440
Value: "value1",
441
},
442
},
443
},
444
},
445
},
446
Name: "ignore different name envvars",
447
Component: "component",
448
Expect: []corev1.EnvVar{},
449
},
450
{
451
Customization: []config.Customization{
452
{
453
TypeMeta: common.TypeMetaDeployment,
454
Metadata: metav1.ObjectMeta{
455
Name: "component",
456
},
457
Spec: config.CustomizationSpec{
458
Env: []corev1.EnvVar{
459
{
460
Name: "key1",
461
Value: "value1",
462
},
463
},
464
},
465
},
466
},
467
Name: "add in name envvars",
468
Component: "component",
469
Expect: []corev1.EnvVar{
470
{
471
Name: "key1",
472
Value: "value1",
473
},
474
},
475
},
476
{
477
Customization: []config.Customization{
478
{
479
Metadata: metav1.ObjectMeta{
480
Name: "*",
481
},
482
Spec: config.CustomizationSpec{
483
Env: []corev1.EnvVar{
484
{
485
Name: "key1",
486
Value: "original",
487
},
488
{
489
Name: "key2",
490
Value: "original",
491
},
492
{
493
Name: "key3",
494
Value: "override",
495
},
496
},
497
},
498
},
499
{
500
Metadata: metav1.ObjectMeta{
501
Name: "component",
502
},
503
Spec: config.CustomizationSpec{
504
Env: []corev1.EnvVar{
505
{
506
Name: "key1",
507
Value: "override",
508
},
509
},
510
},
511
},
512
},
513
ExistingEnnvars: []corev1.EnvVar{
514
{
515
Name: "key4",
516
Value: "value",
517
},
518
{
519
Name: "key3",
520
Value: "original",
521
},
522
},
523
Name: "full-house envvars",
524
Component: "component",
525
Expect: []corev1.EnvVar{
526
{
527
Name: "key4",
528
Value: "value",
529
},
530
{
531
Name: "key3",
532
Value: "original",
533
},
534
{
535
Name: "key1",
536
Value: "override",
537
},
538
{
539
Name: "key2",
540
Value: "original",
541
},
542
},
543
},
544
}
545
546
for _, testCase := range testCases {
547
t.Run(testCase.Name, func(t *testing.T) {
548
ctx, err := common.NewRenderContext(config.Config{
549
Customization: &testCase.Customization,
550
}, versions.Manifest{}, "test_namespace")
551
require.NoError(t, err)
552
553
result := common.CustomizeEnvvar(ctx, testCase.Component, testCase.ExistingEnnvars)
554
555
if !reflect.DeepEqual(testCase.Expect, result) {
556
t.Errorf("expected %v but got %v", testCase.Expect, result)
557
}
558
})
559
}
560
}
561
562