testing

package
v0.0.0-...-157142f Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2019 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buildable

type Buildable interface {
	Build() runtime.Object
}

Buildable allows test fixtures to use the builder pattern. The table test runner will call Build() on any Buildable objects and use the result as the test fixture.

type DynamicMocks

type DynamicMocks struct {
	// MockResources corresponds to dynamic.Interface.
	MockResources []MockDynamicResource

	// All other fields correspond to their dynamic.ResourceInterface equivalents.
	MockCreates           []MockDynamicCreate
	MockUpdates           []MockDynamicUpdate
	MockUpdateStatuses    []MockDynamicUpdateStatus
	MockDeletes           []MockDynamicDelete
	MockDeleteCollections []MockDynamicDeleteCollection
	MockGets              []MockDynamicGet
	MockLists             []MockDynamicList
	MockWatches           []MockDynamicWatch
	MockPatches           []MockDynamicPatch
}

The mocks to run on each function type. Each function will run through the mocks in its list until one responds with 'Handled'. If there is more than one mock in the list, then the one that responds 'Handled' will be removed and not run on subsequent calls to the function. If no mocks respond 'Handled', then the real underlying client is called.

type MockClient

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

mockClient is a client.Client that allows mock responses to be returned, instead of calling the inner client.Client.

func NewMockClient

func NewMockClient(innerClient client.Client, mocks Mocks) *MockClient

func (*MockClient) Create

func (m *MockClient) Create(ctx context.Context, obj runtime.Object) error

func (*MockClient) Delete

func (m *MockClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error

func (*MockClient) Get

func (m *MockClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error

func (*MockClient) List

func (m *MockClient) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error

func (*MockClient) Status

func (m *MockClient) Status() client.StatusWriter

func (*MockClient) Update

func (m *MockClient) Update(ctx context.Context, obj runtime.Object) error

type MockCreate

type MockCreate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)

type MockDelete

type MockDelete func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)

type MockDynamicContext

type MockDynamicContext struct {
	InnerInterface dynamic.ResourceInterface
	Resource       schema.GroupVersionResource
	Namespace      string
}

type MockDynamicCreate

type MockDynamicCreate func(ctx *MockDynamicContext, obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (MockHandled, *unstructured.Unstructured, error)

All of the functions in dynamic.Resource get mocked equivalents. For the function dynamic.Resource.Foo(), the mocked equivalent will be: MockDynamicFoo func(ctx *MockDynamicContext[, Foo's arguments]) (MockHandled[, Foo's returns])

type MockDynamicDelete

type MockDynamicDelete func(ctx *MockDynamicContext, name string, options *metav1.DeleteOptions, subresources ...string) (MockHandled, error)

type MockDynamicDeleteCollection

type MockDynamicDeleteCollection func(ctx *MockDynamicContext, options *metav1.DeleteOptions, listOptions metav1.ListOptions) (MockHandled, error)

type MockDynamicGet

type MockDynamicGet func(ctx *MockDynamicContext, name string, options metav1.GetOptions, subresources ...string) (MockHandled, *unstructured.Unstructured, error)

type MockDynamicInterface

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

func NewMockDynamicInterface

func NewMockDynamicInterface(innerInterface dynamic.Interface, mocks DynamicMocks) *MockDynamicInterface

func (MockDynamicInterface) Resource

type MockDynamicPatch

type MockDynamicPatch func(ctx *MockDynamicContext, name string, pt types.PatchType, data []byte, options metav1.UpdateOptions, subresources ...string) (MockHandled, *unstructured.Unstructured, error)

type MockDynamicResource

type MockDynamicResource func(innerInterface dynamic.Interface, resource schema.GroupVersionResource) (MockHandled, dynamic.NamespaceableResourceInterface)

All of the functions in dynamic.Interface get mocked equivalents.

type MockDynamicUpdate

type MockDynamicUpdate func(ctx *MockDynamicContext, obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (MockHandled, *unstructured.Unstructured, error)

type MockDynamicWatch

type MockDynamicWatch func(ctx *MockDynamicContext, opts metav1.ListOptions) (MockHandled, watch.Interface, error)

type MockEventRecorder

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

MockEventRecorder is a recorder.EventRecorder that saves emitted v1 Events.

func NewEventRecorder

func NewEventRecorder() *MockEventRecorder

func (*MockEventRecorder) AnnotatedEventf

func (m *MockEventRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{})

func (*MockEventRecorder) Event

func (m *MockEventRecorder) Event(object runtime.Object, eventtype, reason, message string)

func (*MockEventRecorder) Eventf

func (m *MockEventRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{})

func (*MockEventRecorder) PastEventf

func (m *MockEventRecorder) PastEventf(object runtime.Object, timestamp metav1.Time, eventtype, reason, messageFmt string, args ...interface{})

type MockGet

type MockGet func(innerClient client.Client, ctx context.Context, key client.ObjectKey, obj runtime.Object) (MockHandled, error)

type MockHandled

type MockHandled int
const (
	// This mock has handled the function call, no further mocks nor the real client should be
	// called.
	Handled MockHandled = iota
	// This mock has not handled the function call, subsequent mocks or the real client should be
	// called.
	Unhandled
)

type MockList

type MockList func(innerClient client.Client, ctx context.Context, opts *client.ListOptions, list runtime.Object) (MockHandled, error)

type MockStatusUpdate

type MockStatusUpdate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)

type MockUpdate

type MockUpdate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)

