github.com/oam-dev/kubevela@v1.9.11/test/e2e-test/trait_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 controllers_test
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	corev1 "k8s.io/api/core/v1"
    28  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/types"
    30  	"k8s.io/utils/pointer"
    31  	"sigs.k8s.io/yaml"
    32  
    33  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    34  )
    35  
    36  func readAppFromFile(filename string) (*v1beta1.Application, error) {
    37  	bs, err := os.ReadFile(filename)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	app := &v1beta1.Application{}
    42  	if err = yaml.Unmarshal(bs, app); err != nil {
    43  		return nil, err
    44  	}
    45  	return app, nil
    46  }
    47  
    48  var _ = Describe("Trait tests", func() {
    49  	ctx := context.Background()
    50  	var namespace string
    51  
    52  	BeforeEach(func() {
    53  		namespace = randomNamespaceName("trait-test")
    54  		Expect(k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: v1.ObjectMeta{Name: namespace}})).Should(Succeed())
    55  	})
    56  
    57  	AfterEach(func() {
    58  		ns := &corev1.Namespace{}
    59  		Expect(k8sClient.Get(ctx, types.NamespacedName{Name: namespace}, ns)).Should(Succeed())
    60  		Expect(k8sClient.Delete(ctx, ns)).Should(Succeed())
    61  	})
    62  
    63  	Context("Test app with traits", func() {
    64  		It("Test json-patch trait", func() {
    65  			app, err := readAppFromFile("../../docs/examples/traits/json-patch/example.yaml")
    66  			Expect(err).Should(Succeed())
    67  			app.SetNamespace(namespace)
    68  			Expect(k8sClient.Create(ctx, app)).Should(Succeed())
    69  			Eventually(func(g Gomega) {
    70  				deploy := &appsv1.Deployment{}
    71  				g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "busybox"}, deploy)).Should(Succeed())
    72  				g.Expect(deploy.Labels).ShouldNot(BeNil())
    73  				g.Expect(deploy.Spec.Replicas).Should(Equal(pointer.Int32(3)))
    74  				g.Expect(deploy.Spec.Template.ObjectMeta.Labels).ShouldNot(BeNil())
    75  				g.Expect(deploy.Spec.Template.ObjectMeta.Labels["pod-label-key"]).Should(Equal("pod-label-modified-value"))
    76  				g.Expect(deploy.Spec.Template.ObjectMeta.Labels["to-delete-label-key"]).ShouldNot(Equal("to-delete-label-value"))
    77  				g.Expect(len(deploy.Spec.Template.Spec.Containers)).Should(Equal(2))
    78  				g.Expect(deploy.Spec.Template.Spec.Containers[1].Name).Should(Equal("busybox-sidecar"))
    79  				g.Expect(deploy.Spec.Template.Spec.Containers[1].Image).Should(Equal("busybox:1.34"))
    80  				g.Expect(deploy.Spec.Template.Spec.Containers[1].Command).Should(Equal([]string{"sleep", "864000"}))
    81  			}, 15*time.Second).Should(Succeed())
    82  		})
    83  
    84  		It("Test json-merge-patch trait", func() {
    85  			app, err := readAppFromFile("../../docs/examples/traits/json-merge-patch/example.yaml")
    86  			Expect(err).Should(Succeed())
    87  			app.SetNamespace(namespace)
    88  			Expect(k8sClient.Create(ctx, app)).Should(Succeed())
    89  			Eventually(func(g Gomega) {
    90  				deploy := &appsv1.Deployment{}
    91  				g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "busybox"}, deploy)).Should(Succeed())
    92  				g.Expect(deploy.Labels).ShouldNot(BeNil())
    93  				g.Expect(deploy.Labels["deploy-label-key"]).Should(Equal("deploy-label-added-value"))
    94  				g.Expect(deploy.Spec.Replicas).Should(Equal(pointer.Int32(3)))
    95  				g.Expect(deploy.Spec.Template.ObjectMeta.Labels).ShouldNot(BeNil())
    96  				g.Expect(deploy.Spec.Template.ObjectMeta.Labels["pod-label-key"]).Should(Equal("pod-label-modified-value"))
    97  				_, exists := deploy.Spec.Template.ObjectMeta.Labels["to-delete-label-key"]
    98  				g.Expect(exists).Should(BeFalse())
    99  				g.Expect(len(deploy.Spec.Template.Spec.Containers)).Should(Equal(1))
   100  				g.Expect(deploy.Spec.Template.Spec.Containers[0].Name).Should(Equal("busybox-new"))
   101  				g.Expect(deploy.Spec.Template.Spec.Containers[0].Image).Should(Equal("busybox:1.34"))
   102  				g.Expect(deploy.Spec.Template.Spec.Containers[0].Command).Should(Equal([]string{"sleep", "864000"}))
   103  			}, 15*time.Second).Should(Succeed())
   104  		})
   105  	})
   106  })