From 56a9bff7d4bffc3893e424bb231a9a0726beebc8 Mon Sep 17 00:00:00 2001 From: Alexander Pavel Date: Mon, 11 Mar 2019 16:24:44 -0700 Subject: [PATCH] pkg/test: remove dependence on ctx.t The test library has functions that are useful outside of `go test`, such as `createFromYAML`, `ctx` resource deletion, and auto setting of resource cleanup when using the framework's client. The only function that required `t` and would fail if it did not exist was `Cleanup`, as other function could handle if it was nil. This commit removes `Cleanup`'s dependence on `t` --- pkg/test/context.go | 19 ++++++------------- pkg/test/main_entry.go | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) 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