Helm in Depth — From Charts to a Platform Library›06 · Testing charts

Lesson 06 of 10 · Charts for a platform

Testing charts

Test charts like code: lint and schema-validate, render and check against Kubernetes schemas, unit-test what templates produce for given values, enforce platform policy on the rendered output, and install on a throwaway cluster in CI.

Advanced
Key wordshelm lintchart-testingct lintct installkubeconformhelm-unittestgolden filespolicy checksKyverno CLIConftestvalues.schema.jsonkind in CI

A test pyramid for charts

            ┌─────────────────────┐
            │ install on kind (ct) │  slow, real cluster, few cases
         ┌──┴─────────────────────┴──┐
         │ policy on rendered output  │  Kyverno CLI / Conftest
      ┌──┴───────────────────────────┴──┐
      │ unit tests (helm-unittest)       │  assertions per values case
   ┌──┴─────────────────────────────────┴──┐
   │ lint + schema + kubeconform             │  fast, every commit
   └─────────────────────────────────────────┘

Before the school play, you check the script spelling (lint), that every actor has a costume that fits (schema), that each scene says the right lines (unit tests), that fire exits stay clear (policy), and finally do a dress rehearsal on the real stage (install).

Fast checks on every commit

  • helm lint --strict and the schema catch structural problems and bad values.
  • Render with realistic values (ci/*-values.yaml files) and validate with kubeconform, adding schemas for your CRDs. This catches wrong API versions and fields before any cluster sees them.

Unit tests

With the helm-unittest plugin, tests live in tests/:

suite: deployment
templates: [ templates/all.yaml ]
tests:
- it: uses three replicas when set
  set: { replicas: 3 }
  documentSelector: { path: kind, value: Deployment }
  asserts:
  - equal: { path: spec.replicas, value: 3 }
- it: always runs as non-root
  documentSelector: { path: kind, value: Deployment }
  asserts:
  - equal: { path: spec.template.spec.securityContext.runAsNonRoot, value: true }

For a library chart, keep a set of representative consumer values and their expected rendered output (golden files). A pull request that changes the library then shows exactly how every kind of service changes.

Policy on rendered output

Run the same rules the cluster enforces, earlier:

$ helm template orders ./orders -f ci/prod-values.yaml > rendered.yaml
$ kyverno apply policies/ --resource rendered.yaml

(Conftest with Rego works the same way for OPA users.) Developers see "missing resource limits" in the pull request, not at deploy time.

Install tests

chart-testing (ct) lints changed charts, checks that the chart version was bumped, and installs them on a cluster (a kind cluster created in CI), running helm test afterwards. Use a few realistic values files, not every combination.

Try it

For the orders chart from the library lesson: add a tests/deployment_test.yaml with the two tests above, install the plugin (helm plugin install https://github.com/helm-unittest/helm-unittest) and run helm unittest ./orders. Then render it and run kubeconform. Break the schema on purpose and watch the pipeline fail at the cheapest step.

Recap

  • Lint, schema and kubeconform on every commit; unit tests for rendered output; policy checks on rendered manifests; a few install tests on kind.
  • Golden rendering tests make library-chart changes reviewable.
  • CI checks give early feedback; admission control remains the enforcement.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.