type Mocks

type Mocks struct {
	MockGets          []MockGet
	MockLists         []MockList
	MockCreates       []MockCreate
	MockDeletes       []MockDelete
	MockUpdates       []MockUpdate
	MockStatusUpdates []MockStatusUpdate
}

The mocks to run on each function type. Each function will run through the mocks in its list until one responds with 'Handled'. If there is more than one mock in the list, then the one that responds 'Handled' will be removed and not run on subsequent calls to the function. If no mocks respond 'Handled', then the real underlying client is called.

type TestCase

type TestCase struct {
	// Name is a descriptive name for this test suitable as a first argument to t.Run()
	Name string

	// InitialState is the list of objects that already exists when reconciliation
	// starts.
	InitialState []runtime.Object

	// ReconcileKey is the key of the object to reconcile in namespace/name form.
	ReconcileKey string

	// WantErr is true when we expect the Reconcile function to return an error.
	WantErr bool

	// WantErrMsg contains the pattern to match the returned error message.
	// Implies WantErr = true.
	WantErrMsg string

	// WantResult is the reconcile result we expect to be returned from the
	// Reconcile function.
	WantResult reconcile.Result

	// WantPresent holds the non-exclusive set of objects we expect to exist
	// after reconciliation completes.
	WantPresent []runtime.Object

	// WantAbsent holds the list of objects expected to not exist
	// after reconciliation completes.
	WantAbsent []runtime.Object

	// WantEvent holds the list of events expected to exist after
	// reconciliation completes.
	WantEvent []corev1.Event

	// Mocks that tamper with the client's responses.
	Mocks Mocks

	// DynamicMocks that tamper with the dynamic client's responses.
	DynamicMocks DynamicMocks

	// Scheme for the dynamic client
	Scheme *runtime.Scheme

	// Fake dynamic objects
	Objects []runtime.Object

	// OtherTestData is arbitrary data needed for the test. It is not used directly by the table
	// testing framework. Instead it is used in the test method. E.g. setting up the responses for a
	// fake GCP PubSub client can go in here, as no other field makes sense for it.
	OtherTestData map[string]interface{}

	// AdditionalVerification is for any verification that needs to be done on top of the normal
	// result/error verification and WantPresent/WantAbsent.
	AdditionalVerification []func(t *testing.T, tc *TestCase)

	// IgnoreTimes causes comparisons to ignore fields of type apis.VolatileTime.
	IgnoreTimes bool
}

TestCase holds a single row of our table test.

func (*TestCase) GetClient

func (tc *TestCase) GetClient() *MockClient

GetClient returns the mockClient to use for this test case.

func (*TestCase) GetDynamicClient

func (tc *TestCase) GetDynamicClient() dynamic.Interface

GetDynamicClient returns the mockDynamicClient to use for this test case.

func (*TestCase) GetEventRecorder

func (tc *TestCase) GetEventRecorder() *MockEventRecorder

GetEventRecorder returns the mockEventRecorder to use for this test case.

func (*TestCase) Reconcile

func (tc *TestCase) Reconcile(r reconcile.Reconciler) (reconcile.Result, error)

Reconcile calls the given reconciler's Reconcile() function with the test case's reconcile request.

func (*TestCase) Runner

func (tc *TestCase) Runner(t *testing.T, r reconcile.Reconciler, c *MockClient, recorder *MockEventRecorder) func(t *testing.T)

Runner returns a testing func that can be passed to t.Run.

func (*TestCase) VerifyErr

func (tc *TestCase) VerifyErr(err error) error

VerifyErr verifies that the given error returned from Reconcile is the error expected by the test case.

func (*TestCase) VerifyResult

func (tc *TestCase) VerifyResult(result reconcile.Result) error

VerifyResult verifies that the given result returned from Reconcile is the result expected by the test case.

func (*TestCase) VerifyWantAbsent

func (tc *TestCase) VerifyWantAbsent(c client.Client) error

VerifyWantAbsent verifies that the client does not contain any of the objects expected to be absent after reconciliation.

func (*TestCase) VerifyWantEvent

func (tc *TestCase) VerifyWantEvent(eventRecorder *MockEventRecorder) error

VerifyWantEvent verifies that the eventRecorder does contain the events expected in the same order as they were emitted after reconciliation.

func (*TestCase) VerifyWantPresent

func (tc *TestCase) VerifyWantPresent(c client.Client) error

VerifyWantPresent verifies that the client contains all the objects expected to be present after reconciliation.

Jump to

Keyboard shortcuts

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