Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix testclient prepend functions #14664

Merged
merged 1 commit into from Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/client/unversioned/testclient/testclient.go
Expand Up @@ -40,6 +40,8 @@ func NewSimpleFake(objects ...runtime.Object) *Fake {
fakeClient := &Fake{}
fakeClient.AddReactor("*", "*", ObjectReaction(o, api.RESTMapper))

fakeClient.AddWatchReactor("*", DefaultWatchReactor(watch.NewFake(), nil))

return fakeClient
}

Expand Down Expand Up @@ -100,21 +102,29 @@ func (c *Fake) AddReactor(verb, resource string, reaction ReactionFunc) {

// PrependReactor adds a reactor to the beginning of the chain
func (c *Fake) PrependReactor(verb, resource string, reaction ReactionFunc) {
newChain := make([]Reactor, 0, len(c.ReactionChain)+1)
newChain[0] = &SimpleReactor{verb, resource, reaction}
newChain = append(newChain, c.ReactionChain...)
c.ReactionChain = append([]Reactor{&SimpleReactor{verb, resource, reaction}}, c.ReactionChain...)
}

// AddWatchReactor appends a reactor to the end of the chain
func (c *Fake) AddWatchReactor(resource string, reaction WatchReactionFunc) {
c.WatchReactionChain = append(c.WatchReactionChain, &SimpleWatchReactor{resource, reaction})
}

// PrependWatchReactor adds a reactor to the beginning of the chain
func (c *Fake) PrependWatchReactor(resource string, reaction WatchReactionFunc) {
c.WatchReactionChain = append([]WatchReactor{&SimpleWatchReactor{resource, reaction}}, c.WatchReactionChain...)
}

// AddProxyReactor appends a reactor to the end of the chain
func (c *Fake) AddProxyReactor(resource string, reaction ProxyReactionFunc) {
c.ProxyReactionChain = append(c.ProxyReactionChain, &SimpleProxyReactor{resource, reaction})
}

// PrependProxyReactor adds a reactor to the beginning of the chain
func (c *Fake) PrependProxyReactor(resource string, reaction ProxyReactionFunc) {
c.ProxyReactionChain = append([]ProxyReactor{&SimpleProxyReactor{resource, reaction}}, c.ProxyReactionChain...)
}

// Invokes records the provided Action and then invokes the ReactFn (if provided).
// defaultReturnObj is expected to be of the same type a normal call would return.
func (c *Fake) Invokes(action Action, defaultReturnObj runtime.Object) (runtime.Object, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/job/controller_test.go
Expand Up @@ -395,9 +395,9 @@ type FakeWatcher struct {
}

func TestWatchJobs(t *testing.T) {
client := testclient.NewSimpleFake()
fakeWatch := watch.NewFake()
client := &testclient.Fake{}
client.AddWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
client.PrependWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You updated the client to have a default fake watch, is this needed now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You updated the client to have a default fake watch, is this needed now?

Yes, the fakeWatch is manipulated below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above

Yes, fakeWatch used below

manager := NewJobController(client)
manager.podStoreSynced = alwaysReady

Expand Down Expand Up @@ -458,9 +458,9 @@ func TestWatchJobs(t *testing.T) {
}

func TestWatchPods(t *testing.T) {
client := testclient.NewSimpleFake()
fakeWatch := watch.NewFake()
client := &testclient.Fake{}
client.AddWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
client.PrependWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
manager := NewJobController(client)
manager.podStoreSynced = alwaysReady

Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/replication/replication_controller_test.go
Expand Up @@ -502,10 +502,7 @@ func TestWatchPods(t *testing.T) {
}

func TestUpdatePods(t *testing.T) {
fakeWatch := watch.NewFake()
client := &testclient.Fake{}
client.AddWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
manager := NewReplicationManager(client, BurstReplicas)
manager := NewReplicationManager(testclient.NewSimpleFake(), BurstReplicas)
manager.podStoreSynced = alwaysReady

received := make(chan string)
Expand Down
9 changes: 3 additions & 6 deletions plugin/pkg/admission/namespace/lifecycle/admission_test.go
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
)

// TestAdmission
Expand All @@ -44,18 +43,16 @@ func TestAdmission(t *testing.T) {

store := cache.NewStore(cache.MetaNamespaceKeyFunc)
store.Add(namespaceObj)
fakeWatch := watch.NewFake()
mockClient := &testclient.Fake{}
mockClient.AddWatchReactor("*", testclient.DefaultWatchReactor(fakeWatch, nil))
mockClient.AddReactor("get", "namespaces", func(action testclient.Action) (bool, runtime.Object, error) {
mockClient := testclient.NewSimpleFake()
mockClient.PrependReactor("get", "namespaces", func(action testclient.Action) (bool, runtime.Object, error) {
namespaceLock.RLock()
defer namespaceLock.RUnlock()
if getAction, ok := action.(testclient.GetAction); ok && getAction.GetName() == namespaceObj.Name {
return true, namespaceObj, nil
}
return true, nil, fmt.Errorf("No result for action %v", action)
})
mockClient.AddReactor("list", "namespaces", func(action testclient.Action) (bool, runtime.Object, error) {
mockClient.PrependReactor("list", "namespaces", func(action testclient.Action) (bool, runtime.Object, error) {
namespaceLock.RLock()
defer namespaceLock.RUnlock()
return true, &api.NamespaceList{Items: []api.Namespace{*namespaceObj}}, nil
Expand Down