github.com/oam-dev/kubevela@v1.9.11/references/docgen/suit_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 docgen
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  	"time"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	corev1 "k8s.io/api/core/v1"
    29  	crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/apimachinery/pkg/runtime"
    32  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    33  	"k8s.io/client-go/rest"
    34  	"sigs.k8s.io/controller-runtime/pkg/client"
    35  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    36  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    37  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    38  	"sigs.k8s.io/yaml"
    39  
    40  	coreoam "github.com/oam-dev/kubevela/apis/core.oam.dev"
    41  	corev1beta1 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    42  	"github.com/oam-dev/kubevela/pkg/oam/util"
    43  	"github.com/oam-dev/kubevela/pkg/utils/system"
    44  	// +kubebuilder:scaffold:imports
    45  )
    46  
    47  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    48  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    49  
    50  var cfg *rest.Config
    51  var k8sClient client.Client
    52  var testEnv *envtest.Environment
    53  var definitionDir string
    54  var td corev1beta1.TraitDefinition
    55  var wd, websvcWD corev1beta1.WorkloadDefinition
    56  var cd, websvcCD corev1beta1.ComponentDefinition
    57  
    58  func TestReferencePlugins(t *testing.T) {
    59  	RegisterFailHandler(Fail)
    60  
    61  	RunSpecs(t, "CLI Suite")
    62  }
    63  
    64  var _ = BeforeSuite(func() {
    65  	logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
    66  	ctx := context.Background()
    67  	By("bootstrapping test environment")
    68  	useExistCluster := false
    69  	testEnv = &envtest.Environment{
    70  		ControlPlaneStartTimeout: time.Minute,
    71  		ControlPlaneStopTimeout:  time.Minute,
    72  		CRDDirectoryPaths:        []string{filepath.Join("..", "..", "charts", "vela-core", "crds")},
    73  		UseExistingCluster:       &useExistCluster,
    74  	}
    75  
    76  	var err error
    77  	cfg, err = testEnv.Start()
    78  	Expect(err).ToNot(HaveOccurred())
    79  	Expect(cfg).ToNot(BeNil())
    80  	scheme := runtime.NewScheme()
    81  	Expect(coreoam.AddToScheme(scheme)).NotTo(HaveOccurred())
    82  	Expect(clientgoscheme.AddToScheme(scheme)).NotTo(HaveOccurred())
    83  	Expect(crdv1.AddToScheme(scheme)).NotTo(HaveOccurred())
    84  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
    85  	Expect(err).ToNot(HaveOccurred())
    86  	Expect(k8sClient).ToNot(BeNil())
    87  
    88  	definitionDir, err = system.GetCapabilityDir()
    89  	Expect(err).Should(BeNil())
    90  	Expect(os.MkdirAll(definitionDir, 0755)).Should(BeNil())
    91  
    92  	Expect(k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: DefinitionNamespace}})).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
    93  
    94  	workloaddata, err := os.ReadFile("testdata/workloadDef.yaml")
    95  	Expect(err).Should(BeNil())
    96  
    97  	Expect(yaml.Unmarshal(workloaddata, &wd)).Should(BeNil())
    98  
    99  	wd.Namespace = DefinitionNamespace
   100  	logf.Log.Info("Creating workload definition", "data", wd)
   101  	Expect(k8sClient.Create(ctx, &wd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
   102  
   103  	componentdata, err := os.ReadFile("testdata/componentDef.yaml")
   104  	Expect(err).Should(BeNil())
   105  
   106  	Expect(yaml.Unmarshal(componentdata, &cd)).Should(BeNil())
   107  
   108  	cd.Namespace = DefinitionNamespace
   109  	logf.Log.Info("Creating component definition", "data", cd)
   110  	Expect(k8sClient.Create(ctx, &cd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
   111  
   112  	websvcWorkloadData, err := os.ReadFile("testdata/websvcWorkloadDef.yaml")
   113  	Expect(err).Should(BeNil())
   114  
   115  	Expect(yaml.Unmarshal(websvcWorkloadData, &websvcWD)).Should(BeNil())
   116  	websvcWD.Namespace = DefinitionNamespace
   117  	logf.Log.Info("Creating workload definition whose CUE template from remote", "data", &websvcWD)
   118  	Expect(k8sClient.Create(ctx, &websvcWD)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
   119  
   120  	websvcComponentDefData, err := os.ReadFile("testdata/websvcComponentDef.yaml")
   121  	Expect(err).Should(BeNil())
   122  
   123  	Expect(yaml.Unmarshal(websvcComponentDefData, &websvcCD)).Should(BeNil())
   124  	websvcCD.Namespace = DefinitionNamespace
   125  	logf.Log.Info("Creating component definition whose CUE template from remote", "data", &websvcCD)
   126  	Expect(k8sClient.Create(ctx, &websvcCD)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
   127  })
   128  
   129  var DefinitionNamespace = "testdef"
   130  var _ = AfterSuite(func() {
   131  	By("tearing down the test environment")
   132  	_ = k8sClient.Delete(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: DefinitionNamespace}})
   133  	_ = k8sClient.Delete(context.Background(), &td)
   134  	_ = k8sClient.Delete(context.Background(), &wd)
   135  	_ = k8sClient.Delete(context.Background(), &websvcWD)
   136  	err := testEnv.Stop()
   137  	Expect(err).ToNot(HaveOccurred())
   138  })