sigs.k8s.io/cluster-api-provider-azure@v1.14.3/exp/controllers/suite_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package controllers 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/go-logr/logr" 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 corev1 "k8s.io/api/core/v1" 27 "sigs.k8s.io/cluster-api-provider-azure/controllers" 28 "sigs.k8s.io/cluster-api-provider-azure/internal/test/env" 29 "sigs.k8s.io/cluster-api-provider-azure/util/reconciler" 30 "sigs.k8s.io/controller-runtime/pkg/controller" 31 "sigs.k8s.io/controller-runtime/pkg/log" 32 ) 33 34 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 35 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 36 37 var ( 38 testEnv *env.TestEnvironment 39 testEnvCancel func() 40 ) 41 42 func TestAPIs(t *testing.T) { 43 RegisterFailHandler(Fail) 44 RunSpecs(t, "Controller Suite") 45 } 46 47 var _ = BeforeSuite(func() { 48 By("bootstrapping test environment") 49 testEnv = env.NewTestEnvironment() 50 51 ctx, cancel := context.WithCancel(context.Background()) 52 defer cancel() 53 54 ctx = log.IntoContext(ctx, logr.New(testEnv.Log)) 55 56 Expect(NewAzureMachinePoolReconciler(testEnv, testEnv.GetEventRecorderFor("azuremachinepool-reconciler"), 57 reconciler.Timeouts{}, "").SetupWithManager(ctx, testEnv.Manager, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: 1}})).To(Succeed()) 58 59 Expect(NewAzureMachinePoolMachineController(testEnv, testEnv.GetEventRecorderFor("azuremachinepoolmachine-reconciler"), 60 reconciler.Timeouts{}, "").SetupWithManager(ctx, testEnv.Manager, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: 1}})).To(Succeed()) 61 62 // +kubebuilder:scaffold:scheme 63 64 mgrCtx, mgrCancel := context.WithCancel(context.Background()) 65 testEnvCancel = mgrCancel 66 67 By("starting the manager") 68 go func() { 69 defer GinkgoRecover() 70 Expect(testEnv.StartManager(mgrCtx)).To(Succeed()) 71 }() 72 73 Eventually(func() bool { 74 nodes := &corev1.NodeList{} 75 if err := testEnv.Client.List(ctx, nodes); err != nil { 76 return false 77 } 78 return true 79 }).Should(BeTrue()) 80 }) 81 82 var _ = AfterSuite(func() { 83 testEnvCancel() 84 if testEnv != nil { 85 By("tearing down the test environment") 86 Expect(testEnv.Stop()).To(Succeed()) 87 } 88 })