assertions

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package assertions contains all the assertions that can be used in tests for general testing. All methods are Gomega compatible and can be used directly with gomega library

Package assertions contains all the assertions that can be used in tests for general testing. All methods are Gomega compatible and can be used directly with gomega library

Package assertions contains all the assertions that can be used in tests for cluster testing. All methods are Gomega compatible as async assertions or assertions that can be used directly with other Gomega methods

Index

Constants

This section is empty.

Variables

View Source
var DeepEqual = DiffEqual

DeepEqual is an alias for DiffEqual uses github.com/google/go-cmp/cmp.Diff to compare objects

View Source
var DefaultIgnoreObjectMetaFields = []string{"CreationTimestamp", "DeletionTimestamp", "DeletionGracePeriodSeconds", "Finalizers", "UID", "Generation", "ManagedFields", "ResourceVersion", "SelfLink", "UID"}

DefaultIgnoreObjectMetaFields common fields to ignore in metav1.ObjectMeta using cmp.Diff method

View Source
var IgnoreRawExtension = cmpopts.IgnoreTypes(runtime.RawExtension{})

IgnoreRawExtension will ignore the fields in runtime.RawExtension in a cmp.Diff comparison

View Source
var IgnoreTypeMeta = cmpopts.IgnoreTypes(metav1.TypeMeta{})

IgnoreTypeMeta will ignore the fields in metav1.TypeMeta in a cmp.Diff comparison

Functions

func DiffEqual

func DiffEqual(expected interface{}, cmpOptions ...cmp.Option) gomega.OmegaMatcher

DiffEqual will use github.com/google/go-cmp/cmp.Diff to compare using a list of cmp.Option to add any additional cmp configuration like ignoring types or fields not necessary in the tests. This package offers a few default options like IgnoreObjectMetaFields that can be used with kubernetes Objects

func IgnoreObjectMetaFields

func IgnoreObjectMetaFields(fields ...string) cmp.Option

IgnoreObjectMetaFields will ignore the fields in metav1.ObjectMeta that are flaky or hard to create expectations for in tests. If no fields are passed, the default set fields are used as DefaultIgnoreTypeMetaFields variable

func IsDone

func IsDone(ctx TestContexter, obj client.Object) error

IsDone will check if a object is done using IsDone() bool function and return nil when it is. Otherwise will return an error printing the object struct and current data. Recommended to be used with WaitResource method Example:

WaitObject(ctx, obj, IsDone).To(Succeed(), "object should be done: %#v"  obj)

func WaitObject

func WaitObject(ctx TestContexter, obj client.Object, checkFunc ...CheckObjectFunc) AsyncAssertion

WaitObject will create an Eventually AsyncAssertion that checks for a resource status given the checkFunc function will print an error and return if getting the resource fails. Since it returns AsyncAssertion it is possible to use it with other Gomega methods Example:

checkPod := func(ctx TestContexter, obj client.Object) error { return check.Pod.Check(ctx, obj) }
pod := &corev1.Pod{ObjectMeta{Name: "pod", Namespace: "default"}}
WaitObject(ctx, pod, checkPod).To(Succeed())

// or with custom timeout and intervals
WaitObject(ctx, pod, checkPod).
  WithPolling(time.Second).
  WithTimeout(time.Minute*10).To(Succeed())

Types

type CheckObjectFunc

type CheckObjectFunc func(ctx TestContexter, obj client.Object) error

CheckObjectFunc is a function that checks an object and returns an error if it fails useful to be used with WaitResource AsyncAssertion for custom object checks

type DiffEqualMatcher

type DiffEqualMatcher struct {
	// Expected object
	Expected interface{}
	// Options for cmp.Diff function. There are multiple available options
	// inside the github.com/google/go-cmp/cmp/cmpopts package
	// as well as IgnoreTypeMetaFields and the sort of package
	Options []cmp.Option

	// DiffCleanFunc are deprecated and should be replaced with []cmp.Option
	DiffCleanFunc []func(object interface{}) interface{}
	// contains filtered or unexported fields
}

func (*DiffEqualMatcher) FailureMessage

func (matcher *DiffEqualMatcher) FailureMessage(_ interface{}) (message string)

FailureMessage returns a message to be displayed if the actual value does not match the expected value.

func (*DiffEqualMatcher) Match

func (matcher *DiffEqualMatcher) Match(actual interface{}) (success bool, err error)

Match compares the actual and expected values using cmp.Diff and given Options will return true if the values are equivalent, otherwise it will return false with any necessary error messages

func (*DiffEqualMatcher) NegatedFailureMessage

func (matcher *DiffEqualMatcher) NegatedFailureMessage(_ interface{}) (message string)

NegatedFailureMessage returns a message to be displayed if the actual value matches the expected value but is expected to not.

type IsDoneObject

type IsDoneObject interface {
	client.Object
	IsDone() bool
}

IsDoneObject interface to have a client.Object that also implements a IsDone() bool function

type TestContexter

type TestContexter interface {
	GetClient() client.Client
	GetLogger() *zap.SugaredLogger
	GetContext() context.Context
}

TestContexter a basic text context that provides Client, Context and SugeredLogger

Jump to

Keyboard shortcuts

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