github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/workloads/suite_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package workloads 21 22 import ( 23 "context" 24 "path/filepath" 25 "testing" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 "k8s.io/client-go/kubernetes/scheme" 31 "k8s.io/client-go/rest" 32 ctrl "sigs.k8s.io/controller-runtime" 33 "sigs.k8s.io/controller-runtime/pkg/client" 34 "sigs.k8s.io/controller-runtime/pkg/envtest" 35 logf "sigs.k8s.io/controller-runtime/pkg/log" 36 "sigs.k8s.io/controller-runtime/pkg/log/zap" 37 38 // +kubebuilder:scaffold:imports 39 40 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 41 workloadsv1alpha1 "github.com/1aal/kubeblocks/apis/workloads/v1alpha1" 42 "github.com/1aal/kubeblocks/pkg/testutil" 43 ) 44 45 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 46 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 47 48 var cfg *rest.Config 49 var k8sClient client.Client 50 var testEnv *envtest.Environment 51 var ctx context.Context 52 var cancel context.CancelFunc 53 var testCtx testutil.TestContext 54 55 func TestAPIs(t *testing.T) { 56 RegisterFailHandler(Fail) 57 58 RunSpecs(t, "Controller Suite") 59 } 60 61 var _ = BeforeSuite(func() { 62 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 63 64 ctx, cancel = context.WithCancel(context.TODO()) 65 66 By("bootstrapping test environment") 67 testEnv = &envtest.Environment{ 68 CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, 69 ErrorIfCRDPathMissing: true, 70 } 71 72 var err error 73 // cfg is defined in this file globally. 74 cfg, err = testEnv.Start() 75 Expect(err).NotTo(HaveOccurred()) 76 Expect(cfg).NotTo(BeNil()) 77 78 err = workloadsv1alpha1.AddToScheme(scheme.Scheme) 79 Expect(err).NotTo(HaveOccurred()) 80 81 // +kubebuilder:scaffold:scheme 82 83 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 84 Expect(err).NotTo(HaveOccurred()) 85 Expect(k8sClient).NotTo(BeNil()) 86 87 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 88 89 // run reconcile 90 k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ 91 Scheme: scheme.Scheme, 92 MetricsBindAddress: "0", 93 }) 94 Expect(err).ToNot(HaveOccurred()) 95 96 recorder := k8sManager.GetEventRecorderFor("consensus-set-controller") 97 err = (&ReplicatedStateMachineReconciler{ 98 Client: k8sManager.GetClient(), 99 Scheme: k8sManager.GetScheme(), 100 Recorder: recorder, 101 }).SetupWithManager(k8sManager) 102 Expect(err).ToNot(HaveOccurred()) 103 104 appsv1alpha1.RegisterWebhookManager(k8sManager) 105 106 go func() { 107 defer GinkgoRecover() 108 err = k8sManager.Start(ctx) 109 Expect(err).ToNot(HaveOccurred(), "failed to run manager") 110 }() 111 }) 112 113 var _ = AfterSuite(func() { 114 cancel() 115 By("tearing down the test environment") 116 err := testEnv.Stop() 117 Expect(err).NotTo(HaveOccurred()) 118 })