...

Package kpoll

import "edge-infra.dev/test/f2/x/ktest/kpoll"
Overview
Index

Overview ▾

Package kpoll provides a wrapper around gotest.tools/v3/poll to facilitate continually evaluating checks on K8s objects against a live cluster.

It is intended to be used for integration/end-to-end testing (e.g., envtest).

kpoll composes with edge-infra.dev/pkg/k8s/testing/kmp to allow sharing Object comparison logic across unit tests and tests against live clusters.

The main way to interact with kpoll is to create an instance of KPoll for each test you want to make assertions against clusters in:

func TestReconciler(t *testing.T) {
	kp := kpoll.New(context.Background(), kubeClient, defaultTimeout, defaultTick)
	kp.WaitOn(t, kp.ObjExists(obj))
	// Use a kmp.Komparison
	kp.WaitOn(t, kp.Check(obj, kmp.IsReady()))
}

Index ▾

func Deleted(ctx context.Context, c client.Client, objs ...client.Object) poll.Check
func Exists(ctx context.Context, c client.Client, objs ...client.Object) poll.Check
func InventoryExists(ctx context.Context, c client.Client, exp *inventory.ResourceInventory) poll.Check
func InventoryPruned(ctx context.Context, c client.Client, curr, old *inventory.ResourceInventory) poll.Check
func JoinResults(results ...poll.Result) poll.Result
type CheckOption
    func WithClient(c client.Client) CheckOption
    func WithContext(ctx context.Context) CheckOption
    func WithoutFetch() CheckOption
type KPoll
    func New(ctx context.Context, c client.Client, timeout, tick time.Duration) *KPoll
    func (k *KPoll) Check(o client.Object, c kmp.Komparison, options ...CheckOption) poll.Check
    func (k *KPoll) CheckAll(objs []client.Object, c kmp.Komparison, options ...CheckOption) poll.Check
    func (k *KPoll) Compare(o client.Object, c kmp.Komparison, options ...CheckOption) poll.Result
    func (k *KPoll) CompareAll(objs []client.Object, c kmp.Komparison, options ...CheckOption) poll.Result
    func (k *KPoll) InventoryExists(exp *inventory.ResourceInventory, options ...CheckOption) poll.Check
    func (k *KPoll) InventoryPruned(curr, old *inventory.ResourceInventory, options ...CheckOption) poll.Check
    func (k *KPoll) ObjDeleted(o client.Object, options ...CheckOption) poll.Check
    func (k *KPoll) ObjExists(o client.Object, options ...CheckOption) poll.Check
    func (k *KPoll) ObjsDeleted(objs []client.Object, options ...CheckOption) poll.Check
    func (k *KPoll) ObjsExist(objs []client.Object, options ...CheckOption) poll.Check
    func (k *KPoll) WaitOn(t *testing.T, c poll.Check, opts ...poll.SettingOp)

Package files

check_options.go checks.go doc.go kpoll.go result.go

func Deleted

func Deleted(ctx context.Context, c client.Client, objs ...client.Object) poll.Check

Deleted checks that none of the provided objs exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func Exists

func Exists(ctx context.Context, c client.Client, objs ...client.Object) poll.Check

Exists checks that all objs exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func InventoryExists

func InventoryExists(ctx context.Context, c client.Client, exp *inventory.ResourceInventory) poll.Check

InventoryExists checks that all the objects in the inventory exist on the cluster.

TODO(aw185176): Could probably use ListMeta() and additional elbow grease to avoid full parsing of unstructured objects and the multiple slice allocations for type massaging

func InventoryPruned

func InventoryPruned(ctx context.Context, c client.Client, curr, old *inventory.ResourceInventory) poll.Check

InventoryPruned checks that the difference between the current and old inventory no longer exists on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func JoinResults

func JoinResults(results ...poll.Result) poll.Result

JoinResults combines one or more polling results into a single gotest.tools/v3/poll.Result.

At least one poll.Result must be provided. If a single poll.Result is passed in, it is returned directly.

If any results contain an error, it is immediately returned. Otherwise, all unfinished results have their messages combined into a single gotest.tools/v3/poll.Continue call. If all results indicate they are done, gotest.tools/v3/poll.Success is returned.

TODO(aw18576): Consider generalizing this

