forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelinefinally_test.go
803 lines (740 loc) · 27 KB
/
pipelinefinally_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
//go:build e2e
// +build e2e
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources"
"github.com/tektoncd/pipeline/test/diff"
"github.com/tektoncd/pipeline/test/parse"
jsonpatch "gomodules.xyz/jsonpatch/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
)
var requireAlphaFeatureFlags = requireAnyGate(map[string]string{
"enable-api-fields": "alpha",
})
func TestPipelineLevelFinally_OneDAGTaskFailed_InvalidTaskResult_Failure(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
task := getFailTask(t, namespace)
task.Spec.Results = append(task.Spec.Results, v1.TaskResult{
Name: "result",
})
if _, err := c.V1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
delayedTask := getDelaySuccessTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, delayedTask, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
successTask := getSuccessTask(t, namespace)
successTask.Spec.Results = append(successTask.Spec.Results, v1.TaskResult{
Name: "result",
})
if _, err := c.V1TaskClient.Create(ctx, successTask, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
finalTaskWithStatus := getTaskVerifyingStatus(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, finalTaskWithStatus, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task checking executing status: %s", err)
}
taskProducingResult := getSuccessTaskProducingResults(t, namespace)
taskProducingResult.Spec.Results = append(taskProducingResult.Spec.Results, v1.TaskResult{
Name: "result",
})
if _, err := c.V1TaskClient.Create(ctx, taskProducingResult, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task producing task results: %s", err)
}
taskConsumingResultInParam := getSuccessTaskConsumingResults(t, namespace, "dagtask-result")
if _, err := c.V1TaskClient.Create(ctx, taskConsumingResultInParam, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task consuming task results in param: %s", err)
}
taskConsumingResultInWhenExpression := getSuccessTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, taskConsumingResultInWhenExpression, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task consuming task results in when expressions: %s", err)
}
pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
finally:
- name: finaltask1
taskRef:
name: %s
- name: finaltask2
params:
- name: dagtask1-status
value: $(tasks.dagtask1.status)
- name: dagtask2-status
value: $(tasks.dagtask2.status)
- name: dagtask3-status
value: $(tasks.dagtask3.status)
- name: dagtasks-aggregate-status
value: $(tasks.status)
taskRef:
name: %s
- name: finaltaskconsumingdagtask1
params:
- name: dagtask-result
value: $(tasks.dagtask1.results.result)
taskRef:
name: %s
- name: finaltaskconsumingdagtask4
params:
- name: dagtask-result
value: $(tasks.dagtask4.results.result)
taskRef:
name: %s
- name: finaltaskconsumingdagtask5
params:
- name: dagtask-result
value: $(tasks.dagtask5.results.result)
taskRef:
name: %s
- name: guardedfinaltaskconsumingdagtask4
taskRef:
name: %s
when:
- input: $(tasks.dagtask4.results.result)
operator: in
values:
- aResult
- name: guardedfinaltaskusingdagtask5result1
taskRef:
name: %s
when:
- input: $(tasks.dagtask5.results.result)
operator: in
values:
- Hello
- name: guardedfinaltaskusingdagtask5result2
taskRef:
name: %s
when:
- input: $(tasks.dagtask5.results.result)
operator: notin
values:
- Hello
- name: guardedfinaltaskusingdagtask5status1
taskRef:
name: %s
when:
- input: $(tasks.dagtask5.status)
operator: in
values:
- Succeeded
- input: $(tasks.status)
operator: in
values:
- Failed
- name: guardedfinaltaskusingdagtask5status2
taskRef:
name: %s
when:
- input: $(tasks.dagtask5.status)
operator: in
values:
- Failed
- input: $(tasks.status)
operator: notin
values:
- Failed
tasks:
- name: dagtask1
taskRef:
name: %s
- name: dagtask2
taskRef:
name: %s
- name: dagtask3
taskRef:
name: %s
when:
- input: banana
operator: in
values:
- apple
- name: dagtask4
taskRef:
name: %s
when:
- input: foo
operator: notin
values:
- foo
- name: dagtask5
taskRef:
name: %s
`, helpers.ObjectNameForTest(t), namespace,
// Finally
successTask.Name, finalTaskWithStatus.Name, taskConsumingResultInParam.Name,
taskConsumingResultInParam.Name, taskConsumingResultInParam.Name, taskConsumingResultInWhenExpression.Name,
taskConsumingResultInWhenExpression.Name, taskConsumingResultInWhenExpression.Name, taskConsumingResultInWhenExpression.Name,
taskConsumingResultInWhenExpression.Name,
// Tasks
task.Name, delayedTask.Name, successTask.Name, successTask.Name, taskProducingResult.Name))
if _, err := c.V1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline: %s", err)
}
pipelineRun := getPipelineRun(t, namespace, pipeline.Name)
if _, err := c.V1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Run `%s`: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, PipelineRunFailed(pipelineRun.Name), "PipelineRunFailed", v1Version); err != nil {
t.Fatalf("Waiting for PipelineRun %s to fail: %v", pipelineRun.Name, err)
}
// Get the status of the PipelineRun.
pr, err := c.V1PipelineRunClient.Get(ctx, pipelineRun.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get PipelineRun %q: %v", pipelineRun.Name, err)
}
taskrunList, err := c.V1TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}
// expecting taskRuns for dagtask1, dagtask2, dagtask5, finaltask1, finaltask2,
// finaltaskconsumingdagtask5, guardedfinaltaskusingdagtask5result1, guardedfinaltaskusingdagtask5status1
expectedTaskRunsCount := 8
if len(taskrunList.Items) != expectedTaskRunsCount {
var s []string
for _, n := range taskrunList.Items {
s = append(s, n.Labels["tekton.dev/pipelineTask"])
}
t.Fatalf("Error retrieving TaskRuns for PipelineRun %s. Expected %d taskRuns and found %d taskRuns for: %s",
pipelineRun.Name, expectedTaskRunsCount, len(taskrunList.Items), strings.Join(s, ", "))
}
var dagTask1EndTime, dagTask2EndTime, finalTaskStartTime *metav1.Time
// verify dag task failed, parallel dag task succeeded, and final task succeeded
for _, taskrunItem := range taskrunList.Items {
switch n := taskrunItem.Labels["tekton.dev/pipelineTask"]; {
case n == "dagtask1":
if !isFailed(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("dag task %s should have failed", n)
}
dagTask1EndTime = taskrunItem.Status.CompletionTime
case n == "dagtask2":
if err := WaitForTaskRunState(ctx, c, taskrunItem.Name, TaskRunSucceed(taskrunItem.Name), "TaskRunSuccess", v1Version); err != nil {
t.Errorf("Error waiting for TaskRun to succeed: %v", err)
}
dagTask2EndTime = taskrunItem.Status.CompletionTime
case n == "dagtask4":
t.Fatalf("task %s should have skipped due to when expression", n)
case n == "dagtask5":
if err := WaitForTaskRunState(ctx, c, taskrunItem.Name, TaskRunSucceed(taskrunItem.Name), "TaskRunSuccess", v1Version); err != nil {
t.Errorf("Error waiting for TaskRun to succeed: %v", err)
}
case n == "finaltask1":
if err := WaitForTaskRunState(ctx, c, taskrunItem.Name, TaskRunSucceed(taskrunItem.Name), "TaskRunSuccess", v1Version); err != nil {
t.Errorf("Error waiting for TaskRun to succeed: %v", err)
}
finalTaskStartTime = taskrunItem.Status.StartTime
case n == "finaltask2":
if err := WaitForTaskRunState(ctx, c, taskrunItem.Name, TaskRunSucceed(taskrunItem.Name), "TaskRunSuccess", v1Version); err != nil {
t.Errorf("Error waiting for TaskRun to succeed: %v", err)
}
for _, p := range taskrunItem.Spec.Params {
switch param := p.Name; param {
case "dagtask1-status":
if p.Value.StringVal != v1.TaskRunReasonFailed.String() {
t.Errorf("Task param \"%s\" is set to \"%s\", expected it to resolve to \"%s\"", param, p.Value.StringVal, v1.TaskRunReasonFailed.String())
}
case "dagtask2-status":
if p.Value.StringVal != v1.TaskRunReasonSuccessful.String() {
t.Errorf("Task param \"%s\" is set to \"%s\", expected it to resolve to \"%s\"", param, p.Value.StringVal, v1.TaskRunReasonSuccessful.String())
}
case "dagtask3-status":
if p.Value.StringVal != resources.PipelineTaskStateNone {
t.Errorf("Task param \"%s\" is set to \"%s\", expected it to resolve to \"%s\"", param, p.Value.StringVal, resources.PipelineTaskStateNone)
}
case "dagtasks-aggregate-status":
if p.Value.StringVal != v1.PipelineRunReasonFailed.String() {
t.Errorf("Task param \"%s\" is set to \"%s\", expected it to resolve to \"%s\"", param, p.Value.StringVal, v1.PipelineRunReasonFailed.String())
}
}
}
case n == "finaltaskconsumingdagtask5":
if err := WaitForTaskRunState(ctx, c, taskrunItem.Name, TaskRunSucceed(taskrunItem.Name), "TaskRunSuccess", v1Version); err != nil {
t.Errorf("Error waiting for TaskRun to succeed: %v", err)
}
for _, p := range taskrunItem.Spec.Params {
if p.Name == "dagtask-result" && p.Value.StringVal != "Hello" {
t.Errorf("Error resolving task result reference in a finally task %s", n)
}
}
case n == "guardedfinaltaskusingdagtask5result1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("final task %s should have succeeded", n)
}
case n == "guardedfinaltaskusingdagtask5status1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("final task %s should have succeeded", n)
}
case n == "guardedfinaltaskusingdagtask5result2":
t.Fatalf("final task %s should have skipped due to when expression evaluating to false", n)
case n == "finaltaskconsumingdagtask1" || n == "finaltaskconsumingdagtask4" || n == "guardedfinaltaskconsumingdagtask4":
t.Fatalf("final task %s should have skipped due to missing task result reference", n)
default:
t.Fatalf("Found unexpected taskRun %s", n)
}
}
// final task should start executing after dagtask1 fails and dagtask2 is done
if finalTaskStartTime.Before(dagTask1EndTime) || finalTaskStartTime.Before(dagTask2EndTime) {
t.Fatalf("Final Tasks should start getting executed after all DAG tasks finishes")
}
// two final tasks referring to results must be skipped
// finaltaskconsumingdagtask1 has a reference to a task result from failed task
// finaltaskconsumingdagtask4 has a reference to a task result from skipped task with when expression
expectedSkippedTasks := []v1.SkippedTask{{
Name: "dagtask3",
Reason: v1.StoppingSkip,
WhenExpressions: v1.WhenExpressions{{
Input: "banana",
Operator: "in",
Values: []string{"apple"},
}},
}, {
Name: "dagtask4",
Reason: v1.StoppingSkip,
WhenExpressions: v1.WhenExpressions{{
Input: "foo",
Operator: "notin",
Values: []string{"foo"},
}},
}, {
Name: "finaltaskconsumingdagtask1",
Reason: v1.MissingResultsSkip,
}, {
Name: "finaltaskconsumingdagtask4",
Reason: v1.MissingResultsSkip,
}, {
Name: "guardedfinaltaskconsumingdagtask4",
Reason: v1.MissingResultsSkip,
}, {
Name: "guardedfinaltaskusingdagtask5result2",
Reason: v1.WhenExpressionsSkip,
WhenExpressions: v1.WhenExpressions{{
Input: "Hello",
Operator: "notin",
Values: []string{"Hello"},
}},
}, {
Name: "guardedfinaltaskusingdagtask5status2",
Reason: v1.WhenExpressionsSkip,
WhenExpressions: v1.WhenExpressions{{
Input: "Succeeded",
Operator: "in",
Values: []string{"Failed"},
}, {
Input: "Failed",
Operator: "notin",
Values: []string{"Failed"},
}},
}}
actualSkippedTasks := pr.Status.SkippedTasks
// Sort tasks based on their names to get similar order as in expected list
if d := cmp.Diff(expectedSkippedTasks, actualSkippedTasks, cmpopts.SortSlices(func(i, j v1.SkippedTask) bool {
return i.Name < j.Name
})); d != "" {
t.Fatalf("Expected four skipped tasks, dag task with condition failure dagtask3, dag task with when expression,"+
"two final tasks with missing result reference finaltaskconsumingdagtask1 and finaltaskconsumingdagtask4 in SkippedTasks."+
" Diff: %s", diff.PrintWantGot(d))
}
}
func TestPipelineLevelFinally_OneFinalTaskFailed_Failure(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
task := getSuccessTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
finalTask := getFailTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, finalTask, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
finally:
- name: finaltask1
taskRef:
name: %s
tasks:
- name: dagtask1
taskRef:
name: %s
`, helpers.ObjectNameForTest(t), namespace, finalTask.Name, task.Name))
if _, err := c.V1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline: %s", err)
}
pipelineRun := getPipelineRun(t, namespace, pipeline.Name)
if _, err := c.V1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Run `%s`: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, PipelineRunFailed(pipelineRun.Name), "PipelineRunFailed", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err)
t.Fatalf("PipelineRun execution failed")
}
taskrunList, err := c.V1TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}
// verify dag task succeeded and final task failed
for _, taskrunItem := range taskrunList.Items {
switch n := taskrunItem.Labels["tekton.dev/pipelineTask"]; {
case n == "dagtask1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("dag task %s should have succeeded", n)
}
case n == "finaltask1":
if !isFailed(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("final task %s should have failed", n)
}
default:
t.Fatalf("TaskRuns were not found for both final and dag tasks")
}
}
}
func TestPipelineLevelFinally_OneFinalTask_CancelledRunFinally(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t, requireAlphaFeatureFlags)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
task1 := getDelaySuccessTaskProducingResults(t, namespace)
task1.Spec.Results = append(task1.Spec.Results, v1.TaskResult{
Name: "result",
})
if _, err := c.V1TaskClient.Create(ctx, task1, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
task2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result")
if _, err := c.V1TaskClient.Create(ctx, task2, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
finalTask1 := getSuccessTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, finalTask1, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
finalTask2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result")
if _, err := c.V1TaskClient.Create(ctx, finalTask2, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
finally:
- name: finaltask1
taskRef:
name: %s
- name: finaltask2
params:
- name: dagtask1-result
value: $(tasks.dagtask1.results.result)
taskRef:
name: %s
tasks:
- name: dagtask1
taskRef:
name: %s
- name: dagtask2
params:
- name: dagtask1-result
value: $(tasks.dagtask1.results.result)
taskRef:
name: %s
`, helpers.ObjectNameForTest(t), namespace, finalTask1.Name, finalTask2.Name, task1.Name, task2.Name))
if _, err := c.V1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline: %s", err)
}
pipelineRun := getPipelineRun(t, namespace, pipeline.Name)
if _, err := c.V1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Run `%s`: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, Running(pipelineRun.Name), "PipelineRunRunning", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun %s to start: %s", pipelineRun.Name, err)
t.Fatalf("PipelineRun execution failed")
}
patches := []jsonpatch.JsonPatchOperation{{
Operation: "add",
Path: "/spec/status",
Value: v1.PipelineRunSpecStatusCancelledRunFinally,
}}
patchBytes, err := json.Marshal(patches)
if err != nil {
t.Fatalf("failed to marshal patch bytes in order to stop")
}
if _, err := c.V1PipelineRunClient.Patch(ctx, pipelineRun.Name, types.JSONPatchType, patchBytes, metav1.PatchOptions{}, ""); err != nil {
t.Fatalf("Failed to patch PipelineRun `%s` with graceful stop: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, FailedWithReason(v1.PipelineRunReasonCancelled.String(), pipelineRun.Name), "PipelineRunCancelled", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err)
t.Fatalf("PipelineRun execution failed")
}
taskrunList, err := c.V1TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}
// verify dag task succeeded and final task failed
for _, taskrunItem := range taskrunList.Items {
switch n := taskrunItem.Labels["tekton.dev/pipelineTask"]; n {
case "dagtask1":
if !isCancelled(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("dag task %s should have been cancelled", n)
}
case "dagtask2":
t.Fatalf("second dag task %s should be skipped as it depends on the result from cancelled 'dagtask1'", n)
case "finaltask1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("first final task %s should have succeeded", n)
}
case "finaltask2":
t.Fatalf("second final task %s should be skipped as it depends on the result from cancelled 'dagtask1'", n)
default:
t.Fatalf("TaskRuns were not found for both final and dag tasks")
}
}
}
func TestPipelineLevelFinally_OneFinalTask_StoppedRunFinally(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t, requireAlphaFeatureFlags)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
task1 := getDelaySuccessTaskProducingResults(t, namespace)
task1.Spec.Results = append(task1.Spec.Results, v1.TaskResult{
Name: "result",
})
if _, err := c.V1TaskClient.Create(ctx, task1, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
task2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result")
if _, err := c.V1TaskClient.Create(ctx, task2, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag Task: %s", err)
}
finalTask1 := getSuccessTask(t, namespace)
if _, err := c.V1TaskClient.Create(ctx, finalTask1, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
finalTask2 := getSuccessTaskConsumingResults(t, namespace, "dagtask1-result")
if _, err := c.V1TaskClient.Create(ctx, finalTask2, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create final Task: %s", err)
}
pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
finally:
- name: finaltask1
taskRef:
name: %s
- name: finaltask2
params:
- name: dagtask1-result
value: $(tasks.dagtask1.results.result)
taskRef:
name: %s
tasks:
- name: dagtask1
taskRef:
name: %s
- name: dagtask2
params:
- name: dagtask1-result
value: $(tasks.dagtask1.results.result)
taskRef:
name: %s
`, helpers.ObjectNameForTest(t), namespace, finalTask1.Name, finalTask2.Name, task1.Name, task2.Name))
if _, err := c.V1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline: %s", err)
}
pipelineRun := getPipelineRun(t, namespace, pipeline.Name)
if _, err := c.V1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline Run `%s`: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, Running(pipelineRun.Name), "PipelineRunRunning", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun %s to start: %s", pipelineRun.Name, err)
t.Fatalf("PipelineRun execution failed")
}
patches := []jsonpatch.JsonPatchOperation{{
Operation: "add",
Path: "/spec/status",
Value: v1.PipelineRunSpecStatusStoppedRunFinally,
}}
patchBytes, err := json.Marshal(patches)
if err != nil {
t.Fatalf("failed to marshal patch bytes in order to stop")
}
if _, err := c.V1PipelineRunClient.Patch(ctx, pipelineRun.Name, types.JSONPatchType, patchBytes, metav1.PatchOptions{}, ""); err != nil {
t.Fatalf("Failed to patch PipelineRun `%s` with graceful stop: %s", pipelineRun.Name, err)
}
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, FailedWithReason(v1.PipelineRunReasonCancelled.String(), pipelineRun.Name), "PipelineRunCancelled", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err)
t.Fatalf("PipelineRun execution failed")
}
taskrunList, err := c.V1TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name})
if err != nil {
t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err)
}
// verify dag task succeeded and final task failed
for _, taskrunItem := range taskrunList.Items {
switch n := taskrunItem.Labels["tekton.dev/pipelineTask"]; n {
case "dagtask1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("dag task %s should have succeeded", n)
}
case "finaltask1":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("first final task %s should have succeeded", n)
}
case "finaltask2":
if !isSuccessful(t, n, taskrunItem.Status.Conditions) {
t.Fatalf("second final task %s should have succeeded", n)
}
default:
t.Fatalf("TaskRuns were not found for both final and dag tasks")
}
}
}
func getSuccessTask(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'exit 0'
`, helpers.ObjectNameForTest(t), namespace))
}
func getFailTask(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'exit 1'
`, helpers.ObjectNameForTest(t), namespace))
}
func getDelaySuccessTask(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'sleep 5; exit 0'
`, helpers.ObjectNameForTest(t), namespace))
}
func getTaskVerifyingStatus(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'exit 0'
params:
- name: dagtask1-status
- name: dagtask2-status
- name: dagtask3-status
- name: dagtasks-aggregate-status
`, helpers.ObjectNameForTest(t), namespace))
}
func getSuccessTaskProducingResults(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'echo -n "Hello" > $(results.result.path)'
results:
- name: result
`, helpers.ObjectNameForTest(t), namespace))
}
func getDelaySuccessTaskProducingResults(t *testing.T, namespace string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'sleep 5; echo -n "Hello" > $(results.result.path)'
results:
- name: result
`, helpers.ObjectNameForTest(t), namespace))
}
func getSuccessTaskConsumingResults(t *testing.T, namespace string, paramName string) *v1.Task {
t.Helper()
return parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- image: alpine
script: 'exit 0'
params:
- name: %s
`, helpers.ObjectNameForTest(t), namespace, paramName))
}
func getPipelineRun(t *testing.T, namespace, p string) *v1.PipelineRun {
t.Helper()
return parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
pipelineRef:
name: %s
`, helpers.ObjectNameForTest(t), namespace, p))
}