Article directory
1 Introduction
helm chart
The tests in are templates/
located under this directory and are a job definition that specifies the container with the given command to run. The container should exit successfully (exit 0) and the test is considered successful. The job definition must contain the helm test hook
annotation :helm.sh/hook: test.
Note that Helm v3
before , job definitions needed to contain one of the following helm test hook annotations: helm.sh/hook: test-success
or helm.sh/hook: test-failure
. helm.sh/hook: test-success
is still accepted as a backwards compatible replacement helm.sh/hook: test
.
Example test:
- Verify that the configuration in the
values.yaml
file has been injected correctly. - Make sure your username and password are used correctly
- Make sure incorrect usernames and passwords don't work
- Assert that your service is up and properly load balanced
- etc.
You can run predefined tests in Helm using commands helm test <RELEASE_NAME>
. For chart consumers, this is a great way to check that the charts (or applications) they publish are working as expected.
2. demo
Here is an example of the helm test pod definition in the bitnami wordpress chart. If you download a copy of the diagram, you can view the file locally:
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm pull bitnami/wordpress --untar
wordpress/
Chart.yaml
README.md
values.yaml
charts/
templates/
templates/tests/test-mariadb-connection.yaml
in wordpress/templates/tests/test-mariadb-connection.yaml
, you'll see a test you can try:
{
{
- if .Values.mariadb.enabled }}
apiVersion: v1
kind: Pod
metadata:
name: "{
{ .Release.Name }}-credentials-test"
annotations:
"helm.sh/hook": test
spec:
containers:
- name: {
{
.Release.Name }}-credentials-test
image: {
{
template "wordpress.image" . }}
imagePullPolicy: {
{
.Values.image.pullPolicy | quote }}
{
{
- if .Values.securityContext.enabled }}
securityContext:
runAsUser: {
{
.Values.securityContext.runAsUser }}
{
{
- end }}
env:
- name: MARIADB_HOST
value: {
{
template "mariadb.fullname" . }}
- name: MARIADB_PORT
value: "3306"
- name: WORDPRESS_DATABASE_NAME
value: {
{
default "" .Values.mariadb.db.name | quote }}
- name: WORDPRESS_DATABASE_USER
value: {
{
default "" .Values.mariadb.db.user | quote }}
- name: WORDPRESS_DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: {
{
template "mariadb.fullname" . }}
key: mariadb-password
command:
- /bin/bash
- -ec
- |
mysql --host=$MARIADB_HOST --port=$MARIADB_PORT --user=$WORDPRESS_DATABASE_USER --password=$WORDPRESS_DATABASE_PASSWORD
restartPolicy: Never
{
{
- end }}
Steps to run the test suite on the release
First, install the chart on the cluster to create the release. You may have to wait for all pods to activate; if you test right after this install, it may show a delivery failure and you will need to retest.
$ helm install quirky-walrus wordpress --namespace default
$ helm test quirky-walrus
Pod quirky-walrus-credentials-test pending
Pod quirky-walrus-credentials-test pending
Pod quirky-walrus-credentials-test pending
Pod quirky-walrus-credentials-test succeeded
Pod quirky-walrus-mariadb-test-dqas5 pending
Pod quirky-walrus-mariadb-test-dqas5 pending
Pod quirky-walrus-mariadb-test-dqas5 pending
Pod quirky-walrus-mariadb-test-dqas5 pending
Pod quirky-walrus-mariadb-test-dqas5 succeeded
NAME: quirky-walrus
LAST DEPLOYED: Mon Jun 22 17:24:31 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: quirky-walrus-mariadb-test-dqas5
Last Started: Mon Jun 22 17:27:19 2020
Last Completed: Mon Jun 22 17:27:21 2020
Phase: Succeeded
TEST SUITE: quirky-walrus-credentials-test
Last Started: Mon Jun 22 17:27:17 2020
Last Completed: Mon Jun 22 17:27:19 2020
Phase: Succeeded
[...]
- You can define any number of tests in a single yaml file or spread across
templates/
multiple yaml files in a directory. - Your test suites are nested under tests/ similar directories
<chart-name>/templates/tests/
for more isolation. - A test is a Helm
hook, so annotations can be used with test resourceshelm.sh/hook-weight
.helm.sh/hook-delete-policy