Documentation
¶
Overview ¶
Package testutil provides shared test helpers for controller tests.
Index ¶
- Constants
- func AssertReconcileQuiet(t *testing.T, ctx context.Context, r reconcile.Reconciler, cl client.Client, ...)
- func AssertSSAApplyNoOpThenChange(t *testing.T, ctx context.Context, cl client.Client, key types.NamespacedName, ...)
- func CRDDirPath() string
- func CleanupObject(t *testing.T, ctx context.Context, cl client.Client, obj client.Object)
- func CollectEvents(ch chan string) []string
- func DrainEvents(ch chan string)
- func GetFirstFoundEnvTestBinaryDir() string
- func Request(name string) ctrl.Request
- func RequireCondition(t *testing.T, conditions []metav1.Condition, condType string, ...)
- func RequireConditions(t *testing.T, actual []metav1.Condition, expected []ConditionExpect)
- func RequireEvents(t *testing.T, ch chan string, wantEvents []string)
- func Scheme(t *testing.T, adders ...func(*runtime.Scheme) error) *runtime.Scheme
- func StartEnvtest(adders ...func(*runtime.Scheme) error) (client.Client, func(), error)
- type ConditionExpect
Constants ¶
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
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 ¶
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 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 ¶
RequireEvents asserts that the collected events from ch exactly match wantEvents (order-independent). If wantEvents is nil the check is skipped.
func 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
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.