github.com/oam-dev/kubevela@v1.9.11/pkg/resourcekeeper/suite_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resourcekeeper
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"k8s.io/utils/pointer"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    29  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    30  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    31  
    32  	"github.com/oam-dev/kubevela/pkg/utils/common"
    33  )
    34  
    35  var testEnv *envtest.Environment
    36  var testClient client.Client
    37  
    38  var workerEnv *envtest.Environment
    39  var workerClient client.Client
    40  
    41  func TestResourceKeeper(t *testing.T) {
    42  	RegisterFailHandler(Fail)
    43  	RunSpecs(t, "ResourceKeeper Suite")
    44  }
    45  
    46  var _ = BeforeSuite(func() {
    47  	By("Bootstrapping test environment")
    48  	testEnv = &envtest.Environment{
    49  		ControlPlaneStartTimeout: time.Minute,
    50  		ControlPlaneStopTimeout:  time.Minute,
    51  		CRDDirectoryPaths: []string{
    52  			filepath.Join("../..", "charts/vela-core/crds"), // this has all the required CRDs,
    53  		},
    54  		UseExistingCluster:    pointer.Bool(false),
    55  		ErrorIfCRDPathMissing: true,
    56  	}
    57  	var err error
    58  	cfg, err := testEnv.Start()
    59  	Expect(err).ShouldNot(HaveOccurred())
    60  	Expect(cfg).ShouldNot(BeNil())
    61  
    62  	logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
    63  
    64  	testClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    65  	Expect(err).ShouldNot(HaveOccurred())
    66  	Expect(testClient).ShouldNot(BeNil())
    67  
    68  	workerEnv = &envtest.Environment{
    69  		ControlPlaneStartTimeout: time.Minute,
    70  		ControlPlaneStopTimeout:  time.Minute,
    71  		CRDDirectoryPaths: []string{
    72  			filepath.Join("../..", "charts/vela-core/crds"), // this has all the required CRDs,
    73  		},
    74  		UseExistingCluster:    pointer.Bool(false),
    75  		ErrorIfCRDPathMissing: true,
    76  	}
    77  	cfg, err = workerEnv.Start()
    78  	Expect(err).ShouldNot(HaveOccurred())
    79  	Expect(cfg).ShouldNot(BeNil())
    80  	workerClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    81  	Expect(err).ShouldNot(HaveOccurred())
    82  	Expect(workerClient).ShouldNot(BeNil())
    83  })
    84  
    85  var _ = AfterSuite(func() {
    86  	Expect(testEnv.Stop()).Should(Succeed())
    87  })