testenv package - github.com/fluxcd/pkg/runtime/testenv - Go Packages
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
- type Environment
- func (e *Environment) AddUser(user envtest.User, baseConfig *rest.Config) (*envtest.AuthenticatedUser, error)
- func (e *Environment) Cleanup(ctx context.Context, objs ...client.Object) error
- func (e *Environment) CleanupAndWait(ctx context.Context, objs ...client.Object) error
- func (e *Environment) CreateAndWait(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
- func (e *Environment) CreateNamespace(ctx context.Context, generateName string) (*corev1.Namespace, error)
- func (e *Environment) Start(ctx context.Context) error
- func (e *Environment) Stop() error
- type Option
This section is empty.
This section is empty.
This section is empty.
Environment encapsulates a Kubernetes local test environment.
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()
...
}
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.
Cleanup deletes all the given objects.
func (*Environment) CleanupAndWait ¶
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 ¶
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.
CreateNamespace creates a new namespace with a generated name.
Start starts the test environment.
Stop stops the test environment.
type Option func(*options)
Option sets a configuration for the Environment.
WithCRDPath configures the paths the envtest.Environment should look at for Custom Resource Definitions.
WithMaxConcurrentReconciles configures the maximum number of concurrent Reconciles which can be run.
WithScheme configures the runtime.Scheme for the Environment. If no scheme is configured, the Environment defaults to the global runtime.Scheme.