Skip to content

Commit 9b47dae

Browse files
author
Barkha Choithani
committed
updated mapping and test for security context
1 parent 9f396fb commit 9b47dae

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

charts/templates/statefulset.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,7 @@ spec:
207207
fi
208208
done
209209
{{- if .Values.containerSecurityContext.enabled }}
210-
securityContext:
211-
runAsUser: {{ .Values.containerSecurityContext.runAsUser }}
212-
runAsNonRoot: {{ .Values.containerSecurityContext.runAsNonRoot }}
213-
allowPrivilegeEscalation: {{ .Values.containerSecurityContext.allowPrivilegeEscalation }}
210+
securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
214211
{{- end }}
215212
{{- if .Values.livenessProbe.enabled }}
216213
livenessProbe:

test/template/sec_template_test.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,63 @@ func TestChartTemplateSecurityEnabled(t *testing.T) {
3232
"image.repository": "marklogicdb/marklogic-db",
3333
"image.tag": "latest",
3434
"persistence.enabled": "false",
35-
"securityContext.enabled": "true",
36-
"securityContext.runAsUser": "1000",
37-
"securityContext.runAsNonRoot": "true",
38-
"securityContext.allowPrivilegeEscalation": "false",
35+
"containerSecurityContext.enabled": "true",
3936
},
4037
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
4138
}
4239

4340
// render the tempate
4441
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})
4542

46-
var statefulset appsv1.Deployment
43+
var statefulset appsv1.StatefulSet
4744
helm.UnmarshalK8SYaml(t, output, &statefulset)
4845

4946
// Verify the name and namespace matches
5047
require.Equal(t, namespaceName, statefulset.Namespace)
5148

52-
// Verify the image matches
53-
expectedImage := "marklogicdb/marklogic-db:latest"
49+
// Verify the securityContext values are set for container
50+
expectedRunAsUser := 1000
5451
statefulSetContainers := statefulset.Spec.Template.Spec.Containers
52+
actualRunAsUser := *(statefulSetContainers[0].SecurityContext.RunAsUser)
5553
require.Equal(t, len(statefulSetContainers), 1)
56-
require.Equal(t, statefulSetContainers[0].Image, expectedImage)
54+
require.Equal(t, int(actualRunAsUser), expectedRunAsUser)
55+
}
56+
57+
func TestChartTemplateSecurityDisabled(t *testing.T) {
58+
t.Parallel()
59+
60+
// Path to the helm chart we will test
61+
helmChartPath, err := filepath.Abs("../../charts")
62+
releaseName := "marklogic-sec-test"
63+
t.Log(helmChartPath, releaseName)
64+
require.NoError(t, err)
65+
66+
// Set up the namespace; confirm that the template renders the expected value for the namespace.
67+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
68+
t.Logf("Namespace: %s\n", namespaceName)
69+
70+
// Setup the args for helm install
71+
options := &helm.Options{
72+
SetValues: map[string]string{
73+
"image.repository": "marklogicdb/marklogic-db",
74+
"image.tag": "latest",
75+
"persistence.enabled": "false",
76+
"containerSecurityContext.enabled": "false",
77+
},
78+
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
79+
}
80+
81+
// render the tempate
82+
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})
83+
84+
var statefulset appsv1.StatefulSet
85+
helm.UnmarshalK8SYaml(t, output, &statefulset)
86+
87+
// Verify the name and namespace matches
88+
require.Equal(t, namespaceName, statefulset.Namespace)
89+
90+
// Verify SecurityContext is not set for container
91+
statefulSetContainers := statefulset.Spec.Template.Spec.Containers
92+
require.Equal(t, len(statefulSetContainers), 1)
93+
require.Nil(t, statefulSetContainers[0].SecurityContext)
5794
}

0 commit comments

Comments
 (0)