github.com/oam-dev/kubevela@v1.9.11/pkg/velaql/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 velaql
    18  
    19  import (
    20  	"context"
    21  	"math/rand"
    22  	"testing"
    23  	"time"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/client-go/rest"
    30  	"k8s.io/utils/pointer"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    33  
    34  	"github.com/kubevela/workflow/pkg/cue/packages"
    35  
    36  	"github.com/oam-dev/kubevela/pkg/utils/common"
    37  )
    38  
    39  var cfg *rest.Config
    40  var k8sClient client.Client
    41  var testEnv *envtest.Environment
    42  var viewHandler *ViewHandler
    43  var pod corev1.Pod
    44  var readView corev1.ConfigMap
    45  var applyView corev1.ConfigMap
    46  
    47  var _ = BeforeSuite(func() {
    48  	rand.Seed(time.Now().UnixNano())
    49  	By("bootstrapping test environment")
    50  
    51  	testEnv = &envtest.Environment{
    52  		ControlPlaneStartTimeout: time.Minute * 3,
    53  		ControlPlaneStopTimeout:  time.Minute,
    54  		UseExistingCluster:       pointer.Bool(false),
    55  		CRDDirectoryPaths:        []string{"../../charts/vela-core/crds"},
    56  	}
    57  
    58  	By("start kube test env")
    59  	var err error
    60  	cfg, err = testEnv.Start()
    61  	Expect(err).ShouldNot(HaveOccurred())
    62  	Expect(cfg).ToNot(BeNil())
    63  
    64  	By("new kube client")
    65  	cfg.Timeout = time.Minute * 2
    66  	k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
    67  	Expect(err).Should(BeNil())
    68  	Expect(k8sClient).ToNot(BeNil())
    69  	By("new kube client success")
    70  
    71  	pd, err := packages.NewPackageDiscover(cfg)
    72  	Expect(err).To(BeNil())
    73  
    74  	viewHandler = NewViewHandler(k8sClient, cfg, pd)
    75  	ctx := context.Background()
    76  
    77  	ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "vela-system"}}
    78  	Expect(k8sClient.Create(ctx, &ns)).Should(BeNil())
    79  
    80  	Expect(common.ReadYamlToObject("./testdata/example-pod.yaml", &pod)).Should(BeNil())
    81  	Expect(k8sClient.Create(ctx, &pod)).Should(BeNil())
    82  
    83  	Expect(common.ReadYamlToObject("./testdata/read-object.yaml", &readView)).Should(BeNil())
    84  	Expect(k8sClient.Create(ctx, &readView)).Should(BeNil())
    85  
    86  	Expect(common.ReadYamlToObject("./testdata/apply-object.yaml", &applyView)).Should(BeNil())
    87  	Expect(k8sClient.Create(ctx, &applyView)).Should(BeNil())
    88  })
    89  
    90  var _ = AfterSuite(func() {
    91  	By("tearing down the test environment")
    92  	err := testEnv.Stop()
    93  	Expect(err).ToNot(HaveOccurred())
    94  })
    95  
    96  func TestVelaQL(t *testing.T) {
    97  	RegisterFailHandler(Fail)
    98  	RunSpecs(t, "VelaQL Suite")
    99  }