github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/restore/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 restore
    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  	corev1 "k8s.io/api/core/v1"
    31  
    32  	"github.com/go-logr/logr"
    33  	vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
    34  	"go.uber.org/zap/zapcore"
    35  	"k8s.io/client-go/kubernetes/scheme"
    36  	"k8s.io/client-go/rest"
    37  	"k8s.io/client-go/tools/record"
    38  	ctrl "sigs.k8s.io/controller-runtime"
    39  	"sigs.k8s.io/controller-runtime/pkg/client"
    40  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    41  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    42  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    43  
    44  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    45  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/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 = corev1.AddToScheme(scheme.Scheme)
   111  	Expect(err).NotTo(HaveOccurred())
   112  
   113  	err = dpv1alpha1.AddToScheme(scheme.Scheme)
   114  	Expect(err).NotTo(HaveOccurred())
   115  
   116  	// +kubebuilder:scaffold:scheme
   117  
   118  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
   119  	Expect(err).NotTo(HaveOccurred())
   120  	Expect(k8sClient).NotTo(BeNil())
   121  
   122  	// run reconcile
   123  	k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
   124  		Scheme:                scheme.Scheme,
   125  		MetricsBindAddress:    "0",
   126  		ClientDisableCacheFor: ctrlutil.GetUncachedObjects(),
   127  	})
   128  	Expect(err).ToNot(HaveOccurred())
   129  
   130  	testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv)
   131  	recorder = k8sManager.GetEventRecorderFor("dataprotection-restore-test")
   132  
   133  	go func() {
   134  		defer GinkgoRecover()
   135  		err = k8sManager.Start(ctx)
   136  		Expect(err).ToNot(HaveOccurred(), "failed to run manager")
   137  	}()
   138  })
   139  
   140  var _ = AfterSuite(func() {
   141  	cancel()
   142  	By("tearing down the test environment")
   143  	err := testEnv.Stop()
   144  	Expect(err).NotTo(HaveOccurred())
   145  })