testenv

package
v0.115.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package testenv contains helpers to create and work with an encapsulated local Kubernetes test environment.

For general advice around testing, see: https://cluster-api.sigs.k8s.io/developer/testing.html

For more information about the encapsulated local Kubernetes test environment, see: https://book.kubebuilder.io/reference/envtest.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEvents added in v0.113.0

func GetEvents(ctx context.Context, client client.Client, objName, namespace string, annotations map[string]string) ([]corev1.Event, error)

GetEvents lists corev1.Event objects from the apiserver that match the given object name. If namespace is non-empty, events are also filtered by the involved object's namespace. If annotations is non-empty, only events whose annotations contain at least one matching key-value pair are returned. It returns an error if listing events from the client fails.

It reads core/v1 Events because that is the representation the events Recorder writes: the legacy core/v1 event recorder does not set EventTime and therefore is not subject to the apiserver's strict note-length validation.

func WaitForEvents added in v0.115.0

func WaitForEvents(ctx context.Context, c client.Client, objName, namespace string, annotations map[string]string, minCount int, timeout time.Duration) ([]corev1.Event, error)

WaitForEvents polls the apiserver until at least minCount corev1.Event objects matching the given object name (and optional namespace and annotations) are found, or the timeout elapses. It returns the matching events.

Kubernetes events are recorded asynchronously, so a controller emitting an event does not guarantee it is immediately queryable. More importantly, the apiserver may reject an event during admission (for example, a message longer than the server-enforced limit on the strict events path) without surfacing an error to the recorder. In that case the event never lands and this helper times out, turning a silently dropped event into a test failure. Use it to assert that events a controller emits actually survive the round-trip to the apiserver.

Types

type Environment

type Environment struct {
	manager.Manager
	client.Client
	Config *rest.Config
	// contains filtered or unexported fields
}

Environment encapsulates a Kubernetes local test environment.

func New

func New(o ...Option) *Environment

New creates a new environment spinning up a local api-server.

NOTE: This function should be called only once for each package you are running tests within, usually the environment is initialised in a suite_test.go or <package>_test.go file within a `TestMain` function.

When a testenv Environment is created, it initializes the controller-runtime's deferred logger with a default logger based on klog. In order to override this behavior, the controller-runtime logger can be initialized before creating testenv Environment.

import (
    "testing"

    "github.com/fluxcd/pkg/runtime/testenv"
    ctrl "sigs.k8s.io/controller-runtime"
    "sigs.k8s.io/controller-runtime/pkg/log/zap"
}

func TestMain(m *testing.M) {
    zlog := zap.New(zap.UseDevMode(true))
    ctrl.SetLogger(zlog)

    testEnv = testenv.New()
    ...
}

func (*Environment) AddUser added in v0.12.2

func (e *Environment) AddUser(user envtest.User, baseConfig *rest.Config) (*envtest.AuthenticatedUser, error)

AddUser provisions a new user for connecting to this Environment. The user will have the specified name & belong to the specified groups.

If a "base" config is specified, the returned REST Config will contain those settings as well as any required by the authentication method. It can also be used to specify options like QPS.

func (*Environment) Cleanup

func (e *Environment) Cleanup(ctx context.Context, objs ...client.Object) error

Cleanup deletes all the given objects.

func (*Environment) CleanupAndWait

func (e *Environment) CleanupAndWait(ctx context.Context, objs ...client.Object) error

CleanupAndWait deletes all the given objects and waits for the cache to be updated accordingly.

NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.

func (*Environment) CreateAndWait

func (e *Environment) CreateAndWait(ctx context.Context, obj client.Object, opts ...client.CreateOption) error

CreateAndWait creates the given object and waits for the cache to be updated accordingly.

NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.

func (*Environment) CreateNamespace

func (e *Environment) CreateNamespace(ctx context.Context, generateName string) (*corev1.Namespace, error)

CreateNamespace creates a new namespace with a generated name.

func (*Environment) Start

func (e *Environment) Start(ctx context.Context) error

Start starts the test environment.

func (*Environment) Stop

func (e *Environment) Stop() error

Stop stops the test environment.

type Option

type Option func(*options)

Option sets a configuration for the Environment.

func WithCRDPath

func WithCRDPath(path ...string) Option

WithCRDPath configures the paths the envtest.Environment should look at for Custom Resource Definitions.

func WithMaxConcurrentReconciles added in v0.38.0

func WithMaxConcurrentReconciles(max int) Option

WithMaxConcurrentReconciles configures the maximum number of concurrent Reconciles which can be run.

func WithScheme

func WithScheme(scheme *runtime.Scheme) Option

WithScheme configures the runtime.Scheme for the Environment. If no scheme is configured, the Environment defaults to the global runtime.Scheme.

Jump to

Keyboard shortcuts

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