From 5962c3ca236f3b1093a4c4d353f7b3da730cd64e Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Mon, 9 Aug 2021 15:37:48 +0200 Subject: [PATCH] introduce RunTests util func --- api/v1alpha3/suite_test.go | 28 +-- bootstrap/kubeadm/api/v1alpha3/suite_test.go | 28 +-- bootstrap/kubeadm/controllers/suite_test.go | 29 +--- bootstrap/util/suite_test.go | 28 +-- controllers/remote/suite_test.go | 29 +--- controllers/suite_test.go | 159 ++++++++---------- .../kubeadm/api/v1alpha3/suite_test.go | 28 +-- .../kubeadm/controllers/suite_test.go | 28 +-- controlplane/kubeadm/internal/suite_test.go | 25 +-- exp/addons/api/v1alpha3/suite_test.go | 29 +--- exp/addons/controllers/suite_test.go | 82 ++++----- exp/api/v1alpha3/suite_test.go | 29 +--- exp/controllers/suite_test.go | 59 +++---- internal/envtest/environment.go | 48 ++++++ util/collections/suite_test.go | 28 +-- util/patch/suite_test.go | 29 +--- 16 files changed, 225 insertions(+), 461 deletions(-) diff --git a/api/v1alpha3/suite_test.go b/api/v1alpha3/suite_test.go index b42170fd9159..ead26cea3a31 100644 --- a/api/v1alpha3/suite_test.go +++ b/api/v1alpha3/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha3 import ( - "fmt" "os" "testing" @@ -36,27 +35,8 @@ var ( func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/bootstrap/kubeadm/api/v1alpha3/suite_test.go b/bootstrap/kubeadm/api/v1alpha3/suite_test.go index b42170fd9159..ead26cea3a31 100644 --- a/bootstrap/kubeadm/api/v1alpha3/suite_test.go +++ b/bootstrap/kubeadm/api/v1alpha3/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha3 import ( - "fmt" "os" "testing" @@ -36,27 +35,8 @@ var ( func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/bootstrap/kubeadm/controllers/suite_test.go b/bootstrap/kubeadm/controllers/suite_test.go index 97eb343a82ae..70464cc8bc77 100644 --- a/bootstrap/kubeadm/controllers/suite_test.go +++ b/bootstrap/kubeadm/controllers/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package controllers import ( - "fmt" "os" "testing" @@ -32,28 +31,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating new test environment") - env = envtest.New() - - go func() { - fmt.Println("Starting the manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - code := m.Run() - - fmt.Println("Tearing down test suite") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop envtest: %v", err)) - } - - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/bootstrap/util/suite_test.go b/bootstrap/util/suite_test.go index fdc367c67ec5..fec69f36dec7 100644 --- a/bootstrap/util/suite_test.go +++ b/bootstrap/util/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "fmt" "os" "testing" @@ -32,27 +31,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/controllers/remote/suite_test.go b/controllers/remote/suite_test.go index 556a8c0adc18..0902f033bb13 100644 --- a/controllers/remote/suite_test.go +++ b/controllers/remote/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package remote import ( - "fmt" "os" "testing" "time" @@ -37,28 +36,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating a new test environment") - env = envtest.New() - - go func() { - fmt.Println("Starting the test environment manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the test environment manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - code := m.Run() - - fmt.Println("Stopping the test environment") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the test environment: %v", err)) - } - - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 9b24370d8a92..00299b964c3e 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -60,102 +60,85 @@ func init() { } func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating a new test environment") - env = envtest.New() - - // Set up the MachineNodeIndex - if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine node index: %v", err)) - } + preManagerStart := func() { + // Set up the MachineNodeIndex + if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine node index: %v", err)) + } - // Set up the MachineProviderIDIndex - if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) - } + // Set up the MachineProviderIDIndex + if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) + } - // Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers - // requiring a connection to a remote cluster - tracker, err := remote.NewClusterCacheTracker( - env.Manager, - remote.ClusterCacheTrackerOptions{ - Log: log.Log, - Indexes: []remote.Index{ - { - Object: &corev1.Node{}, - Field: noderefutil.NodeProviderIDIndex, - ExtractValue: noderefutil.IndexNodeByProviderID, + // Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers + // requiring a connection to a remote cluster + tracker, err := remote.NewClusterCacheTracker( + env.Manager, + remote.ClusterCacheTrackerOptions{ + Log: log.Log, + Indexes: []remote.Index{ + { + Object: &corev1.Node{}, + Field: noderefutil.NodeProviderIDIndex, + ExtractValue: noderefutil.IndexNodeByProviderID, + }, }, }, - }, - ) - if err != nil { - panic(fmt.Sprintf("unable to create cluster cache tracker: %v", err)) - } - if err := (&remote.ClusterCacheReconciler{ - Client: env, - Log: log.Log, - Tracker: tracker, - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err)) - } - if err := (&ClusterReconciler{ - Client: env, - recorder: env.GetEventRecorderFor("cluster-controller"), - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err)) - } - if err := (&MachineReconciler{ - Client: env, - Tracker: tracker, - recorder: env.GetEventRecorderFor("machine-controller"), - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) - } - if err := (&MachineSetReconciler{ - Client: env, - Tracker: tracker, - recorder: env.GetEventRecorderFor("machineset-controller"), - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err)) - } - if err := (&MachineDeploymentReconciler{ - Client: env, - recorder: env.GetEventRecorderFor("machinedeployment-controller"), - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start MMachineDeploymentReconciler: %v", err)) - } - if err := (&MachineHealthCheckReconciler{ - Client: env, - Tracker: tracker, - recorder: env.GetEventRecorderFor("machinehealthcheck-controller"), - }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to start MachineHealthCheckReconciler : %v", err)) - } - - go func() { - fmt.Println("Starting the test environment manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the test environment manager: %v", err)) + ) + if err != nil { + panic(fmt.Sprintf("unable to create cluster cache tracker: %v", err)) + } + if err := (&remote.ClusterCacheReconciler{ + Client: env, + Log: log.Log, + Tracker: tracker, + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err)) + } + if err := (&ClusterReconciler{ + Client: env, + recorder: env.GetEventRecorderFor("cluster-controller"), + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err)) + } + if err := (&MachineReconciler{ + Client: env, + Tracker: tracker, + recorder: env.GetEventRecorderFor("machine-controller"), + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err)) + } + if err := (&MachineSetReconciler{ + Client: env, + Tracker: tracker, + recorder: env.GetEventRecorderFor("machineset-controller"), + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err)) + } + if err := (&MachineDeploymentReconciler{ + Client: env, + recorder: env.GetEventRecorderFor("machinedeployment-controller"), + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start MMachineDeploymentReconciler: %v", err)) + } + if err := (&MachineHealthCheckReconciler{ + Client: env, + Tracker: tracker, + recorder: env.GetEventRecorderFor("machinehealthcheck-controller"), + }).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to start MachineHealthCheckReconciler : %v", err)) } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - SetDefaultEventuallyPollingInterval(100 * time.Millisecond) - SetDefaultEventuallyTimeout(timeout) - - code := m.Run() - fmt.Println("Stopping the test environment") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the test environment: %v", err)) + SetDefaultEventuallyPollingInterval(100 * time.Millisecond) + SetDefaultEventuallyTimeout(timeout) } - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + PreManagerStart: preManagerStart, + })) } func ContainRefOfGroupKind(group, kind string) types.GomegaMatcher { diff --git a/controlplane/kubeadm/api/v1alpha3/suite_test.go b/controlplane/kubeadm/api/v1alpha3/suite_test.go index b42170fd9159..ead26cea3a31 100644 --- a/controlplane/kubeadm/api/v1alpha3/suite_test.go +++ b/controlplane/kubeadm/api/v1alpha3/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha3 import ( - "fmt" "os" "testing" @@ -36,27 +35,8 @@ var ( func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/controlplane/kubeadm/controllers/suite_test.go b/controlplane/kubeadm/controllers/suite_test.go index 53019944feab..70464cc8bc77 100644 --- a/controlplane/kubeadm/controllers/suite_test.go +++ b/controlplane/kubeadm/controllers/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package controllers import ( - "fmt" "os" "testing" @@ -32,27 +31,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/controlplane/kubeadm/internal/suite_test.go b/controlplane/kubeadm/internal/suite_test.go index 863a7da37b56..d37176b02d73 100644 --- a/controlplane/kubeadm/internal/suite_test.go +++ b/controlplane/kubeadm/internal/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package internal import ( - "fmt" "os" "testing" @@ -31,24 +30,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - code := m.Run() - - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop envtest: %v", err)) - } - - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/exp/addons/api/v1alpha3/suite_test.go b/exp/addons/api/v1alpha3/suite_test.go index bb226c08e94a..ead26cea3a31 100644 --- a/exp/addons/api/v1alpha3/suite_test.go +++ b/exp/addons/api/v1alpha3/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha3 import ( - "fmt" "os" "testing" @@ -34,28 +33,10 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment utilruntime.Must(AddToScheme(scheme.Scheme)) - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/exp/addons/controllers/suite_test.go b/exp/addons/controllers/suite_test.go index fc8ea6c01aa0..907820d1542c 100644 --- a/exp/addons/controllers/suite_test.go +++ b/exp/addons/controllers/suite_test.go @@ -38,56 +38,44 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating new test environment") - env = envtest.New([]client.Object{&corev1.ConfigMap{}, &corev1.Secret{}, &v1alpha4.ClusterResourceSetBinding{}}...) - - // Set up the MachineNodeIndex - if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine node index: %v", err)) - } - - // Set up the MachineProviderIDIndex - if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) - } - - trckr, err := remote.NewClusterCacheTracker(env.Manager, remote.ClusterCacheTrackerOptions{}) - if err != nil { - panic(fmt.Sprintf("Failed to create new cluster cache tracker: %v", err)) - } - reconciler := ClusterResourceSetReconciler{ - Client: env, - Tracker: trckr, - } - if err = reconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to set up cluster resource set reconciler: %v", err)) - } - bindingReconciler := ClusterResourceSetBindingReconciler{ - Client: env, - } - if err = bindingReconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { - panic(fmt.Sprintf("Failed to set up cluster resource set binding reconciler: %v", err)) - } - - go func() { - fmt.Println("Starting the manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) + preManagerStart := func() { + // Set up the MachineNodeIndex + if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine node index: %v", err)) } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - code := m.Run() + // Set up the MachineProviderIDIndex + if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) + } - fmt.Println("Tearing down test suite") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop envtest: %v", err)) + trckr, err := remote.NewClusterCacheTracker(env.Manager, remote.ClusterCacheTrackerOptions{}) + if err != nil { + panic(fmt.Sprintf("Failed to create new cluster cache tracker: %v", err)) + } + reconciler := ClusterResourceSetReconciler{ + Client: env, + Tracker: trckr, + } + if err = reconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to set up cluster resource set reconciler: %v", err)) + } + bindingReconciler := ClusterResourceSetBindingReconciler{ + Client: env, + } + if err = bindingReconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil { + panic(fmt.Sprintf("Failed to set up cluster resource set binding reconciler: %v", err)) + } } - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + ManagerUncachedObjs: []client.Object{ + &corev1.ConfigMap{}, + &corev1.Secret{}, + &v1alpha4.ClusterResourceSetBinding{}, + }, + PreManagerStart: preManagerStart, + })) } diff --git a/exp/api/v1alpha3/suite_test.go b/exp/api/v1alpha3/suite_test.go index bb226c08e94a..ead26cea3a31 100644 --- a/exp/api/v1alpha3/suite_test.go +++ b/exp/api/v1alpha3/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha3 import ( - "fmt" "os" "testing" @@ -34,28 +33,10 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment utilruntime.Must(AddToScheme(scheme.Scheme)) - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/exp/controllers/suite_test.go b/exp/controllers/suite_test.go index a24ad0f22e0e..cd3c3cc87129 100644 --- a/exp/controllers/suite_test.go +++ b/exp/controllers/suite_test.go @@ -34,47 +34,30 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating new test environment") - env = envtest.New() - - // Set up the MachineNodeIndex - if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine node index: %v", err)) - } - - // Set up the MachineProviderIDIndex - if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { - panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) - } - - machinePoolReconciler := MachinePoolReconciler{ - Client: env, - recorder: env.GetEventRecorderFor("machinepool-controller"), - } - err := machinePoolReconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}) - if err != nil { - panic(fmt.Sprintf("Failed to set up machine pool reconciler: %v", err)) - } - - go func() { - fmt.Println("Starting the manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) + preManagerStart := func() { + // Set up the MachineNodeIndex + if err := noderefutil.AddMachineNodeIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine node index: %v", err)) } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - code := m.Run() + // Set up the MachineProviderIDIndex + if err := noderefutil.AddMachineProviderIDIndex(ctx, env.Manager); err != nil { + panic(fmt.Sprintf("unable to setup machine providerID index: %v", err)) + } - fmt.Println("Tearing down test suite") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop envtest: %v", err)) + machinePoolReconciler := MachinePoolReconciler{ + Client: env, + recorder: env.GetEventRecorderFor("machinepool-controller"), + } + err := machinePoolReconciler.SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}) + if err != nil { + panic(fmt.Sprintf("Failed to set up machine pool reconciler: %v", err)) + } } - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + PreManagerStart: preManagerStart, + })) } diff --git a/internal/envtest/environment.go b/internal/envtest/environment.go index 8e89b868b8f0..1c1395a47917 100644 --- a/internal/envtest/environment.go +++ b/internal/envtest/environment.go @@ -20,11 +20,13 @@ import ( "context" "fmt" "net" + "os" "path" "path/filepath" goruntime "runtime" "strconv" "sync" + "testing" "time" "github.com/onsi/ginkgo" @@ -75,6 +77,52 @@ func init() { utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme)) } +// RunTestsInput is the input for RunTests. +type RunTestsInput struct { + M *testing.M + Env **Environment + ManagerUncachedObjs []client.Object + PreManagerStart func() +} + +// RunTests executes the tests of the given testing.M in a test environment. +// Note: The environment will be created in this func and should not be created before. This func takes a **Environment +// because our tests require access to the *Environment. We use this field to make the created Environment available +// to the consumer. +// Note: Test environment creation can be skipped by setting the environment variable `CAPI_DISABLE_TEST_ENV`. This only +// makes sense when executing tests which don't require the test environment, e.g. tests using only the fake client. +func RunTests(ctx context.Context, input RunTestsInput) int { + if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { + return input.M.Run() + } + + // Bootstrapping test environment + *input.Env = New(input.ManagerUncachedObjs...) + + if input.PreManagerStart != nil { + input.PreManagerStart() + } + + go func() { + fmt.Println("Starting the test environment manager") + if err := (*input.Env).Start(ctx); err != nil { + panic(fmt.Sprintf("Failed to start the test environment manager: %v", err)) + } + }() + <-(*input.Env).Manager.Elected() + (*input.Env).WaitForWebhooks() + + // Run tests + code := input.M.Run() + + // Tearing down the test environment + fmt.Println("Stopping the test environment") + if err := (*input.Env).Stop(); err != nil { + panic(fmt.Sprintf("Failed to stop the test environment: %v", err)) + } + return code +} + var ( cacheSyncBackoff = wait.Backoff{ Duration: 100 * time.Millisecond, diff --git a/util/collections/suite_test.go b/util/collections/suite_test.go index 48058d58c850..d0484d4d7fcb 100644 --- a/util/collections/suite_test.go +++ b/util/collections/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package collections_test import ( - "fmt" "os" "testing" @@ -31,27 +30,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - // Bootstrapping test environment - env = envtest.New() - go func() { - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - // Run tests - code := m.Run() - // Tearing down the test environment - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop the envtest: %v", err)) - } - - // Report exit code - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) } diff --git a/util/patch/suite_test.go b/util/patch/suite_test.go index c4c3263634c5..a0343860955a 100644 --- a/util/patch/suite_test.go +++ b/util/patch/suite_test.go @@ -17,7 +17,6 @@ limitations under the License. package patch import ( - "fmt" "os" "testing" "time" @@ -37,28 +36,8 @@ var ( ) func TestMain(m *testing.M) { - if os.Getenv("CAPI_DISABLE_TEST_ENV") != "" { - os.Exit(m.Run()) - } - - fmt.Println("Creating new test environment") - env = envtest.New() - - go func() { - fmt.Println("Starting the manager") - if err := env.Start(ctx); err != nil { - panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) - } - }() - <-env.Manager.Elected() - env.WaitForWebhooks() - - code := m.Run() - - fmt.Println("Tearing down test suite") - if err := env.Stop(); err != nil { - panic(fmt.Sprintf("Failed to stop envtest: %v", err)) - } - - os.Exit(code) + os.Exit(envtest.RunTests(ctx, envtest.RunTestsInput{ + M: m, + Env: &env, + })) }