Documentation
¶
Overview ¶
Package controllertest provides a small envtest harness that generated `<crd>_controller_test.go` files use in their TestMain setup. The harness exists for two reasons:
envtest binaries (kube-apiserver, etcd) are not available in every developer environment. New(t) returns a non-nil *envtest.Environment when KUBEBUILDER_ASSETS is set or when setup-envtest has installed binaries to the standard cache directory; otherwise it skips the test cleanly via t.Skip.
CRD path resolution is project-specific. The WithCRDs option lets generated tests point at "config/crd/bases" or "deploy/crd/" without re-implementing the join + glob logic.
All public surface is option-shaped so the harness is forward- compatible: adding a new knob is a new option, not a new parameter.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(t *testing.T, opts ...Option) *envtest.Environment
New constructs an envtest.Environment configured by opts, or skips the test when envtest binaries are not available.
Availability is detected via:
- KUBEBUILDER_ASSETS env var (set by `setup-envtest use ...`).
- The standard envtest cache directory ($HOME/.local/share/kubebuilder-envtest/k8s/<version>).
When neither is available, New calls t.Skip and returns nil. The generated test scaffolds check for nil and bail early.
Types ¶
type Option ¶
type Option func(*envtest.Environment)
Option configures the envtest environment built by New.
func WithCRDs ¶
WithCRDs adds CRD YAML directories to the environment's CRDDirectoryPaths. Paths may be relative to the test's working directory.
func WithScheme ¶
WithScheme registers the runtime scheme on the environment. The scheme is consumed by callers via env.Config — controller-runtime builders must AddToScheme themselves; this option is a marker that preserves the scheme alongside the env for callers that want to stash it.