github.com/kubevela/workflow@v0.6.0/pkg/hooks/data_passing_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 hooks 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/crossplane/crossplane-runtime/pkg/test" 24 "github.com/stretchr/testify/require" 25 "k8s.io/apimachinery/pkg/runtime" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 "github.com/kubevela/workflow/api/v1alpha1" 29 wfContext "github.com/kubevela/workflow/pkg/context" 30 "github.com/kubevela/workflow/pkg/cue/model/value" 31 ) 32 33 func TestInput(t *testing.T) { 34 wfCtx := mockContext(t) 35 r := require.New(t) 36 paramValue, err := wfCtx.MakeParameter(`"name": "foo"`) 37 r.NoError(err) 38 score, err := paramValue.MakeValue(`score: 99`) 39 r.NoError(err) 40 err = wfCtx.SetVar(score, "foo") 41 r.NoError(err) 42 err = Input(wfCtx, paramValue, v1alpha1.WorkflowStep{ 43 WorkflowStepBase: v1alpha1.WorkflowStepBase{ 44 DependsOn: []string{"mystep"}, 45 Inputs: v1alpha1.StepInputs{{ 46 From: "foo.score", 47 ParameterKey: "myscore", 48 }}, 49 }, 50 }) 51 r.NoError(err) 52 result, err := paramValue.LookupValue("parameter", "myscore") 53 r.NoError(err) 54 s, err := result.String() 55 r.NoError(err) 56 r.Equal(s, `99 57 `) 58 // test set value 59 paramValue, err = wfCtx.MakeParameter(`parameter: {myscore: "test"}`) 60 r.NoError(err) 61 err = Input(wfCtx, paramValue, v1alpha1.WorkflowStep{ 62 WorkflowStepBase: v1alpha1.WorkflowStepBase{ 63 DependsOn: []string{"mystep"}, 64 Inputs: v1alpha1.StepInputs{{ 65 From: "foo.score", 66 ParameterKey: "myscore", 67 }}, 68 }, 69 }) 70 r.NoError(err) 71 result, err = paramValue.LookupValue("parameter", "myscore") 72 r.NoError(err) 73 s, err = result.String() 74 r.NoError(err) 75 r.Equal(s, `99 76 `) 77 paramValue, err = wfCtx.MakeParameter(`context: {name: "test"}`) 78 r.NoError(err) 79 err = Input(wfCtx, paramValue, v1alpha1.WorkflowStep{ 80 WorkflowStepBase: v1alpha1.WorkflowStepBase{ 81 Inputs: v1alpha1.StepInputs{{ 82 From: "context.name", 83 ParameterKey: "contextname", 84 }}, 85 }, 86 }) 87 r.NoError(err) 88 result, err = paramValue.LookupValue("parameter", "contextname") 89 r.NoError(err) 90 s, err = result.String() 91 r.NoError(err) 92 r.Equal(s, `"test" 93 `) 94 } 95 96 func TestOutput(t *testing.T) { 97 wfCtx := mockContext(t) 98 r := require.New(t) 99 taskValue, err := value.NewValue(` 100 output: score: 99 101 `, nil, "") 102 r.NoError(err) 103 stepStatus := make(map[string]v1alpha1.StepStatus) 104 err = Output(wfCtx, taskValue, v1alpha1.WorkflowStep{ 105 WorkflowStepBase: v1alpha1.WorkflowStepBase{ 106 Properties: &runtime.RawExtension{ 107 Raw: []byte("{\"name\":\"mystep\"}"), 108 }, 109 Outputs: v1alpha1.StepOutputs{{ 110 ValueFrom: "output.score", 111 Name: "myscore", 112 }}, 113 }, 114 }, v1alpha1.StepStatus{ 115 Phase: v1alpha1.WorkflowStepPhaseSucceeded, 116 }, stepStatus) 117 r.NoError(err) 118 result, err := wfCtx.GetVar("myscore") 119 r.NoError(err) 120 s, err := result.String() 121 r.NoError(err) 122 r.Equal(s, `99 123 `) 124 r.Equal(stepStatus["mystep"].Phase, v1alpha1.WorkflowStepPhaseSucceeded) 125 } 126 127 func mockContext(t *testing.T) wfContext.Context { 128 cli := &test.MockClient{ 129 MockCreate: func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { 130 return nil 131 }, 132 MockPatch: func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { 133 return nil 134 }, 135 MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error { 136 return nil 137 }, 138 } 139 wfCtx, err := wfContext.NewContext(context.Background(), cli, "default", "v1", nil) 140 require.NoError(t, err) 141 return wfCtx 142 }