diff --git a/pkg/test/context.go b/pkg/test/context.go index 6ab01991d3c..9427faf2457 100644 --- a/pkg/test/context.go +++ b/pkg/test/context.go @@ -67,26 +67,19 @@ func (ctx *TestCtx) GetID() string { } func (ctx *TestCtx) Cleanup() { - for i := len(ctx.cleanupFns) - 1; i >= 0; i-- { - err := ctx.cleanupFns[i]() - if err != nil { - ctx.t.Errorf("A cleanup function failed with error: (%v)\n", err) - } - } -} - -// cleanupNoT is a modified version of Cleanup; does not use t for logging, instead uses log -// intended for use by MainEntry, which does not have a testing.T -func (ctx *TestCtx) cleanupNoT() { failed := false for i := len(ctx.cleanupFns) - 1; i >= 0; i-- { err := ctx.cleanupFns[i]() if err != nil { failed = true - log.Errorf("A cleanup function failed with error: (%v)", err) + if ctx.t != nil { + ctx.t.Errorf("A cleanup function failed with error: (%v)\n", err) + } else { + log.Errorf("A cleanup function failed with error: (%v)", err) + } } } - if failed { + if ctx.t == nil && failed { log.Fatal("A cleanup function failed") } } diff --git a/pkg/test/main_entry.go b/pkg/test/main_entry.go index 746c7cb9b04..ddabe5f38bc 100644 --- a/pkg/test/main_entry.go +++ b/pkg/test/main_entry.go @@ -117,7 +117,7 @@ func MainEntry(m *testing.M) { log.Infof("Local operator stdout: %s", string(localCmdOutBuf.Bytes())) log.Infof("Local operator stderr: %s", string(localCmdErrBuf.Bytes())) } - ctx.cleanupNoT() + ctx.Cleanup() os.Exit(exitCode) }() // create crd