Skip to content

Commit

Permalink
Merge pull request #5030 from sbueringer/pr-add-disable-test-env
Browse files Browse the repository at this point in the history
🌱 Migrate envtest setup to envtest.Run, add CAPI_DISABLE_TEST_ENV option
  • Loading branch information
k8s-ci-robot authored Aug 10, 2021
2 parents 9044184 + 7c9a332 commit 46a708d
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 406 deletions.
25 changes: 5 additions & 20 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 @@ -34,24 +33,10 @@ var (
)

func TestMain(m *testing.M) {
// 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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
25 changes: 5 additions & 20 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 @@ -34,24 +33,10 @@ var (
)

func TestMain(m *testing.M) {
// 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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
25 changes: 4 additions & 21 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,24 +31,8 @@ var (
)

func TestMain(m *testing.M) {
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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
24 changes: 4 additions & 20 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,23 +31,8 @@ var (
)

func TestMain(m *testing.M) {
// 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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
25 changes: 4 additions & 21 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,24 +36,8 @@ var (
)

func TestMain(m *testing.M) {
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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
143 changes: 66 additions & 77 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -30,14 +31,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/api/v1alpha4/index"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/internal/envtest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
// +kubebuilder:scaffold:imports
)

Expand All @@ -59,86 +58,76 @@ func init() {
}

func TestMain(m *testing.M) {
fmt.Println("Creating a new test environment")
env = envtest.New()

if err := index.AddDefaultIndexes(ctx, env.Manager); err != nil {
panic(fmt.Sprintf("unable to setup 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.DefaultIndexes,
},
)
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))
setupIndexes := func(ctx context.Context, mgr ctrl.Manager) {
if err := index.AddDefaultIndexes(ctx, mgr); err != nil {
panic(fmt.Sprintf("unable to setup index: %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))
setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) {
// Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers
// requiring a connection to a remote cluster
tracker, err := remote.NewClusterCacheTracker(
mgr,
remote.ClusterCacheTrackerOptions{
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheTracker"),
Indexes: remote.DefaultIndexes,
},
)
if err != nil {
panic(fmt.Sprintf("unable to create cluster cache tracker: %v", err))
}
if err := (&remote.ClusterCacheReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err))
}
if err := (&ClusterReconciler{
Client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor("cluster-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err))
}
if err := (&MachineReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
recorder: mgr.GetEventRecorderFor("machine-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err))
}
if err := (&MachineSetReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
recorder: mgr.GetEventRecorderFor("machineset-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err))
}
}()
<-env.Manager.Elected()
env.WaitForWebhooks()
if err := (&MachineDeploymentReconciler{
Client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor("machinedeployment-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MMachineDeploymentReconciler: %v", err))
}
if err := (&MachineHealthCheckReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
recorder: mgr.GetEventRecorderFor("machinehealthcheck-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MachineHealthCheckReconciler : %v", err))
}
}

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))
}

os.Exit(code)
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupIndexes: setupIndexes,
SetupReconcilers: setupReconcilers,
}))
}

func ContainRefOfGroupKind(group, kind string) types.GomegaMatcher {
Expand Down
25 changes: 5 additions & 20 deletions controlplane/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 @@ -34,24 +33,10 @@ var (
)

func TestMain(m *testing.M) {
// 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.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
}))
}
Loading

0 comments on commit 46a708d

Please sign in to comment.