Skip to content

Commit

Permalink
introduce RunTests util func
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Aug 9, 2021
1 parent 68b2c79 commit 5962c3c
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 461 deletions.
28 changes: 4 additions & 24 deletions api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1alpha3

import (
"fmt"
"os"
"testing"

Expand All @@ -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,
}))
}
28 changes: 4 additions & 24 deletions bootstrap/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1alpha3

import (
"fmt"
"os"
"testing"

Expand All @@ -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,
}))
}
29 changes: 4 additions & 25 deletions bootstrap/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"testing"

Expand All @@ -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,
}))
}
28 changes: 4 additions & 24 deletions bootstrap/util/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

import (
"fmt"
"os"
"testing"

Expand All @@ -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,
}))
}
29 changes: 4 additions & 25 deletions controllers/remote/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package remote

import (
"fmt"
"os"
"testing"
"time"
Expand All @@ -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,
}))
}
159 changes: 71 additions & 88 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 5962c3c

Please sign in to comment.