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

     1  /*
     2  Copyright 2020-2022 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 multicluster
    18  
    19  import (
    20  	"math/rand"
    21  	"testing"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"k8s.io/client-go/rest"
    27  	"k8s.io/utils/pointer"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    30  
    31  	"github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
    32  
    33  	"github.com/oam-dev/kubevela/pkg/utils/common"
    34  )
    35  
    36  var cfg *rest.Config
    37  var k8sClient client.Client
    38  var testEnv *envtest.Environment
    39  
    40  func TestUtils(t *testing.T) {
    41  	RegisterFailHandler(Fail)
    42  	RunSpecs(t, "Utils Suite")
    43  }
    44  
    45  var _ = BeforeSuite(func() {
    46  	rand.Seed(time.Now().UnixNano())
    47  	By("bootstrapping test environment for utils test")
    48  
    49  	testEnv = &envtest.Environment{
    50  		ControlPlaneStartTimeout: time.Minute * 3,
    51  		ControlPlaneStopTimeout:  time.Minute,
    52  		UseExistingCluster:       pointer.Bool(false),
    53  		CRDDirectoryPaths:        []string{"./testdata"},
    54  	}
    55  
    56  	By("start kube test env")
    57  	var err error
    58  	cfg, err = testEnv.Start()
    59  	Expect(err).ShouldNot(HaveOccurred())
    60  	Expect(cfg).ToNot(BeNil())
    61  
    62  	By("new kube client")
    63  	cfg.Timeout = time.Minute * 2
    64  	Expect(v1alpha1.AddToScheme(common.Scheme)).Should(Succeed())
    65  	k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    66  	Expect(err).Should(BeNil())
    67  	Expect(k8sClient).ToNot(BeNil())
    68  })
    69  
    70  var _ = AfterSuite(func() {
    71  	By("tearing down the test environment")
    72  	err := testEnv.Stop()
    73  	Expect(err).ToNot(HaveOccurred())
    74  })