charttest

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package charttest holds the shared assertions for the site-metadata contract (commonLabels, commonAnnotations, podLabels, podAnnotations) that every chart in this directory implements.

Clusters running an admission policy engine (OPA Gatekeeper, Kyverno) commonly require site-specific labels on every object before they will admit it. A chart with no way to set them cannot be installed there at all, so these assertions are what keep the knobs working as templates change.

The assertions sweep every rendered object rather than a hand-listed sample. That matters more than it sounds: the equivalent contract was first written with a hand-maintained list of chart-owned keys, and the list was wrong twice. Anything derived from a render cannot drift from the templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Describe

func Describe(obj map[string]any) string

Describe identifies an object in assertion failures.

func DuplicateKeys

func DuplicateKeys(t *testing.T, manifests string) []string

DuplicateKeys reports every mapping in the stream that defines the same key twice. sigs.k8s.io/yaml routes through JSON and silently keeps the last value, so a duplicate is invisible to the other assertions here — but the API server rejects it under strict decoding, which is how a user would find out.

func PodTemplates

func PodTemplates(objects []Object, ownChartOnly bool) map[string]map[string]any

PodTemplates returns the spec.template of every workload object. When ownChartOnly is set, subchart objects are skipped.

func Render

func Render(t *testing.T, spec Spec, extraArgs ...string) string

Render runs `helm template` and returns the rendered manifests.

func RunContract

func RunContract(t *testing.T, spec Spec)

RunContract asserts the full site-metadata contract for one chart, and for each of its Variants.

func SetJSON

func SetJSON(key, jsonValue string) []string

SetJSON builds a --set-json argument pair.

func StringMap

func StringMap(obj map[string]any, path ...string) map[string]string

StringMap reads a nested map[string]string, e.g. metadata.labels.

Types

type Object

type Object struct {
	// Source is the `# Source:` path Helm emits above each document.
	Source string
	Doc    map[string]any
}

Object is one rendered document plus the chart path Helm attributed it to.

func Objects

func Objects(t *testing.T, manifests string) []Object

Objects decodes every document in a rendered manifest stream.

func (Object) FromSubchart

func (o Object) FromSubchart() bool

FromSubchart reports whether Helm rendered this object from a subchart.

Helm values are chart-scoped: a chart-local commonLabels applies to the chart it is set on and does not cross into a subchart, which is what global.* is for. Assertions about a chart-local value therefore have to skip these, while the global assertion covers every object.

type Spec

type Spec struct {
	// ChartDir is the chart directory, relative to the test file.
	ChartDir string
	// BaseArgs are the --set flags this chart needs in order to render at all.
	BaseArgs []string
	// SubchartPrefix, when set, is a rendered-source prefix that must appear in
	// the output, proving the parent's global values reached that subchart.
	SubchartPrefix string
	// Variants are additional configurations the whole contract is re-run
	// against, named for the subtest.
	//
	// A chart that renders a metadata block only under some condition satisfies
	// the contract in the default render and breaks it elsewhere — the nodemon
	// DaemonSet put its annotations inside `if $v.withDcgm`, so turning DCGM off
	// dropped commonAnnotations entirely while the default render stayed green.
	Variants map[string][]string
}

Spec describes one chart under test.

Jump to

Keyboard shortcuts

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