github.com/oam-dev/kubevela@v1.9.11/pkg/utils/apply/apply_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 apply
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	oamcore "github.com/oam-dev/kubevela/apis/core.oam.dev"
    29  
    30  	corev1 "k8s.io/api/core/v1"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/apimachinery/pkg/runtime"
    33  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    34  	"k8s.io/client-go/rest"
    35  	"sigs.k8s.io/controller-runtime/pkg/client"
    36  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    37  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    38  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    39  )
    40  
    41  var testEnv *envtest.Environment
    42  var cfg *rest.Config
    43  var rawClient client.Client
    44  var k8sApplicator Applicator
    45  var testScheme = runtime.NewScheme()
    46  var ns = "test-apply"
    47  var applyNS corev1.Namespace
    48  
    49  func TestApplicator(t *testing.T) {
    50  	RegisterFailHandler(Fail)
    51  	RunSpecs(t, "Applicator Suite")
    52  }
    53  
    54  var _ = BeforeSuite(func() {
    55  	By("Bootstrapping test environment")
    56  	testEnv = &envtest.Environment{
    57  		ControlPlaneStartTimeout: time.Minute,
    58  		ControlPlaneStopTimeout:  time.Minute,
    59  		CRDDirectoryPaths: []string{
    60  			filepath.Join("../../..", "charts/vela-core/crds"), // this has all the required CRDs,
    61  		},
    62  	}
    63  	var err error
    64  	cfg, err = testEnv.Start()
    65  	Expect(err).ShouldNot(HaveOccurred())
    66  	Expect(cfg).ShouldNot(BeNil())
    67  
    68  	logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
    69  	Expect(clientgoscheme.AddToScheme(testScheme)).Should(Succeed())
    70  	Expect(oamcore.AddToScheme(testScheme)).Should(Succeed())
    71  
    72  	By("Setting up applicator")
    73  	rawClient, err = client.New(cfg, client.Options{Scheme: testScheme})
    74  	Expect(err).ShouldNot(HaveOccurred())
    75  	Expect(rawClient).ShouldNot(BeNil())
    76  	k8sApplicator = NewAPIApplicator(rawClient)
    77  
    78  	By("Create test namespace")
    79  	applyNS = corev1.Namespace{
    80  		ObjectMeta: metav1.ObjectMeta{
    81  			Name: ns,
    82  		},
    83  	}
    84  	Expect(rawClient.Create(context.Background(), &applyNS)).Should(Succeed())
    85  })
    86  
    87  var _ = AfterSuite(func() {
    88  	Expect(testEnv.Stop()).Should(Succeed())
    89  })