github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/action/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 action_test 21 22 import ( 23 "context" 24 "go/build" 25 "path/filepath" 26 "testing" 27 28 . "github.com/onsi/ginkgo/v2" 29 . "github.com/onsi/gomega" 30 31 "github.com/go-logr/logr" 32 vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" 33 "go.uber.org/zap/zapcore" 34 "k8s.io/client-go/kubernetes/scheme" 35 "k8s.io/client-go/rest" 36 "k8s.io/client-go/tools/record" 37 ctrl "sigs.k8s.io/controller-runtime" 38 "sigs.k8s.io/controller-runtime/pkg/client" 39 "sigs.k8s.io/controller-runtime/pkg/envtest" 40 logf "sigs.k8s.io/controller-runtime/pkg/log" 41 "sigs.k8s.io/controller-runtime/pkg/log/zap" 42 43 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 44 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 45 ctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 46 "github.com/1aal/kubeblocks/pkg/dataprotection/action" 47 "github.com/1aal/kubeblocks/pkg/testutil" 48 viper "github.com/1aal/kubeblocks/pkg/viperx" 49 ) 50 51 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 52 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 53 54 var ( 55 cfg *rest.Config 56 k8sClient client.Client 57 testEnv *envtest.Environment 58 ctx context.Context 59 cancel context.CancelFunc 60 testCtx testutil.TestContext 61 logger logr.Logger 62 recorder record.EventRecorder 63 64 buildActionCtx = func() action.Context { 65 return action.Context{ 66 Ctx: testCtx.Ctx, 67 Client: testCtx.Cli, 68 Recorder: recorder, 69 Scheme: testEnv.Scheme, 70 RestClientConfig: cfg, 71 } 72 } 73 ) 74 75 func init() { 76 viper.AutomaticEnv() 77 } 78 79 func TestAction(t *testing.T) { 80 RegisterFailHandler(Fail) 81 82 RunSpecs(t, "Data Protection Action Suite") 83 } 84 85 var _ = BeforeSuite(func() { 86 if viper.GetBool("ENABLE_DEBUG_LOG") { 87 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 88 o.TimeEncoder = zapcore.ISO8601TimeEncoder 89 })) 90 } 91 92 ctx, cancel = context.WithCancel(context.TODO()) 93 logger = logf.FromContext(ctx).WithValues() 94 logger.Info("logger start") 95 96 By("bootstrapping test environment") 97 testEnv = &envtest.Environment{ 98 CRDDirectoryPaths: []string{ 99 filepath.Join("..", "..", "..", "config", "crd", "bases"), 100 // use dependent external crds. 101 // resolved by ref: https://github.com/operator-framework/operator-sdk/issues/4434#issuecomment-786794418 102 filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "kubernetes-csi/external-snapshotter/", 103 "client/v6@v6.2.0", "config", "crd"), 104 }, 105 ErrorIfCRDPathMissing: true, 106 } 107 108 var err error 109 // cfg is defined in this file globally. 110 cfg, err = testEnv.Start() 111 Expect(err).NotTo(HaveOccurred()) 112 Expect(cfg).NotTo(BeNil()) 113 114 err = appsv1alpha1.AddToScheme(scheme.Scheme) 115 Expect(err).NotTo(HaveOccurred()) 116 117 err = vsv1.AddToScheme(scheme.Scheme) 118 Expect(err).NotTo(HaveOccurred()) 119 120 err = dpv1alpha1.AddToScheme(scheme.Scheme) 121 Expect(err).NotTo(HaveOccurred()) 122 123 // +kubebuilder:scaffold:scheme 124 125 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 126 Expect(err).NotTo(HaveOccurred()) 127 Expect(k8sClient).NotTo(BeNil()) 128 129 // run reconcile 130 k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ 131 Scheme: scheme.Scheme, 132 MetricsBindAddress: "0", 133 ClientDisableCacheFor: ctrlutil.GetUncachedObjects(), 134 }) 135 Expect(err).ToNot(HaveOccurred()) 136 137 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 138 recorder = k8sManager.GetEventRecorderFor("dataprotection-action-test") 139 140 go func() { 141 defer GinkgoRecover() 142 err = k8sManager.Start(ctx) 143 Expect(err).ToNot(HaveOccurred(), "failed to run manager") 144 }() 145 }) 146 147 var _ = AfterSuite(func() { 148 cancel() 149 By("tearing down the test environment") 150 err := testEnv.Stop() 151 Expect(err).NotTo(HaveOccurred()) 152 })