github.com/oam-dev/kubevela@v1.9.11/pkg/webhook/utils/utils_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 utils 18 19 import ( 20 "testing" 21 22 "cuelang.org/go/cue/errors" 23 "github.com/crossplane/crossplane-runtime/pkg/test" 24 "github.com/google/go-cmp/cmp" 25 ) 26 27 func TestValidateCueTemplate(t *testing.T) { 28 cases := map[string]struct { 29 cueTemplate string 30 want error 31 }{ 32 "normalCueTemp": { 33 cueTemplate: "name: 'name'", 34 want: nil, 35 }, 36 "contextNouFoundCueTemp": { 37 cueTemplate: ` 38 output: { 39 metadata: { 40 name: context.name 41 label: context.label 42 annotation: "default" 43 } 44 }`, 45 want: nil, 46 }, 47 "inValidCueTemp": { 48 cueTemplate: ` 49 output: { 50 metadata: { 51 name: context.name 52 label: context.label 53 annotation: "default" 54 }, 55 hello: world 56 }`, 57 want: errors.New("output.hello: reference \"world\" not found"), 58 }, 59 } 60 61 for caseName, cs := range cases { 62 t.Run(caseName, func(t *testing.T) { 63 err := ValidateCueTemplate(cs.cueTemplate) 64 if diff := cmp.Diff(cs.want, err, test.EquateErrors()); diff != "" { 65 t.Errorf("\n%s\nValidateCueTemplate: -want , +got \n%s\n", cs.want, diff) 66 } 67 }) 68 } 69 }