Documentation ¶
Overview ¶
Package testing includes utilities for testing controllers.
Copyright 2019 The Knative Authors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Package testing includes utilities for testing controllers.
Index ¶
- func Eventf(eventType, reason, messageFmt string, args ...interface{}) string
- func InduceFailure(verb, resource string) clientgotesting.ReactionFunc
- func KeyOrDie(obj interface{}) string
- func PrependGenerateNameReactor(f *clientgotesting.Fake)
- func RunAndSyncInformers(ctx context.Context, informers ...controller.Informer) (func(), error)
- func SetupFakeContext(t testing.TB, fs ...func(context.Context) context.Context) (context.Context, []controller.Informer)
- func SetupFakeContextWithCancel(t testing.TB, fs ...func(context.Context) context.Context) (context.Context, context.CancelFunc, []controller.Informer)
- func ValidateCreates(ctx context.Context, action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
- func ValidateUpdates(ctx context.Context, action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
- type ActionRecorder
- type ActionRecorderList
- type Actions
- type CreateHookFunc
- type DeleteHookFunc
- type EventList
- type Factory
- type FakeTracker
- func (n *FakeTracker) GetObservers(obj interface{}) []types.NamespacedName
- func (*FakeTracker) OnChanged(interface{})
- func (n *FakeTracker) OnDeletedObserver(obj interface{})
- func (n *FakeTracker) References() []tracker.Reference
- func (n *FakeTracker) Track(ref corev1.ObjectReference, obj interface{}) error
- func (n *FakeTracker) TrackReference(ref tracker.Reference, obj interface{}) error
- type GenerateNameReactor
- type HookResult
- type Hooks
- func (h *Hooks) OnCreate(fake *kubetesting.Fake, resource string, rf CreateHookFunc)
- func (h *Hooks) OnDelete(fake *kubetesting.Fake, resource string, rf DeleteHookFunc)
- func (h *Hooks) OnUpdate(fake *kubetesting.Fake, resource string, rf UpdateHookFunc)
- func (h *Hooks) WaitForHooks(timeout time.Duration) error
- type NullTracker
- type ObjectSorter
- func (o *ObjectSorter) AddObjects(objs ...runtime.Object)
- func (o *ObjectSorter) IndexerForObjectType(obj runtime.Object) cache.Indexer
- func (o *ObjectSorter) ObjectsForScheme(scheme *runtime.Scheme) []runtime.Object
- func (o *ObjectSorter) ObjectsForSchemeFunc(funcs ...func(scheme *runtime.Scheme) error) []runtime.Object
- type TableRow
- type TableTest
- type UpdateHookFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InduceFailure ¶
func InduceFailure(verb, resource string) clientgotesting.ReactionFunc
InduceFailure is used in conjunction with TableTest's WithReactors field. Tests that want to induce a failure in a row of a TableTest would add:
WithReactors: []clientgotesting.ReactionFunc{ // Makes calls to create revisions return an error. InduceFailure("create", "revisions"), },
Or to target a subresource, say a patch to InMemoryChannel.Status, you would add:
WithReactors: []clientgotesting.ReactionFunc{ // Makes calls to patch inmemorychannels status subresource return an error. InduceFailure("patch", "inmemorychannels/status"), },
func KeyOrDie ¶
func KeyOrDie(obj interface{}) string
KeyOrDie returns the string key of the Kubernetes object or panics if a key cannot be generated.
func PrependGenerateNameReactor ¶
func PrependGenerateNameReactor(f *clientgotesting.Fake)
PrependGenerateNameReactor will instrument a client-go testing Fake with a reactor that simulates 'generateName' functionality
func RunAndSyncInformers ¶
func RunAndSyncInformers(ctx context.Context, informers ...controller.Informer) (func(), error)
RunAndSyncInformers runs the given informers, then makes sure their caches are all synced and in addition makes sure that all the Watch calls have been properly setup. See https://github.com/kubernetes/kubernetes/issues/95372 for background on the Watch calls tragedy.
func SetupFakeContext ¶
func SetupFakeContext(t testing.TB, fs ...func(context.Context) context.Context) (context.Context, []controller.Informer)
SetupFakeContext sets up the the Context and the fake informers for the tests. The optional fs() can be used to edit ctx before the SetupInformer steps
func SetupFakeContextWithCancel ¶
func SetupFakeContextWithCancel(t testing.TB, fs ...func(context.Context) context.Context) (context.Context, context.CancelFunc, []controller.Informer)
SetupFakeContextWithCancel sets up the the Context and the fake informers for the tests The provided context can be canceled using provided callback. The optional fs() can be used to edit ctx before the SetupInformer steps
func ValidateCreates ¶
func ValidateUpdates ¶
Types ¶
type ActionRecorder ¶
type ActionRecorder interface {
Actions() []clientgotesting.Action
}
ActionRecorder contains list of K8s request actions.
type ActionRecorderList ¶
type ActionRecorderList []ActionRecorder
ActionRecorderList is a list of ActionRecorder objects.
func (ActionRecorderList) ActionsByVerb ¶
func (l ActionRecorderList) ActionsByVerb() (Actions, error)
ActionsByVerb fills in Actions objects, sorting the actions by verb.
type Actions ¶
type Actions struct { Gets []clientgotesting.GetAction Creates []clientgotesting.CreateAction Updates []clientgotesting.UpdateAction Deletes []clientgotesting.DeleteAction DeleteCollections []clientgotesting.DeleteCollectionAction Patches []clientgotesting.PatchAction }
Actions stores list of Actions recorded by the reactors.
type CreateHookFunc ¶
type CreateHookFunc func(runtime.Object) HookResult
CreateHookFunc is a function for handling a Create hook. Its runtime.Object parameter will be the Kubernetes resource created. The resource can be cast to its actual type like this:
pod := obj.(*v1.Pod)
A return value of true marks the hook as completed. Returning false allows the hook to run again when the next resource of the requested type is created.
func ExpectNormalEventDelivery ¶
func ExpectNormalEventDelivery(t *testing.T, messageRegexp string) CreateHookFunc
ExpectNormalEventDelivery returns a hook function that can be passed to a Hooks.OnCreate() call to verify that an event of type Normal was created matching the given regular expression. For this expectation to be effective the test must also call Hooks.WaitForHooks().
func ExpectWarningEventDelivery ¶
func ExpectWarningEventDelivery(t *testing.T, messageRegexp string) CreateHookFunc
ExpectWarningEventDelivery returns a hook function that can be passed to a Hooks.OnCreate() call to verify that an event of type Warning was created matching the given regular expression. For this expectation to be effective the test must also call Hooks.WaitForHooks().
type DeleteHookFunc ¶
type DeleteHookFunc func(string) HookResult
DeleteHookFunc is a function for handling a delete hook. Its name parameter will be the name of the resource deleted. The resource itself is not available to the reactor.
type EventList ¶
type EventList struct {
Recorder *record.FakeRecorder
}
EventList exports all events during reconciliation through fake event recorder with event channel with buffer of given size.
type Factory ¶
type Factory func(*testing.T, *TableRow) (controller.Reconciler, ActionRecorderList, EventList)
Factory returns a Reconciler.Interface to perform reconciliation in table test, and ActionRecorderList/EventList to capture k8s actions/events produced during reconciliation.
type FakeTracker ¶
FakeTracker implements Tracker.
func (*FakeTracker) GetObservers ¶
func (n *FakeTracker) GetObservers(obj interface{}) []types.NamespacedName
GetObservers implements GetObservers.
func (*FakeTracker) OnChanged ¶
func (*FakeTracker) OnChanged(interface{})
OnChanged implements OnChanged.
func (*FakeTracker) OnDeletedObserver ¶
func (n *FakeTracker) OnDeletedObserver(obj interface{})
OnDeletedObserver implements OnDeletedObserver.
func (*FakeTracker) References ¶
func (n *FakeTracker) References() []tracker.Reference
References returns the list of objects being tracked
func (*FakeTracker) Track ¶
func (n *FakeTracker) Track(ref corev1.ObjectReference, obj interface{}) error
Track implements tracker.Interface.
func (*FakeTracker) TrackReference ¶
func (n *FakeTracker) TrackReference(ref tracker.Reference, obj interface{}) error
TrackReference implements tracker.Interface.
type GenerateNameReactor ¶
type GenerateNameReactor struct {
// contains filtered or unexported fields
}
GenerateNameReactor will simulate the k8s API server and generate a name for resources who's metadata.generateName property is set. This happens only for CreateAction types
This generator is deterministic (unliked k8s) and uses a global counter to help make test names predictable
func (*GenerateNameReactor) Handles ¶
func (r *GenerateNameReactor) Handles(action clientgotesting.Action) bool
Handles contains all the logic to generate the name and mutates the create action object
This is a hack as 'React' is passed a DeepCopy of the action hence this is the only opportunity to 'mutate' the action in the ReactionChain and have to continue executing additional reactors
We should push changes upstream to client-go to help us with mocking
func (*GenerateNameReactor) React ¶
func (r *GenerateNameReactor) React(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
React is noop-function
type HookResult ¶
type HookResult bool
HookResult is the return value of hook functions.
const ( // HookComplete indicates the hook function completed, and WaitForHooks should // not wait for it. HookComplete HookResult = true // HookIncomplete indicates the hook function is incomplete, and WaitForHooks // should wait for it to complete. HookIncomplete HookResult = false )
type Hooks ¶
type Hooks struct {
// contains filtered or unexported fields
}
Hooks is a utility struct that simplifies controller testing with fake clients. A Hooks struct allows attaching hook functions to actions (create, update, delete) on a specified resource type within a fake client and ensuring that all hooks complete in a timely manner.
Example ¶
h := NewHooks() f := fake.NewSimpleClientset() h.OnCreate(&f.Fake, "pods", func(obj runtime.Object) HookResult { pod := obj.(*v1.Pod) fmt.Printf("Pod %s has restart policy %v\n", pod.Name, pod.Spec.RestartPolicy) return true }) h.OnUpdate(&f.Fake, "pods", func(obj runtime.Object) HookResult { pod := obj.(*v1.Pod) fmt.Printf("Pod %s restart policy was updated to %v\n", pod.Name, pod.Spec.RestartPolicy) return true }) h.OnDelete(&f.Fake, "pods", func(name string) HookResult { fmt.Printf("Pod %s was deleted\n", name) return true }) pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "test-pod", }, Spec: v1.PodSpec{ RestartPolicy: v1.RestartPolicyAlways, }, } f.CoreV1().Pods("test").Create(context.Background(), pod, metav1.CreateOptions{}) updatedPod := pod.DeepCopy() updatedPod.Spec.RestartPolicy = v1.RestartPolicyNever f.CoreV1().Pods("test").Update(context.Background(), updatedPod, metav1.UpdateOptions{}) f.CoreV1().Pods("test").Delete(context.Background(), pod.Name, metav1.DeleteOptions{}) if err := h.WaitForHooks(time.Second); err != nil { log.Fatal(err) }
Output: Pod test-pod has restart policy Always Pod test-pod restart policy was updated to Never Pod test-pod was deleted
func NewHooks ¶
func NewHooks() *Hooks
NewHooks returns a Hooks struct that can be used to attach hooks to one or more fake clients and wait for all hooks to complete. TODO(grantr): Allow validating that a hook never fires
func (*Hooks) OnCreate ¶
func (h *Hooks) OnCreate(fake *kubetesting.Fake, resource string, rf CreateHookFunc)
OnCreate attaches a create hook to the given Fake. The hook function is executed every time a resource of the given type is created.
func (*Hooks) OnDelete ¶
func (h *Hooks) OnDelete(fake *kubetesting.Fake, resource string, rf DeleteHookFunc)
OnDelete attaches a delete hook to the given Fake. The hook function is executed every time a resource of the given type is deleted.
func (*Hooks) OnUpdate ¶
func (h *Hooks) OnUpdate(fake *kubetesting.Fake, resource string, rf UpdateHookFunc)
OnUpdate attaches an update hook to the given Fake. The hook function is executed every time a resource of the given type is updated.
func (*Hooks) WaitForHooks ¶
WaitForHooks waits until all attached hooks have returned true at least once. If the given timeout expires before that happens, an error is returned. The registered actions will no longer be executed after WaitForHooks has returned.
type NullTracker ¶
type NullTracker = FakeTracker
NullTracker implements Tracker
Alias is preserved for backwards compatibility
type ObjectSorter ¶
type ObjectSorter struct {
// contains filtered or unexported fields
}
func NewObjectSorter ¶
func NewObjectSorter(scheme *runtime.Scheme) ObjectSorter
func (*ObjectSorter) AddObjects ¶
func (o *ObjectSorter) AddObjects(objs ...runtime.Object)
func (*ObjectSorter) IndexerForObjectType ¶
func (o *ObjectSorter) IndexerForObjectType(obj runtime.Object) cache.Indexer
func (*ObjectSorter) ObjectsForScheme ¶
func (o *ObjectSorter) ObjectsForScheme(scheme *runtime.Scheme) []runtime.Object
func (*ObjectSorter) ObjectsForSchemeFunc ¶
type TableRow ¶
type TableRow struct { // Name is a descriptive name for this test suitable as a first argument to t.Run() Name string // Ctx is the context to pass to Reconcile. Defaults to context.Background() Ctx context.Context // Objects holds the state of the world at the onset of reconciliation. Objects []runtime.Object // Key is the parameter to reconciliation. // This has the form "namespace/name". Key string // WantErr holds whether we should expect the reconciliation to result in an error. WantErr bool // WantCreates holds the ordered list of Create calls we expect during reconciliation. WantCreates []runtime.Object // WantUpdates holds the ordered list of Update calls we expect during reconciliation. WantUpdates []clientgotesting.UpdateActionImpl // WantStatusUpdates holds the ordered list of Update calls, with `status` subresource set, // that we expect during reconciliation. WantStatusUpdates []clientgotesting.UpdateActionImpl // WantDeletes holds the ordered list of Delete calls we expect during reconciliation. WantDeletes []clientgotesting.DeleteActionImpl // WantDeleteCollections holds the ordered list of DeleteCollection calls we expect during reconciliation. WantDeleteCollections []clientgotesting.DeleteCollectionActionImpl // WantPatches holds the ordered list of Patch calls we expect during reconciliation. WantPatches []clientgotesting.PatchActionImpl // WantEvents holds the ordered list of events we expect during reconciliation. WantEvents []string // WithReactors is a set of functions that are installed as Reactors for the execution // of this row of the table-driven-test. WithReactors []clientgotesting.ReactionFunc // For cluster-scoped resources like ClusterIngress, it does not have to be // in the same namespace with its child resources. SkipNamespaceValidation bool // PostConditions allows custom assertions to be made after reconciliation PostConditions []func(*testing.T, *TableRow) // Reconciler holds the controller.Reconciler that was used to evaluate this row. // It is populated here to make it accessible to PostConditions. Reconciler controller.Reconciler // 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 // mock client can go in here. OtherTestData map[string]interface{} CmpOpts []cmp.Option }
TableRow holds a single row of our table test.
type UpdateHookFunc ¶
type UpdateHookFunc func(runtime.Object) HookResult
UpdateHookFunc is a function for handling an update hook. its runtime.Object parameter will be the Kubernetes resource updated. The resource can be cast to its actual type like this:
pod := obj.(*v1.Pod)
A return value of true marks the hook as completed. Returning false allows the hook to run again when the next resource of the requested type is updated.