From be0d7f0aeb615260be44f57ae492b4dfc884e5a5 Mon Sep 17 00:00:00 2001 From: shashidharatd Date: Thu, 25 May 2017 18:51:15 +0530 Subject: [PATCH] Add RegisterFakeOnDelete to test federation object deletion --- .../util/test/test_helper.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/federation/pkg/federation-controller/util/test/test_helper.go b/federation/pkg/federation-controller/util/test/test_helper.go index 9647cf9d209..af28fde4ca0 100644 --- a/federation/pkg/federation-controller/util/test/test_helper.go +++ b/federation/pkg/federation-controller/util/test/test_helper.go @@ -288,6 +288,32 @@ func RegisterFakeCopyOnUpdate(resource string, client *core.Fake, watcher *Watch return objChan } +// RegisterFakeOnDelete registers a reactor in the given fake client that passes +// all deleted objects to the given watcher. Since we could get only name of the +// deleted object from DeleteAction, this register function relies on the getObject +// function passed to get the object by name and pass it watcher. +func RegisterFakeOnDelete(resource string, client *core.Fake, watcher *WatcherDispatcher, getObject func(name, namespace string) runtime.Object) { + client.AddReactor("delete", resource, func(action core.Action) (bool, runtime.Object, error) { + deleteAction := action.(core.DeleteAction) + obj := getObject(deleteAction.GetName(), deleteAction.GetNamespace()) + glog.V(7).Infof("Deleting %s: %v", resource, obj) + + operation := func() { + glog.V(4).Infof("Object deleted %v", obj) + watcher.Delete(obj) + } + select { + case watcher.orderExecution <- operation: + break + case <-time.After(pushTimeout): + glog.Errorf("Fake client execution channel blocked") + glog.Errorf("Tried to push %v", deleteAction) + } + return true, obj, nil + }) + return +} + // Adds an update reactor to the given fake client. // The reactor just returns the object passed to update action. // This is used as a hack to workaround https://github.com/kubernetes/kubernetes/issues/40939.