testutil

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package testutil provides shared test helpers for controller tests.

Index

Constants

View Source
const (
	// TestNamespace is the namespace used in tests.
	TestNamespace = "default"
	// TestNodeName is the node name used in tests.
	TestNodeName = "test-node"
)

Variables

This section is empty.

Functions

func AssertReconcileQuiet added in v0.4.1

func AssertReconcileQuiet(
	t *testing.T,
	ctx context.Context,
	r reconcile.Reconciler,
	cl client.Client,
	key types.NamespacedName,
	obj client.Object,
	maxSettle, checkRounds int,
	conditions func(obj client.Object) *[]metav1.Condition,
	applyStatus func(obj client.Object) error,
)

AssertReconcileQuiet settles the object via repeated reconciles until its resourceVersion stops changing, then backdates every status condition's LastTransitionTime so a per-reconcile re-stamp becomes observable regardless of wall-clock (second) granularity (a fast in-process test would otherwise hide it), and finally asserts that further reconciles move neither resourceVersion nor any condition timestamp. Drift in either is the reconcile-storm signature.

obj is a non-nil empty typed object used for fetching. conditions returns a pointer to obj's status conditions slice; applyStatus persists obj's status under the controller's field manager.

func AssertSSAApplyNoOpThenChange added in v0.4.1

func AssertSSAApplyNoOpThenChange(
	t *testing.T,
	ctx context.Context,
	cl client.Client,
	key types.NamespacedName,
	obj client.Object,
	apply func() error,
	mutate func() error,
)

AssertSSAApplyNoOpThenChange proves the server-side-apply contract the steady-state quiet relies on: re-applying identical status content does not bump resourceVersion (no watch event), while a real change does. apply must be idempotent; mutate must change the status. obj is a non-nil empty typed object used for fetching. RV-equality holds only because status writes use client.Apply with ForceOwnership; a switch to Status().Update would bump resourceVersion every apply.

func CRDDirPath

func CRDDirPath() string

CRDDirPath returns the CRD directory path relative to an instance controller package (controllers/instance/X/).

func CleanupObject added in v0.4.1

func CleanupObject(t *testing.T, ctx context.Context, cl client.Client, obj client.Object)

CleanupObject removes finalizers (retrying on conflict) and deletes obj, tolerating an already-deleted object. Use it from t.Cleanup so a finalized object never strands and blocks a later test reusing the same name.

func CollectEvents

func CollectEvents(ch chan string) []string

CollectEvents drains all currently buffered events from the FakeRecorder channel and returns them. It reads until the channel is empty (non-blocking).

func DrainEvents

func DrainEvents(ch chan string)

DrainEvents discards all currently buffered events from the FakeRecorder channel.

func GetFirstFoundEnvTestBinaryDir

func GetFirstFoundEnvTestBinaryDir() string

GetFirstFoundEnvTestBinaryDir returns the first found envtest binary directory, or empty string if not found.

func Request

func Request(name string) ctrl.Request

Request creates a ctrl.Request for the given resource name in TestNamespace.

func RequireCondition

func RequireCondition(t *testing.T, conditions []metav1.Condition, condType string, status metav1.ConditionStatus, reason string)

RequireCondition finds a condition by type and asserts its status and reason.

func RequireConditions

func RequireConditions(t *testing.T, actual []metav1.Condition, expected []ConditionExpect)

RequireConditions asserts that the given conditions match all expectations (count + each entry).

func RequireEvents

func RequireEvents(t *testing.T, ch chan string, wantEvents []string)

RequireEvents asserts that the collected events from ch exactly match wantEvents (order-independent). If wantEvents is nil the check is skipped.

func Scheme

func Scheme(t *testing.T, adders ...func(*runtime.Scheme) error) *runtime.Scheme

Scheme creates a runtime.Scheme with common K8s types (core, apps, rbac) and any additional types registered via the provided adders.

func StartEnvtest added in v0.4.1

func StartEnvtest(adders ...func(*runtime.Scheme) error) (client.Client, func(), error)

StartEnvtest boots an envtest API server with the project CRDs installed and a freshly built scheme (Kubernetes built-ins plus the given adders), returning a client and a stop function to defer. The scheme is local to this call, never the client-go global, so concurrent suites and repeated calls cannot contaminate one another.

Types

type ConditionExpect

type ConditionExpect struct {
	Type   string
	Status metav1.ConditionStatus
	Reason string
}

ConditionExpect describes an expected condition for declarative assertions.

Jump to

Keyboard shortcuts

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