github.com/oam-dev/kubevela@v1.9.11/pkg/cue/convert_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 cue 18 19 import ( 20 "os" 21 "testing" 22 23 "cuelang.org/go/cue" 24 "github.com/stretchr/testify/assert" 25 26 "github.com/oam-dev/kubevela/apis/types" 27 ) 28 29 func TestGetParameter(t *testing.T) { 30 data, _ := os.ReadFile("testdata/workloads/metrics.cue") 31 params, err := GetParameters(string(data), nil) 32 assert.NoError(t, err) 33 assert.Equal(t, params, []types.Parameter{ 34 {Name: "format", Required: false, Default: "prometheus", Usage: "format of the metrics, " + 35 "default as prometheus", Short: "f", Type: cue.StringKind}, 36 {Name: "enabled", Required: false, Default: true, Type: cue.BoolKind}, 37 {Name: "port", Required: false, Default: int64(8080), Type: cue.IntKind}, 38 {Name: "selector", Required: false, Usage: "the label selector for the pods, default is the workload labels", Type: cue.StructKind}, 39 }) 40 data, _ = os.ReadFile("testdata/workloads/deployment.cue") 41 params, err = GetParameters(string(data), nil) 42 assert.NoError(t, err) 43 assert.Equal(t, []types.Parameter{ 44 {Name: "name", Required: true, Default: "", Type: cue.StringKind}, 45 {Name: "image", Short: "i", Required: true, Usage: "Which image would you like to use for your service", Default: "", Type: cue.StringKind}, 46 {Name: "port", Short: "p", Required: false, Usage: "Which port do you want customer traffic sent to", Default: int64(8080), 47 Type: cue.IntKind}, 48 {Name: "env", Required: false, Default: nil, Type: cue.ListKind}, 49 {Name: "cpu", Short: "", Required: false, Usage: "", Default: "", Type: cue.StringKind}}, 50 params) 51 52 data, _ = os.ReadFile("testdata/workloads/test-param.cue") 53 params, err = GetParameters(string(data), nil) 54 assert.NoError(t, err) 55 assert.Equal(t, []types.Parameter{ 56 {Name: "name", Required: true, Default: "", Type: cue.StringKind}, 57 {Name: "image", Short: "i", Required: true, Usage: "Which image would you like to use for your service", Default: "", Type: cue.StringKind}, 58 {Name: "port", Short: "p", Usage: "Which port do you want customer traffic sent to", Default: int64(8080), Type: cue.IntKind}, 59 {Name: "env", Required: false, Default: nil, Type: cue.ListKind}, 60 {Name: "enable", Default: false, Type: cue.BoolKind}, 61 {Name: "fval", Default: 64.3, Type: cue.FloatKind}, 62 {Name: "nval", Default: float64(0), Required: true, Type: cue.NumberKind}}, params) 63 data, _ = os.ReadFile("testdata/workloads/empty.cue") 64 params, err = GetParameters(string(data), nil) 65 assert.NoError(t, err) 66 var exp []types.Parameter 67 assert.Equal(t, exp, params) 68 69 data, _ = os.ReadFile("testdata/workloads/webservice.cue") // test cue parameter with "// +ignore" annotation 70 params, err = GetParameters(string(data), nil) // Only test for func RetrieveComments 71 assert.NoError(t, err) 72 var flag bool 73 for _, para := range params { 74 if para.Name == "addRevisionLabel" { 75 flag = true 76 assert.Equal(t, para.Usage, "If addRevisionLabel is true, the appRevision label will be added to the underlying pods") 77 assert.Equal(t, para.Ignore, true) 78 } 79 } 80 assert.Equal(t, flag, true) 81 }