From 33e7e1cbef312b670152f7c93ccaa7addb809391 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 13 Aug 2020 15:39:09 +0200 Subject: [PATCH] client now has a cleanup method to hook lifecycle of event tracker to the client lifecycle Signed-off-by: Francesco Guardiani --- test/lib/client.go | 27 +++++++++++++++++++++++ test/lib/recordevents/event_info_store.go | 2 +- test/lib/test_runner.go | 7 +++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/test/lib/client.go b/test/lib/client.go index f338a951708..b26e043473a 100644 --- a/test/lib/client.go +++ b/test/lib/client.go @@ -54,6 +54,8 @@ type Client struct { podsCreated []string tracingEnv corev1.EnvVar + + cleanup func() } // NewClient instantiates and returns several clientsets required for making request to the @@ -98,6 +100,31 @@ func NewClient(configPath string, clusterName string, namespace string, t *testi return client, nil } +// Cleanup acts similarly to testing.T, but it's tied to the client lifecycle +func (c *Client) Cleanup(f func()) { + oldCleanup := c.cleanup + c.cleanup = func() { + if oldCleanup != nil { + defer oldCleanup() + } + f() + } +} + +func (c *Client) runCleanup() (err error) { + if c.cleanup == nil { + return nil + } + defer func() { + if panicVal := recover(); panicVal != nil { + err = fmt.Errorf("panic in cleanup function: %+v", panicVal) + } + }() + + c.cleanup() + return nil +} + func getTracingConfig(c *kubernetes.Clientset) (corev1.EnvVar, error) { cm, err := c.CoreV1().ConfigMaps(resources.SystemNamespace).Get("config-tracing", metav1.GetOptions{}) if err != nil { diff --git a/test/lib/recordevents/event_info_store.go b/test/lib/recordevents/event_info_store.go index e8bde788934..3b0c46cf44e 100644 --- a/test/lib/recordevents/event_info_store.go +++ b/test/lib/recordevents/event_info_store.go @@ -93,7 +93,7 @@ func NewEventInfoStore(client *testlib.Client, podName string) (*EventInfoStore, ei := newTestableEventInfoStore(egi, -1, -1) ei.podName = podName ei.tb = client.T - client.T.Cleanup(ei.cleanup) + client.Cleanup(ei.cleanup) return ei, nil } diff --git a/test/lib/test_runner.go b/test/lib/test_runner.go index c32f04a05de..553ebeff08e 100644 --- a/test/lib/test_runner.go +++ b/test/lib/test_runner.go @@ -28,11 +28,12 @@ import ( "k8s.io/apimachinery/pkg/util/wait" "k8s.io/apiserver/pkg/storage/names" - "knative.dev/eventing/pkg/utils" pkgTest "knative.dev/pkg/test" "knative.dev/pkg/test/helpers" "knative.dev/pkg/test/prow" + "knative.dev/eventing/pkg/utils" + // Mysteriously required to support GCP auth (required by k8s libs). // Apparently just importing it is enough. @_@ side effects @_@. // https://github.com/kubernetes/client-go/issues/242 @@ -179,6 +180,10 @@ func makeK8sNamespace(baseFuncName string) string { // TearDown will delete created names using clients. func TearDown(client *Client) { + if err := client.runCleanup(); err != nil { + client.T.Logf("Cleanup error: %+v", err) + } + // Dump the events in the namespace el, err := client.Kube.Kube.CoreV1().Events(client.Namespace).List(metav1.ListOptions{}) if err != nil {