testing

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: Apache-2.0 Imports: 24 Imported by: 1

Documentation

Overview

Package testing provides support for testing reconcilers. It was inspired by knative.dev/pkg/reconciler/testing.

Index

Constants

This section is empty.

Variables

View Source
var (
	IgnoreLastTransitionTime = cmp.FilterPath(func(p cmp.Path) bool {
		return strings.HasSuffix(p.String(), "LastTransitionTime")
	}, cmp.Ignore())
	IgnoreTypeMeta = cmp.FilterPath(func(p cmp.Path) bool {
		path := p.String()
		return strings.HasSuffix(path, "TypeMeta.APIVersion") || strings.HasSuffix(path, "TypeMeta.Kind")
	}, cmp.Ignore())
	IgnoreResourceVersion = cmp.FilterPath(func(p cmp.Path) bool {
		path := p.String()
		return strings.HasSuffix(path, "ObjectMeta.ResourceVersion")
	}, cmp.Ignore())

	SafeDeployDiff = cmpopts.IgnoreUnexported(resource.Quantity{})
)

Functions

func BoolPtr

func BoolPtr(b bool) *bool

func CompareActions added in v0.4.0

func CompareActions(t *testing.T, actionName string, expectedActionFactories []client.Object, actualActions []objectAction, diffOptions ...cmp.Option)

func CreateTrackRequest

func CreateTrackRequest(trackedObjGroup, trackedObjKind, trackedObjNamespace, trackedObjName string) trackBy

func Int32Ptr

func Int32Ptr(i int32) *int32

func Int64Ptr

func Int64Ptr(i int64) *int64

func NewFakeClient added in v0.3.0

func NewFakeClient(scheme *runtime.Scheme, objs ...client.Object) *clientWrapper

func StringPtr

func StringPtr(str string) *string

Types

type Action

type Action = clientgotesting.Action

type CreateAction

type CreateAction = clientgotesting.CreateAction

type DeleteAction

type DeleteAction = clientgotesting.DeleteAction

type DeleteCollectionAction added in v0.3.0

type DeleteCollectionAction = clientgotesting.DeleteCollectionAction

type DeleteCollectionRef added in v0.5.0

type DeleteCollectionRef struct {
	Group     string
	Kind      string
	Namespace string
	Labels    labels.Selector
	Fields    fields.Selector
}

func NewDeleteCollectionRef added in v0.5.0

func NewDeleteCollectionRef(action DeleteCollectionAction) DeleteCollectionRef

type DeleteRef

type DeleteRef struct {
	Group     string
	Kind      string
	Namespace string
	Name      string
}

func NewDeleteRef

func NewDeleteRef(action DeleteAction) DeleteRef

func NewDeleteRefFromObject added in v0.5.0

func NewDeleteRefFromObject(obj client.Object, scheme *runtime.Scheme) DeleteRef

type Event

type Event struct {
	metav1.TypeMeta
	types.NamespacedName
	Type    string
	Reason  string
	Message string
}

func NewEvent

func NewEvent(factory client.Object, scheme *runtime.Scheme, eventtype, reason, messageFormat string, a ...interface{}) Event

type GetAction

type GetAction = clientgotesting.GetAction

type InduceFailureOpts

type InduceFailureOpts struct {
	Error       error
	Namespace   string
	Name        string
	SubResource string
}

type ListAction

type ListAction = clientgotesting.ListAction

type PatchAction

type PatchAction = clientgotesting.PatchAction

type PatchRef added in v0.5.0

type PatchRef struct {
	Group     string
	Kind      string
	Namespace string
	Name      string
	PatchType types.PatchType
	Patch     []byte
}

func NewPatchRef added in v0.5.0

func NewPatchRef(action PatchAction) PatchRef

type ReactionFunc

type ReactionFunc = clientgotesting.ReactionFunc

func InduceFailure

func InduceFailure(verb, kind string, o ...InduceFailureOpts) ReactionFunc

InduceFailure is used in conjunction with reconciler test's WithReactors field. Tests that want to induce a failure in a testcase of a reconciler test would add:

WithReactors: []rtesting.ReactionFunc{
   // Makes calls to create stream return an error.
   rtesting.InduceFailure("create", "Stream"),
},

type Reactor

type Reactor = clientgotesting.Reactor

type ReconcilerFactory

