From bacd7b745421171c2efd37e220caeeb96ec7f057 Mon Sep 17 00:00:00 2001 From: shashidharatd Date: Wed, 15 Feb 2017 22:19:06 +0530 Subject: [PATCH] Add few new fake hooks to test --- .../util/test/test_helper.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/federation/pkg/federation-controller/util/test/test_helper.go b/federation/pkg/federation-controller/util/test/test_helper.go index 4a9020725e5..0dd86f156c1 100644 --- a/federation/pkg/federation-controller/util/test/test_helper.go +++ b/federation/pkg/federation-controller/util/test/test_helper.go @@ -177,6 +177,38 @@ func RegisterFakeList(resource string, client *core.Fake, obj runtime.Object) { }) } +// RegisterFakeClusterGet registers a get response for the cluster resource inside the given fake client. +func RegisterFakeClusterGet(client *core.Fake, obj runtime.Object) { + clusterList, ok := obj.(*federationapi.ClusterList) + client.AddReactor("get", "clusters", func(action core.Action) (bool, runtime.Object, error) { + name := action.(core.GetAction).GetName() + if ok { + for _, cluster := range clusterList.Items { + if cluster.Name == name { + return true, &cluster, nil + } + } + } + return false, nil, fmt.Errorf("could not find the requested cluster: %s", name) + }) +} + +// RegisterFakeOnCreate registers a reactor in the given fake client that passes +// all created objects to the given watcher. +func RegisterFakeOnCreate(resource string, client *core.Fake, watcher *WatcherDispatcher) { + client.AddReactor("create", resource, func(action core.Action) (bool, runtime.Object, error) { + createAction := action.(core.CreateAction) + originalObj := createAction.GetObject() + // Create a copy of the object here to prevent data races while reading the object in go routine. + obj := copy(originalObj) + watcher.orderExecution <- func() { + glog.V(4).Infof("Object created: %v", obj) + watcher.Add(obj) + } + return true, originalObj, nil + }) +} + // RegisterFakeCopyOnCreate registers a reactor in the given fake client that passes // all created objects to the given watcher and also copies them to a channel for // in-test inspection. @@ -197,6 +229,32 @@ func RegisterFakeCopyOnCreate(resource string, client *core.Fake, watcher *Watch return objChan } +// RegisterFakeOnUpdate registers a reactor in the given fake client that passes +// all updated objects to the given watcher. +func RegisterFakeOnUpdate(resource string, client *core.Fake, watcher *WatcherDispatcher) { + client.AddReactor("update", resource, func(action core.Action) (bool, runtime.Object, error) { + updateAction := action.(core.UpdateAction) + originalObj := updateAction.GetObject() + glog.V(7).Infof("Updating %s: %v", resource, updateAction.GetObject()) + + // Create a copy of the object here to prevent data races while reading the object in go routine. + obj := copy(originalObj) + operation := func() { + glog.V(4).Infof("Object updated %v", obj) + watcher.Modify(obj) + } + select { + case watcher.orderExecution <- operation: + break + case <-time.After(pushTimeout): + glog.Errorf("Fake client execution channel blocked") + glog.Errorf("Tried to push %v", updateAction) + } + return true, originalObj, nil + }) + return +} + // RegisterFakeCopyOnUpdate registers a reactor in the given fake client that passes // all updated objects to the given watcher and also copies them to a channel for // in-test inspection.