testing

package
v0.0.0-...-83b13a5 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: Apache-2.0 Imports: 26 Imported by: 0

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 (
	// GroupVersion is group version used to register these objects
	GroupVersion = schema.GroupVersion{Group: "testing.reconciler.runtime", Version: "v1"}

	// SchemeBuilder is used to add go types to the GroupVersionKind scheme
	SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

	// AddToScheme adds the types in this group-version to the given scheme.
	AddToScheme = SchemeBuilder.AddToScheme
)
View Source
var (
	IgnoreLastTransitionTime = cmp.FilterPath(func(p cmp.Path) bool {
		return strings.HasSuffix(p.String(), "LastTransitionTime.Inner.Time")
	}, cmp.Ignore())
)
View Source
var SchemeGroupVersion = GroupVersion

compatibility with k8s.io/code-generator

Functions

func BoolPtr

func BoolPtr(b bool) *bool

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 StringPtr

func StringPtr(str string) *string

func TestLogger

func TestLogger(t *gotesting.T) logr.Logger

TestLogger gets a logger to use in unit and end to end tests

Types

type Action

type Action = clientgotesting.Action

type CreateAction

type CreateAction = clientgotesting.CreateAction

type DeleteAction

type DeleteAction = clientgotesting.DeleteAction

type DeleteRef

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

func NewDeleteRef

func NewDeleteRef(action DeleteAction) DeleteRef

type Event

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

func NewEvent

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

type Factory

type Factory interface {
	// CreateObject creates a new Kubernetes object
	CreateObject() apis.Object
}

Factory creates Kubernetes objects

func Wrapper

func Wrapper(obj apis.Object) Factory

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 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: []rifftesting.ReactionFunc{
   // Makes calls to create stream return an error.
   rifftesting.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 focussed tests are to be executed.
	// If one or more tests are focussed, 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 []Factory
	// APIGivenObjects contains objects that are only available via an API reader instead of the normal cache
	APIGivenObjects []Factory

	// 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 []Factory
	// ExpectUpdates builds the ordered list of objects expected to be updated during reconciliation
	ExpectUpdates []Factory
	// ExpectDeletes holds the ordered list of objects expected to be deleted during reconciliation
	ExpectDeletes []DeleteRef
	// ExpectStatusUpdates builds the ordered list of objects whose status is updated during reconciliation
	ExpectStatusUpdates []Factory

	// 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) Test

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

Test executes the test case.

type ReconcilerTestSuite

type ReconcilerTestSuite []ReconcilerTestCase

ReconcilerTestSuite represents a list of reconciler test cases.

func (ReconcilerTestSuite) Test

func (tb ReconcilerTestSuite) Test(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory)

Test 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 focussed tests are to be executed.
	// If one or more tests are focussed, 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 Factory
	// 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 []Factory
	// APIGivenObjects contains objects that are only available via an API reader instead of the normal cache
	APIGivenObjects []Factory

	// ExpectParent is the expected parent as mutated after the sub reconciler, or nil if no modification
	ExpectParent Factory
	// 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 []Factory
	// ExpectUpdates builds the ordered list of objects expected to be updated during reconciliation
	ExpectUpdates []Factory
	// ExpectDeletes holds the ordered list of objects expected to be deleted during reconciliation
	ExpectDeletes []DeleteRef

	// 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) Test

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

Test executes the test case.

type SubReconcilerTestSuite

type SubReconcilerTestSuite []SubReconcilerTestCase

SubReconcilerTestSuite represents a list of subreconciler test cases.

func (SubReconcilerTestSuite) Test

func (tb SubReconcilerTestSuite) Test(t *testing.T, scheme *runtime.Scheme, factory SubReconcilerFactory)

Test executes the subreconciler test suite.

type TestResource

type TestResource struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   TestResourceSpec   `json:"spec"`
	Status TestResourceStatus `json:"status"`
}

func (*TestResource) DeepCopy

func (in *TestResource) DeepCopy() *TestResource

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestResource.

func (*TestResource) DeepCopyInto

func (in *TestResource) DeepCopyInto(out *TestResource)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (*TestResource) DeepCopyObject

func (in *TestResource) DeepCopyObject() runtime.Object

DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.

func (*TestResource) Default

func (r *TestResource) Default()

type TestResourceList

type TestResourceList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

	Items []TestResource `json:"items"`
}

func (*TestResourceList) DeepCopy

func (in *TestResourceList) DeepCopy() *TestResourceList

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestResourceList.

func (*TestResourceList) DeepCopyInto

func (in *TestResourceList) DeepCopyInto(out *TestResourceList)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (*TestResourceList) DeepCopyObject

func (in *TestResourceList) DeepCopyObject() runtime.Object

DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.

type TestResourceSpec

type TestResourceSpec struct {
	Fields map[string]string `json:"fields,omitempty"`
}

+kubebuilder:object:generate=true

func (*TestResourceSpec) DeepCopy

func (in *TestResourceSpec) DeepCopy() *TestResourceSpec

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestResourceSpec.

func (*TestResourceSpec) DeepCopyInto

func (in *TestResourceSpec) DeepCopyInto(out *TestResourceSpec)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

type TestResourceStatus

type TestResourceStatus struct {
	apis.Status `json:",inline"`
	Fields      map[string]string `json:"fields,omitempty"`
}

+kubebuilder:object:generate=true

func (*TestResourceStatus) DeepCopy

func (in *TestResourceStatus) DeepCopy() *TestResourceStatus

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestResourceStatus.

func (*TestResourceStatus) DeepCopyInto

func (in *TestResourceStatus) DeepCopyInto(out *TestResourceStatus)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (*TestResourceStatus) InitializeConditions

func (rs *TestResourceStatus) InitializeConditions()

func (*TestResourceStatus) MarkNotReady

func (rs *TestResourceStatus) MarkNotReady(reason, message string, messageA ...interface{})

func (*TestResourceStatus) MarkReady

func (rs *TestResourceStatus) MarkReady()

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 Factory, 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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