Skip to content

Commit

Permalink
Simplify controller tests setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Feb 11, 2021
1 parent c9707a9 commit 0fc3f72
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/controllers"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -38,14 +37,11 @@ import (
const controllerName = "rabbitmqcluster-controller"

var (
cfg *rest.Config
testEnv *envtest.Environment
client runtimeClient.Client
clientSet *kubernetes.Clientset
scheme *runtime.Scheme
fakeExecutor *fakePodExecutor
ctx = context.Background()
cancelFunc context.CancelFunc
updateWithRetry = func(cr *rabbitmqv1beta1.RabbitmqCluster, mutateFn func(r *rabbitmqv1beta1.RabbitmqCluster)) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
if err := client.Get(ctx, runtimeClient.ObjectKeyFromObject(cr), cr); err != nil {
Expand All @@ -65,56 +61,54 @@ func TestControllers(t *testing.T) {
[]Reporter{printer.NewlineReporter{}})
}

var _ = BeforeSuite(func() {
var err error
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
}

cfg, err = testEnv.Start()
cfg, err := testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

Expect(scheme.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(rabbitmqv1beta1.AddToScheme(scheme.Scheme)).To(Succeed())

clientSet, err = kubernetes.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())

scheme = runtime.NewScheme()
Expect(rabbitmqv1beta1.AddToScheme(scheme)).To(Succeed())
Expect(defaultscheme.AddToScheme(scheme)).To(Succeed())

cancelFunc = startManager(scheme)
})

var _ = AfterSuite(func() {
cancelFunc()
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
})

func startManager(scheme *runtime.Scheme) context.CancelFunc {
mgr, err := ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
client = mgr.GetClient()
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())

fakeExecutor = &fakePodExecutor{}
reconciler := &controllers.RabbitmqClusterReconciler{
Client: client,
err = (&controllers.RabbitmqClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor(controllerName),
Namespace: "rabbitmq-system",
PodExecutor: fakeExecutor,
}
Expect(reconciler.SetupWithManager(mgr)).To(Succeed())
}).SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())

managerCtx, cancel := context.WithCancel(context.Background())
go func() {
Expect(mgr.Start(managerCtx)).To(Succeed())
err = mgr.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred())
}()
return cancel
}

client = mgr.GetClient()
Expect(client).ToNot(BeNil())

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
})

type fakePodExecutor struct {
executedCommands []command
Expand Down

0 comments on commit 0fc3f72

Please sign in to comment.