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

     1  /*
     2  Copyright 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 config
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	gomock "github.com/golang/mock/gomock"
    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  var ctl *gomock.Controller
    38  
    39  var _ = BeforeSuite(func() {
    40  	By("Bootstrapping test environment")
    41  	testEnv = &envtest.Environment{
    42  		UseExistingCluster:       pointer.Bool(false),
    43  		ControlPlaneStartTimeout: time.Minute,
    44  		ControlPlaneStopTimeout:  time.Minute,
    45  		CRDDirectoryPaths:        []string{"../../charts/vela-core/crds"},
    46  	}
    47  	var err error
    48  	cfg, err = testEnv.Start()
    49  	Expect(err).ToNot(HaveOccurred())
    50  	Expect(cfg).ToNot(BeNil())
    51  
    52  	// +kubebuilder:scaffold:scheme
    53  	By("Create the k8s client")
    54  	k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    55  	Expect(err).ToNot(HaveOccurred())
    56  	Expect(k8sClient).ToNot(BeNil())
    57  })
    58  
    59  var _ = AfterSuite(func() {
    60  	By("Tearing down the test environment")
    61  	err := testEnv.Stop()
    62  	Expect(err).ToNot(HaveOccurred())
    63  })
    64  
    65  func TestConfig(t *testing.T) {
    66  	ctl = gomock.NewController(t)
    67  	RegisterFailHandler(Fail)
    68  	RunSpecs(t, "Config Suite")
    69  
    70  }