github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/backup/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 backup
    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  	storagev1alpha1 "github.com/1aal/kubeblocks/apis/storage/v1alpha1"
    46  	ctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    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  
    65  func init() {
    66  	viper.AutomaticEnv()
    67  }
    68  
    69  func TestAction(t *testing.T) {
    70  	RegisterFailHandler(Fail)
    71  
    72  	RunSpecs(t, "Data Protection Backup Suite")
    73  }
    74  
    75  var _ = BeforeSuite(func() {
    76  	if viper.GetBool("ENABLE_DEBUG_LOG") {
    77  		logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) {
    78  			o.TimeEncoder = zapcore.ISO8601TimeEncoder
    79  		}))
    80  	}
    81  
    82  	ctx, cancel = context.WithCancel(context.TODO())
    83  	logger = logf.FromContext(ctx).WithValues()
    84  	logger.Info("logger start")
    85  
    86  	By("bootstrapping test environment")
    87  	testEnv = &envtest.Environment{
    88  		CRDDirectoryPaths: []string{
    89  			filepath.Join("..", "..", "..", "config", "crd", "bases"),
    90  			// use dependent external crds.
    91  			// resolved by ref: https://github.com/operator-framework/operator-sdk/issues/4434#issuecomment-786794418
    92  			filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "kubernetes-csi/external-snapshotter/",
    93  				"client/v6@v6.2.0", "config", "crd"),
    94  		},
    95  		ErrorIfCRDPathMissing: true,
    96  	}
    97  
    98  	var err error
    99  	// cfg is defined in this file globally.
   100  	cfg, err = testEnv.Start()
   101  	Expect(err).NotTo(HaveOccurred())
   102  	Expect(cfg).NotTo(BeNil())
   103  
   104  	err = appsv1alpha1.AddToScheme(scheme.Scheme)
   105  	Expect(err).NotTo(HaveOccurred())
   106  
   107  	err = vsv1.AddToScheme(scheme.Scheme)
   108  	Expect(err).NotTo(HaveOccurred())
   109  
   110  	err = dpv1alpha1.AddToScheme(scheme.Scheme)
   111  	Expect(err).NotTo(HaveOccurred())
   112  
   113  	err = storagev1alpha1.AddToScheme(scheme.Scheme)
   114  	Expect(err).NotTo(HaveOccurred())
   115  
   116  	err = vsv1.AddToScheme(scheme.Scheme)
   117  	Expect(err).NotTo(HaveOccurred())
   118  
   119  	// +kubebuilder:scaffold:scheme
   120  
   121  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
   122  	Expect(err).NotTo(HaveOccurred())
   123  	Expect(k8sClient).NotTo(BeNil())
   124  
   125  	// run reconcile
   126  	k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
   127  		Scheme:                scheme.Scheme,
   128  		MetricsBindAddress:    "0",
   129  		ClientDisableCacheFor: ctrlutil.GetUncachedObjects(),
   130  	})
   131  	Expect(err).ToNot(HaveOccurred())
   132  
   133  	testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv)
   134  	recorder = k8sManager.GetEventRecorderFor("dataprotection-backup-test")
   135  
   136  	go func() {
   137  		defer GinkgoRecover()
   138  		err = k8sManager.Start(ctx)
   139  		Expect(err).ToNot(HaveOccurred(), "failed to run manager")
   140  	}()
   141  })
   142  
   143  var _ = AfterSuite(func() {
   144  	cancel()
   145  	By("tearing down the test environment")
   146  	err := testEnv.Stop()
   147  	Expect(err).NotTo(HaveOccurred())
   148  })