helm

package
v0.0.0-...-cd6015a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

README

Helm template tests

Running the tests

make test

The tests are written in Go, which is the only requirement. If you run these from here, be sure to update helm dependencies first by running

make -C ../../ helm-dependency-update

Adding new golden file tests

Aside from normal Go tests, we have a number of golden file tests. Golden file tests are automatically discovered - any file matching *.input.yaml will be picked up as a golden file input, and the framework will expect a corresponding *.output.yaml to exist in the same directory.

The files can be placed in any subdirectory, including nested ones. The only requirement is that they should be placed in the same one.

Generating the golden output file automatically

All the golden file test data can be regenerated by setting the REGENERATE_GOLDENFILES environment variable:

make regenerate-goldenfiles

Remember to also run the yaml formatter afterwards at the repository root:

make -C ../.. yaml-format
Generating the golden output file manually

Writing the output files by hand is painful. In most circumstances, if you have an input file, you want to generate the output using Helm, and then verify it for correctness.

This can be done by running:

helm template deploy/helm/sumologic/ \
  --namespace sumologic \
  --set sumologic.accessId='accessId' \
  --set sumologic.accessKey='accessKey' \
  -f test_name.input.yaml \
  -s ${path_to_template} >test_name.output.yaml

Some aspects of the output files would change very frequently if stored literally. For example, the Chart version appears as a label on all resources. To avoid updating it with every release, we replace it in output templates.

You'll need to modify the output files in the following ways:

  • replace all instances of the release name (collection by default) with RELEASE-NAME
  • replace all instances of the chart version with %CURRENT_CHART_VERSION%
  • replace all checksum annotation (config/checksum) values with %CONFIG_CHECKSUM%

You can use the following snippet to regenerate the output templates in a single directory:

export PATH_TO_TEMPLATE=
export CHART_VERSION=3.0.0

for filename in *.input.yaml; do
  test_name=${"${filename%%.input.yaml}"}
  helm template ../../../../deploy/helm/sumologic/ \
  --namespace sumologic \
  --set sumologic.accessId='accessId' \
  --set sumologic.accessKey='accessKey' \
  -f ${test_name}.input.yaml \
  -s ${PATH_TO_TEMPLATE} >${test_name}.output.yaml

  sed -i "s/release-name/RELEASE-NAME/g" ${test_name}.output.yaml
  sed -i "s/${CHART_VERSION}/%CURRENT_CHART_VERSION%/g" ${test_name}.output.yaml
  sed -i "s#config/checksum: .*$#checksum: '%CONFIG_CHECKSUM%'#g" ${test_name}.output.yaml
done

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetChartVersion

func GetChartVersion() (string, error)

get the chart version from Chart.yaml

func GetOtelConfigFromTemplate

func GetOtelConfigFromTemplate(t *testing.T, templateContent string) string

GetOtelConfigFromTemplate takes otel ConfigMap template content and returns the otel configuration as a yaml string

func GetOtelConfigYaml

func GetOtelConfigYaml(t *testing.T, valuesYaml string, templatePath string) string

GetOtelConfigYaml renders the given template using a values string, assumes it's an otel ConfigMap, and returns the otel configuration as a yaml string

func GetServiceMonitors

func GetServiceMonitors(t *testing.T, valuesYaml string, templatePath string) []*monitoringv1.ServiceMonitor

GetServiceMonitors returns serviceMonitors list from the given templatePath In case of error it returns empty list

func RenderTemplate

func RenderTemplate(t *testing.T, options *helm.Options, chartDir string, releaseName string, templateFiles []string, skipDependencies bool, extraHelmArgs ...string) string

RenderTemplate runs `helm template` to render the template given the provided options and returns stdout/stderr from the template command. If you pass in templateFiles, this will only render those templates. This function will fail the test if there is an error rendering the template.

func RenderTemplateE

func RenderTemplateE(t *testing.T, options *helm.Options, chartDir string, releaseName string, templateFiles []string, skipDependencies bool, extraHelmArgs ...string) (string, error)

RenderTemplateE runs `helm template` to render the template given the provided options and returns stdout/stderr from the template command. If you pass in templateFiles, this will only render those templates.

func RenderTemplateFromValuesFile

func RenderTemplateFromValuesFile(t *testing.T, valuesYaml string, templatePath string) (string, error)

RenderTemplateFromValuesYaml renders a template based on its path and a values file it uses package defaults for other parameters.

func RenderTemplateFromValuesString

func RenderTemplateFromValuesString(t *testing.T, valuesYaml string, templatePath string) string

RenderTemplateFromValuesString renders a template based on its path and a values string it uses package defaults for other parameters. This function will fail the test if there is an error rendering the template.

func RenderTemplateFromValuesStringE

func RenderTemplateFromValuesStringE(t *testing.T, valuesYaml string, templatePath string) (string, error)

RenderTemplateFromValuesStringE renders a template based on its path and a values string it uses package defaults for other parameters.

func SplitYaml

func SplitYaml(yamlDocs string) ([]string, error)

SplitYaml splits a yaml string containing multiple yaml documents into strings containing one document each

func UnmarshalMultipleFromYaml

func UnmarshalMultipleFromYaml[T any](t *testing.T, yamlDocs string) []T

UnmarshalMultipleFromYaml can unmarshal multiple objects of the same type from a yaml string containing multiple documents, separated by ---

func UnmarshalMultipleK8sObjectsFromYaml

func UnmarshalMultipleK8sObjectsFromYaml(yamlDocs string) (objects []runtime.Object, err error)

UnmarshalMultipleK8sObjectsFromYaml unmarshals all the objects in the provided yaml string into Kubernetes object. The returned type is runtime.Object, and these can later be cast to the right concrete type based on meta information.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL