@@ -3,11 +3,10 @@ package tests
33import (
44 "testing"
55
6+ "github.com/stretchr/testify/assert"
67 "github.com/stretchr/testify/require"
7- "helm.sh/helm/v3/pkg/chart/loader"
88 "helm.sh/helm/v3/pkg/chartutil"
99 "helm.sh/helm/v3/pkg/engine"
10- appsv1 "k8s.io/api/apps/v1"
1110 corev1 "k8s.io/api/core/v1"
1211 "k8s.io/utils/pointer"
1312)
@@ -17,50 +16,24 @@ import (
1716func TestExamples (t * testing.T ) {
1817 t .Parallel ()
1918
20- chart , err := loader .LoadDir (".." )
21- require .NoError (t , err , "loaded chart successfully" )
22- require .NotNil (t , chart , "chart must be non-nil" )
19+ chart := LoadChart (t )
2320
24- exampleOpenShift , err := ReadValuesAsMap ("../examples/openshift/openshift.values.yaml" )
21+ exampleOpenShift , err := ReadValuesFileAsMap ("../examples/openshift/openshift.values.yaml" )
2522 require .NoError (t , err , "failed to load OpenShift example values" )
2623
27- exampleKind , err := ReadValuesAsMap ("../examples/kind/kind.values.yaml" )
24+ exampleKind , err := ReadValuesFileAsMap ("../examples/kind/kind.values.yaml" )
2825 require .NoError (t , err , "failed to load Kind example values" )
2926
3027 tests := []struct {
3128 Name string
3229 Values map [string ]interface {}
3330 PodSecurityContext * corev1.PodSecurityContext
3431 ContainerSecurityContext * corev1.SecurityContext
32+ Postgres * PostgresValues
3533 }{
3634 {
3735 Name : "default" ,
3836 Values : nil ,
39- PodSecurityContext : & corev1.PodSecurityContext {
40- RunAsUser : pointer .Int64 (1000 ),
41- RunAsGroup : nil ,
42- RunAsNonRoot : pointer .Bool (true ),
43- SeccompProfile : & corev1.SeccompProfile {
44- Type : corev1 .SeccompProfileTypeRuntimeDefault ,
45- LocalhostProfile : nil ,
46- },
47- },
48- ContainerSecurityContext : & corev1.SecurityContext {
49- RunAsUser : nil ,
50- RunAsGroup : nil ,
51- RunAsNonRoot : nil ,
52- Capabilities : nil ,
53- Privileged : nil ,
54- SELinuxOptions : nil ,
55- WindowsOptions : nil ,
56- ReadOnlyRootFilesystem : pointer .Bool (true ),
57- AllowPrivilegeEscalation : pointer .Bool (false ),
58- ProcMount : nil ,
59- SeccompProfile : & corev1.SeccompProfile {
60- Type : corev1 .SeccompProfileTypeRuntimeDefault ,
61- LocalhostProfile : nil ,
62- },
63- },
6437 }, {
6538 Name : "openshift" ,
6639 Values : exampleOpenShift ,
@@ -113,40 +86,58 @@ func TestExamples(t *testing.T) {
11386 },
11487 }
11588
89+ var (
90+ defaultPsp = & corev1.PodSecurityContext {
91+ RunAsUser : pointer .Int64 (1000 ),
92+ RunAsNonRoot : pointer .Bool (true ),
93+ SeccompProfile : & corev1.SeccompProfile {
94+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
95+ },
96+ }
97+
98+ defaultCsc = & corev1.SecurityContext {
99+ ReadOnlyRootFilesystem : pointer .Bool (true ),
100+ AllowPrivilegeEscalation : pointer .Bool (false ),
101+ SeccompProfile : & corev1.SeccompProfile {
102+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
103+ },
104+ }
105+ )
106+
116107 for _ , test := range tests {
117108 test := test
109+
110+ if test .PodSecurityContext == nil {
111+ test .PodSecurityContext = defaultPsp
112+ }
113+ if test .ContainerSecurityContext == nil {
114+ test .ContainerSecurityContext = defaultCsc
115+ }
116+
118117 t .Run (test .Name , func (t * testing.T ) {
119118 t .Parallel ()
120119
121- values , err := chartutil .ToRenderValues (chart , test .Values , DefaultReleaseOptions (), chartutil .DefaultCapabilities .Copy ())
120+ values , err := chartutil .ToRenderValues (chart . chart , test .Values , DefaultReleaseOptions (), chartutil .DefaultCapabilities .Copy ())
122121 require .NoError (t , err , "failed to generate render values" )
123122
124- manifests , err := engine .Render (chart , values )
123+ manifests , err := engine .Render (chart . chart , values )
125124 require .NoError (t , err , "failed to render chart" )
126125
127126 objs , err := LoadObjectsFromManifests (manifests )
128127 require .NoError (t , err , "failed to convert manifests to objects" )
129128
130129 // Find the coderd Deployment
131- var found bool
132- for _ , obj := range objs {
133- deployment , ok := obj .(* appsv1.Deployment )
134- if ok && deployment .Name == "coderd" {
135- found = true
136-
137- require .Equal (t , test .PodSecurityContext ,
138- deployment .Spec .Template .Spec .SecurityContext ,
139- "expected matching pod securityContext" )
140- require .Len (t , deployment .Spec .Template .Spec .Containers , 1 ,
141- "expected one container" )
142- require .Equal (t , test .ContainerSecurityContext ,
143- deployment .Spec .Template .Spec .Containers [0 ].SecurityContext ,
144- "expected matching container securityContext" )
145-
146- break
147- }
148- }
149- require .True (t , found , "expected coderd deployment in manifests" )
130+ coderd := MustFindDeployment (t , objs , "coderd" )
131+
132+ assert .Equal (t , test .PodSecurityContext , coderd .Spec .Template .Spec .SecurityContext ,
133+ "expected matching pod securityContext" ,
134+ )
135+ require .Len (t , coderd .Spec .Template .Spec .Containers , 1 ,
136+ "expected one container" ,
137+ )
138+ assert .Equal (t , test .ContainerSecurityContext , coderd .Spec .Template .Spec .Containers [0 ].SecurityContext ,
139+ "expected matching container securityContext" ,
140+ )
150141 })
151142 }
152143}
0 commit comments