github.com/oam-dev/kubevela@v1.9.11/pkg/cue/process/handle_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 process 18 19 import ( 20 "testing" 21 22 "cuelang.org/go/cue/cuecontext" 23 "github.com/stretchr/testify/assert" 24 25 "github.com/kubevela/workflow/pkg/cue/model" 26 "github.com/kubevela/workflow/pkg/cue/model/value" 27 "github.com/kubevela/workflow/pkg/cue/process" 28 29 "github.com/oam-dev/kubevela/apis/types" 30 ) 31 32 func TestContext(t *testing.T) { 33 baseTemplate := ` 34 image: "myserver" 35 ` 36 37 inst := cuecontext.New().CompileString(baseTemplate) 38 base, err := model.NewBase(inst) 39 if err != nil { 40 t.Error(err) 41 return 42 } 43 44 serviceTemplate := ` 45 apiVersion: "v1" 46 kind: "ConfigMap" 47 ` 48 49 svcInst := cuecontext.New().CompileString(serviceTemplate) 50 51 svcIns, err := model.NewOther(svcInst) 52 if err != nil { 53 t.Error(err) 54 return 55 } 56 57 svcAux := process.Auxiliary{ 58 Ins: svcIns, 59 Name: "service", 60 } 61 62 svcAuxWithAbnormalName := process.Auxiliary{ 63 Ins: svcIns, 64 Name: "service-1", 65 } 66 67 targetParams := map[string]interface{}{ 68 "parameter1": "string", 69 "parameter2": map[string]string{ 70 "key1": "value1", 71 "key2": "value2", 72 }, 73 "parameter3": []string{"item1", "item2"}, 74 } 75 targetData := map[string]interface{}{ 76 "int": 10, 77 "string": "mytxt", 78 "bool": false, 79 "map": map[string]interface{}{ 80 "key": "value", 81 }, 82 "slice": []string{ 83 "str1", "str2", "str3", 84 }, 85 } 86 targetArbitraryData := map[string]interface{}{ 87 "int": 10, 88 "string": "mytxt", 89 "bool": false, 90 "map": map[string]interface{}{ 91 "key": "value", 92 }, 93 "slice": []string{ 94 "str1", "str2", "str3", 95 }, 96 } 97 98 ctx := NewContext(ContextData{ 99 AppName: "myapp", 100 CompName: "mycomp", 101 Namespace: "myns", 102 AppRevisionName: "myapp-v1", 103 WorkflowName: "myworkflow", 104 PublishVersion: "mypublishversion", 105 }) 106 ctx.SetBase(base) 107 ctx.AppendAuxiliaries(svcAux) 108 ctx.AppendAuxiliaries(svcAuxWithAbnormalName) 109 ctx.SetParameters(targetParams) 110 ctx.PushData(ContextDataArtifacts, targetData) 111 ctx.PushData("arbitraryData", targetArbitraryData) 112 113 c, err := ctx.BaseContextFile() 114 if err != nil { 115 t.Error(err) 116 return 117 } 118 ctxInst := cuecontext.New().CompileString(c) 119 120 gName, err := ctxInst.LookupPath(value.FieldPath("context", ContextName)).String() 121 assert.Equal(t, nil, err) 122 assert.Equal(t, "mycomp", gName) 123 124 myAppName, err := ctxInst.LookupPath(value.FieldPath("context", ContextAppName)).String() 125 assert.Equal(t, nil, err) 126 assert.Equal(t, "myapp", myAppName) 127 128 myAppRevision, err := ctxInst.LookupPath(value.FieldPath("context", ContextAppRevision)).String() 129 assert.Equal(t, nil, err) 130 assert.Equal(t, "myapp-v1", myAppRevision) 131 132 myAppRevisionNum, err := ctxInst.LookupPath(value.FieldPath("context", ContextAppRevisionNum)).Int64() 133 assert.Equal(t, nil, err) 134 assert.Equal(t, int64(1), myAppRevisionNum) 135 136 myWorkflowName, err := ctxInst.LookupPath(value.FieldPath("context", ContextWorkflowName)).String() 137 assert.Equal(t, nil, err) 138 assert.Equal(t, "myworkflow", myWorkflowName) 139 140 myPublishVersion, err := ctxInst.LookupPath(value.FieldPath("context", ContextPublishVersion)).String() 141 assert.Equal(t, nil, err) 142 assert.Equal(t, "mypublishversion", myPublishVersion) 143 144 inputJs, err := ctxInst.LookupPath(value.FieldPath("context", OutputFieldName)).MarshalJSON() 145 assert.Equal(t, nil, err) 146 assert.Equal(t, `{"image":"myserver"}`, string(inputJs)) 147 148 outputsJs, err := ctxInst.LookupPath(value.FieldPath("context", OutputsFieldName, "service")).MarshalJSON() 149 assert.Equal(t, nil, err) 150 assert.Equal(t, "{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\"}", string(outputsJs)) 151 152 outputsJs, err = ctxInst.LookupPath(value.FieldPath("context", OutputsFieldName, "service-1")).MarshalJSON() 153 assert.Equal(t, nil, err) 154 assert.Equal(t, "{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\"}", string(outputsJs)) 155 156 ns, err := ctxInst.LookupPath(value.FieldPath("context", ContextNamespace)).String() 157 assert.Equal(t, nil, err) 158 assert.Equal(t, "myns", ns) 159 160 params, err := ctxInst.LookupPath(value.FieldPath("context", ParameterFieldName)).MarshalJSON() 161 assert.Equal(t, nil, err) 162 assert.Equal(t, "{\"parameter1\":\"string\",\"parameter2\":{\"key1\":\"value1\",\"key2\":\"value2\"},\"parameter3\":[\"item1\",\"item2\"]}", string(params)) 163 164 artifacts, err := ctxInst.LookupPath(value.FieldPath("context", ContextDataArtifacts)).MarshalJSON() 165 assert.Equal(t, nil, err) 166 assert.Equal(t, "{\"bool\":false,\"int\":10,\"map\":{\"key\":\"value\"},\"slice\":[\"str1\",\"str2\",\"str3\"],\"string\":\"mytxt\"}", string(artifacts)) 167 168 arbitraryData, err := ctxInst.LookupPath(value.FieldPath("context", "arbitraryData")).MarshalJSON() 169 assert.Equal(t, nil, err) 170 assert.Equal(t, "{\"bool\":false,\"int\":10,\"map\":{\"key\":\"value\"},\"slice\":[\"str1\",\"str2\",\"str3\"],\"string\":\"mytxt\"}", string(arbitraryData)) 171 } 172 173 func TestParseClusterVersion(t *testing.T) { 174 types.ControlPlaneClusterVersion = types.ClusterVersion{Minor: "18+"} 175 got := parseClusterVersion(types.ClusterVersion{}) 176 assert.Equal(t, got["minor"], int64(18)) 177 178 types.ControlPlaneClusterVersion = types.ClusterVersion{Minor: "22-"} 179 got = parseClusterVersion(types.ClusterVersion{}) 180 assert.Equal(t, got["minor"], int64(22)) 181 }