github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controllerutil/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 controllerutil 21 22 import ( 23 "context" 24 "go/build" 25 "path/filepath" 26 "testing" 27 28 snapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v3/apis/volumesnapshot/v1beta1" 29 . "github.com/onsi/ginkgo/v2" 30 . "github.com/onsi/gomega" 31 32 "github.com/go-logr/logr/testr" 33 "go.uber.org/zap/zapcore" 34 "k8s.io/client-go/kubernetes/scheme" 35 "k8s.io/client-go/rest" 36 ctrl "sigs.k8s.io/controller-runtime" 37 "sigs.k8s.io/controller-runtime/pkg/client" 38 "sigs.k8s.io/controller-runtime/pkg/envtest" 39 logf "sigs.k8s.io/controller-runtime/pkg/log" 40 "sigs.k8s.io/controller-runtime/pkg/log/zap" 41 42 "github.com/1aal/kubeblocks/pkg/testutil" 43 viper "github.com/1aal/kubeblocks/pkg/viperx" 44 ) 45 46 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 47 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 48 49 var cfg *rest.Config 50 var k8sClient client.Client 51 var testEnv *envtest.Environment 52 var ctx context.Context 53 var cancel context.CancelFunc 54 var testCtx testutil.TestContext 55 var reqCtx RequestCtx 56 57 func init() { 58 viper.AutomaticEnv() 59 } 60 61 func TestAPIs(t *testing.T) { 62 reqCtx.Log = testr.New(t) 63 reqCtx.Req = ctrl.Request{} 64 65 RegisterFailHandler(Fail) 66 RunSpecs(t, "Controller Util. Suite") 67 } 68 69 var _ = BeforeSuite(func() { 70 if viper.GetBool("ENABLE_DEBUG_LOG") { 71 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 72 o.TimeEncoder = zapcore.ISO8601TimeEncoder 73 })) 74 } 75 76 ctx, cancel = context.WithCancel(context.TODO()) 77 reqCtx.Ctx = ctx 78 79 By("bootstrapping test environment") 80 testEnv = &envtest.Environment{ 81 CRDDirectoryPaths: []string{ 82 filepath.Join("..", "..", "config", "crd", "bases"), 83 // use VolumeSnapshot v1beta1 API CRDs. 84 filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "kubernetes-csi/external-snapshotter/", 85 "client/v3@v3.0.0", "config", "crd"), 86 }, 87 ErrorIfCRDPathMissing: true, 88 } 89 90 var err error 91 // cfg is defined in this file globally. 92 cfg, err = testEnv.Start() 93 Expect(err).NotTo(HaveOccurred()) 94 Expect(cfg).NotTo(BeNil()) 95 96 // +kubebuilder:scaffold:scheme 97 scheme := scheme.Scheme 98 99 err = snapshotv1beta1.AddToScheme(scheme) 100 Expect(err).NotTo(HaveOccurred()) 101 102 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) 103 Expect(err).NotTo(HaveOccurred()) 104 Expect(k8sClient).NotTo(BeNil()) 105 106 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 107 }) 108 109 var _ = AfterSuite(func() { 110 cancel() 111 By("tearing down the test environment") 112 err := testEnv.Stop() 113 Expect(err).NotTo(HaveOccurred()) 114 })