From 4cc63313e71e0e18ec409cbf018af2074bc94b23 Mon Sep 17 00:00:00 2001 From: deads2k Date: Mon, 28 Sep 2015 13:28:12 -0400 Subject: [PATCH] fix testclient prepend functions --- pkg/client/unversioned/testclient/testclient.go | 16 +++++++++++++--- pkg/controller/job/controller_test.go | 8 ++++---- .../replication/replication_controller_test.go | 5 +---- .../namespace/lifecycle/admission_test.go | 9 +++------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pkg/client/unversioned/testclient/testclient.go b/pkg/client/unversioned/testclient/testclient.go index 8a675674e14..be0aabab848 100644 --- a/pkg/client/unversioned/testclient/testclient.go +++ b/pkg/client/unversioned/testclient/testclient.go @@ -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 } @@ -100,9 +102,7 @@ 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 @@ -110,11 +110,21 @@ 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) { diff --git a/pkg/controller/job/controller_test.go b/pkg/controller/job/controller_test.go index 14cb560d9b0..d5e0ea7afcb 100644 --- a/pkg/controller/job/controller_test.go +++ b/pkg/controller/job/controller_test.go @@ -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)) manager := NewJobController(client) manager.podStoreSynced = alwaysReady @@ -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 diff --git a/pkg/controller/replication/replication_controller_test.go b/pkg/controller/replication/replication_controller_test.go index 9174688c159..96a91d8638c 100644 --- a/pkg/controller/replication/replication_controller_test.go +++ b/pkg/controller/replication/replication_controller_test.go @@ -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) diff --git a/plugin/pkg/admission/namespace/lifecycle/admission_test.go b/plugin/pkg/admission/namespace/lifecycle/admission_test.go index dec42f15297..984d9ab4a74 100644 --- a/plugin/pkg/admission/namespace/lifecycle/admission_test.go +++ b/plugin/pkg/admission/namespace/lifecycle/admission_test.go @@ -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 @@ -44,10 +43,8 @@ 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 { @@ -55,7 +52,7 @@ func TestAdmission(t *testing.T) { } 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