github.com/kubevela/workflow@v0.6.0/pkg/providers/workspace/workspace_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 workspace 18 19 import ( 20 "encoding/json" 21 "testing" 22 "time" 23 24 corev1 "k8s.io/api/core/v1" 25 "sigs.k8s.io/yaml" 26 27 "github.com/kubevela/workflow/api/v1alpha1" 28 wfContext "github.com/kubevela/workflow/pkg/context" 29 "github.com/kubevela/workflow/pkg/cue/model" 30 "github.com/kubevela/workflow/pkg/cue/model/value" 31 "github.com/kubevela/workflow/pkg/cue/process" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestProvider_DoVar(t *testing.T) { 36 wfCtx := newWorkflowContextForTest(t) 37 p := &provider{} 38 r := require.New(t) 39 40 v, err := value.NewValue(` 41 method: "Put" 42 path: "clusterIP" 43 value: "1.1.1.1" 44 `, nil, "") 45 r.NoError(err) 46 err = p.DoVar(nil, wfCtx, v, &mockAction{}) 47 r.NoError(err) 48 varV, err := wfCtx.GetVar("clusterIP") 49 r.NoError(err) 50 s, err := varV.CueValue().String() 51 r.NoError(err) 52 r.Equal(s, "1.1.1.1") 53 54 v, err = value.NewValue(` 55 method: "Get" 56 path: "clusterIP" 57 `, nil, "") 58 r.NoError(err) 59 err = p.DoVar(nil, wfCtx, v, &mockAction{}) 60 r.NoError(err) 61 varV, err = v.LookupValue("value") 62 r.NoError(err) 63 s, err = varV.CueValue().String() 64 r.NoError(err) 65 r.Equal(s, "1.1.1.1") 66 67 errCases := []string{` 68 value: "1.1.1.1" 69 `, ` 70 method: "Get" 71 `, ` 72 path: "ClusterIP" 73 `, ` 74 method: "Put" 75 path: "ClusterIP" 76 `} 77 78 for _, tCase := range errCases { 79 v, err = value.NewValue(tCase, nil, "") 80 r.NoError(err) 81 err = p.DoVar(nil, wfCtx, v, &mockAction{}) 82 r.Error(err) 83 } 84 } 85 86 func TestProvider_Wait(t *testing.T) { 87 wfCtx := newWorkflowContextForTest(t) 88 p := &provider{} 89 r := require.New(t) 90 act := &mockAction{} 91 v, err := value.NewValue(` 92 continue: 100!=100 93 message: "test log" 94 `, nil, "") 95 r.NoError(err) 96 err = p.Wait(nil, wfCtx, v, act) 97 r.NoError(err) 98 r.Equal(act.wait, true) 99 r.Equal(act.msg, "test log") 100 101 act = &mockAction{} 102 v, err = value.NewValue(` 103 continue: 100==100 104 message: "not invalid" 105 `, nil, "") 106 r.NoError(err) 107 err = p.Wait(nil, wfCtx, v, act) 108 r.NoError(err) 109 r.Equal(act.wait, false) 110 r.Equal(act.msg, "") 111 112 act = &mockAction{} 113 v, err = value.NewValue(` 114 continue: bool 115 message: string 116 `, nil, "") 117 r.NoError(err) 118 err = p.Wait(nil, wfCtx, v, act) 119 r.NoError(err) 120 r.Equal(act.wait, true) 121 122 act = &mockAction{} 123 v, err = value.NewValue(``, nil, "") 124 r.NoError(err) 125 err = p.Wait(nil, wfCtx, v, act) 126 r.NoError(err) 127 r.Equal(act.wait, true) 128 } 129 130 func TestProvider_Break(t *testing.T) { 131 wfCtx := newWorkflowContextForTest(t) 132 p := &provider{} 133 r := require.New(t) 134 act := &mockAction{} 135 err := p.Break(nil, wfCtx, nil, act) 136 r.NoError(err) 137 r.Equal(act.terminate, true) 138 139 act = &mockAction{} 140 v, err := value.NewValue(` 141 message: "terminate" 142 `, nil, "") 143 r.NoError(err) 144 err = p.Break(nil, wfCtx, v, act) 145 r.NoError(err) 146 r.Equal(act.terminate, true) 147 r.Equal(act.msg, "terminate") 148 } 149 150 func TestProvider_Suspend(t *testing.T) { 151 wfCtx := newWorkflowContextForTest(t) 152 pCtx := process.NewContext(process.ContextData{}) 153 pCtx.PushData(model.ContextStepSessionID, "test-id") 154 p := &provider{pCtx: pCtx} 155 r := require.New(t) 156 act := &mockAction{} 157 v, err := value.NewValue(` 158 duration: "1s" 159 `, nil, "") 160 r.NoError(err) 161 err = p.Suspend(nil, wfCtx, v, act) 162 r.NoError(err) 163 r.Equal(act.suspend, true) 164 r.Equal(act.msg, "Suspended by field ") 165 // test second time to check if the suspend is resumed in 1s 166 err = p.Suspend(nil, wfCtx, v, act) 167 r.NoError(err) 168 r.Equal(act.suspend, true) 169 time.Sleep(time.Second) 170 err = p.Suspend(nil, wfCtx, v, act) 171 r.NoError(err) 172 r.Equal(act.suspend, false) 173 } 174 175 func TestProvider_Fail(t *testing.T) { 176 wfCtx := newWorkflowContextForTest(t) 177 p := &provider{} 178 r := require.New(t) 179 act := &mockAction{} 180 err := p.Fail(nil, wfCtx, nil, act) 181 r.NoError(err) 182 r.Equal(act.terminate, true) 183 184 act = &mockAction{} 185 v, err := value.NewValue(` 186 message: "fail" 187 `, nil, "") 188 r.NoError(err) 189 err = p.Fail(nil, wfCtx, v, act) 190 r.NoError(err) 191 r.Equal(act.terminate, true) 192 r.Equal(act.msg, "fail") 193 } 194 195 func TestProvider_Message(t *testing.T) { 196 wfCtx := newWorkflowContextForTest(t) 197 p := &provider{} 198 r := require.New(t) 199 act := &mockAction{} 200 v, err := value.NewValue(` 201 message: "test" 202 `, nil, "") 203 r.NoError(err) 204 err = p.Message(nil, wfCtx, nil, act) 205 r.NoError(err) 206 r.Equal(act.msg, "") 207 err = p.Message(nil, wfCtx, v, act) 208 r.NoError(err) 209 r.Equal(act.msg, "test") 210 err = p.Message(nil, wfCtx, nil, act) 211 r.NoError(err) 212 r.Equal(act.msg, "test") 213 214 act = &mockAction{} 215 v, err = value.NewValue(` 216 message: "fail" 217 `, nil, "") 218 r.NoError(err) 219 err = p.Fail(nil, wfCtx, v, act) 220 r.NoError(err) 221 r.Equal(act.msg, "fail") 222 } 223 224 type mockAction struct { 225 suspend bool 226 terminate bool 227 wait bool 228 msg string 229 } 230 231 func (act *mockAction) GetStatus() v1alpha1.StepStatus { 232 return v1alpha1.StepStatus{} 233 } 234 235 func (act *mockAction) Suspend(msg string) { 236 act.suspend = true 237 if msg != "" { 238 act.msg = msg 239 } 240 } 241 242 func (act *mockAction) Resume(msg string) { 243 act.suspend = false 244 if msg != "" { 245 act.msg = msg 246 } 247 } 248 249 func (act *mockAction) Terminate(msg string) { 250 act.terminate = true 251 act.msg = msg 252 } 253 254 func (act *mockAction) Wait(msg string) { 255 act.wait = true 256 if msg != "" { 257 act.msg = msg 258 } 259 } 260 261 func (act *mockAction) Fail(msg string) { 262 act.terminate = true 263 if msg != "" { 264 act.msg = msg 265 } 266 } 267 268 func (act *mockAction) Message(msg string) { 269 if msg != "" { 270 act.msg = msg 271 } 272 } 273 274 func newWorkflowContextForTest(t *testing.T) wfContext.Context { 275 cm := corev1.ConfigMap{} 276 r := require.New(t) 277 testCaseJson, err := yaml.YAMLToJSON([]byte(testCaseYaml)) 278 r.NoError(err) 279 err = json.Unmarshal(testCaseJson, &cm) 280 r.NoError(err) 281 282 wfCtx := new(wfContext.WorkflowContext) 283 err = wfCtx.LoadFromConfigMap(cm) 284 r.NoError(err) 285 return wfCtx 286 } 287 288 var ( 289 testCaseYaml = `apiVersion: v1 290 data: 291 test: "" 292 kind: ConfigMap 293 metadata: 294 name: app-v1 295 ` 296 )