|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2023 The Tekton Authors |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package test |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" |
| 28 | + "github.com/tektoncd/pipeline/test/parse" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + knativetest "knative.dev/pkg/test" |
| 31 | + "knative.dev/pkg/test/helpers" |
| 32 | +) |
| 33 | + |
| 34 | +func TestTaskResultsFromFailedTasks(t *testing.T) { |
| 35 | + ctx := context.Background() |
| 36 | + ctx, cancel := context.WithCancel(ctx) |
| 37 | + defer cancel() |
| 38 | + c, namespace := setup(ctx, t) |
| 39 | + knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf) |
| 40 | + defer tearDown(ctx, t, c, namespace) |
| 41 | + |
| 42 | + pipelineRun := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(` |
| 43 | +metadata: |
| 44 | + name: %s |
| 45 | +spec: |
| 46 | + pipelineSpec: |
| 47 | + tasks: |
| 48 | + - name: task1 |
| 49 | + taskSpec: |
| 50 | + results: |
| 51 | + - name: result1 |
| 52 | + - name: result2 |
| 53 | + steps: |
| 54 | + - name: failing-step |
| 55 | + image: busybox |
| 56 | + script: 'echo -n 123 | tee $(results.result1.path); exit 1; echo -n 456 | tee $(results.result2.path)' |
| 57 | + finally: |
| 58 | + - name: finaltask1 |
| 59 | + params: |
| 60 | + - name: param1 |
| 61 | + value: $(tasks.task1.results.result1) |
| 62 | + taskSpec: |
| 63 | + params: |
| 64 | + - name: param1 |
| 65 | + steps: |
| 66 | + - image: busybox |
| 67 | + script: 'exit 0' |
| 68 | + - name: finaltask2 |
| 69 | + params: |
| 70 | + - name: param1 |
| 71 | + value: $(tasks.task1.results.result2) |
| 72 | + taskSpec: |
| 73 | + params: |
| 74 | + - name: param1 |
| 75 | + steps: |
| 76 | + - image: busybox |
| 77 | + script: exit 0`, helpers.ObjectNameForTest(t))) |
| 78 | + |
| 79 | + if _, err := c.V1beta1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil { |
| 80 | + t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRun.Name, err) |
| 81 | + } |
| 82 | + |
| 83 | + t.Logf("Waiting for PipelineRun in namespace %s to fail", namespace) |
| 84 | + if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, FailedWithReason(v1beta1.PipelineRunReasonFailed.String(), pipelineRun.Name), "InvalidTaskResultReference", v1beta1Version); err != nil { |
| 85 | + t.Errorf("Error waiting for PipelineRun to fail: %s", err) |
| 86 | + } |
| 87 | + |
| 88 | + taskrunList, err := c.V1beta1TaskRunClient.List(ctx, metav1.ListOptions{LabelSelector: "tekton.dev/pipelineRun=" + pipelineRun.Name}) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("Error listing TaskRuns for PipelineRun %s: %s", pipelineRun.Name, err) |
| 91 | + } |
| 92 | + |
| 93 | + if len(taskrunList.Items) != 2 { |
| 94 | + t.Fatalf("The pipelineRun \"%s\" should have exactly 2 taskRuns, one for the task \"task1\""+ |
| 95 | + "and one more for the final task \"finaltask1\" instead it has \"%d\" taskRuns", pipelineRun.Name, len(taskrunList.Items)) |
| 96 | + } |
| 97 | + |
| 98 | + for _, taskrunItem := range taskrunList.Items { |
| 99 | + switch n := taskrunItem.Labels["tekton.dev/pipelineTask"]; n { |
| 100 | + case "task1": |
| 101 | + if !isFailed(t, "", taskrunItem.Status.Conditions) { |
| 102 | + t.Fatalf("task1 should have been a failure") |
| 103 | + } |
| 104 | + if len(taskrunItem.Status.TaskRunResults) != 1 { |
| 105 | + t.Fatalf("task1 should have produced a result even with the failing step") |
| 106 | + } |
| 107 | + for _, r := range taskrunItem.Status.TaskRunResults { |
| 108 | + if r.Name == "result1" && r.Value.StringVal != "123" { |
| 109 | + t.Fatalf("task1 should have initialized a result \"result1\" to \"123\"") |
| 110 | + } |
| 111 | + } |
| 112 | + case "finaltask1": |
| 113 | + if !isSuccessful(t, "", taskrunItem.Status.Conditions) { |
| 114 | + t.Fatalf("finaltask1 should have been successful") |
| 115 | + } |
| 116 | + default: |
| 117 | + t.Fatalf("TaskRuns were not found for both final and dag tasks") |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments