Sub topic 10
Writing and running chart tests
A chart test is a Kubernetes resource that checks whether an installed release works. This chapter assumes you know Helm 3 basics (install, release), can write Go templates in templates/, and have seen hooks in the previous chapters. It builds on the helm.sh/hook annotation.
What a chart test is
A chart test is a Pod, or sometimes a Job, stored under templates/tests/ and annotated with helm.sh/hook: test. Helm does not create it during helm install or helm upgrade. It creates the resource only when you run helm test <release>.
The rule for passing is simple. The test passes if its container exits with code 0. Any other exit code, or a pod that never completes within the timeout, fails the test.
Because tests are hooks, they use the same annotation system as the migration Job. The test hook event is the only difference.
A first test
This test sends an HTTP request to the chart's Service. It assumes your chart defines the helper webapp.fullname and the value service.port.
apiVersion: v1
kind: Pod
metadata:
name: {{ include "webapp.fullname" . }}-test-connection
annotations:
"helm.sh/hook": test
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
containers:
- name: wget
image: busybox:1.36
command: ["wget"]
args: ['-qO-', '{{ include "webapp.fullname" . }}:{{ .Values.service.port }}']Three details matter here:
restartPolicy: Neverstops Kubernetes from restarting a failed container. Without it, a failing test can loop until the timeout.wgetexits non-zero when the request fails or returns an HTTP error status, so the Pod's exit code reflects the result.- The
hook-delete-policyline is not in the minimal example, but you should add it. Helm 3 does not delete test Pods on its own. A secondhelm testrun then fails because a Pod with that name already exists.before-hook-creationremoves the old Pod before a new run.hook-succeededalso cleans up after a pass.
If you want to inspect a failed test, leave out hook-failed from the policy. The failed Pod then stays in the cluster so you can run kubectl describe pod on it.
Running the tests
Install the release first. Tests run against live resources, so a release that does not exist cannot be tested.
helm install demo ./webapp --wait
helm test demo --logs--wait makes the install return only after the Pods are ready. Without it, the test might run while the application is still starting and fail for no real reason. --logs prints each test Pod's output after it finishes.
Useful flags:
| Flag | Effect |
|---|---|
| `--logs` | Print the test Pods' logs after the run |
| `--timeout 120s` | Change how long Helm waits for each test (default 5m) |
| `--filter name=<test>` | Run only the named test hook |
What happens during `helm test`
Testing charts with dependencies
If a dependency can be turned off with a condition, the test must respect that. Otherwise it will fail when the cache is disabled. Wrap the template in the same condition:
{{- if .Values.cache.enabled }}
apiVersion: v1
kind: Pod
metadata:
name: {{ include "webapp.fullname" . }}-test-cache
annotations:
"helm.sh/hook": test
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
containers:
- name: check
image: busybox:1.36
command: ["nc"]
args: ["-z", "{{ .Release.Name }}-cache", "6379"]
{{- end }}The Service host name and port depend on how your cache subchart names things. Check its templates or run helm template to confirm the real values before you rely on them.
Common problems
- Test passes immediately but proves nothing. A test that only runs
echo okalways passes. Check real behavior, such as a response from the Service. - "already exists" on the second run. The delete policy is missing
before-hook-creation. - Test fails right after install. The application was not ready. Use
--waiton install, or add retries in the test command. - Pod stays in
ImagePullBackOff. The test image cannot be pulled. Use a small image your cluster can reach, and pin its tag. - Test never runs. The file is missing the
helm.sh/hook: testannotation, or it is a plain resource that Helm installs with the release.
Exercise
You run helm test demo twice. The first run passes, but the second fails with an error that the Pod demo-webapp-test-connection already exists. What is missing from the test template, and what annotation fixes it?
Show answer
The test Pod has no hook delete policy, so Helm leaves the first run's Pod in the cluster. Add "helm.sh/hook-delete-policy": before-hook-creation (optionally with hook-succeeded) so Helm removes the old Pod before creating a new one.
Exercise
Why should you install with --wait before running helm test?
Show answer
--wait makes Helm return only after the release's resources are ready. Without it, the test can run while the application is still starting and fail even though the chart is correct.