github.com/kubevela/workflow@v0.6.0/pkg/webhook/v1alpha1/workflowrun/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 workflowrun
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	. "github.com/onsi/ginkgo"
    26  	. "github.com/onsi/gomega"
    27  	corev1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	"k8s.io/client-go/kubernetes/scheme"
    31  	"k8s.io/client-go/rest"
    32  	"sigs.k8s.io/controller-runtime/pkg/client"
    33  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    34  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    35  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    36  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    37  
    38  	"github.com/kubevela/workflow/api/v1alpha1"
    39  	"github.com/kubevela/workflow/pkg/cue/packages"
    40  	// +kubebuilder:scaffold:imports
    41  )
    42  
    43  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    44  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    45  
    46  var cfg *rest.Config
    47  var k8sClient client.Client
    48  var testEnv *envtest.Environment
    49  var testScheme = runtime.NewScheme()
    50  var decoder *admission.Decoder
    51  var pd *packages.PackageDiscover
    52  var ctx = context.Background()
    53  var handler *ValidatingHandler
    54  
    55  func TestAPIs(t *testing.T) {
    56  
    57  	RegisterFailHandler(Fail)
    58  
    59  	RunSpecs(t, "Controller Suite")
    60  }
    61  
    62  var _ = BeforeSuite(func(done Done) {
    63  	logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
    64  	By("bootstrapping test environment")
    65  
    66  	yamlPath := filepath.Join("../../../../..", "charts", "vela-workflow", "crds")
    67  	testEnv = &envtest.Environment{
    68  		ControlPlaneStartTimeout: time.Minute,
    69  		ControlPlaneStopTimeout:  time.Minute,
    70  		CRDDirectoryPaths:        []string{yamlPath},
    71  	}
    72  
    73  	var err error
    74  	cfg, err = testEnv.Start()
    75  	Expect(err).ToNot(HaveOccurred())
    76  	Expect(cfg).ToNot(BeNil())
    77  
    78  	err = v1alpha1.SchemeBuilder.AddToScheme(testScheme)
    79  	Expect(err).NotTo(HaveOccurred())
    80  
    81  	err = scheme.AddToScheme(testScheme)
    82  	Expect(err).NotTo(HaveOccurred())
    83  	// +kubebuilder:scaffold:scheme
    84  
    85  	k8sClient, err = client.New(cfg, client.Options{Scheme: testScheme})
    86  	Expect(err).ToNot(HaveOccurred())
    87  	Expect(k8sClient).ToNot(BeNil())
    88  
    89  	pd, err = packages.NewPackageDiscover(cfg)
    90  	Expect(err).ToNot(HaveOccurred())
    91  	Expect(pd).ToNot(BeNil())
    92  
    93  	handler = &ValidatingHandler{
    94  		pd: pd,
    95  	}
    96  
    97  	decoder, err = admission.NewDecoder(testScheme)
    98  	Expect(err).Should(BeNil())
    99  	Expect(decoder).ToNot(BeNil())
   100  
   101  	ctx := context.Background()
   102  	ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "vela-system"}}
   103  	Expect(k8sClient.Create(ctx, &ns)).Should(BeNil())
   104  
   105  	close(done)
   106  }, 60)
   107  
   108  var _ = AfterSuite(func() {
   109  	By("tearing down the test environment")
   110  	err := testEnv.Stop()
   111  	Expect(err).ToNot(HaveOccurred())
   112  })