Documentation
¶
Overview ¶
Package testing contains testing utilities for controller runtimes.
Index ¶
- Variables
- func CompareAction(want Action, got ktesting.Action) error
- func CompareActions(t testinginterface.T, test ActionTest, got []ktesting.Action)
- func CompareNames(want, got NameGetter) error
- func CompareObjects(want, got ObjectGetter) error
- func ComparePatches(want, got PatchGetter) error
- func GetFullResourceName(action ktesting.Action) string
- func InitFixture(ctx context.Context, client controllercontext.Clientsets, ...) error
- func InitFixtures(ctx context.Context, client controllercontext.Clientsets, ...) error
- func NewGroupVersionResource(group, version, resource string) schema.GroupVersionResource
- func RunCommandTests(t *testing.T, cases []CommandTest)
- func RunCompleterTests(t *testing.T, cases []CompleterTest)
- type Action
- func NewCreateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action
- func NewCreateJobAction(namespace string, object runtime.Object) Action
- func NewCreateJobConfigAction(namespace string, object runtime.Object) Action
- func NewCreatePodAction(namespace string, object runtime.Object) Action
- func NewDeleteAction(resource schema.GroupVersionResource, namespace, name string) Action
- func NewDeleteJobAction(namespace, name string) Action
- func NewDeleteJobConfigAction(namespace, name string) Action
- func NewDeletePodAction(namespace, name string) Action
- func NewPatchAction(resource schema.GroupVersionResource, namespace, name string, ...) Action
- func NewPatchJobAction(namespace, name string, pt types.PatchType, patch []byte) Action
- func NewPatchJobConfigAction(namespace, name string, pt types.PatchType, patch []byte) Action
- func NewPatchPodAction(namespace, name string, pt types.PatchType, patch []byte) Action
- func NewUpdateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action
- func NewUpdateJobAction(namespace string, object runtime.Object) Action
- func NewUpdateJobConfigAction(namespace string, object runtime.Object) Action
- func NewUpdateJobConfigStatusAction(namespace string, object runtime.Object) Action
- func NewUpdateJobStatusAction(namespace string, object runtime.Object) Action
- func NewUpdatePodAction(namespace string, object runtime.Object) Action
- func NewUpdatePodStatusAction(namespace string, object runtime.Object) Action
- func NewUpdateStatusAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) Action
- func WrapAction(action ktesting.Action) Action
- type ActionGenerator
- type ActionTest
- type CombinedActions
- type CombinedReactors
- type CommandTest
- type CompleterTest
- type ControllerContext
- type Event
- type FakeRecorder
- func (f *FakeRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, ...)
- func (f *FakeRecorder) Event(object runtime.Object, eventtype, reason, message string)
- func (f *FakeRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, ...)
- func (f *FakeRecorder) GetEvents() []Event
- type Input
- type NameGetter
- type ObjectGetter
- type Output
- type PatchGetter
- type ReconcilerTest
- type ReconcilerTestCase
- type RunContext
- type SyncTarget
Constants ¶
This section is empty.
Variables ¶
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 CompareAction ¶
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 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 ¶
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 RunCompleterTests ¶ added in v0.3.0
func RunCompleterTests(t *testing.T, cases []CompleterTest)
RunCompleterTests executes all CompleterTest cases.
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 NewDeleteAction ¶
func NewDeleteAction(resource schema.GroupVersionResource, namespace, name string) Action
func NewDeleteJobAction ¶
func NewDeletePodAction ¶
func NewPatchAction ¶
func NewPatchJobAction ¶
func NewPatchJobConfigAction ¶
func NewPatchPodAction ¶
func NewUpdateAction ¶
func NewUpdateStatusAction ¶
func WrapAction ¶
type ActionGenerator ¶
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
// If true, all actions will be ignored.
Ignore bool
}
func (*CombinedActions) Compare ¶ added in v0.3.0
func (c *CombinedActions) Compare(t testinginterface.T, client *mock.Clientsets)
Compare the expected actions against that received by the mock clientsets.
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
// Procedure for running custom assertions.
Procedure func(t *testing.T, rc RunContext)
// 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 CompleterTest ¶ added in v0.3.0
type CompleterTest struct {
// Name of the test case.
Name string
// Completer under test.
Completer completion.Completer
// Fixtures to be created prior to calling the command.
Fixtures []runtime.Object
// Sets the current time.
Now time.Time
// Namespace to pass to the Completer.
Namespace string
// Expected completions, order is important.
Want []string
// Whether an error is expected, and if so, specifies a function to check if the
// error is equal.
WantError assert.ErrorAssertionFunc
}
CompleterTest is a single Completer test case to be run.
func (*CompleterTest) Run ¶ added in v0.3.0
func (c *CompleterTest) Run(t *testing.T)
Run the test case.
type ControllerContext ¶
type ControllerContext interface {
controllercontext.Context
GetHasSynced() []cache.InformerSynced
}
ControllerContext is implemented by controller contexts used in reconciler tests.
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 (*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 NameGetter ¶
type NameGetter interface {
GetName() string
}
type ObjectGetter ¶
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 the given regular expressions.
MatchesAll []*regexp.Regexp
// If specified, expects that the output to NOT contain the given string.
Excludes string
// If specified, expects that the output to NOT contain any of the given strings.
ExcludesAll []string
// If non-nil, expects that the number of lines printed matches.
NumLines *int
}
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
// Generator functions that returns Fixtures. The generators 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 FixtureGenerators
// instead of Fixtures.
FixtureGenerators []func() 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 RunContext ¶ added in v0.3.0
type RunContext struct {
// Console to interact with the TTY.
Console *console.Console
// CtrlContext to interact with clientsets.
CtrlContext *mock.Context
// Context for the command. It will be canceled on interrupt.
Context context.Context
// Cancel the Context. Exposed to allow tests to interrupt the command midway.
Cancel context.CancelFunc
}