github.com/kubevela/workflow@v0.6.0/pkg/cue/process/handle_test.go (about)

     1  /*
     2  Copyright 2022 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/require"
    24  
    25  	"github.com/kubevela/workflow/pkg/cue/model"
    26  	"github.com/kubevela/workflow/pkg/cue/model/value"
    27  )
    28  
    29  func TestContext(t *testing.T) {
    30  	baseTemplate := `
    31  image: "myserver"
    32  `
    33  
    34  	r := require.New(t)
    35  	inst := cuecontext.New().CompileString(baseTemplate)
    36  	base, err := model.NewBase(inst)
    37  	r.NoError(err)
    38  
    39  	serviceTemplate := `
    40  	apiVersion: "v1"
    41      kind:       "ConfigMap"
    42  `
    43  
    44  	svcInst := cuecontext.New().CompileString(serviceTemplate)
    45  
    46  	svcIns, err := model.NewOther(svcInst)
    47  	r.NoError(err)
    48  
    49  	svcAux := Auxiliary{
    50  		Ins:  svcIns,
    51  		Name: "service",
    52  	}
    53  
    54  	svcAuxWithAbnormalName := Auxiliary{
    55  		Ins:  svcIns,
    56  		Name: "service-1",
    57  	}
    58  
    59  	targetParams := map[string]interface{}{
    60  		"parameter1": "string",
    61  		"parameter2": map[string]string{
    62  			"key1": "value1",
    63  			"key2": "value2",
    64  		},
    65  		"parameter3": []string{"item1", "item2"},
    66  	}
    67  	targetArbitraryData := map[string]interface{}{
    68  		"int":    10,
    69  		"string": "mytxt",
    70  		"bool":   false,
    71  		"map": map[string]interface{}{
    72  			"key": "value",
    73  		},
    74  		"slice": []string{
    75  			"str1", "str2", "str3",
    76  		},
    77  	}
    78  
    79  	ctx := NewContext(ContextData{
    80  		Name:           "myrun",
    81  		Namespace:      "myns",
    82  		WorkflowName:   "myworkflow",
    83  		PublishVersion: "mypublishversion",
    84  	})
    85  	err = ctx.SetBase(base)
    86  	r.NoError(err)
    87  	err = ctx.AppendAuxiliaries(svcAux)
    88  	r.NoError(err)
    89  	err = ctx.AppendAuxiliaries(svcAuxWithAbnormalName)
    90  	r.NoError(err)
    91  	ctx.SetParameters(targetParams)
    92  	ctx.PushData("arbitraryData", targetArbitraryData)
    93  	get := ctx.GetData("arbitraryData")
    94  	r.Equal(get, targetArbitraryData)
    95  
    96  	c, err := ctx.BaseContextFile()
    97  	r.NoError(err)
    98  	ctxInst := cuecontext.New().CompileString(c)
    99  
   100  	gName, err := ctxInst.LookupPath(value.FieldPath("context", model.ContextName)).String()
   101  	r.Equal(nil, err)
   102  	r.Equal("myrun", gName)
   103  
   104  	myWorkflowName, err := ctxInst.LookupPath(value.FieldPath("context", model.ContextWorkflowName)).String()
   105  	r.Equal(nil, err)
   106  	r.Equal("myworkflow", myWorkflowName)
   107  
   108  	myPublishVersion, err := ctxInst.LookupPath(value.FieldPath("context", model.ContextPublishVersion)).String()
   109  	r.Equal(nil, err)
   110  	r.Equal("mypublishversion", myPublishVersion)
   111  
   112  	inputJs, err := ctxInst.LookupPath(value.FieldPath("context", model.OutputFieldName)).MarshalJSON()
   113  	r.Equal(nil, err)
   114  	r.Equal(`{"image":"myserver"}`, string(inputJs))
   115  
   116  	outputsJs, err := ctxInst.LookupPath(value.FieldPath("context", model.OutputsFieldName, "service")).MarshalJSON()
   117  	r.Equal(nil, err)
   118  	r.Equal("{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\"}", string(outputsJs))
   119  
   120  	outputsJs, err = ctxInst.LookupPath(value.FieldPath("context", model.OutputsFieldName, "service-1")).MarshalJSON()
   121  	r.Equal(nil, err)
   122  	r.Equal("{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\"}", string(outputsJs))
   123  
   124  	ns, err := ctxInst.LookupPath(value.FieldPath("context", model.ContextNamespace)).String()
   125  	r.Equal(nil, err)
   126  	r.Equal("myns", ns)
   127  
   128  	params, err := ctxInst.LookupPath(value.FieldPath("context", model.ParameterFieldName)).MarshalJSON()
   129  	r.Equal(nil, err)
   130  	r.Equal("{\"parameter1\":\"string\",\"parameter2\":{\"key1\":\"value1\",\"key2\":\"value2\"},\"parameter3\":[\"item1\",\"item2\"]}", string(params))
   131  
   132  	arbitraryData, err := ctxInst.LookupPath(value.FieldPath("context", "arbitraryData")).MarshalJSON()
   133  	r.Equal(nil, err)
   134  	r.Equal("{\"bool\":false,\"int\":10,\"map\":{\"key\":\"value\"},\"slice\":[\"str1\",\"str2\",\"str3\"],\"string\":\"mytxt\"}", string(arbitraryData))
   135  }