testing

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2025 License: Apache-2.0 Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const (
	SimpleEnvironmentDefaultKey = "default"
)

Variables

This section is empty.

Functions

func DefaultScheme

func DefaultScheme() *runtime.Scheme

func ExpectNoRequeue

func ExpectNoRequeue(res reconcile.Result)

ExpectNoRequeue expects the given result to indicate no requeue.

func ExpectRequeue

func ExpectRequeue(res reconcile.Result)

ExpectRequeue expects the given result to indicate a requeue.

func GetFakeClient

func GetFakeClient(scheme *runtime.Scheme, initObjects ...client.Object) (client.WithWatch, error)

GetFakeClient returns a fake Kubernetes client with the given objects initialized.

func GetFakeClientWithDynamicObjects

func GetFakeClientWithDynamicObjects(scheme *runtime.Scheme, dynamicObjects []client.Object, initObjects ...client.Object) (client.WithWatch, error)

GetFakeClientWithDynamicObjects returns a fake Kubernetes client with the given objects initialized. Dynamic objects are objects that are not known at compile time, but are create while running the controller. The dynamic objects are used to initialize the status subresource.

func LoadObject

func LoadObject(obj any, paths ...string) error

LoadObject reads a file and unmarshals it into the given object. obj must be a non-nil pointer.

func LoadObjects

func LoadObjects(p string, scheme *runtime.Scheme) ([]client.Object, error)

LoadObjects loads all Kubernetes manifests from the given path and returns them as `client.Object`.

func RequestFromObject

func RequestFromObject(obj client.Object) reconcile.Request

RequestFromObject creates a reconcile.Request from the given object.

func RequestFromStrings

func RequestFromStrings(name string, maybeNamespace ...string) reconcile.Request

RequestFromStrings creates a reconcile.Request using the specified name and namespace. The first argument is the name of the object. An optional second argument contains the namespace. All further arguments are ignored.

func ShouldNotReconcile

