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

Test Client now has a Cleanup method #3828

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/recordevents/event_info_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion test/lib/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down