type ReconcilerFactory func(t *testing.T, rtc *ReconcilerTestCase, c reconcilers.Config) reconcile.Reconciler

ReconcilerFactory returns a Reconciler.Interface to perform reconciliation of a test case, ActionRecorderList/EventList to capture k8s actions/events produced during reconciliation and FakeStatsReporter to capture stats.

type ReconcilerTestCase

type ReconcilerTestCase struct {
	// Name is a descriptive name for this test suitable as a first argument to t.Run()
	Name string
	// Focus is true if and only if only this and any other focused tests are to be executed.
	// If one or more tests are focused, the overall test suite will fail.
	Focus bool
	// Skip is true if and only if this test should be skipped.
	Skip bool
	// Metadata contains arbitrary value that are stored with the test case
	Metadata map[string]interface{}

	// Key identifies the object to be reconciled
	Key types.NamespacedName
	// WithReactors installs each ReactionFunc into each fake clientset. ReactionFuncs intercept
	// each call to the clientset providing the ability to mutate the resource or inject an error.
	WithReactors []ReactionFunc
	// GivenObjects build the kubernetes objects which are present at the onset of reconciliation
	GivenObjects []client.Object
	// APIGivenObjects contains objects that are only available via an API reader instead of the normal cache
	APIGivenObjects []client.Object

	// ExpectTracks holds the ordered list of Track calls expected during reconciliation
	ExpectTracks []TrackRequest
	// ExpectEvents holds the ordered list of events recorded during the reconciliation
	ExpectEvents []Event
	// ExpectCreates builds the ordered list of objects expected to be created during reconciliation
	ExpectCreates []client.Object
	// ExpectUpdates builds the ordered list of objects expected to be updated during reconciliation
	ExpectUpdates []client.Object
	// ExpectPatches builds the ordered list of objects expected to be patched during reconciliation
	ExpectPatches []PatchRef
	// ExpectDeletes holds the ordered list of objects expected to be deleted during reconciliation
	ExpectDeletes []DeleteRef
	// ExpectDeleteCollections holds the ordered list of collections expected to be deleted during reconciliation
	ExpectDeleteCollections []DeleteCollectionRef
	// ExpectStatusUpdates builds the ordered list of objects whose status is updated during reconciliation
	ExpectStatusUpdates []client.Object
	// ExpectStatusPatches builds the ordered list of objects whose status is patched during reconciliation
	ExpectStatusPatches []PatchRef

	// ShouldErr is true if and only if reconciliation is expected to return an error
	ShouldErr bool
	// ExpectedResult is compared to the result returned from the reconciler if there was no error
	ExpectedResult controllerruntime.Result
	// Verify provides the reconciliation Result and error for custom assertions
	Verify VerifyFunc

	// Prepare is called before the reconciler is executed. It is intended to prepare the broader
	// environment before the specific test case is executed. For example, setting mock expectations.
	Prepare func(t *testing.T) error
	// CleanUp is called after the test case is finished and all defined assertions complete.
	// It is indended to clean up any state created in the Prepare step or during the test
	// execution, or to make assertions for mocks.
	CleanUp func(t *testing.T) error
}

ReconcilerTestCase holds a single test case of a reconciler test suite.

func (*ReconcilerTestCase) Run added in v0.3.0

func (tc *ReconcilerTestCase) Run(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory)

Run executes the test case.

type ReconcilerTestSuite

type ReconcilerTestSuite []ReconcilerTestCase

ReconcilerTestSuite represents a list of reconciler test cases.

func (ReconcilerTestSuite) Run added in v0.3.0

func (ts ReconcilerTestSuite) Run(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory)

Run executes the reconciler test suite.

type SubReconcilerFactory

type SubReconcilerFactory func(t *testing.T, rtc *SubReconcilerTestCase, c reconcilers.Config) reconcilers.SubReconciler

SubReconcilerFactory returns a Reconciler.Interface to perform reconciliation of a test case, ActionRecorderList/EventList to capture k8s actions/events produced during reconciliation and FakeStatsReporter to capture stats.

type SubReconcilerTestCase

