testing

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package testing contains testing utilities for controller runtimes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ReconcilerDefaultConcurrency is the default concurrency to use for tests.
	// For simplicity, reconcilers should not have concurrent workers.
	ReconcilerDefaultConcurrency = &configv1alpha1.Concurrency{
		Workers: 1,
	}
)

Functions

func AssertErrorIs

func AssertErrorIs(target error) assert.ErrorAssertionFunc

AssertErrorIs returns assert.ErrorAssertionFunc that asserts that the error is target.

func AssertErrorIsNotFound

func AssertErrorIsNotFound() assert.ErrorAssertionFunc

AssertErrorIsNotFound returns assert.ErrorAssertionFunc that asserts that the error is a NotFoundError.

func CompareAction

func CompareAction(want Action, got ktesting.Action) error

CompareAction compares two Actions.

func CompareActions

func CompareActions(t testinginterface.T, test ActionTest, got []ktesting.Action)

CompareActions compares the actions we received against the ActionTest spec.

func CompareNames

func CompareNames(want, got NameGetter) error

CompareNames compares two names.

func CompareObjects

func CompareObjects(want, got ObjectGetter) error

CompareObjects compares two objects.

func ComparePatches

func ComparePatches(want, got PatchGetter) error

ComparePatches compares two patches.

func GetFullResourceName

func GetFullResourceName(action ktesting.Action) string

GetFullResourceName returns the full resource name, including subresource if any.

func InitFixture

func InitFixture(ctx context.Context, client controllercontext.Clientsets, fixture runtime.Object) error

InitFixture initializes a single fixture against a clientset. TODO(irvinlim): Currently we just hardcode a list of types to be initialized,

we could probably use reflection instead.

func InitFixtures

func InitFixtures(ctx context.Context, client controllercontext.Clientsets, fixtures []runtime.Object) error

InitFixtures initializes a set of fixtures against a clientset.

func NewGroupVersionResource

func NewGroupVersionResource(group, version, resource string) schema.GroupVersionResource

NewGroupVersionResource is a shorthand method to return a GroupVersionResource.

func RunCommandTests

func RunCommandTests(t *testing.T, cases []CommandTest)

RunCommandTests executes all CommandTest cases.

func WantError

func WantError(t assert.TestingT, wantErr assert.ErrorAssertionFunc, err error, i ...interface{}) bool

WantError checks err against assert.ErrorAssertionFunc, returning true if an error was encountered for short-circuiting.

Types

type Action

type Action struct {
	ktesting.Action

	// If true, will not check the given object for equality.
	IgnoreObject bool
}

Action describes a single expected action to be taken.

func NewCreateAction

func NewCreateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action

func NewCreateJobAction

func NewCreateJobAction(namespace string, object runtime.Object) Action

func NewCreateJobConfigAction

func NewCreateJobConfigAction(namespace string, object runtime.Object) Action

func NewCreatePodAction

func NewCreatePodAction(namespace string, object runtime.Object) Action

func NewDeleteAction

func NewDeleteAction(resource schema.GroupVersionResource, namespace, name string) Action

func NewDeleteJobAction

func NewDeleteJobAction(namespace, name string) Action

func NewDeleteJobConfigAction

func NewDeleteJobConfigAction(namespace, name string) Action

func NewDeletePodAction

func NewDeletePodAction(namespace, name string) Action

func NewPatchAction

func NewPatchAction(
	resource schema.GroupVersionResource,
	namespace, name string,
	pt types.PatchType,
	patch []byte,
) Action

func NewPatchJobAction

func NewPatchJobAction(namespace, name string, pt types.PatchType, patch []byte) Action

func NewPatchJobConfigAction

func NewPatchJobConfigAction(namespace, name string, pt types.PatchType, patch []byte) Action

func NewPatchPodAction

func NewPatchPodAction(namespace, name string, pt types.PatchType, patch []byte) Action

func NewUpdateAction

func NewUpdateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action

func NewUpdateJobAction

func NewUpdateJobAction(namespace string, object runtime.Object) Action

func NewUpdateJobConfigAction

func NewUpdateJobConfigAction(namespace string, object runtime.Object) Action

func NewUpdateJobConfigStatusAction

func NewUpdateJobConfigStatusAction(namespace string, object runtime.Object) Action

func NewUpdateJobStatusAction

func NewUpdateJobStatusAction(namespace string, object runtime.Object) Action

func NewUpdatePodAction

func NewUpdatePodAction(namespace string, object runtime.Object) Action

func NewUpdatePodStatusAction

func NewUpdatePodStatusAction(namespace string, object runtime.Object) Action

func NewUpdateStatusAction

func NewUpdateStatusAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action

func WrapAction

func WrapAction(action ktesting.Action) Action

type ActionGenerator

type ActionGenerator func() (Action, error)

ActionGenerator generates an Action or throws an error.

type ActionTest

type ActionTest struct {
	// Types contains the list of verbs that should be checked.
	// Defaults to write-only verbs.
	Verbs []string

	// Actions contains a list of actions that should exist in the result.
	// It is expected that they will be in the correct order.
	Actions []Action

	// ActionGenerators is like Actions except that generating an Action may fail
	// with an error. If specified, will take precedence over Actions.
	ActionGenerators []ActionGenerator
}

ActionTest describes a single test to compare Actions that were recorded versus what is expected.

func (ActionTest) GetActions

func (t ActionTest) GetActions() ([]Action, error)

func (ActionTest) GetVerbs

