forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
841 lines (796 loc) · 26.4 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
package util
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kubefake "k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/yaml"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
argofake "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned/fake"
"github.com/argoproj/argo-workflows/v3/workflow/common"
hydratorfake "github.com/argoproj/argo-workflows/v3/workflow/hydrator/fake"
)
// TestSubmitDryRun
func TestSubmitDryRun(t *testing.T) {
workflowName := "test-dry-run"
workflowYaml := `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: ` + workflowName + `
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
`
wf := wfv1.MustUnmarshalWorkflow(workflowYaml)
newWf := wf.DeepCopy()
wfClientSet := argofake.NewSimpleClientset()
ctx := context.Background()
newWf, err := SubmitWorkflow(ctx, nil, wfClientSet, "test-namespace", newWf, &wfv1.SubmitOpts{DryRun: true})
assert.NoError(t, err)
assert.Equal(t, wf.Spec, newWf.Spec)
assert.Equal(t, wf.Status, newWf.Status)
}
// TestResubmitWorkflowWithOnExit ensures we do not carry over the onExit node even if successful
func TestResubmitWorkflowWithOnExit(t *testing.T) {
wfName := "test-wf"
onExitName := wfName + ".onExit"
wf := wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: wfName,
},
Status: wfv1.WorkflowStatus{
Phase: wfv1.WorkflowFailed,
Nodes: map[string]wfv1.NodeStatus{},
},
}
onExitID := wf.NodeID(onExitName)
wf.Status.Nodes[onExitID] = wfv1.NodeStatus{
Name: onExitName,
Phase: wfv1.NodeSucceeded,
}
newWF, err := FormulateResubmitWorkflow(&wf, true)
assert.NoError(t, err)
newWFOnExitName := newWF.ObjectMeta.Name + ".onExit"
newWFOneExitID := newWF.NodeID(newWFOnExitName)
_, ok := newWF.Status.Nodes[newWFOneExitID]
assert.False(t, ok)
}
// TestReadFromSingleorMultiplePath ensures we can read the content of a single file or multiple files correctly using the ReadFromFilePathsOrUrls function
func TestReadFromSingleorMultiplePath(t *testing.T) {
tests := map[string]struct {
fileNames []string
contents []string
}{
"singleFile": {
fileNames: []string{"singleFile"},
contents: []string{"test file's content"},
},
"multipleFiles": {
fileNames: []string{"file1", "file2"},
contents: []string{"file1 content", "file2 content"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dir, err := ioutil.TempDir("", name)
if err != nil {
t.Error("Could not create temporary directory")
}
defer os.RemoveAll(dir)
var filePaths []string
for i := range tc.fileNames {
content := []byte(tc.contents[i])
tmpfn := filepath.Join(dir, tc.fileNames[i])
filePaths = append(filePaths, tmpfn)
err := ioutil.WriteFile(tmpfn, content, 0o666)
if err != nil {
t.Error("Could not write to temporary file")
}
}
body, err := ReadFromFilePathsOrUrls(filePaths...)
assert.Equal(t, len(body), len(filePaths))
assert.NoError(t, err)
for i := range body {
assert.Equal(t, body[i], []byte(tc.contents[i]))
}
})
}
}
// TestReadFromSingleorMultiplePathErrorHandling ensures that an error is returned if there is any error while reading files or urls
func TestReadFromSingleorMultiplePathErrorHandling(t *testing.T) {
tests := map[string]struct {
fileNames []string
contents []string
exists []bool
}{
"nonExistingFile": {
fileNames: []string{"nonExistingFile"},
contents: []string{"this content should not exist"},
exists: []bool{false},
},
"multipleNonExistingFiles": {
fileNames: []string{"file1", "file2"},
contents: []string{"this content should not exist", "this content should not exist"},
exists: []bool{false, false},
},
"mixedExistingAndNonExistingFiles": {
fileNames: []string{"file1", "file2", "file3", "file4"},
contents: []string{"actual file content", "", "", "actual file content 2"},
exists: []bool{true, false, false, true},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dir, err := ioutil.TempDir("", name)
if err != nil {
t.Error("Could not create temporary directory")
}
defer os.RemoveAll(dir)
var filePaths []string
for i := range tc.fileNames {
content := []byte(tc.contents[i])
tmpfn := filepath.Join(dir, tc.fileNames[i])
filePaths = append(filePaths, tmpfn)
if tc.exists[i] {
err := ioutil.WriteFile(tmpfn, content, 0o666)
if err != nil {
t.Error("Could not write to temporary file")
}
}
}
body, err := ReadFromFilePathsOrUrls(filePaths...)
assert.NotNil(t, err)
assert.Equal(t, len(body), 0)
})
}
}
var yamlStr = `
containers:
- name: main
resources:
limits:
cpu: 1000m
`
func TestPodSpecPatchMerge(t *testing.T) {
tmpl := wfv1.Template{PodSpecPatch: "{\"containers\":[{\"name\":\"main\", \"resources\":{\"limits\":{\"cpu\": \"1000m\"}}}]}"}
wf := wfv1.Workflow{Spec: wfv1.WorkflowSpec{PodSpecPatch: "{\"containers\":[{\"name\":\"main\", \"resources\":{\"limits\":{\"memory\": \"100Mi\"}}}]}"}}
merged, err := PodSpecPatchMerge(&wf, &tmpl)
assert.NoError(t, err)
var spec v1.PodSpec
wfv1.MustUnmarshal([]byte(merged), &spec)
assert.Equal(t, "1.000", spec.Containers[0].Resources.Limits.Cpu().AsDec().String())
assert.Equal(t, "104857600", spec.Containers[0].Resources.Limits.Memory().AsDec().String())
tmpl = wfv1.Template{PodSpecPatch: yamlStr}
wf = wfv1.Workflow{Spec: wfv1.WorkflowSpec{PodSpecPatch: "{\"containers\":[{\"name\":\"main\", \"resources\":{\"limits\":{\"memory\": \"100Mi\"}}}]}"}}
merged, err = PodSpecPatchMerge(&wf, &tmpl)
assert.NoError(t, err)
wfv1.MustUnmarshal([]byte(merged), &spec)
assert.Equal(t, "1.000", spec.Containers[0].Resources.Limits.Cpu().AsDec().String())
assert.Equal(t, "104857600", spec.Containers[0].Resources.Limits.Memory().AsDec().String())
}
var suspendedWf = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
creationTimestamp: "2020-04-10T15:21:23Z"
name: suspend
generation: 2
labels:
workflows.argoproj.io/phase: Running
resourceVersion: "238969"
selfLink: /apis/argoproj.io/v1alpha1/namespaces/argo/workflows/suspend
uid: 4f08d325-dc5a-43a3-9986-259e259e6ea3
spec:
entrypoint: suspend
templates:
-
inputs: {}
metadata: {}
name: suspend
outputs: {}
steps:
- -
name: approve
template: approve
-
inputs: {}
metadata: {}
name: approve
outputs: {}
suspend: {}
status:
finishedAt: null
nodes:
suspend-template-xjsg2:
children:
- suspend-template-xjsg2-4125372399
displayName: suspend-template-xjsg2
finishedAt: null
id: suspend-template-xjsg2
name: suspend-template-xjsg2
phase: Running
startedAt: "2020-04-10T15:21:23Z"
templateName: suspend
templateScope: local/suspend-template-xjsg2
type: Steps
suspend-template-xjsg2-1771269240:
boundaryID: suspend-template-xjsg2
displayName: approve
finishedAt: null
id: suspend-template-xjsg2-1771269240
name: suspend-template-xjsg2[0].approve
phase: Running
startedAt: "2020-04-10T15:21:23Z"
templateName: approve
templateScope: local/suspend-template-xjsg2
type: Suspend
suspend-template-xjsg2-4125372399:
boundaryID: suspend-template-xjsg2
children:
- suspend-template-xjsg2-1771269240
displayName: '[0]'
finishedAt: null
id: suspend-template-xjsg2-4125372399
name: suspend-template-xjsg2[0]
phase: Running
startedAt: "2020-04-10T15:21:23Z"
templateName: suspend
templateScope: local/suspend-template-xjsg2
type: StepGroup
phase: Running
startedAt: "2020-04-10T15:21:23Z"
`
func TestResumeWorkflowByNodeName(t *testing.T) {
wfIf := argofake.NewSimpleClientset().ArgoprojV1alpha1().Workflows("")
origWf := wfv1.MustUnmarshalWorkflow(suspendedWf)
ctx := context.Background()
_, err := wfIf.Create(ctx, origWf, metav1.CreateOptions{})
assert.NoError(t, err)
// will return error as displayName does not match any nodes
err = ResumeWorkflow(ctx, wfIf, hydratorfake.Noop, "suspend", "displayName=nonexistant")
assert.Error(t, err)
// displayName didn't match suspend node so should still be running
wf, err := wfIf.Get(ctx, "suspend", metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, wfv1.NodeRunning, wf.Status.Nodes.FindByDisplayName("approve").Phase)
err = ResumeWorkflow(ctx, wfIf, hydratorfake.Noop, "suspend", "displayName=approve")
assert.NoError(t, err)
// displayName matched node so has succeeded
wf, err = wfIf.Get(ctx, "suspend", metav1.GetOptions{})
if assert.NoError(t, err) {
assert.Equal(t, wfv1.NodeSucceeded, wf.Status.Nodes.FindByDisplayName("approve").Phase)
}
}
func TestStopWorkflowByNodeName(t *testing.T) {
wfIf := argofake.NewSimpleClientset().ArgoprojV1alpha1().Workflows("")
origWf := wfv1.MustUnmarshalWorkflow(suspendedWf)
ctx := context.Background()
_, err := wfIf.Create(ctx, origWf, metav1.CreateOptions{})
assert.NoError(t, err)
// will return error as displayName does not match any nodes
err = StopWorkflow(ctx, wfIf, hydratorfake.Noop, "suspend", "displayName=nonexistant", "error occurred")
assert.Error(t, err)
// displayName didn't match suspend node so should still be running
wf, err := wfIf.Get(ctx, "suspend", metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, wfv1.NodeRunning, wf.Status.Nodes.FindByDisplayName("approve").Phase)
err = StopWorkflow(ctx, wfIf, hydratorfake.Noop, "suspend", "displayName=approve", "error occurred")
assert.NoError(t, err)
// displayName matched node so has succeeded
wf, err = wfIf.Get(ctx, "suspend", metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, wfv1.NodeFailed, wf.Status.Nodes.FindByDisplayName("approve").Phase)
}
// Regression test for #6478
func TestAddParamToGlobalScopeValueNil(t *testing.T) {
paramValue := wfv1.AnyString("test")
wf := wfv1.Workflow{
Status: wfv1.WorkflowStatus{
Outputs: &wfv1.Outputs{
Parameters: []wfv1.Parameter{
{
Name: "test",
Value: ¶mValue,
GlobalName: "global_output_param",
},
},
},
},
}
p := AddParamToGlobalScope(&wf, nil, wfv1.Parameter{
Name: "test",
Value: nil,
GlobalName: "test",
})
assert.False(t, p)
}
var susWorkflow = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: suspend-template
spec:
entrypoint: suspend
templates:
-
inputs: {}
metadata: {}
name: suspend
outputs: {}
steps:
- -
name: approve
template: approve
- - arguments:
parameters:
- name: message
value: '{{steps.approve.outputs.parameters.message}}'
name: release
template: whalesay
-
inputs: {}
metadata: {}
name: approve
outputs:
parameters:
- name: message
valueFrom:
supplied: {}
suspend: {}
-
container:
args:
- '{{inputs.parameters.message}}'
command:
- cowsay
image: docker/whalesay
name: ""
resources: {}
inputs:
parameters:
- name: message
metadata: {}
name: whalesay
outputs: {}
status:
finishedAt: null
nodes:
suspend-template-kgfn7:
children:
- suspend-template-kgfn7-1405476480
displayName: suspend-template-kgfn7
finishedAt: null
id: suspend-template-kgfn7
name: suspend-template-kgfn7
phase: Running
startedAt: "2020-06-25T18:01:56Z"
templateName: suspend
templateScope: local/suspend-template-kgfn7
type: Steps
suspend-template-kgfn7-1405476480:
boundaryID: suspend-template-kgfn7
children:
- suspend-template-kgfn7-2667278707
displayName: '[0]'
finishedAt: null
id: suspend-template-kgfn7-1405476480
name: suspend-template-kgfn7[0]
phase: Running
startedAt: "2020-06-25T18:01:56Z"
templateName: suspend
templateScope: local/suspend-template-kgfn7
type: StepGroup
suspend-template-kgfn7-2667278707:
boundaryID: suspend-template-kgfn7
displayName: approve
finishedAt: null
id: suspend-template-kgfn7-2667278707
name: suspend-template-kgfn7[0].approve
outputs:
parameters:
- name: message
valueFrom:
supplied: {}
- name: message2
globalName: message-global-param
valueFrom:
supplied: {}
phase: Running
startedAt: "2020-06-25T18:01:56Z"
templateName: approve
templateScope: local/suspend-template-kgfn7
type: Suspend
phase: Running
startedAt: "2020-06-25T18:01:56Z"
`
func TestUpdateSuspendedNode(t *testing.T) {
wfIf := argofake.NewSimpleClientset().ArgoprojV1alpha1().Workflows("")
origWf := wfv1.MustUnmarshalWorkflow(susWorkflow)
ctx := context.Background()
_, err := wfIf.Create(ctx, origWf, metav1.CreateOptions{})
if assert.NoError(t, err) {
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "does-not-exist", "displayName=approve", SetOperationValues{OutputParameters: map[string]string{"message": "Hello World"}})
assert.EqualError(t, err, "workflows.argoproj.io \"does-not-exist\" not found")
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "suspend-template", "displayName=does-not-exists", SetOperationValues{OutputParameters: map[string]string{"message": "Hello World"}})
assert.EqualError(t, err, "currently, set only targets suspend nodes: no suspend nodes matching nodeFieldSelector: displayName=does-not-exists")
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "suspend-template", "displayName=approve", SetOperationValues{OutputParameters: map[string]string{"does-not-exist": "Hello World"}})
assert.EqualError(t, err, "node is not expecting output parameter 'does-not-exist'")
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "suspend-template", "displayName=approve", SetOperationValues{OutputParameters: map[string]string{"message": "Hello World"}})
assert.NoError(t, err)
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "suspend-template", "name=suspend-template-kgfn7[0].approve", SetOperationValues{OutputParameters: map[string]string{"message2": "Hello World 2"}})
assert.NoError(t, err)
//make sure global variable was updated
wf, err := wfIf.Get(ctx, "suspend-template", metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, "Hello World 2", wf.Status.Outputs.Parameters[0].Value.String())
}
noSpaceWf := wfv1.MustUnmarshalWorkflow(susWorkflow)
noSpaceWf.Name = "suspend-template-no-outputs"
node := noSpaceWf.Status.Nodes["suspend-template-kgfn7-2667278707"]
node.Outputs = nil
noSpaceWf.Status.Nodes["suspend-template-kgfn7-2667278707"] = node
_, err = wfIf.Create(ctx, noSpaceWf, metav1.CreateOptions{})
if assert.NoError(t, err) {
err = updateSuspendedNode(ctx, wfIf, hydratorfake.Noop, "suspend-template-no-outputs", "displayName=approve", SetOperationValues{OutputParameters: map[string]string{"message": "Hello World"}})
assert.EqualError(t, err, "cannot set output parameters because node is not expecting any raw parameters")
}
}
func TestGetNodeType(t *testing.T) {
t.Run("getNodeType", func(t *testing.T) {
assert.Equal(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{Script: &wfv1.ScriptTemplate{}}))
assert.Equal(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{Container: &v1.Container{}}))
assert.Equal(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{Resource: &wfv1.ResourceTemplate{}}))
assert.NotEqual(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{Steps: []wfv1.ParallelSteps{}}))
assert.NotEqual(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{DAG: &wfv1.DAGTemplate{}}))
assert.NotEqual(t, wfv1.NodeTypePod, GetNodeType(&wfv1.Template{Suspend: &wfv1.SuspendTemplate{}}))
})
}
func TestApplySubmitOpts(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
assert.NoError(t, ApplySubmitOpts(&wfv1.Workflow{}, nil))
})
t.Run("InvalidLabels", func(t *testing.T) {
assert.Error(t, ApplySubmitOpts(&wfv1.Workflow{}, &wfv1.SubmitOpts{Labels: "a"}))
})
t.Run("Labels", func(t *testing.T) {
wf := &wfv1.Workflow{}
err := ApplySubmitOpts(wf, &wfv1.SubmitOpts{Labels: "a=1,b=1"})
assert.NoError(t, err)
assert.Len(t, wf.GetLabels(), 2)
})
t.Run("MergeLabels", func(t *testing.T) {
wf := &wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"a": "0", "b": "0"}}}
err := ApplySubmitOpts(wf, &wfv1.SubmitOpts{Labels: "a=1"})
assert.NoError(t, err)
if assert.Len(t, wf.GetLabels(), 2) {
assert.Equal(t, "1", wf.GetLabels()["a"])
assert.Equal(t, "0", wf.GetLabels()["b"])
}
})
t.Run("InvalidParameters", func(t *testing.T) {
assert.Error(t, ApplySubmitOpts(&wfv1.Workflow{}, &wfv1.SubmitOpts{Parameters: []string{"a"}}))
})
t.Run("Parameters", func(t *testing.T) {
wf := &wfv1.Workflow{
Spec: wfv1.WorkflowSpec{
Arguments: wfv1.Arguments{
Parameters: []wfv1.Parameter{{Name: "a", Value: wfv1.AnyStringPtr("0")}},
},
},
}
err := ApplySubmitOpts(wf, &wfv1.SubmitOpts{Parameters: []string{"a=81861780812"}})
assert.NoError(t, err)
parameters := wf.Spec.Arguments.Parameters
if assert.Len(t, parameters, 1) {
assert.Equal(t, "a", parameters[0].Name)
assert.Equal(t, "81861780812", parameters[0].Value.String())
}
})
t.Run("ParameterFile", func(t *testing.T) {
wf := &wfv1.Workflow{}
file, err := ioutil.TempFile("", "")
assert.NoError(t, err)
defer func() { _ = os.Remove(file.Name()) }()
err = ioutil.WriteFile(file.Name(), []byte(`a: 81861780812`), 0o644)
assert.NoError(t, err)
err = ApplySubmitOpts(wf, &wfv1.SubmitOpts{ParameterFile: file.Name()})
assert.NoError(t, err)
parameters := wf.Spec.Arguments.Parameters
if assert.Len(t, parameters, 1) {
assert.Equal(t, "a", parameters[0].Name)
assert.Equal(t, "81861780812", parameters[0].Value.String())
}
})
}
func TestFormulateResubmitWorkflow(t *testing.T) {
t.Run("Labels", func(t *testing.T) {
wf := &wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
common.LabelKeyControllerInstanceID: "1",
common.LabelKeyClusterWorkflowTemplate: "1",
common.LabelKeyCronWorkflow: "1",
common.LabelKeyWorkflowTemplate: "1",
common.LabelKeyCreator: "1",
common.LabelKeyPhase: "1",
common.LabelKeyCompleted: "1",
common.LabelKeyWorkflowArchivingStatus: "1",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "test",
Name: "testObj",
},
},
},
}
wf, err := FormulateResubmitWorkflow(wf, false)
if assert.NoError(t, err) {
assert.Contains(t, wf.GetLabels(), common.LabelKeyControllerInstanceID)
assert.Contains(t, wf.GetLabels(), common.LabelKeyClusterWorkflowTemplate)
assert.Contains(t, wf.GetLabels(), common.LabelKeyCronWorkflow)
assert.Contains(t, wf.GetLabels(), common.LabelKeyWorkflowTemplate)
assert.NotContains(t, wf.GetLabels(), common.LabelKeyCreator)
assert.NotContains(t, wf.GetLabels(), common.LabelKeyPhase)
assert.NotContains(t, wf.GetLabels(), common.LabelKeyCompleted)
assert.NotContains(t, wf.GetLabels(), common.LabelKeyWorkflowArchivingStatus)
assert.Contains(t, wf.GetLabels(), common.LabelKeyPreviousWorkflowName)
assert.Equal(t, 1, len(wf.OwnerReferences))
assert.Equal(t, "test", wf.OwnerReferences[0].APIVersion)
assert.Equal(t, "testObj", wf.OwnerReferences[0].Name)
}
})
}
var deepDeleteOfNodes = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
creationTimestamp: "2020-09-16T16:07:54Z"
generateName: steps-
generation: 13
labels:
workflows.argoproj.io/completed: "true"
workflows.argoproj.io/phase: Failed
name: steps-9fkqc
resourceVersion: "383660"
selfLink: /apis/argoproj.io/v1alpha1/namespaces/argo/workflows/steps-9fkqc
uid: 241a39ef-4ff1-487f-8461-98df5d2b50fb
spec:
entrypoint: foo
templates:
-
inputs: {}
metadata: {}
name: foo
outputs: {}
steps:
- -
name: pass
template: pass
- -
name: fail
template: fail
-
container:
args:
- exit 0
command:
- sh
- -c
image: alpine
name: ""
resources: {}
inputs: {}
metadata: {}
name: pass
outputs: {}
-
container:
args:
- exit 1
command:
- sh
- -c
image: alpine
name: ""
resources: {}
inputs: {}
metadata: {}
name: fail
outputs: {}
status:
conditions:
- status: "True"
type: Completed
finishedAt: "2020-09-16T16:09:32Z"
message: child 'steps-9fkqc-3224593506' failed
nodes:
steps-9fkqc:
children:
- steps-9fkqc-2929074125
displayName: steps-9fkqc
finishedAt: "2020-09-16T16:09:32Z"
id: steps-9fkqc
message: child 'steps-9fkqc-3224593506' failed
name: steps-9fkqc
outboundNodes:
- steps-9fkqc-3224593506
phase: Failed
startedAt: "2020-09-16T16:07:54Z"
templateName: foo
templateScope: local/steps-9fkqc
type: Steps
steps-9fkqc-1411266092:
boundaryID: steps-9fkqc
children:
- steps-9fkqc-2862110744
displayName: pass
finishedAt: "2020-09-16T16:07:57Z"
hostNodeName: minikube
id: steps-9fkqc-1411266092
name: steps-9fkqc[0].pass
outputs:
exitCode: "0"
phase: Succeeded
resourcesDuration:
cpu: 2
memory: 1
startedAt: "2020-09-16T16:07:54Z"
templateName: pass
templateScope: local/steps-9fkqc
type: Pod
steps-9fkqc-2862110744:
boundaryID: steps-9fkqc
children:
- steps-9fkqc-3224593506
displayName: '[1]'
finishedAt: "2020-09-16T16:09:32Z"
id: steps-9fkqc-2862110744
message: child 'steps-9fkqc-3224593506' failed
name: steps-9fkqc[1]
phase: Failed
startedAt: "2020-09-16T16:07:59Z"
templateName: foo
templateScope: local/steps-9fkqc
type: StepGroup
steps-9fkqc-2929074125:
boundaryID: steps-9fkqc
children:
- steps-9fkqc-1411266092
displayName: '[0]'
finishedAt: "2020-09-16T16:07:59Z"
id: steps-9fkqc-2929074125
name: steps-9fkqc[0]
phase: Succeeded
startedAt: "2020-09-16T16:07:54Z"
templateName: foo
templateScope: local/steps-9fkqc
type: StepGroup
steps-9fkqc-3224593506:
boundaryID: steps-9fkqc
displayName: fail
finishedAt: "2020-09-16T16:09:30Z"
hostNodeName: minikube
id: steps-9fkqc-3224593506
message: failed with exit code 1
name: steps-9fkqc[1].fail
outputs:
exitCode: "1"
phase: Failed
resourcesDuration:
cpu: 2
memory: 1
startedAt: "2020-09-16T16:09:27Z"
templateName: fail
templateScope: local/steps-9fkqc
type: Pod
phase: Failed
resourcesDuration:
cpu: 4
memory: 2
startedAt: "2020-09-16T16:07:54Z"
`
func TestDeepDeleteNodes(t *testing.T) {
wfIf := argofake.NewSimpleClientset().ArgoprojV1alpha1().Workflows("")
kubeClient := &kubefake.Clientset{}
origWf := wfv1.MustUnmarshalWorkflow(deepDeleteOfNodes)
ctx := context.Background()
wf, err := wfIf.Create(ctx, origWf, metav1.CreateOptions{})
if assert.NoError(t, err) {
newWf, err := RetryWorkflow(ctx, kubeClient, hydratorfake.Noop, wfIf, wf.Name, false, "")
assert.NoError(t, err)
newWfBytes, err := yaml.Marshal(newWf)
assert.NoError(t, err)
assert.NotContains(t, string(newWfBytes), "steps-9fkqc-3224593506")
}
}
func TestRetryWorkflow(t *testing.T) {
kubeClient := kubefake.NewSimpleClientset()
wfClient := argofake.NewSimpleClientset().ArgoprojV1alpha1().Workflows("my-ns")
createdTime := metav1.Time{Time: time.Now().UTC()}
finishedTime := metav1.Time{Time: createdTime.Add(time.Second * 2)}
wf := &wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{
common.LabelKeyCompleted: "true",
common.LabelKeyWorkflowArchivingStatus: "Pending",
}},
Status: wfv1.WorkflowStatus{
Phase: wfv1.WorkflowFailed,
StartedAt: createdTime,
FinishedAt: finishedTime,
Nodes: map[string]wfv1.NodeStatus{
"failed-node": {Name: "failed-node", StartedAt: createdTime, FinishedAt: finishedTime, Phase: wfv1.NodeFailed, Message: "failed"},
"succeeded-node": {Name: "succeeded-node", StartedAt: createdTime, FinishedAt: finishedTime, Phase: wfv1.NodeSucceeded, Message: "succeeded"}},
},
}
ctx := context.Background()
_, err := wfClient.Create(ctx, wf, metav1.CreateOptions{})
assert.NoError(t, err)
wf, err = RetryWorkflow(ctx, kubeClient, hydratorfake.Always, wfClient, wf.Name, false, "")
if assert.NoError(t, err) {
assert.Equal(t, wfv1.WorkflowRunning, wf.Status.Phase)
assert.Equal(t, metav1.Time{}, wf.Status.FinishedAt)
assert.True(t, wf.Status.StartedAt.After(createdTime.Time))
assert.NotContains(t, wf.Labels, common.LabelKeyCompleted)
assert.NotContains(t, wf.Labels, common.LabelKeyWorkflowArchivingStatus)
for _, node := range wf.Status.Nodes {
switch node.Phase {
case wfv1.NodeSucceeded:
assert.Equal(t, "succeeded", node.Message)
assert.Equal(t, wfv1.NodeSucceeded, node.Phase)
assert.Equal(t, createdTime, node.StartedAt)
assert.Equal(t, finishedTime, node.FinishedAt)
case wfv1.NodeFailed:
assert.Equal(t, "", node.Message)
assert.Equal(t, wfv1.NodeRunning, node.Phase)
assert.Equal(t, metav1.Time{}, node.FinishedAt)
assert.True(t, node.StartedAt.After(createdTime.Time))
}
}
}
}
func TestFromUnstructuredObj(t *testing.T) {
un := &unstructured.Unstructured{}
wfv1.MustUnmarshal([]byte(`apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: example-integers
spec:
schedule: "* * * * *"
workflowSpec:
entrypoint: whalesay
templates:
- name: whalesay
inputs:
parameters:
- name: age
value: 20
container:
image: my-image`), un)
x := &wfv1.CronWorkflow{}
err := FromUnstructuredObj(un, x)
assert.NoError(t, err)
}
func TestToUnstructured(t *testing.T) {
un, err := ToUnstructured(&wfv1.Workflow{})
if assert.NoError(t, err) {
gv := un.GetObjectKind().GroupVersionKind()
assert.Equal(t, workflow.WorkflowKind, gv.Kind)
assert.Equal(t, workflow.Group, gv.Group)
assert.Equal(t, workflow.Version, gv.Version)
}
}