type SubReconcilerTestCase struct {
	// Name is a descriptive name for this test suitable as a first argument to t.Run()
	Name string
	// Focus is true if and only if only this and any other focused tests are to be executed.
	// If one or more tests are focused, the overall test suite will fail.
	Focus bool
	// Skip is true if and only if this test should be skipped.
	Skip bool
	// Metadata contains arbitrary value that are stored with the test case
	Metadata map[string]interface{}

	// Parent is the initial object passed to the sub reconciler
	Parent client.Object
	// GivenStashedValues adds these items to the stash passed into the reconciler. Factories are resolved to their object.
	GivenStashedValues map[reconcilers.StashKey]interface{}
	// WithReactors installs each ReactionFunc into each fake clientset. ReactionFuncs intercept
	// each call to the clientset providing the ability to mutate the resource or inject an error.
	WithReactors []ReactionFunc
	// GivenObjects build the kubernetes objects which are present at the onset of reconciliation
	GivenObjects []client.Object
	// APIGivenObjects contains objects that are only available via an API reader instead of the normal cache
	APIGivenObjects []client.Object

	// ExpectParent is the expected parent as mutated after the sub reconciler, or nil if no modification
	ExpectParent client.Object
	// ExpectStashedValues ensures each value is stashed. Values in the stash that are not expected are ignored. Factories are resolved to their object.
	ExpectStashedValues map[reconcilers.StashKey]interface{}
	// ExpectTracks holds the ordered list of Track calls expected during reconciliation
	ExpectTracks []TrackRequest
	// ExpectEvents holds the ordered list of events recorded during the reconciliation
	ExpectEvents []Event
	// ExpectCreates builds the ordered list of objects expected to be created during reconciliation
	ExpectCreates []client.Object
	// ExpectUpdates builds the ordered list of objects expected to be updated during reconciliation
	ExpectUpdates []client.Object
	// ExpectPatches builds the ordered list of objects expected to be patched during reconciliation
	ExpectPatches []PatchRef
	// ExpectDeletes holds the ordered list of objects expected to be deleted during reconciliation
	ExpectDeletes []DeleteRef
	// ExpectDeleteCollections holds the ordered list of collections expected to be deleted during reconciliation
	ExpectDeleteCollections []DeleteCollectionRef

	// ShouldErr is true if and only if reconciliation is expected to return an error
	ShouldErr bool
	// ShouldPanic is true if and only if reconciliation is expected to panic. A panic should only be
	// used to indicate the reconciler is misconfigured.
	ShouldPanic bool
	// ExpectedResult is compared to the result returned from the reconciler if there was no error
	ExpectedResult controllerruntime.Result
	// Verify provides the reconciliation Result and error for custom assertions
	Verify VerifyFunc

	// Prepare is called before the reconciler is executed. It is intended to prepare the broader
	// environment before the specific test case is executed. For example, setting mock expectations.
	Prepare func(t *testing.T) error
	// CleanUp is called after the test case is finished and all defined assertions complete.
	// It is indended to clean up any state created in the Prepare step or during the test
	// execution, or to make assertions for mocks.
	CleanUp func(t *testing.T) error
}

SubReconcilerTestCase holds a single testcase of a sub reconciler test.

func (*SubReconcilerTestCase) Run added in v0.3.0

func (tc *SubReconcilerTestCase) Run(t *testing.T, scheme *runtime.Scheme, factory SubReconcilerFactory)

Run executes the test case.

type SubReconcilerTestSuite

type SubReconcilerTestSuite []SubReconcilerTestCase

SubReconcilerTestSuite represents a list of subreconciler test cases.

func (SubReconcilerTestSuite) Run added in v0.3.0

func (ts SubReconcilerTestSuite) Run(t *testing.T, scheme *runtime.Scheme, factory SubReconcilerFactory)

Run executes the subreconciler test suite.

type TrackRequest

type TrackRequest struct {
	// Tracker is the object doing the tracking
	Tracker types.NamespacedName
	// Tracked is the object being tracked
	Tracked tracker.Key
}

TrackRequest records that one object is tracking another object.

func NewTrackRequest

func NewTrackRequest(t, b client.Object, scheme *runtime.Scheme) TrackRequest

type UpdateAction

type UpdateAction = clientgotesting.UpdateAction

type VerifyFunc

type VerifyFunc func(t *testing.T, result controllerruntime.Result, err error)

VerifyFunc is a verification function

func AssertErrorEqual

func AssertErrorEqual(expected error) VerifyFunc

func AssertErrorMessagef

func AssertErrorMessagef(message string, a ...interface{}) VerifyFunc

Jump to

Keyboard shortcuts

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