github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/util/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 util 21 22 import ( 23 "context" 24 "path/filepath" 25 "testing" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 "go.uber.org/zap/zapcore" 31 "k8s.io/client-go/kubernetes/scheme" 32 "k8s.io/client-go/rest" 33 ctrl "sigs.k8s.io/controller-runtime" 34 "sigs.k8s.io/controller-runtime/pkg/client" 35 "sigs.k8s.io/controller-runtime/pkg/envtest" 36 logf "sigs.k8s.io/controller-runtime/pkg/log" 37 "sigs.k8s.io/controller-runtime/pkg/log/zap" 38 39 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 40 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 41 "github.com/1aal/kubeblocks/pkg/testutil" 42 viper "github.com/1aal/kubeblocks/pkg/viperx" 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 k8sManager ctrl.Manager 54 var testCtx testutil.TestContext 55 56 func init() { 57 viper.AutomaticEnv() 58 } 59 60 func TestAPIs(t *testing.T) { 61 RegisterFailHandler(Fail) 62 63 RunSpecs(t, "Operation Controller Suite") 64 } 65 66 var _ = BeforeSuite(func() { 67 if viper.GetBool("ENABLE_DEBUG_LOG") { 68 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 69 o.TimeEncoder = zapcore.ISO8601TimeEncoder 70 })) 71 } 72 73 ctx, cancel = context.WithCancel(context.TODO()) 74 75 By("bootstrapping test environment") 76 testEnv = &envtest.Environment{ 77 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "config", "crd", "bases")}, 78 ErrorIfCRDPathMissing: true, 79 } 80 81 var err error 82 // cfg is defined in this file globally. 83 cfg, err = testEnv.Start() 84 Expect(err).NotTo(HaveOccurred()) 85 Expect(cfg).NotTo(BeNil()) 86 87 err = appsv1alpha1.AddToScheme(scheme.Scheme) 88 Expect(err).NotTo(HaveOccurred()) 89 90 // +kubebuilder:scaffold:scheme 91 92 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 93 Expect(err).NotTo(HaveOccurred()) 94 Expect(k8sClient).NotTo(BeNil()) 95 96 // run reconcile 97 k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{ 98 Scheme: scheme.Scheme, 99 MetricsBindAddress: "0", 100 ClientDisableCacheFor: intctrlutil.GetUncachedObjects(), 101 }) 102 Expect(err).ToNot(HaveOccurred()) 103 104 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 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 })