github.com/oam-dev/kubevela@v1.9.11/pkg/appfile/validate_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  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  
    23  	"github.com/oam-dev/kubevela/apis/types"
    24  	"github.com/oam-dev/kubevela/pkg/cue/definition"
    25  )
    26  
    27  var _ = Describe("Test validate CUE schematic Appfile", func() {
    28  	type SubTestCase struct {
    29  		compDefTmpl   string
    30  		traitDefTmpl1 string
    31  		traitDefTmpl2 string
    32  		wantErrMsg    string
    33  	}
    34  
    35  	DescribeTable("Test validate outputs name unique", func(tc SubTestCase) {
    36  		Expect("").Should(BeEmpty())
    37  		wl := &Component{
    38  			Name:               "myweb",
    39  			Type:               "worker",
    40  			CapabilityCategory: types.CUECategory,
    41  			Traits: []*Trait{
    42  				{
    43  					Name:               "myscaler",
    44  					CapabilityCategory: types.CUECategory,
    45  					Template:           tc.traitDefTmpl1,
    46  					engine:             definition.NewTraitAbstractEngine("myscaler", pd),
    47  				},
    48  				{
    49  					Name:               "myingress",
    50  					CapabilityCategory: types.CUECategory,
    51  					Template:           tc.traitDefTmpl2,
    52  					engine:             definition.NewTraitAbstractEngine("myingress", pd),
    53  				},
    54  			},
    55  			FullTemplate: &Template{
    56  				TemplateStr: tc.compDefTmpl,
    57  			},
    58  			engine: definition.NewWorkloadAbstractEngine("myweb", pd),
    59  		}
    60  
    61  		ctxData := GenerateContextDataFromAppFile(&Appfile{
    62  			Name:            "myapp",
    63  			Namespace:       "test-ns",
    64  			AppRevisionName: "myapp-v1",
    65  		}, wl.Name)
    66  		pCtx, err := newValidationProcessContext(wl, ctxData)
    67  		Expect(err).Should(BeNil())
    68  		Eventually(func() string {
    69  			for _, tr := range wl.Traits {
    70  				if err := tr.EvalContext(pCtx); err != nil {
    71  					return err.Error()
    72  				}
    73  			}
    74  			return ""
    75  		}).Should(ContainSubstring(tc.wantErrMsg))
    76  	},
    77  		Entry("Succeed", SubTestCase{
    78  			compDefTmpl: `
    79  			output: {
    80  				apiVersion: "apps/v1" 
    81  				kind: "Deployment"
    82  				}
    83  			outputs: mysvc: {
    84  				apiVersion: "v1"
    85  				kind: "Service"
    86  			}
    87  			`,
    88  			traitDefTmpl1: `
    89  			outputs: mysvc1: {
    90  				apiVersion: "v1"
    91  				kind: "Service"
    92  			}
    93  			`,
    94  			traitDefTmpl2: `
    95  			outputs: mysvc2: {
    96  				apiVersion: "v1"
    97  				kind: "Service"
    98  			}
    99  			`,
   100  			wantErrMsg: "",
   101  		}),
   102  		Entry("CompDef and TraitDef have same outputs", SubTestCase{
   103  			compDefTmpl: `
   104  			output: {
   105  				apiVersion: "apps/v1" 
   106  				kind: "Deployment"
   107  				}
   108  			outputs: mysvc1: {
   109  				apiVersion: "v1"
   110  				kind: "Service"
   111  			}
   112  			`,
   113  			traitDefTmpl1: `
   114  			outputs: mysvc1: {
   115  				apiVersion: "v1"
   116  				kind: "Service"
   117  			}
   118  			`,
   119  			traitDefTmpl2: `
   120  			outputs: mysvc2: {
   121  				apiVersion: "v1"
   122  				kind: "Service"
   123  			}
   124  			`,
   125  			wantErrMsg: `auxiliary "mysvc1" already exits`,
   126  		}),
   127  		Entry("TraitDefs have same outputs", SubTestCase{
   128  			compDefTmpl: `
   129  			output: {
   130  				apiVersion: "apps/v1" 
   131  				kind: "Deployment"
   132  				}
   133  			outputs: mysvc: {
   134  				apiVersion: "v1"
   135  				kind: "Service"
   136  			}
   137  			`,
   138  			traitDefTmpl1: `
   139  			outputs: mysvc1: {
   140  				apiVersion: "v1"
   141  				kind: "Service"
   142  			}
   143  			`,
   144  			traitDefTmpl2: `
   145  			outputs: mysvc1: {
   146  				apiVersion: "v1"
   147  				kind: "Service"
   148  			}
   149  			`,
   150  			wantErrMsg: `auxiliary "mysvc1" already exits`,
   151  		}),
   152  	)
   153  })