Skip to content

Commit 16e49a3

Browse files
committed
install watch reaction function to fake client
Signed-off-by: Ashish Naware <ashishnaware3@gmail.com>
1 parent a740129 commit 16e49a3

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

testing/aliases.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
type Reactor = clientgotesting.Reactor
2424
type ReactionFunc = clientgotesting.ReactionFunc
25+
type WatchReactor = clientgotesting.WatchReactor
26+
type WatchReactionFunc = clientgotesting.WatchReactionFunc
2527

2628
type Action = clientgotesting.Action
2729
type GetAction = clientgotesting.GetAction

testing/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type clientWrapper struct {
5050
StatusPatchActions []PatchAction
5151
genCount int
5252
reactionChain []Reactor
53+
watchReactionChain []WatchReactor
5354
}
5455

5556
var _ TestClient = (*clientWrapper)(nil)
@@ -67,6 +68,7 @@ func NewFakeClientWrapper(client client.Client, tracker clientgotesting.ObjectTr
6768
StatusPatchActions: []PatchAction{},
6869
genCount: 0,
6970
reactionChain: []Reactor{},
71+
watchReactionChain: []WatchReactor{},
7072
}
7173
// generate names on create
7274
c.AddReactor("create", "*", func(action Action) (bool, runtime.Object, error) {
@@ -115,6 +117,10 @@ func (w *clientWrapper) PrependReactor(verb, kind string, reaction ReactionFunc)
115117
w.reactionChain = append([]Reactor{&clientgotesting.SimpleReactor{Verb: verb, Resource: kind, Reaction: reaction}}, w.reactionChain...)
116118
}
117119

120+
func (w *clientWrapper) PrependWatchReactor(kind string, reaction WatchReactionFunc) {
121+
w.watchReactionChain = append([]WatchReactor{&clientgotesting.SimpleWatchReactor{Resource: kind, Reaction: reaction}}, w.watchReactionChain...)
122+
}
123+
118124
func (w *clientWrapper) objmeta(obj runtime.Object) (schema.GroupVersionResource, string, string, error) {
119125
objref, err := ref.GetReference(w.Scheme(), obj)
120126
if err != nil {

testing/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ type ExpectConfig struct {
6969
// WithReactors installs each ReactionFunc into each fake clientset. ReactionFuncs intercept
7070
// each call to the clientset providing the ability to mutate the resource or inject an error.
7171
WithReactors []ReactionFunc
72+
// WithWatchReactors installs WatchReactionFunc into the fake clientset. This provides ability
73+
// to simulate events in the watcher.
74+
WithWatchReactors []WatchReactionFunc
7275
// GivenTracks provide a set of tracked resources to seed the tracker with
7376
GivenTracks []TrackRequest
7477

@@ -119,6 +122,11 @@ func (c *ExpectConfig) init() {
119122
reactor := c.WithReactors[len(c.WithReactors)-1-i]
120123
c.client.PrependReactor("*", "*", reactor)
121124
}
125+
for i := range c.WithWatchReactors {
126+
// in reverse order since we prepend
127+
watchReactor := c.WithWatchReactors[len(c.WithWatchReactors)-1-i]
128+
c.client.PrependWatchReactor("*", watchReactor)
129+
}
122130
c.apiReader = c.createClient(apiGivenObjects, c.StatusSubResourceTypes)
123131
c.recorder = &eventRecorder{
124132
events: []Event{},

0 commit comments

Comments
 (0)