github.com/oam-dev/kubevela@v1.9.11/references/appfile/addon_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 appfile
    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  var cfg *rest.Config
    48  var scheme *runtime.Scheme
    49  var k8sClient client.Client
    50  var testEnv *envtest.Environment
    51  var definitionDir string
    52  var wd corev1beta1.WorkloadDefinition
    53  var addonNamespace = "test-addon"
    54  
    55  func TestAppFile(t *testing.T) {
    56  	RegisterFailHandler(Fail)
    57  	RunSpecs(t, "Cli Suite")
    58  }
    59  
    60  var _ = BeforeSuite(func() {
    61  	logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
    62  	ctx := context.Background()
    63  	By("bootstrapping test environment")
    64  	useExistCluster := false
    65  	testEnv = &envtest.Environment{
    66  		ControlPlaneStartTimeout: time.Minute,
    67  		ControlPlaneStopTimeout:  time.Minute,
    68  		CRDDirectoryPaths:        []string{filepath.Join("..", "..", "charts", "vela-core", "crds")},
    69  		UseExistingCluster:       &useExistCluster,
    70  	}
    71  
    72  	var err error
    73  	cfg, err = testEnv.Start()
    74  	Expect(err).ToNot(HaveOccurred())
    75  	Expect(cfg).ToNot(BeNil())
    76  	scheme = runtime.NewScheme()
    77  	Expect(coreoam.AddToScheme(scheme)).NotTo(HaveOccurred())
    78  	Expect(clientgoscheme.AddToScheme(scheme)).NotTo(HaveOccurred())
    79  	Expect(crdv1.AddToScheme(scheme)).NotTo(HaveOccurred())
    80  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
    81  	Expect(err).ToNot(HaveOccurred())
    82  	Expect(k8sClient).ToNot(BeNil())
    83  
    84  	definitionDir, err = system.GetCapabilityDir()
    85  	Expect(err).Should(BeNil())
    86  	Expect(os.MkdirAll(definitionDir, 0755)).Should(BeNil())
    87  
    88  	Expect(k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}})).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
    89  
    90  	workloadData, err := os.ReadFile("testdata/workloadDef.yaml")
    91  	Expect(err).Should(BeNil())
    92  
    93  	Expect(yaml.Unmarshal(workloadData, &wd)).Should(BeNil())
    94  
    95  	wd.Namespace = addonNamespace
    96  	logf.Log.Info("Creating workload definition", "data", wd)
    97  	Expect(k8sClient.Create(ctx, &wd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
    98  
    99  	def, err := os.ReadFile("testdata/terraform-aliyun-oss-workloadDefinition.yaml")
   100  	Expect(err).Should(BeNil())
   101  	var terraformDefinition corev1beta1.WorkloadDefinition
   102  	Expect(yaml.Unmarshal(def, &terraformDefinition)).Should(BeNil())
   103  	terraformDefinition.Namespace = addonNamespace
   104  	logf.Log.Info("Creating workload definition", "data", terraformDefinition)
   105  	Expect(k8sClient.Create(ctx, &terraformDefinition)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
   106  })
   107  
   108  var _ = AfterSuite(func() {
   109  	By("tearing down the test environment")
   110  	_ = k8sClient.Delete(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}})
   111  	_ = k8sClient.Delete(context.Background(), &wd)
   112  	err := testEnv.Stop()
   113  	Expect(err).ToNot(HaveOccurred())
   114  })