From 2b389123a5de07b8ddf0b2497f9f4f1914e0e6b5 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Fri, 24 Jan 2020 06:05:13 +0200 Subject: [PATCH] test/e2e/framework: handle the case where BeforeEach was never called Some tests under e2e/storage never end up calling the Framework#BeforeEach() prolog. Handle such cases by returning early in AfterEach() by checking a new field "beforeEachStarted". Also add a nil check for ClientSet in AfterEach(). --- test/e2e/framework/framework.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index e68c1a8cdb9..45d443d04b8 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -110,6 +110,9 @@ type Framework struct { // the various afterEaches afterEaches map[string]AfterEachActionFunc + // beforeEachStarted indicates that BeforeEach has started + beforeEachStarted bool + // configuration for framework's client Options Options @@ -179,6 +182,8 @@ func NewFramework(baseName string, options Options, client clientset.Interface) // BeforeEach gets a client and makes a namespace. func (f *Framework) BeforeEach() { + f.beforeEachStarted = true + // The fact that we need this feels like a bug in ginkgo. // https://github.com/onsi/ginkgo/issues/222 f.cleanupHandle = AddCleanupAction(f.AfterEach) @@ -356,8 +361,20 @@ func (f *Framework) AddAfterEach(name string, fn AfterEachActionFunc) { // AfterEach deletes the namespace, after reading its events. func (f *Framework) AfterEach() { + // If BeforeEach never started AfterEach should be skipped. + // Currently some tests under e2e/storage have this condition. + if !f.beforeEachStarted { + return + } + RemoveCleanupAction(f.cleanupHandle) + // This should not happen. Given ClientSet is a public field a test must have updated it! + // Error out early before any API calls during cleanup. + if f.ClientSet == nil { + Failf("The framework ClientSet must not be nil at this point") + } + // DeleteNamespace at the very end in defer, to avoid any // expectation failures preventing deleting the namespace. defer func() {