Chart Tests

A chart contains a number of Kubernetes resources and components that work together. As a chart author, you may want to write some tests that validate that your chart works as expected when it is installed. These tests also help the chart consumer understand what your chart is supposed to do.

A test in a helm chart lives under the templates/ directory and is a pod definition that specifies a container with a given command to run. The container should exit successfully (exit 0) for a test to be considered a success. The pod definition must contain one of the helm test hook annotations: helm.sh/hook: test-success or helm.sh/hook: test-failure.

Example tests:

  • Validate that your configuration from the values.yaml file was properly injected.
    • Make sure your username and password work correctly
    • Make sure an incorrect username and password does not work
  • Assert that your services are up and correctly loadbalanced.
  • etc.You can run the pre-defined tests in Helm on a release using the command helm test <RELEASE_NAME>. For a chart consumer, this is a great way to sanity check that their release of a chart (or application) works as expected.

A Breakdown of the Helm Test Hooks

In Helm, there are two test hooks: test-success and test-failure

test-success indicates that test pod should complete successfully. In other words, the containers in the pod should exit 0.test-failure is a way to assert that a test pod should not complete successfully. If the containers in the pod do not exit 0, that indicates success.

Example Test

Here is an example of a helm test pod definition in an example wordpress chart. The test verifies the access and login to the mariadb database:

  1. wordpress/
  2. Chart.yaml
  3. README.md
  4. values.yaml
  5. charts/
  6. templates/
  7. templates/tests/test-mariadb-connection.yaml

In wordpress/templates/tests/test-mariadb-connection.yaml:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: "{{ .Release.Name }}-credentials-test"
  5. annotations:
  6. "helm.sh/hook": test-success
  7. spec:
  8. containers:
  9. - name: {{ .Release.Name }}-credentials-test
  10. image: {{ .Values.image }}
  11. env:
  12. - name: MARIADB_HOST
  13. value: {{ template "mariadb.fullname" . }}
  14. - name: MARIADB_PORT
  15. value: "3306"
  16. - name: WORDPRESS_DATABASE_NAME
  17. value: {{ default "" .Values.mariadb.mariadbDatabase | quote }}
  18. - name: WORDPRESS_DATABASE_USER
  19. value: {{ default "" .Values.mariadb.mariadbUser | quote }}
  20. - name: WORDPRESS_DATABASE_PASSWORD
  21. valueFrom:
  22. secretKeyRef:
  23. name: {{ template "mariadb.fullname" . }}
  24. key: mariadb-password
  25. command: ["sh", "-c", "mysql --host=$MARIADB_HOST --port=$MARIADB_PORT --user=$WORDPRESS_DATABASE_USER --password=$WORDPRESS_DATABASE_PASSWORD"]
  26. restartPolicy: Never

Steps to Run a Test Suite on a Release

  • $ helm install stable/wordpress
  1. NAME: quirky-walrus
  2. LAST DEPLOYED: Mon Feb 13 13:50:43 2017
  3. NAMESPACE: default
  4. STATUS: DEPLOYED
  • $ helm test quirky-walrus
  1. RUNNING: quirky-walrus-credentials-test
  2. SUCCESS: quirky-walrus-credentials-test

Notes

  • You can define as many tests as you would like in a single yaml file or spread across several yaml files in the templates/ directory
  • You are welcome to nest your test suite under a tests/ directory like <chart-name>/templates/tests/ for more isolation