forked from gruntwork-io/helm-kubernetes-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_service_template_test.go
1079 lines (915 loc) · 38.6 KB
/
k8s_service_template_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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build all || tpl
// +build all tpl
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
// run just the template tests. See the test README for more information.
package test
import (
"path/filepath"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
)
// Test that setting ingress.enabled = false will cause the helm template to not render the Ingress resource
func TestK8SServiceIngressEnabledFalseDoesNotCreateIngress(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{"ingress.enabled": "false"},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "ingress", []string{"templates/ingress.yaml"})
require.Error(t, err)
}
// Test that setting service.enabled = false will cause the helm template to not render the Service resource
func TestK8SServiceServiceEnabledFalseDoesNotCreateService(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{"service.enabled": "false"},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "service", []string{"templates/service.yaml"})
require.Error(t, err)
}
// Test each of the required values. Here, we take advantage of the fact that linter_values.yaml is supposed to define
// all the required values, so we check the template rendering by nulling out each field.
func TestK8SServiceRequiredValuesAreRequired(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
eachRequired := []string{
"containerImage.repository",
"containerImage.tag",
"applicationName",
}
for _, requiredVal := range eachRequired {
// Capture the range value and force it into this scope. Otherwise, it is defined outside this block so it can
// change when the subtests parallelize and switch contexts.
requiredVal := requiredVal
t.Run(requiredVal, func(t *testing.T) {
t.Parallel()
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to null out the value.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{requiredVal: "null"},
}
_, err := helm.RenderTemplateE(t, options, helmChartPath, strings.ToLower(t.Name()), []string{})
assert.Error(t, err)
})
}
}
// Test each of the optional values defined in linter_values.yaml. Here, we take advantage of the fact that
// linter_values.yaml is supposed to define all the required values, so we check the template rendering by nulling out
// each field.
func TestK8SServiceOptionalValuesAreOptional(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
eachOptional := []string{
"containerImage.pullPolicy",
}
for _, optionalVal := range eachOptional {
// Capture the range value and force it into this scope. Otherwise, it is defined outside this block so it can
// change when the subtests parallelize and switch contexts.
optionalVal := optionalVal
t.Run(optionalVal, func(t *testing.T) {
t.Parallel()
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to null out the value.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{optionalVal: "null"},
}
// Make sure it renders without error
helm.RenderTemplate(t, options, helmChartPath, "all", []string{})
})
}
}
// Test that deploymentAnnotations render correctly to annotate the Deployment resource
func TestK8SServiceDeploymentAnnotationsRenderCorrectly(t *testing.T) {
t.Parallel()
uniqueID := random.UniqueId()
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{"deploymentAnnotations.unique-id": uniqueID})
assert.Equal(t, len(deployment.Annotations), 1)
assert.Equal(t, deployment.Annotations["unique-id"], uniqueID)
}
func TestK8SServiceSecurityContextAnnotationRenderCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"securityContext.privileged": "true",
"securityContext.runAsUser": "1000",
},
)
renderedContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedContainers), 1)
testContainer := renderedContainers[0]
assert.NotNil(t, testContainer.SecurityContext)
assert.True(t, *testContainer.SecurityContext.Privileged)
assert.Equal(t, *testContainer.SecurityContext.RunAsUser, int64(1000))
}
func TestK8SServicePodSecurityContextAnnotationRenderCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"podSecurityContext.fsGroup": "2000",
},
)
renderedPodSpec := deployment.Spec.Template.Spec
assert.NotNil(t, renderedPodSpec.SecurityContext)
assert.Equal(t, *renderedPodSpec.SecurityContext.FSGroup, int64(2000))
}
// Test that podAnnotations render correctly to annotate the Pod Template Spec on the Deployment resource
func TestK8SServicePodAnnotationsRenderCorrectly(t *testing.T) {
t.Parallel()
uniqueID := random.UniqueId()
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{"podAnnotations.unique-id": uniqueID})
renderedPodAnnotations := deployment.Spec.Template.Annotations
assert.Equal(t, len(renderedPodAnnotations), 1)
assert.Equal(t, renderedPodAnnotations["unique-id"], uniqueID)
}
// Test that containerPorts render correctly to convert the map to a list
func TestK8SServiceContainerPortsSetPortsCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
// disable the default ports
"containerPorts.http.disabled": "true",
"containerPorts.https.disabled": "true",
// ... and specify a new port
"containerPorts.app.port": "9876",
"containerPorts.app.protocol": "TCP",
},
)
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
require.Equal(t, len(appContainer.Ports), 1)
setPort := appContainer.Ports[0]
assert.Equal(t, setPort.Name, "app")
assert.Equal(t, setPort.ContainerPort, int32(9876))
assert.Equal(t, setPort.Protocol, corev1.Protocol("TCP"))
}
// Test that default imagePullSecrets do not render any
func TestK8SServiceNoImagePullSecrets(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{},
)
renderedImagePullSecrets := deployment.Spec.Template.Spec.ImagePullSecrets
require.Equal(t, len(renderedImagePullSecrets), 0)
}
// Test that multiple imagePullSecrets renders each one correctly
func TestK8SServiceMultipleImagePullSecrets(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"imagePullSecrets[0]": "docker-private-registry-key",
"imagePullSecrets[1]": "gcr-registry-key",
},
)
renderedImagePullSecrets := deployment.Spec.Template.Spec.ImagePullSecrets
require.Equal(t, len(renderedImagePullSecrets), 2)
assert.Equal(t, renderedImagePullSecrets[0].Name, "docker-private-registry-key")
assert.Equal(t, renderedImagePullSecrets[1].Name, "gcr-registry-key")
}
func TestK8SServiceIngressPortNumberTypeConversionWithValues(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithValuesFile(t, filepath.Join("fixtures", "ingress_values_with_number_port.yaml"))
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Number, int32(80))
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, secondPath.Backend.Service.Name, "black-hole")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(80))
}
func TestK8SServiceIngressPortStringTypeConversionWithValues(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithValuesFile(t, filepath.Join("fixtures", "ingress_values_with_name_port.yaml"))
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Name, "app")
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, secondPath.Backend.Service.Name, "black-hole")
assert.Equal(t, secondPath.Backend.Service.Port.Name, "black-hole")
}
// Test that setting additionalPaths on ingress add paths after service path
func TestK8SServiceIngressAdditionalPathsAfterMainServicePath(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPaths[0].path": "/black-hole",
"ingress.additionalPaths[0].serviceName": "black-hole",
"ingress.additionalPaths[0].servicePort": "80",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Name, "app")
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, secondPath.Backend.Service.Name, "black-hole")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(80))
}
// Test that setting additionalPaths with multiple entries on ingress add paths after service path in order
func TestK8SServiceIngressAdditionalPathsMultipleAfterMainServicePath(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPaths[0].path": "/sun",
"ingress.additionalPaths[0].serviceName": "sun",
"ingress.additionalPaths[0].servicePort": "3000",
"ingress.additionalPaths[1].path": "/black-hole",
"ingress.additionalPaths[1].serviceName": "black-hole",
"ingress.additionalPaths[1].servicePort": "80",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 3)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Name, "app")
// The second path should be the sun
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/sun")
assert.Equal(t, secondPath.Backend.Service.Name, "sun")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(3000))
// The third path should be the black hole
thirdPath := pathRules[2]
assert.Equal(t, thirdPath.Path, "/black-hole")
assert.Equal(t, thirdPath.Backend.Service.Name, "black-hole")
assert.Equal(t, thirdPath.Backend.Service.Port.Number, int32(80))
}
// Test that omitting a serviceName on additionalPaths reuses the application service name
func TestK8SServiceIngressAdditionalPathsNoServiceName(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPaths[0].path": "/black-hole",
"ingress.additionalPaths[0].servicePort": "3000",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Name, "app")
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, strings.ToLower(secondPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(3000))
}
// Test that setting additionalPathsHigherPriority on ingress add paths before service path
func TestK8SServiceIngressAdditionalPathsHigherPriorityBeforeMainServicePath(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPathsHigherPriority[0].path": "/black-hole",
"ingress.additionalPathsHigherPriority[0].serviceName": "black-hole",
"ingress.additionalPathsHigherPriority[0].servicePort": "80",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the black hole
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/black-hole")
assert.Equal(t, firstPath.Backend.Service.Name, "black-hole")
assert.Equal(t, firstPath.Backend.Service.Port.Number, int32(80))
// The second path should be the main service path
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/app")
assert.Equal(t, strings.ToLower(secondPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, secondPath.Backend.Service.Port.Name, "app")
}
// Test that setting additionalPathsHigherPriority with multiple entries on ingress add paths berfore service path in
// order
func TestK8SServiceIngressAdditionalPathsHigherPriorityMultipleBeforeMainServicePath(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPathsHigherPriority[0].path": "/sun",
"ingress.additionalPathsHigherPriority[0].serviceName": "sun",
"ingress.additionalPathsHigherPriority[0].servicePort": "3000",
"ingress.additionalPathsHigherPriority[1].path": "/black-hole",
"ingress.additionalPathsHigherPriority[1].serviceName": "black-hole",
"ingress.additionalPathsHigherPriority[1].servicePort": "80",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 3)
// The first path should be the sun
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/sun")
assert.Equal(t, firstPath.Backend.Service.Name, "sun")
assert.Equal(t, firstPath.Backend.Service.Port.Number, int32(3000))
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, secondPath.Backend.Service.Name, "black-hole")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(80))
// The last path should be the main service path
thirdPath := pathRules[2]
assert.Equal(t, thirdPath.Path, "/app")
assert.Equal(t, strings.ToLower(thirdPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, thirdPath.Backend.Service.Port.Name, "app")
}
// Test that omitting a serviceName on additionalPathsHigherPriority reuses the application service name
func TestK8SServiceIngressAdditionalPathsHigherPriorityNoServiceName(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPathsHigherPriority[0].path": "/black-hole",
"ingress.additionalPathsHigherPriority[0].servicePort": "3000",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the black hole
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/black-hole")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Number, int32(3000))
// The second path should be the main service path
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/app")
assert.Equal(t, strings.ToLower(secondPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, secondPath.Backend.Service.Port.Name, "app")
}
// Test that omitting a serviceName on additionalPaths reuses the application service name even when hosts is set
func TestK8SServiceIngressWithHostsAdditionalPathsNoServiceName(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.hosts[0]": "chart-example.local",
"ingress.additionalPaths[0].path": "/black-hole",
"ingress.additionalPaths[0].servicePort": "3000",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Name, "app")
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, strings.ToLower(secondPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, secondPath.Backend.Service.Port.Number, int32(3000))
}
// Test that omitting a serviceName on additionalPathsHigherPriority reuses the application service name even when hosts
// is set
func TestK8SServiceIngressWithHostsAdditionalPathsHigherPriorityNoServiceName(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.hosts[0]": "chart-example.local",
"ingress.additionalPathsHigherPriority[0].path": "/black-hole",
"ingress.additionalPathsHigherPriority[0].servicePort": "3000",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the black hole
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/black-hole")
assert.Equal(t, strings.ToLower(firstPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, firstPath.Backend.Service.Port.Number, int32(3000))
// The second path should be the main service path
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/app")
assert.Equal(t, strings.ToLower(secondPath.Backend.Service.Name), "ingress-linter")
assert.Equal(t, secondPath.Backend.Service.Port.Name, "app")
}
// Test rendering Managed Certificate
func TestK8SServiceManagedCertDomainNameAndName(t *testing.T) {
t.Parallel()
cert := renderK8SServiceManagedCertificateWithSetValues(
t,
map[string]string{
"google.managedCertificate.enabled": "true",
"google.managedCertificate.domainName": "api.acme.io",
"google.managedCertificate.name": "acme-cert",
},
)
domains := cert.Spec.Domains
certName := cert.ObjectMeta.Name
assert.Equal(t, len(domains), 1)
assert.Equal(t, domains[0], "api.acme.io")
assert.Equal(t, certName, "acme-cert")
}
// Test that setting ingress.enabled = false will cause the helm template to not render the ManagedCertificate resource
func TestK8SServiceManagedCertificateDefaultsDoesNotCreateManagedCertificate(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "gmc", []string{"templates/gmc.yaml"})
require.Error(t, err)
}
// Test that omitting containerCommand does not set command attribute on the Deployment container spec.
func TestK8SServiceDefaultHasNullCommandSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{})
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
assert.Nil(t, appContainer.Command)
}
// Test that setting containerCommand sets the command attribute on the Deployment container spec.
func TestK8SServiceWithContainerCommandHasCommandSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"containerCommand[0]": "echo",
"containerCommand[1]": "Hello world",
},
)
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
assert.Equal(t, appContainer.Command, []string{"echo", "Hello world"})
}
// Test that omitting containerArgs does not set args attribute on the Deployment container spec.
func TestK8SServiceDefaultHasNullArgSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{})
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
assert.Nil(t, appContainer.Args)
}
// Test that setting containerCommand sets the command attribute on the Deployment container spec.
func TestK8SServiceWithContainerArgsHasArgsSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"containerArgs[0]": "echo",
"containerArgs[1]": "Hello world",
},
)
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
assert.Equal(t, appContainer.Args, []string{"echo", "Hello world"})
}
// Test that omitting hostAliases does not set hostAliases attribute on the Deployment container spec.
func TestK8SServiceDefaultHasNullHostAliasesSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{})
renderedPodSpec := deployment.Spec.Template.Spec
assert.Nil(t, renderedPodSpec.HostAliases)
}
// Test that setting hostAliases sets the hostAliases attribute on the Deployment container spec.
func TestK8SServiceWithHostAliasesHasHostAliasesSpec(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"hostAliases[0].ip": "127.0.0.1",
"hostAliases[0].hostnames[0]": "foo.local",
"hostAliases[0].hostnames[1]": "bar.local",
"hostAliases[1].ip": "10.1.2.3",
"hostAliases[1].hostnames[0]": "foo.remote",
"hostAliases[1].hostnames[1]": "bar.remote",
},
)
renderedPodSpec := deployment.Spec.Template.Spec
assert.Equal(t, len(renderedPodSpec.HostAliases), 2)
// order should be preserved, since order is important for /etc/hosts
assert.Equal(t, renderedPodSpec.HostAliases[0].IP, "127.0.0.1")
assert.Equal(t, renderedPodSpec.HostAliases[0].Hostnames, []string{"foo.local", "bar.local"})
assert.Equal(t, renderedPodSpec.HostAliases[1].IP, "10.1.2.3")
assert.Equal(t, renderedPodSpec.HostAliases[1].Hostnames, []string{"foo.remote", "bar.remote"})
}
// Test that providing tls configuration to Ingress renders correctly
func TestK8SServiceIngressMultiCert(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceIngressWithSetValues(
t,
map[string]string{
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.tls[0].secretName": "chart0-example-tls",
"ingress.tls[0].hosts[0]": "chart0-example-tls-host",
"ingress.tls[1].secretName": "chart1-example-tls",
"ingress.tls[1].hosts[0]": "chart1-example-tls-host",
"ingress.tls[1].hosts[1]": "chart1-example-tls-host2",
},
)
tls := ingress.Spec.TLS
assert.Equal(t, len(tls), 2)
// The first tls should be chart0
firstTls := tls[0]
assert.Equal(t, firstTls.SecretName, "chart0-example-tls")
firstTlsHosts := firstTls.Hosts
assert.Equal(t, len(firstTlsHosts), 1)
assert.Equal(t, firstTlsHosts[0], "chart0-example-tls-host")
// The second tls should be chart1 with multiple hosts
secondTls := tls[1]
assert.Equal(t, secondTls.SecretName, "chart1-example-tls")
secondTlsHosts := secondTls.Hosts
assert.Equal(t, len(secondTlsHosts), 2)
assert.Equal(t, secondTlsHosts[0], "chart1-example-tls-host")
assert.Equal(t, secondTlsHosts[1], "chart1-example-tls-host2")
}
func TestK8SServiceSideCarContainersRendersCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"sideCarContainers.datadog.image": "datadog/agent:latest",
},
)
renderedContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedContainers), 2)
sideCarContainer := renderedContainers[1]
assert.Equal(t, sideCarContainer.Image, "datadog/agent:latest")
}
func TestK8SServiceInitContainersRendersCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"initContainers.flyway.image": "flyway/flyway",
},
)
renderedContainers := deployment.Spec.Template.Spec.InitContainers
require.Equal(t, len(renderedContainers), 1)
assert.Equal(t, renderedContainers[0].Image, "flyway/flyway")
}
func TestK8SServiceDisableDefaultPort(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"containerPorts.http.disabled": "true",
},
)
renderedContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedContainers), 1)
mainContainer := renderedContainers[0]
assert.Equal(t, len(mainContainer.Ports), 0)
}
func TestK8SServiceCanaryDeploymentContainersLabeledCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceCanaryDeploymentWithSetValues(
t,
map[string]string{
"canary.enabled": "true",
"canary.replicaCount": "1",
"canary.containerImage.repository": "nginx",
"canary.containerImage.tag": "1.16.0",
},
)
// Ensure a canary deployment has the canary deployment-type label
assert.Equal(t, deployment.Spec.Selector.MatchLabels["gruntwork.io/deployment-type"], "canary")
}
func TestK8SServiceMainDeploymentContainersLabeledCorrectly(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"containerImage.repository": "nginx",
"containerImage.tag": "1.16.0",
},
)
// Ensure a "main" type deployment is properly labeled as such
assert.Equal(t, deployment.Spec.Selector.MatchLabels["gruntwork.io/deployment-type"], "main")
}
func TestK8SServiceDeploymentAddingAdditionalLabels(t *testing.T) {
t.Parallel()
first_custom_deployment_label_value := "first-custom-value"
second_custom_deployment_label_value := "second-custom-value"
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{"additionalDeploymentLabels.first-label": first_custom_deployment_label_value,
"additionalDeploymentLabels.second-label": second_custom_deployment_label_value})
assert.Equal(t, deployment.Labels["first-label"], first_custom_deployment_label_value)
assert.Equal(t, deployment.Labels["second-label"], second_custom_deployment_label_value)
}
func TestK8SServicePodAddingAdditionalLabels(t *testing.T) {
t.Parallel()
first_custom_pod_label_value := "first-custom-value"
second_custom_pod_label_value := "second-custom-value"
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{"additionalPodLabels.first-label": first_custom_pod_label_value,
"additionalPodLabels.second-label": second_custom_pod_label_value})
assert.Equal(t, deployment.Spec.Template.Labels["first-label"], first_custom_pod_label_value)
assert.Equal(t, deployment.Spec.Template.Labels["second-label"], second_custom_pod_label_value)
}
func TestK8SServiceDeploymentStrategyOnlySetIfEnabled(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"deploymentStrategy.enabled": "false",
},
)
// Strategy shouldn't be set
assert.Equal(t, "", string(deployment.Spec.Strategy.Type))
assert.Nil(t, deployment.Spec.Strategy.RollingUpdate)
}
func TestK8SServiceDeploymentRollingUpdateStrategy(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"deploymentStrategy.enabled": "true",
"deploymentStrategy.type": "RollingUpdate",
},
)
assert.EqualValues(t, "RollingUpdate", string(deployment.Spec.Strategy.Type))
require.Nil(t, deployment.Spec.Strategy.RollingUpdate)
}
func TestK8SServiceDeploymentRollingUpdateStrategyWithCustomOptions(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"deploymentStrategy.enabled": "true",
"deploymentStrategy.type": "RollingUpdate",
"deploymentStrategy.rollingUpdate.maxSurge": "30%",
"deploymentStrategy.rollingUpdate.maxUnavailable": "20%",
},
)
assert.EqualValues(t, "RollingUpdate", string(deployment.Spec.Strategy.Type))
rollingUpdateOptions := deployment.Spec.Strategy.RollingUpdate
require.NotNil(t, rollingUpdateOptions)
assert.Equal(t, rollingUpdateOptions.MaxSurge.String(), "30%")
assert.Equal(t, rollingUpdateOptions.MaxUnavailable.String(), "20%")
}
func TestK8SServiceDeploymentRecreateStrategy(t *testing.T) {
t.Parallel()
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"deploymentStrategy.enabled": "true",
"deploymentStrategy.type": "Recreate",
},
)
assert.Equal(t, "Recreate", string(deployment.Spec.Strategy.Type))
assert.Nil(t, deployment.Spec.Strategy.RollingUpdate)
// Test that custom rolling update options are ignore if the strategy is set to recreate
deployment = renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"deploymentStrategy.enabled": "true",
"deploymentStrategy.type": "Recreate",
"deploymentStrategy.rollingUpdate.maxSurge": "30%",
"deploymentStrategy.rollingUpdate.maxUnavailable": "20%",
},
)
assert.Equal(t, "Recreate", string(deployment.Spec.Strategy.Type))
assert.Nil(t, deployment.Spec.Strategy.RollingUpdate)
}
func TestK8SServiceFullnameOverride(t *testing.T) {
t.Parallel()
overiddenName := "overidden-name"
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{
"fullnameOverride": overiddenName,
},
)
assert.Equal(t, deployment.Name, overiddenName)
}
func TestK8SServiceEnvFrom(t *testing.T) {
t.Parallel()
t.Run("BothConfigMapsAndSecretsEnvFrom", func(t *testing.T) {
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{
"configMaps.test-configmap.as": "envFrom",
"secrets.test-secret.as": "envFrom",
},
)
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 2)
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name, "test-configmap")
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[1].SecretRef.Name, "test-secret")
})
t.Run("OnlyConfigMapsEnvFrom", func(t *testing.T) {
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{
"configMaps.test-configmap.as": "envFrom",
},
)
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 1)
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name, "test-configmap")
})
t.Run("OnlySecretsEnvFrom", func(t *testing.T) {
deployment := renderK8SServiceDeploymentWithSetValues(t,
map[string]string{
"secrets.test-secret.as": "envFrom",
},
)
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 1)
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].SecretRef.Name, "test-secret")
})
}
func TestK8SServiceMinPodsAvailableZeroMeansNoPDB(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{"minPodsAvailable": "0"},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "pdb", []string{"templates/pdb.yaml"})
require.Error(t, err)
}
func TestK8SServiceMinPodsAvailableGreaterThanZeroMeansPDB(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{"minPodsAvailable": "1"},
}
out := helm.RenderTemplate(t, options, helmChartPath, "pdb", []string{"templates/pdb.yaml"})
var pdb policyv1beta1.PodDisruptionBudget
helm.UnmarshalK8SYaml(t, out, &pdb)
assert.Equal(t, 1, pdb.Spec.MinAvailable.IntValue())
}
// Test that rendering extensions.v1beta1 Ingress works.
func TestK8SServiceRenderExtV1Beta1Ingress(t *testing.T) {
t.Parallel()
ingress := renderK8SServiceExtV1Beta1IngressWithSetValues(
t,
map[string]string{
"kubeVersionOverride": "1.17.0",
"ingress.enabled": "true",
"ingress.path": "/app",
"ingress.servicePort": "app",
"ingress.additionalPaths[0].path": "/black-hole",
"ingress.additionalPaths[0].serviceName": "black-hole",
"ingress.additionalPaths[0].servicePort": "80",
},
)
pathRules := ingress.Spec.Rules[0].HTTP.Paths
assert.Equal(t, len(pathRules), 2)
// The first path should be the main service path
firstPath := pathRules[0]
assert.Equal(t, firstPath.Path, "/app")
assert.Equal(t, strings.ToLower(firstPath.Backend.ServiceName), "ingress-linter")
assert.Equal(t, firstPath.Backend.ServicePort.StrVal, "app")
// The second path should be the black hole
secondPath := pathRules[1]
assert.Equal(t, secondPath.Path, "/black-hole")
assert.Equal(t, secondPath.Backend.ServiceName, "black-hole")
assert.Equal(t, secondPath.Backend.ServicePort.IntVal, int32(80))
}
// Test that sessionAffinity and sessionAffinityConfig render correctly when set
func TestK8SServiceSessionAffinityConfig(t *testing.T) {
t.Parallel()
service := renderK8SServiceWithSetValues(