func (t ActionTest) GetVerbs() []string

type CombinedActions

type CombinedActions struct {
	// List of actions that are expected on the kubernetes clientset.
	Kubernetes ActionTest

	// List of actions that are expected on the furiko clientset.
	Furiko ActionTest
}

type CombinedReactors

type CombinedReactors struct {
	// List of reactors for the kubernetes clientset.
	Kubernetes []*ktesting.SimpleReactor

	// List of reactors for the furiko clientset.
	Furiko []*ktesting.SimpleReactor
}

type CommandTest

type CommandTest struct {
	// Name of the test case.
	Name string

	// Arguments to be passed to the command.
	Args []string

	// Fixtures to be created prior to calling the command.
	Fixtures []runtime.Object

	// Sets the current time.
	Now time.Time

	// Input rules for standard input.
	Stdin Input

	// Output rules for standard output.
	Stdout Output

	// Output rules for standard error.
	Stderr Output

	// Whether an error is expected, and if so, specifies a function to check if the
	// error is equal.
	WantError assert.ErrorAssertionFunc

	// List of actions that we expect to see.
	WantActions CombinedActions
}

CommandTest encapsulates a single CLI command test case to be run.

func (*CommandTest) Run

func (c *CommandTest) Run(t *testing.T)

type ControllerContext

type ControllerContext interface {
	controllercontext.Context
	GetHasSynced() []cache.InformerSynced
}

ControllerContext is implemented by controller contexts used in reconciler tests.

type Event

type Event struct {
	UID     types.UID
	Type    string
	Reason  string
	Message string
}

type FakeRecorder

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

FakeRecorder implements record.EventRecorder to intercept and compare sent events.

func NewFakeRecorder

func NewFakeRecorder() *FakeRecorder

func (*FakeRecorder) AnnotatedEventf

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

func (*FakeRecorder) Event

func (f *FakeRecorder) Event(object runtime.Object, eventtype, reason, message string)

func (*FakeRecorder) Eventf

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

func (*FakeRecorder) GetEvents

func (f *FakeRecorder) GetEvents() []Event

type Input

type Input struct {
	// If specified, the procedure will be called with a pseudo-TTY. The console
	// argument can be used to expect and send values to the TTY.
	Procedure func(console *console.Console)
}

type NameGetter

type NameGetter interface {
	GetName() string
}

type ObjectGetter

type ObjectGetter interface {
	GetObject() runtime.Object
}

type Output

type Output struct {
	// If specified, expects the output to match exactly.
	Exact string

	// If specified, expects the output to contain the given string.
	Contains string

	// If specified, expects the output to contain all the given strings.
	ContainsAll []string

	// If specified, expects the output to match the specified regexp.
	Matches *regexp.Regexp

	// If specified, expects the output to match all of the given regular expressions.
	MatchesAll []*regexp.Regexp
}

type PatchGetter

type PatchGetter interface {
	GetPatch() []byte
}

type ReconcilerTest

type ReconcilerTest struct {
	// Returns a new controller context.
	ContextFunc func(c controllercontext.Context, recorder record.EventRecorder) ControllerContext

	// Returns the reconciler to test and informer synced methods to wait for.
	ReconcilerFunc func(c ControllerContext) reconciler.Reconciler

	// Sets the current time.
	Now time.Time

	// Specifies the timeout for each test case. Defaults to 1 second.
	Timeout time.Duration

	// Stores to create.
	Stores []mock.StoreFactory
}

ReconcilerTest encapsulates multiple test cases, providing a standard framework for testing standard Reconcilers.

func (*ReconcilerTest) Run

func (r *ReconcilerTest) Run(t *testing.T, cases []ReconcilerTestCase)

Run executes all test cases.

func (*ReconcilerTest) RunTestCase

func (r *ReconcilerTest) RunTestCase(t testinginterface.T, tt ReconcilerTestCase)

RunTestCase executes a single ReconcilerTestCase.

type ReconcilerTestCase

type ReconcilerTestCase struct {
	// Name of the test case.
	Name string

	// Target to be synced. It will be created before syncing.
	Target runtime.Object

	// Generator function that returns the Target. This generator will be called
	// lazily, evaluated only after all initialization is done. For example, if the
	// target depends on the clock to have already been mocked, use TargetGenerator
	// instead of Target.
	TargetGenerator func() runtime.Object

	// SyncTarget explicitly specifies the namespace and name to sync.
	// If left empty, will derive from Target.
	SyncTarget *SyncTarget

	// Other fixtures to be created prior to the sync.
	Fixtures []runtime.Object

	// Optionally specifies the current time to mock to override from the parent
	// ReconcilerTest.
	Now time.Time

	// Optionally specifies configs to set.
	Configs controllercontext.ConfigsMap

	// Optional list of Reactors, which intercepts clientset actions.
	Reactors CombinedReactors

	// Defines additional assertion functions.
	Assert func(t assert.TestingT, tt ReconcilerTestCase, ctrlContext ControllerContext)

	// List of actions that we expect to see.
	WantActions CombinedActions

	// List of events that we expect to see.
	WantEvents []Event

	// Whether an error is expected, and if so, specifies a function to check if the
	// error is equal.
	WantError assert.ErrorAssertionFunc
}

ReconcilerTestCase encapsulates a single Reconciler test case, including fixtures, mocks, target to be synced and expected actions.

type SyncTarget

type SyncTarget struct {
	Namespace string
	Name      string
}

Jump to

Keyboard shortcuts

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