func ShouldNotReconcile(ctx context.Context, reconciler reconcile.Reconciler, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldNotReconcile calls the given reconciler with the given request and expects an error.

func ShouldReconcile

func ShouldReconcile(ctx context.Context, reconciler reconcile.Reconciler, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldReconcile calls the given reconciler with the given request and expects no error.

Types

type ClusterEnvironment

type ClusterEnvironment struct {
	// Client is the client for accessing the cluster.
	Client client.Client
	// Scheme is the scheme used by the client.
	Scheme *runtime.Scheme
	// FakeClientBuilderMethodCalls are the method calls that should be made on the fake.ClientBuilder during client creation.
	FakeClientBuilderMethodCalls []FakeClientBuilderMethodCall
}

type ComplexEnvironment

type ComplexEnvironment struct {
	Ctx         context.Context
	Log         logging.Logger
	Clusters    map[string]client.Client
	Reconcilers map[string]reconcile.Reconciler
}

ComplexEnvironment helps with testing controllers. Construct a new ComplexEnvironment via its builder using NewEnvironmentBuilder().

func (*ComplexEnvironment) Client

func (e *ComplexEnvironment) Client(name string) client.Client

Client returns the cluster client for the cluster with the given name.

func (*ComplexEnvironment) Reconciler

func (e *ComplexEnvironment) Reconciler(name string) reconcile.Reconciler

Reconciler returns the reconciler with the given name.

func (*ComplexEnvironment) ShouldEventuallyNotReconcile

func (e *ComplexEnvironment) ShouldEventuallyNotReconcile(reconciler string, req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result

ShouldEventuallyNotReconcile calls the given reconciler with the given request and retries until an error occurred or the timeout is reached.

func (*ComplexEnvironment) ShouldEventuallyReconcile

func (e *ComplexEnvironment) ShouldEventuallyReconcile(reconciler string, req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result

ShouldEventuallyReconcile calls the given reconciler with the given request and retries until no error occurred or the timeout is reached.

func (*ComplexEnvironment) ShouldNotReconcile

func (e *ComplexEnvironment) ShouldNotReconcile(reconciler string, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldNotReconcile calls the given reconciler with the given request and expects an error.

func (*ComplexEnvironment) ShouldReconcile

func (e *ComplexEnvironment) ShouldReconcile(reconciler string, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldReconcile calls the given reconciler with the given request and expects no error.

type ComplexEnvironmentBuilder

type ComplexEnvironmentBuilder struct {
	Clusters                map[string]*ClusterEnvironment
	Reconcilers             map[string]*ReconcilerEnvironment
	ClusterInitObjects      map[string][]client.Object
	ClusterStatusObjects    map[string][]client.Object
	ClusterInitObjectPaths  map[string][]string
	ClientCreationCallbacks map[string][]func(client.Client)
	// contains filtered or unexported fields
}

func NewComplexEnvironmentBuilder

func NewComplexEnvironmentBuilder() *ComplexEnvironmentBuilder

NewComplexEnvironmentBuilder creates a new EnvironmentBuilder.

func (*ComplexEnvironmentBuilder) Build

Build constructs the environment from the builder. Note that this function panics instead of throwing an error, as it is intended to be used in tests, where all information is static anyway.

func (*ComplexEnvironmentBuilder) WithAfterClientCreationCallback

func (eb *ComplexEnvironmentBuilder) WithAfterClientCreationCallback(name string, callback func(client.Client)) *ComplexEnvironmentBuilder

WithAfterClientCreationCallback adds a callback function that will be called with the client with the given name as argument after the client has been created (during Build()).

func (*ComplexEnvironmentBuilder) WithClient

WithClient sets the client for the cluster with the given name. You should use either WithFakeClient or WithClient for each cluster, but not both.

func (*ComplexEnvironmentBuilder) WithContext

WithContext sets the context for the environment.

func (*ComplexEnvironmentBuilder) WithDynamicObjectsWithStatus

func (eb *ComplexEnvironmentBuilder) WithDynamicObjectsWithStatus(name string, objects ...client.Object) *ComplexEnvironmentBuilder

WithDynamicObjectsWithStatus enables the status subresource for the given objects for the cluster with the given name. All objects that are created on a cluster during a test (after Build() has been called) and where interaction with the object's status is desired must be added here, otherwise the fake client will return that no status was found for the object.

func (*ComplexEnvironmentBuilder) WithFakeClient

func (eb *ComplexEnvironmentBuilder) WithFakeClient(name string, scheme *runtime.Scheme) *ComplexEnvironmentBuilder

WithFakeClient sets a fake client for the cluster with the given name. If no specific scheme is required, set it to nil or DefaultScheme(). You should use either WithFakeClient or WithClient for each cluster, but not both.

func (*ComplexEnvironmentBuilder) WithFakeClientBuilderCall

func (eb *ComplexEnvironmentBuilder) WithFakeClientBuilderCall(name string, method string, args ...any) *ComplexEnvironmentBuilder

WithFakeClientBuilderCall allows to inject method calls to fake.ClientBuilder when the fake clients are created during Build(). The fake clients are usually created using WithScheme(...).WithObjects(...).WithStatusSubresource(...).Build(). This function allows to inject additional method calls. It is only required for advanced use-cases. The method calls are executed using reflection, so take care to not make any mistakes with the spelling of the method name or the order or type of the arguments. Has no effect if the client for the respective cluster is passed in directly (and thus no fake client is constructed).

func (*ComplexEnvironmentBuilder) WithInitObjectPath

func (eb *ComplexEnvironmentBuilder) WithInitObjectPath(name string, pathSegments ...string) *ComplexEnvironmentBuilder

WithInitObjectPath adds a path to the list of paths from which to load initial objects for the cluster with the given name. If the objects should be specified directly, use WithInitObjects instead. If both are specified, the resulting object lists are concatenated. Note that this function concatenates all arguments into a single path, if you want to load files from multiple paths, call this function multiple times. Has no effect if the client for the respective cluster is passed in directly.

func (*ComplexEnvironmentBuilder) WithInitObjects

func (eb *ComplexEnvironmentBuilder) WithInitObjects(name string, objects ...client.Object) *ComplexEnvironmentBuilder

WithInitObjects sets the initial objects for the cluster with the given name. If the objects should be loaded from files, use WithInitObjectPath instead. If both are specified, the resulting object lists are concatenated. Has no effect if the client for the respective cluster is passed in directly.

func (*ComplexEnvironmentBuilder) WithLogger

WithLogger sets the logger for the environment. If the context is not set, the logger is injected into a new context.

func (*ComplexEnvironmentBuilder) WithReconciler

func (eb *ComplexEnvironmentBuilder) WithReconciler(name string, reconciler reconcile.Reconciler) *ComplexEnvironmentBuilder

WithReconciler sets the reconciler for the given name.

func (*ComplexEnvironmentBuilder) WithReconcilerConstructor

func (eb *ComplexEnvironmentBuilder) WithReconcilerConstructor(name string, constructor ReconcilerConstructorForMultipleClusters, targets ...string) *ComplexEnvironmentBuilder

WithReconcilerConstructor sets the constructor for the Reconciler for the given name. The Reconciler will be constructed during Build() from the given function and the clients retrieved from the passed in targets list. The clients are passed in the function in the same order as they are listed in the targets list.

type Environment

type Environment struct {
	*ComplexEnvironment
}

Environment is a wrapper around ComplexEnvironment. It is meant to ease the usage for simple use-cases (meaning only one reconciler and only one cluster). Use the EnvironmentBuilder to construct a new Environment.

func (*Environment) Client

func (e *Environment) Client() client.Client

Client returns the client for the cluster.

func (*Environment) Reconciler

func (e *Environment) Reconciler() reconcile.Reconciler

Reconciler returns the reconciler.

func (*Environment) ShouldEventuallyNotReconcile

func (e *Environment) ShouldEventuallyNotReconcile(req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result

ShouldEventuallyNotReconcile calls the given reconciler with the given request and retries until an error occurred or the timeout is reached.

func (*Environment) ShouldEventuallyReconcile

func (e *Environment) ShouldEventuallyReconcile(req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result

ShouldEventuallyReconcile calls the given reconciler with the given request and retries until no error occurred or the timeout is reached.

func (*Environment) ShouldNotReconcile

func (e *Environment) ShouldNotReconcile(req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldNotReconcile calls the given reconciler with the given request and expects an error.

func (*Environment) ShouldReconcile

func (e *Environment) ShouldReconcile(req reconcile.Request, optionalDescription ...interface{}) reconcile.Result

ShouldReconcile calls the given reconciler with the given request and expects no error.

type EnvironmentBuilder

type EnvironmentBuilder struct {
	*ComplexEnvironmentBuilder
}

func NewEnvironmentBuilder

func NewEnvironmentBuilder() *EnvironmentBuilder

NewEnvironmentBuilder creates a new SimpleEnvironmentBuilder. Use this to construct a SimpleEnvironment, if you need only one reconciler and one cluster. For more complex test scenarios, use NewEnvironmentBuilder() instead.

func (*EnvironmentBuilder) Build

func (eb *EnvironmentBuilder) Build() *Environment

Build constructs the environment from the builder. Note that this function panics instead of throwing an error, as it is intended to be used in tests, where all information is static anyway.

func (*EnvironmentBuilder) WithAfterClientCreationCallback

func (eb *EnvironmentBuilder) WithAfterClientCreationCallback(callback func(client.Client)) *EnvironmentBuilder

WithAfterClientCreationCallback adds a callback function that will be called with the client as argument after it has been created (during Build()).

func (*EnvironmentBuilder) WithClient

func (eb *EnvironmentBuilder) WithClient(client client.Client) *EnvironmentBuilder

WithClient sets the client for the cluster. If not called, a fake client will be constructed.

func (*EnvironmentBuilder) WithContext

func (eb *EnvironmentBuilder) WithContext(ctx context.Context) *EnvironmentBuilder

WithContext sets the context for the environment.

func (*EnvironmentBuilder) WithDynamicObjectsWithStatus

func (eb *EnvironmentBuilder) WithDynamicObjectsWithStatus(objects ...client.Object) *EnvironmentBuilder

WithDynamicObjectsWithStatus enables the status subresource for the given objects. All objects that are created on a cluster during a test (after Build() has been called) and where interaction with the object's status is desired must be added here, otherwise the fake client will return that no status was found for the object.

func (*EnvironmentBuilder) WithFakeClient

func (eb *EnvironmentBuilder) WithFakeClient(scheme *runtime.Scheme) *EnvironmentBuilder

WithFakeClient requests a fake client. If no specific scheme is required, set it to nil or DefaultScheme(). You should use either WithFakeClient or WithClient, not both.

func (*EnvironmentBuilder) WithFakeClientBuilderCall

func (eb *EnvironmentBuilder) WithFakeClientBuilderCall(method string, args ...any) *EnvironmentBuilder

WithFakeClientBuilderCall allows to inject method calls to fake.ClientBuilder when the fake client is created during Build(). The fake client is usually created using WithScheme(...).WithObjects(...).WithStatusSubresource(...).Build(). This function allows to inject additional method calls. It is only required for advanced use-cases. The method calls are executed using reflection, so take care to not make any mistakes with the spelling of the method name or the order or type of the arguments. Has no effect if the client is passed in directly (and thus no fake client is constructed).

func (*EnvironmentBuilder) WithInitObjectPath

func (eb *EnvironmentBuilder) WithInitObjectPath(pathSegments ...string) *EnvironmentBuilder

WithInitObjectPath adds a path to the list of paths from which to load initial objects for the cluster. If the objects should be specified directly, use WithInitObjects instead. If both are specified, the resulting object lists are concatenated. Note that this function concatenates all arguments into a single path, if you want to load files from multiple paths, call this function multiple times. Has no effect if the client for the cluster is set directly.

func (*EnvironmentBuilder) WithInitObjects

func (eb *EnvironmentBuilder) WithInitObjects(objects ...client.Object) *EnvironmentBuilder

WithInitObjects sets the initial objects for the cluster. If the objects should be loaded from files, use WithInitObjectPath instead. If both are specified, the resulting object lists are concatenated. Has no effect if the client for the cluster is set directly.

func (*EnvironmentBuilder) WithLogger

func (eb *EnvironmentBuilder) WithLogger(log logging.Logger) *EnvironmentBuilder

WithLogger sets the logger for the environment. If the context is not set, the logger is injected into a new context.

func (*EnvironmentBuilder) WithReconciler

func (eb *EnvironmentBuilder) WithReconciler(reconciler reconcile.Reconciler) *EnvironmentBuilder

WithReconciler sets the reconciler. Takes precedence over WithReconcilerConstructor, if both are called.

func (*EnvironmentBuilder) WithReconcilerConstructor

func (eb *EnvironmentBuilder) WithReconcilerConstructor(constructor ReconcilerConstructor) *EnvironmentBuilder

WithReconcilerConstructor sets the constructor for the Reconciler. The Reconciler will be constructed during Build() using the client from this constructor. No effect if the reconciler is set directly via WithReconciler.

type FakeClientBuilderMethodCall

type FakeClientBuilderMethodCall struct {
	// Method is the name of the method that should be called.
	Method string
	// Args are the arguments that should be passed to the method.
	// They will be passed in in the same order as they are listed here.
	Args []any
}

FakeClientBuilderMethodCall represents a method call on a fake.ClientBuilder.

type ReconcilerConstructor

type ReconcilerConstructor func(client.Client) reconcile.Reconciler

type ReconcilerConstructorForMultipleClusters

type ReconcilerConstructorForMultipleClusters func(...client.Client) reconcile.Reconciler

type ReconcilerEnvironment

type ReconcilerEnvironment struct {
	// Reconciler is the reconciler to be tested.
	// Takes precedence over ReconcilerConstructor.
	Reconciler reconcile.Reconciler
	// ReconcilerConstructor is a function that provides the reconciler to be tested.
	// If the Reconciler field is set, this field is ignored.
	ReconcilerConstructor ReconcilerConstructorForMultipleClusters
	// Targets references the names clusterEnvironments, which represent the clusters that the controller interacts with.
	// Has no effect if the Reconciler is set directly.
	Targets []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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