From 040ba7ae797096db9d0481c70b0f9879a208620c Mon Sep 17 00:00:00 2001 From: Jonathan Juares Beber Date: Wed, 15 Dec 2021 21:42:38 +0100 Subject: [PATCH] Remove os.Exit calls from TestMain As described in [go#34129], `os.Exit()` in the way we have in multiple tests in this project make panics to fail silently. That was first described in kubernetes-sigs#3032. Since go 1.15 the `os.Exit(code)` calls are not required, therefore, let's make panics visible and just run `m.Run()`. [go#34129]: golang/go#34129 --- api/v1alpha3/webhook_suite_test.go | 5 +---- api/v1alpha4/suite_test.go | 6 ++---- bootstrap/eks/api/v1alpha3/webhook_suite_test.go | 9 +++------ bootstrap/eks/controllers/suite_test.go | 8 ++------ controllers/suite_test.go | 5 +---- controlplane/eks/api/v1alpha3/webhook_suite_test.go | 7 ++----- controlplane/eks/api/v1alpha4/suite_test.go | 6 ++---- exp/api/v1alpha3/webhook_suite_test.go | 5 +---- exp/controlleridentitycreator/suite_test.go | 6 +----- exp/controllers/suite_test.go | 5 +---- exp/instancestate/suite_test.go | 5 +---- 11 files changed, 17 insertions(+), 50 deletions(-) diff --git a/api/v1alpha3/webhook_suite_test.go b/api/v1alpha3/webhook_suite_test.go index 41e24738d5..93aed2624e 100644 --- a/api/v1alpha3/webhook_suite_test.go +++ b/api/v1alpha3/webhook_suite_test.go @@ -18,7 +18,6 @@ package v1alpha3 import ( "fmt" - "os" "path" "testing" @@ -52,11 +51,9 @@ func TestAPIs(t *testing.T) { } func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { diff --git a/api/v1alpha4/suite_test.go b/api/v1alpha4/suite_test.go index 6c7e765ea3..b8837a759e 100644 --- a/api/v1alpha4/suite_test.go +++ b/api/v1alpha4/suite_test.go @@ -18,7 +18,6 @@ package v1alpha4 import ( "fmt" - "os" "path" "testing" @@ -47,9 +46,8 @@ func TestAPIs(t *testing.T) { func TestMain(m *testing.M) { setup() - code := m.Run() - teardown() - os.Exit(code) + defer teardown() + m.Run() } func setup() { diff --git a/bootstrap/eks/api/v1alpha3/webhook_suite_test.go b/bootstrap/eks/api/v1alpha3/webhook_suite_test.go index 22d6faecaa..794c973d67 100644 --- a/bootstrap/eks/api/v1alpha3/webhook_suite_test.go +++ b/bootstrap/eks/api/v1alpha3/webhook_suite_test.go @@ -18,7 +18,6 @@ package v1alpha3 import ( "fmt" - "os" "path" "testing" @@ -36,11 +35,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { @@ -48,9 +45,9 @@ func setup() { utilruntime.Must(bootstrapv1alpha4.AddToScheme(scheme.Scheme)) testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{ - path.Join("bootstrap", "eks", "config", "crd", "bases"), + path.Join("config", "crd", "bases"), }, - ).WithWebhookConfiguration("unmanaged", path.Join("bootstrap", "eks", "config", "webhook", "manifests.yaml")) + ).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml")) var err error testEnv, err = testEnvConfig.Build() if err != nil { diff --git a/bootstrap/eks/controllers/suite_test.go b/bootstrap/eks/controllers/suite_test.go index 460092c61d..e72dc06acd 100644 --- a/bootstrap/eks/controllers/suite_test.go +++ b/bootstrap/eks/controllers/suite_test.go @@ -18,7 +18,6 @@ package controllers import ( "fmt" - "os" "path" "testing" @@ -37,11 +36,8 @@ var ( func TestMain(m *testing.M) { setup() - defer func() { - teardown() - }() - code := m.Run() - os.Exit(code) // nolint:gocritic + defer teardown() + m.Run() } func setup() { diff --git a/controllers/suite_test.go b/controllers/suite_test.go index d5f5fa4dd7..0949f421c9 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -18,7 +18,6 @@ package controllers import ( "fmt" - "os" "path" "testing" @@ -37,11 +36,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { diff --git a/controlplane/eks/api/v1alpha3/webhook_suite_test.go b/controlplane/eks/api/v1alpha3/webhook_suite_test.go index d8b2c98f34..592470305e 100644 --- a/controlplane/eks/api/v1alpha3/webhook_suite_test.go +++ b/controlplane/eks/api/v1alpha3/webhook_suite_test.go @@ -18,7 +18,6 @@ package v1alpha3 import ( "fmt" - "os" "path" "testing" @@ -36,11 +35,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { @@ -48,7 +45,7 @@ func setup() { utilruntime.Must(controlplanev1alpha4.AddToScheme(scheme.Scheme)) testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{ - path.Join("..", "config", "crd", "bases"), + path.Join("config", "crd", "bases"), }, ).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml")) var err error diff --git a/controlplane/eks/api/v1alpha4/suite_test.go b/controlplane/eks/api/v1alpha4/suite_test.go index 06fc483659..08afd81e40 100644 --- a/controlplane/eks/api/v1alpha4/suite_test.go +++ b/controlplane/eks/api/v1alpha4/suite_test.go @@ -18,7 +18,6 @@ package v1alpha4 import ( "fmt" - "os" "path" "testing" @@ -35,9 +34,8 @@ var ( func TestMain(m *testing.M) { setup() - code := m.Run() - teardown() - os.Exit(code) + defer teardown() + m.Run() } func setup() { diff --git a/exp/api/v1alpha3/webhook_suite_test.go b/exp/api/v1alpha3/webhook_suite_test.go index e343e768b8..90a104ff1c 100644 --- a/exp/api/v1alpha3/webhook_suite_test.go +++ b/exp/api/v1alpha3/webhook_suite_test.go @@ -18,7 +18,6 @@ package v1alpha3 import ( "fmt" - "os" "path" "testing" @@ -51,11 +50,9 @@ func TestAPIs(t *testing.T) { } func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { diff --git a/exp/controlleridentitycreator/suite_test.go b/exp/controlleridentitycreator/suite_test.go index d7682e3aa4..40dfa5393f 100644 --- a/exp/controlleridentitycreator/suite_test.go +++ b/exp/controlleridentitycreator/suite_test.go @@ -18,7 +18,6 @@ package controlleridentitycreator import ( "fmt" - "os" "path" "testing" @@ -43,11 +42,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { @@ -57,7 +54,6 @@ func setup() { testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{ path.Join("config", "crd", "bases"), - path.Join("controlplane", "eks", "config", "crd", "bases"), }, ).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml")) var err error diff --git a/exp/controllers/suite_test.go b/exp/controllers/suite_test.go index 85dc3b57e5..6aba3c9c32 100644 --- a/exp/controllers/suite_test.go +++ b/exp/controllers/suite_test.go @@ -18,7 +18,6 @@ package controllers import ( "fmt" - "os" "path" "testing" @@ -42,11 +41,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() { diff --git a/exp/instancestate/suite_test.go b/exp/instancestate/suite_test.go index b5a57381c9..72f7a50e05 100644 --- a/exp/instancestate/suite_test.go +++ b/exp/instancestate/suite_test.go @@ -18,7 +18,6 @@ package instancestate import ( "fmt" - "os" "path" "testing" @@ -47,11 +46,9 @@ var ( ) func TestMain(m *testing.M) { - code := 0 - defer func() { os.Exit(code) }() setup() defer teardown() - code = m.Run() + m.Run() } func setup() {