github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/storage/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 storage 21 22 import ( 23 "context" 24 "fmt" 25 "path/filepath" 26 "testing" 27 28 . "github.com/onsi/ginkgo/v2" 29 . "github.com/onsi/gomega" 30 31 "go.uber.org/zap/zapcore" 32 "k8s.io/client-go/kubernetes/scheme" 33 "k8s.io/client-go/rest" 34 ctrl "sigs.k8s.io/controller-runtime" 35 "sigs.k8s.io/controller-runtime/pkg/client" 36 "sigs.k8s.io/controller-runtime/pkg/envtest" 37 logf "sigs.k8s.io/controller-runtime/pkg/log" 38 "sigs.k8s.io/controller-runtime/pkg/log/zap" 39 40 // +kubebuilder:scaffold:imports 41 storagev1alpha1 "github.com/1aal/kubeblocks/apis/storage/v1alpha1" 42 "github.com/1aal/kubeblocks/pkg/constant" 43 "github.com/1aal/kubeblocks/pkg/testutil" 44 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 45 viper "github.com/1aal/kubeblocks/pkg/viperx" 46 ) 47 48 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 49 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 50 51 var cfg *rest.Config 52 var k8sClient client.Client 53 var testEnv *envtest.Environment 54 var ctx context.Context 55 var cancel context.CancelFunc 56 var testCtx testutil.TestContext 57 58 func init() { 59 viper.AutomaticEnv() 60 } 61 62 func TestAPIs(t *testing.T) { 63 RegisterFailHandler(Fail) 64 65 RunSpecs(t, "Controller Suite") 66 } 67 68 var _ = BeforeSuite(func() { 69 if viper.GetBool("ENABLE_DEBUG_LOG") { 70 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 71 o.TimeEncoder = zapcore.ISO8601TimeEncoder 72 })) 73 } 74 75 ctx, cancel = context.WithCancel(context.TODO()) 76 77 viper.SetDefault(constant.CfgKeyCtrlrMgrNS, "default") 78 viper.SetDefault(constant.KBToolsImage, "apecloud/kubeblocks-tools:latest") 79 fmt.Printf("config settings: %v\n", viper.AllSettings()) 80 81 By("bootstrapping test environment") 82 testEnv = &envtest.Environment{ 83 CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, 84 ErrorIfCRDPathMissing: true, 85 } 86 87 var err error 88 // cfg is defined in this file globally. 89 cfg, err = testEnv.Start() 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(cfg).NotTo(BeNil()) 92 93 err = storagev1alpha1.AddToScheme(scheme.Scheme) 94 Expect(err).NotTo(HaveOccurred()) 95 96 // +kubebuilder:scaffold:scheme 97 98 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 99 Expect(err).NotTo(HaveOccurred()) 100 Expect(k8sClient).NotTo(BeNil()) 101 102 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 103 testapps.ToIgnoreFinalizers = append(testapps.ToIgnoreFinalizers, storageFinalizerName) 104 105 // run reconcile 106 k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ 107 Scheme: scheme.Scheme, 108 MetricsBindAddress: "0", 109 }) 110 Expect(err).ToNot(HaveOccurred()) 111 112 err = (&StorageProviderReconciler{ 113 Client: k8sManager.GetClient(), 114 Scheme: k8sManager.GetScheme(), 115 }).SetupWithManager(k8sManager) 116 Expect(err).ToNot(HaveOccurred()) 117 118 go func() { 119 defer GinkgoRecover() 120 err = k8sManager.Start(ctx) 121 Expect(err).ToNot(HaveOccurred(), "failed to run manager") 122 }() 123 }) 124 125 var _ = AfterSuite(func() { 126 By("tearing down the test environment") 127 cancel() 128 Eventually(func(g Gomega) { 129 err := testEnv.Stop() 130 Expect(err).NotTo(HaveOccurred()) 131 }).Should(Succeed()) 132 })