github.com/oam-dev/kubevela@v1.9.11/pkg/utils/suit_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 utils
    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/kubevela/pkg/utils/common"
    32  )
    33  
    34  var cfg *rest.Config
    35  var k8sClient client.Client
    36  var testEnv *envtest.Environment
    37  
    38  func TestUtils(t *testing.T) {
    39  	RegisterFailHandler(Fail)
    40  	RunSpecs(t, "Utils Suite")
    41  }
    42  
    43  var _ = BeforeSuite(func() {
    44  	rand.Seed(time.Now().UnixNano())
    45  	By("bootstrapping test environment for utils test")
    46  
    47  	testEnv = &envtest.Environment{
    48  		ControlPlaneStartTimeout: time.Minute * 3,
    49  		ControlPlaneStopTimeout:  time.Minute,
    50  		UseExistingCluster:       pointer.Bool(false),
    51  	}
    52  
    53  	By("start kube test env")
    54  	var err error
    55  	cfg, err = testEnv.Start()
    56  	Expect(err).ShouldNot(HaveOccurred())
    57  	Expect(cfg).ToNot(BeNil())
    58  
    59  	By("new kube client")
    60  	cfg.Timeout = time.Minute * 2
    61  	k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    62  	Expect(err).Should(BeNil())
    63  	Expect(k8sClient).ToNot(BeNil())
    64  })
    65  
    66  var _ = AfterSuite(func() {
    67  	By("tearing down the test environment")
    68  	err := testEnv.Stop()
    69  	Expect(err).ToNot(HaveOccurred())
    70  })