github.com/oam-dev/kubevela@v1.9.11/pkg/controller/utils/capability_integrate_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  
    18  package utils
    19  
    20  import (
    21  	"context"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/utils/pointer"
    28  	"sigs.k8s.io/yaml"
    29  
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	"github.com/oam-dev/kubevela/pkg/oam/util"
    32  )
    33  
    34  var _ = Describe("Test Capability", func() {
    35  	ctx := context.Background()
    36  	var (
    37  		namespace = "ns-cap"
    38  		ns        corev1.Namespace
    39  	)
    40  
    41  	BeforeEach(func() {
    42  		ns = corev1.Namespace{
    43  			ObjectMeta: metav1.ObjectMeta{
    44  				Name: namespace,
    45  			},
    46  		}
    47  		By("Create a namespace")
    48  		Expect(k8sClient.Create(ctx, &ns)).Should(SatisfyAny(Succeed(), &util.AlreadyExistMatcher{}))
    49  	})
    50  
    51  	Context("When the definition is ComponentDefinition", func() {
    52  		var componentDefinitionName = "web1"
    53  
    54  		It("Test CapabilityComponentDefinition", func() {
    55  			By("Apply ComponentDefinition")
    56  			var validComponentDefinition = `
    57  apiVersion: core.oam.dev/v1beta1
    58  kind: ComponentDefinition
    59  metadata:
    60    name: web1
    61    namespace: ns-cap
    62    annotations:
    63      definition.oam.dev/description: "test"
    64  spec:
    65    workload:
    66      type: deployments.apps
    67    schematic:
    68      cue:
    69        template: |
    70          outputs: {
    71          	apiVersion: "apps/v1"
    72          	kind:       "Deployment"
    73          	spec: {
    74          		selector: matchLabels: {
    75          			"app.oam.dev/component": context.name
    76          		}
    77  
    78          		template: {
    79          			metadata: labels: {
    80          				"app.oam.dev/component": context.name
    81          			}
    82  
    83          			spec: {
    84          				containers: [{
    85          					name:  context.name
    86          					image: parameter.image
    87  
    88          					if parameter["cmd"] != _|_ {
    89          						command: parameter.cmd
    90          					}
    91          				}]
    92          			}
    93          		}
    94          	}
    95          }
    96          parameter: {
    97          	// +usage=Which image would you like to use for your service
    98          	// +short=i
    99          	image: string
   100  
   101          	// +usage=Commands to run in the container
   102          	cmd?: [...string]
   103          }
   104  
   105  `
   106  			var componentDefinition v1beta1.ComponentDefinition
   107  			Expect(yaml.Unmarshal([]byte(validComponentDefinition), &componentDefinition)).Should(BeNil())
   108  			Expect(k8sClient.Create(ctx, &componentDefinition)).Should(Succeed())
   109  
   110  			By("Test GetCapabilityObject")
   111  			def := &CapabilityComponentDefinition{Name: componentDefinitionName, ComponentDefinition: *componentDefinition.DeepCopy()}
   112  
   113  			By("Test GetOpenAPISchema")
   114  			schema, err := def.GetOpenAPISchema(namespace)
   115  			Expect(err).Should(BeNil())
   116  			Expect(schema).Should(Not(BeNil()))
   117  		})
   118  	})
   119  
   120  	Context("When the definition is CapabilityBaseDefinition", func() {
   121  
   122  		It("Test CapabilityTraitDefinition", func() {
   123  			By("Test CreateOrUpdateConfigMap")
   124  			definitionName := "n1"
   125  			def := &CapabilityBaseDefinition{}
   126  			ownerReference := []metav1.OwnerReference{{
   127  				APIVersion:         "v1",
   128  				Kind:               "k1",
   129  				Name:               definitionName,
   130  				UID:                "123456",
   131  				Controller:         pointer.Bool(true),
   132  				BlockOwnerDeletion: pointer.Bool(true),
   133  			}}
   134  			_, err := def.CreateOrUpdateConfigMap(ctx, k8sClient, namespace, definitionName, typeTraitDefinition, nil, nil, []byte(""), ownerReference)
   135  			Expect(err).Should(BeNil())
   136  		})
   137  	})
   138  })