github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/extensions/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 extensions 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 batchv1 "k8s.io/api/batch/v1" 33 "k8s.io/client-go/kubernetes/scheme" 34 "k8s.io/client-go/rest" 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 extensionsv1alpha1 "github.com/1aal/kubeblocks/apis/extensions/v1alpha1" 41 "github.com/1aal/kubeblocks/pkg/constant" 42 43 // +kubebuilder:scaffold:imports 44 45 "github.com/1aal/kubeblocks/pkg/testutil" 46 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 47 viper "github.com/1aal/kubeblocks/pkg/viperx" 48 ) 49 50 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 51 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 52 53 var cfg *rest.Config 54 var k8sClient client.Client 55 var testEnv *envtest.Environment 56 var ctx context.Context 57 var cancel context.CancelFunc 58 var testCtx testutil.TestContext 59 60 func init() { 61 viper.AutomaticEnv() 62 } 63 64 func TestAPIs(t *testing.T) { 65 RegisterFailHandler(Fail) 66 67 RunSpecs(t, "Controller Suite") 68 } 69 70 var _ = BeforeSuite(func() { 71 if viper.GetBool("ENABLE_DEBUG_LOG") { 72 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 73 o.TimeEncoder = zapcore.ISO8601TimeEncoder 74 })) 75 } 76 77 ctx, cancel = context.WithCancel(context.TODO()) 78 79 viper.SetDefault(constant.CfgKeyCtrlrMgrNS, "default") 80 viper.SetDefault(constant.KBToolsImage, "apecloud/kubeblocks-tools:latest") 81 fmt.Printf("config settings: %v\n", viper.AllSettings()) 82 83 By("bootstrapping test environment") 84 testEnv = &envtest.Environment{ 85 CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, 86 ErrorIfCRDPathMissing: true, 87 } 88 89 var err error 90 // cfg is defined in this file globally. 91 cfg, err = testEnv.Start() 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(cfg).NotTo(BeNil()) 94 95 err = batchv1.AddToScheme(scheme.Scheme) 96 Expect(err).NotTo(HaveOccurred()) 97 98 err = extensionsv1alpha1.AddToScheme(scheme.Scheme) 99 Expect(err).NotTo(HaveOccurred()) 100 101 // +kubebuilder:scaffold:scheme 102 103 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 104 Expect(err).NotTo(HaveOccurred()) 105 Expect(k8sClient).NotTo(BeNil()) 106 107 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv, "extensions-test") 108 testapps.ToIgnoreFinalizers = append(testapps.ToIgnoreFinalizers, addonFinalizerName) 109 }) 110 111 var _ = AfterSuite(func() { 112 By("tearing down the test environment") 113 Eventually(func(g Gomega) { 114 err := testEnv.Stop() 115 Expect(err).NotTo(HaveOccurred()) 116 }).Should(Succeed()) 117 })