charttest

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 17 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

View Source
const (
	PartOfLabel    = "app.kubernetes.io/part-of"
	ComponentLabel = "devzero.io/component"
	PartOf         = "dz-platform"
)

Platform labels every DevZero chart takes from devzero-common.

Variables

This section is empty.

Functions

func AssertImageGlobals added in v0.1.22

func AssertImageGlobals(t *testing.T, spec Spec, g ImageGlobals)

AssertImageGlobals checks the contract for the chart's default render and each variant.

func AssertPlatformLabels added in v0.1.22

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

AssertPlatformLabels checks that every rendered object carries devzero-common's platform labels, naming the chart it was rendered from, and that no pod template does: pod labels come from podLabels and the selector labels only, so adopting the library restarts no pods by itself.

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 Images added in v0.1.22

func Images(template map[string]any) []string

Images lists a pod template's init container and container images, in order.

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 PullSecretNames added in v0.1.22

func PullSecretNames(template map[string]any) []string

PullSecretNames lists the names in a pod template's imagePullSecrets. An entry without a name (the bare-string shape the API rejects) is reported as "<invalid>".

func Render

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

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

func RewriteImage added in v0.1.22

func RewriteImage(registry, ref string) string

RewriteImage is the reference rule devzero-common.imageRef implements, restated independently so the test does not trust the template it checks: keep the path, tag and digest, replace the registry host with registry, and spell out Docker Hub's implicit library/ namespace. A first path component is a host when it has a ".", a ":" or an uppercase letter, or is localhost.

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 Selectors added in v0.1.22

func Selectors(t *testing.T, objects []Object) map[string]string

Selectors returns every object's spec.selector as JSON, keyed by kind/name. Workload selectors are immutable, so tests pin them exactly.

func Set added in v0.1.22

func Set(pairs ...string) []string

Set turns key=value pairs into helm --set flags.

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.

func TryRender added in v0.1.22

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

TryRender runs `helm template` and returns its error, carrying helm's stderr, instead of failing the test, for cases where the render is expected to fail.

Types

type ImageGlobals added in v0.1.22

type ImageGlobals struct {
	// PullSecretsKey is the chart's own pull-secrets value key, merged with
	// global.imagePullSecrets (for example "imagePullSecrets").
	PullSecretsKey string
	// OwnChartOnly skips subchart objects, for a parent whose bundled
	// subchart is covered by its own package.
	OwnChartOnly bool
	// Variants are extra configurations, each rendering pod templates or
	// containers the default does not.
	Variants map[string][]string
}

ImageGlobals describes how a chart takes part in the contract.

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