github.com/kubevela/workflow@v0.6.0/pkg/cue/suit_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 cue
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  	crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    28  	"k8s.io/client-go/rest"
    29  	"k8s.io/utils/pointer"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    32  
    33  	"github.com/kubevela/workflow/pkg/cue/packages"
    34  )
    35  
    36  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    37  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    38  
    39  var cfg *rest.Config
    40  var k8sClient client.Client
    41  var testEnv *envtest.Environment
    42  var scheme = runtime.NewScheme()
    43  var pd *packages.PackageDiscover
    44  
    45  func TestDefinition(t *testing.T) {
    46  	RegisterFailHandler(Fail)
    47  
    48  	RunSpecs(t, "Test Definition Suite")
    49  }
    50  
    51  var _ = BeforeSuite(func(done Done) {
    52  	By("Bootstrapping test environment")
    53  	testEnv = &envtest.Environment{
    54  		ControlPlaneStartTimeout: time.Minute,
    55  		ControlPlaneStopTimeout:  time.Minute,
    56  		UseExistingCluster:       pointer.BoolPtr(false),
    57  	}
    58  	var err error
    59  	cfg, err = testEnv.Start()
    60  	Expect(err).ToNot(HaveOccurred())
    61  	Expect(cfg).ToNot(BeNil())
    62  	Expect(clientgoscheme.AddToScheme(scheme)).Should(BeNil())
    63  	Expect(crdv1.AddToScheme(scheme)).Should(BeNil())
    64  	// +kubebuilder:scaffold:scheme
    65  	By("Create the k8s client")
    66  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
    67  	Expect(err).ToNot(HaveOccurred())
    68  	Expect(k8sClient).ToNot(BeNil())
    69  	pd, err = packages.NewPackageDiscover(cfg)
    70  	Expect(err).ToNot(HaveOccurred())
    71  
    72  	close(done)
    73  }, 60)
    74  
    75  var _ = AfterSuite(func() {
    76  	By("Tearing down the test environment")
    77  	err := testEnv.Stop()
    78  	Expect(err).ToNot(HaveOccurred())
    79  })