Skip to content

Commit

Permalink
Add securityContext config support to Helm hook test pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Iristyle committed Jan 14, 2022
1 parent b8ce183 commit b922791
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions templates/tests/server-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ metadata:
"helm.sh/hook": test
spec:
{{- include "imagePullSecrets" . | nindent 2 }}
{{- if not .Values.global.openshift }}
securityContext:
runAsNonRoot: true
runAsGroup: {{ .Values.server.gid | default 1000 }}
runAsUser: {{ .Values.server.uid | default 100 }}
fsGroup: {{ .Values.server.gid | default 1000 }}
{{- end }}
containers:
- name: {{ .Release.Name }}-server-test
image: {{ .Values.server.image.repository }}:{{ .Values.server.image.tag | default "latest" }}
imagePullPolicy: {{ .Values.server.image.pullPolicy }}
{{- if not .Values.global.openshift }}
securityContext:
allowPrivilegeEscalation: false
{{- end }}
env:
- name: VAULT_ADDR
value: {{ include "vault.scheme" . }}://{{ template "vault.fullname" . }}.{{ .Release.Namespace }}.svc:{{ .Values.server.service.port }}
Expand Down
82 changes: 82 additions & 0 deletions test/unit/server-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,85 @@ load _helpers
yq -r 'map(select(.name=="FOOBAR")) | .[] .value' | tee /dev/stderr)
[ "${name}" = "foobar" ]
}

#--------------------------------------------------------------------
# Security Contexts
@test "server/standalone-server-test-Pod: uid default" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
. | tee /dev/stderr |
yq -r '.spec.securityContext.runAsUser' | tee /dev/stderr)
[ "${actual}" = "100" ]
}

@test "server/standalone-server-test-Pod: uid configurable" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.uid=2000' \
. | tee /dev/stderr |
yq -r '.spec.securityContext.runAsUser' | tee /dev/stderr)
[ "${actual}" = "2000" ]
}

@test "server/standalone-server-test-Pod: gid default" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
. | tee /dev/stderr |
yq -r '.spec.securityContext.runAsGroup' | tee /dev/stderr)
[ "${actual}" = "1000" ]
}

@test "server/standalone-server-test-Pod: gid configurable" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.gid=2000' \
. | tee /dev/stderr |
yq -r '.spec.securityContext.runAsGroup' | tee /dev/stderr)
[ "${actual}" = "2000" ]
}

@test "server/standalone-server-test-Pod: fsgroup default" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
. | tee /dev/stderr |
yq -r '.spec.securityContext.fsGroup' | tee /dev/stderr)
[ "${actual}" = "1000" ]
}

@test "server/standalone-server-test-Pod: fsgroup configurable" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.gid=2000' \
. | tee /dev/stderr |
yq -r '.spec.securityContext.fsGroup' | tee /dev/stderr)
[ "${actual}" = "2000" ]
}

#--------------------------------------------------------------------
# OpenShift

@test "server/standalone-server-test-Pod: OpenShift - runAsUser disabled" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'global.openshift=true' \
. | tee /dev/stderr |
yq '.spec.containers[0].securityContext.runAsUser | length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "server/standalone-server-test-Pod: OpenShift - runAsGroup disabled" {
cd `chart_dir`
local actual=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'global.openshift=true' \
. | tee /dev/stderr |
yq '.spec.containers[0].securityContext.runAsGroup | length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

0 comments on commit b922791

Please sign in to comment.