Skip to content

Commit

Permalink
Add volumes and env vars to helm hook test pod
Browse files Browse the repository at this point in the history
 - Uses the same extraEnvironmentVars, volumes and volumeMounts set on
   the server statefulset to configure the Vault server test pod used by
   the helm test hook
 - This is necessary in situations where TLS is configured, but the
   certificates are not affiliated with the k8s CA / part of k8s PKI

 - Fixes hashicorpGH-665
  • Loading branch information
Iristyle committed Jan 19, 2022
1 parent 00d7022 commit f990f15
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
10 changes: 9 additions & 1 deletion templates/tests/server-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spec:
env:
- name: VAULT_ADDR
value: {{ include "vault.scheme" . }}://{{ template "vault.fullname" . }}.{{ .Release.Namespace }}.svc:{{ .Values.server.service.port }}
{{- include "vault.extraEnvironmentVars" .Values.server | nindent 8 }}
command:
- /bin/sh
- -c
Expand All @@ -37,7 +38,14 @@ spec:
fi
exit 0
volumeMounts:
{{- if .Values.server.volumeMounts }}
{{- toYaml .Values.server.volumeMounts | nindent 8}}
{{- end }}
volumes:
{{- if .Values.server.volumes }}
{{- toYaml .Values.server.volumes | nindent 4}}
{{- end }}
restartPolicy: Never
{{- end }}
{{- end }}
80 changes: 80 additions & 0 deletions test/unit/server-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,83 @@ load _helpers
yq -r '.spec.containers[0].resources' | tee /dev/stderr)
[ "${actual}" = "null" ]
}

#--------------------------------------------------------------------
# volumes

@test "server/standalone-server-test-Pod: server.volumes adds volume" {
cd `chart_dir`

# Test that it defines it
local object=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.volumes[0].name=plugins' \
--set 'server.volumes[0].emptyDir=\{\}' \
. | tee /dev/stderr |
yq -r '.spec.volumes[] | select(.name == "plugins")' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.emptyDir' | tee /dev/stderr)
[ "${actual}" = "{}" ]
}

#--------------------------------------------------------------------
# volumeMounts

@test "server/standalone-server-test-Pod: server.volumeMounts adds volumeMount" {
cd `chart_dir`

# Test that it defines it
local object=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.volumeMounts[0].name=plugins' \
--set 'server.volumeMounts[0].mountPath=/usr/local/libexec/vault' \
--set 'server.volumeMounts[0].readOnly=true' \
. | tee /dev/stderr |
yq -r '.spec.containers[0].volumeMounts[] | select(.name == "plugins")' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.mountPath' | tee /dev/stderr)
[ "${actual}" = "/usr/local/libexec/vault" ]

local actual=$(echo $object |
yq -r '.readOnly' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

#--------------------------------------------------------------------
# extraEnvironmentVars

@test "server/standalone-server-test-Pod: set extraEnvironmentVars" {
cd `chart_dir`
local object=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.standalone.enabled=true' \
--set 'server.extraEnvironmentVars.FOO=bar' \
--set 'server.extraEnvironmentVars.FOOBAR=foobar' \
. | tee /dev/stderr |
yq -r '.spec.containers[0].env' | tee /dev/stderr)

local name=$(echo $object |
yq -r 'map(select(.name=="FOO")) | .[] .value' | tee /dev/stderr)
[ "${name}" = "bar" ]

local name=$(echo $object |
yq -r 'map(select(.name=="FOOBAR")) | .[] .value' | tee /dev/stderr)
[ "${name}" = "foobar" ]

local object=$(helm template \
--show-only templates/tests/server-test.yaml \
--set 'server.extraEnvironmentVars.FOO=bar' \
--set 'server.extraEnvironmentVars.FOOBAR=foobar' \
. | tee /dev/stderr |
yq -r '.spec.containers[0].env' | tee /dev/stderr)

local name=$(echo $object |
yq -r 'map(select(.name=="FOO")) | .[] .value' | tee /dev/stderr)
[ "${name}" = "bar" ]

local name=$(echo $object |
yq -r 'map(select(.name=="FOOBAR")) | .[] .value' | tee /dev/stderr)
[ "${name}" = "foobar" ]
}

0 comments on commit f990f15

Please sign in to comment.