type CheckOption

CheckOption controls the behavior of a check against a cluster via an instance of KPoll.

type CheckOption = func(*checkOpts)

func WithClient

func WithClient(c client.Client) CheckOption

WithClient sets the client that will be used while evaluating checks.

func WithContext

func WithContext(ctx context.Context) CheckOption

WithContext overrides the context.Context for the check. By default, the context used to instantiate the poller is used.

func WithoutFetch

func WithoutFetch() CheckOption

WithoutFetch causes Checks to be evaluated without fetching latest live state from API.

This can be useful if your test logic needs to take full control over the fetching process, or if you need to write a check based on the error value returned from Get():

k.WaitOn(t, func(t poll.LogT) poll.Result {
	// Fetch object with custom Get options, etc
	return k.Compare(obj, myComparison, kpoll.WithoutFetch())
})

type KPoll

KPoll allows asserting K8s Objects against a live cluster's state with default polling options and provides access to common assertions for K8s objects.

KPoll should be treated like other assertion helpers: instantiated for each individual test.

When instantiated as part of a test hook, this ensures that all assertions use a consistent and configurable timeout by default.

type KPoll struct {
    // contains filtered or unexported fields
}

func New

func New(ctx context.Context, c client.Client, timeout, tick time.Duration) *KPoll

func (*KPoll) Check

func (k *KPoll) Check(o client.Object, c kmp.Komparison, options ...CheckOption) poll.Check

Check creates a gotest.tools/v3/poll.Check which evaluates Object o using Komparison c on each interval of a KPoll.WaitOn call. It uses KPoll.Compare to wrap the Komparison into a function that produces a polling result so that the check is always executed against the cluster's live state.

func (*KPoll) CheckAll

func (k *KPoll) CheckAll(objs []client.Object, c kmp.Komparison, options ...CheckOption) poll.Check

CheckAll is KPoll.Check for sets of objects.

func (*KPoll) Compare

func (k *KPoll) Compare(o client.Object, c kmp.Komparison, options ...CheckOption) poll.Result

Compare returns a gotest.tools/v3/poll.Result by evaluating Object o against Komparison c. The Object is re-fetched on each polling loop, allowing a standard edge-infra.dev/pkg/k8s/testing/kmp.Komparison to be continually evaluated against a cluster's state.

The poll.Result return can be used to wrap a Komparison with additional logic in an inline poll.Check:

kwait.On(t, func(t poll.LogT) poll.Result {
	// Evaluate check precondition
	// Perform comparison as polling step
	return kwait.Compare(ctx, obj, myComparison)
})

func (*KPoll) CompareAll

func (k *KPoll) CompareAll(objs []client.Object, c kmp.Komparison, options ...CheckOption) poll.Result

CompareAll is KPoll.Compare for sets of objects. The comparison results are aggregated to produce the overall result.

func (*KPoll) InventoryExists

func (k *KPoll) InventoryExists(exp *inventory.ResourceInventory, options ...CheckOption) poll.Check

InventoryExists checks that all member objects of the expected inventory exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func (*KPoll) InventoryPruned

func (k *KPoll) InventoryPruned(curr, old *inventory.ResourceInventory, options ...CheckOption) poll.Check

func (*KPoll) ObjDeleted

func (k *KPoll) ObjDeleted(o client.Object, options ...CheckOption) poll.Check

ObjDeleted checks that a single object doesn't exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func (*KPoll) ObjExists

func (k *KPoll) ObjExists(o client.Object, options ...CheckOption) poll.Check

ObjExists checks that a single object exists on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func (*KPoll) ObjsDeleted

func (k *KPoll) ObjsDeleted(objs []client.Object, options ...CheckOption) poll.Check

ObjsDeleted checks that none of the provided objects exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func (*KPoll) ObjsExist

func (k *KPoll) ObjsExist(objs []client.Object, options ...CheckOption) poll.Check

ObjsExist checks that all objects exist on the cluster.

edge-infra.dev/pkg/k8s/object.IsDeleted is used to determine object existence.

func (*KPoll) WaitOn

func (k *KPoll) WaitOn(t *testing.T, c poll.Check, opts ...poll.SettingOp)

WaitOn wraps gotest.tools/v3/poll.WaitOn with default options based on the KPoll configuration.