From b85e4efed0a959374fecca67a10ffc5151ad9ffa Mon Sep 17 00:00:00 2001 From: Emma Munley <46881325+EmmaMunley@users.noreply.github.com> Date: Mon, 5 Jun 2023 11:46:00 -0400 Subject: [PATCH] Add Unit Tests for TestMissingResultWhenStepErrorIsIgnored This commit adds test coverage for a pipelinerun that has 2 tasks where the first task produces 2 results that are consumed in the 2nd task. However, the first task contains a failing step so only 1 of the 2 results are produced which results in a failing pipeline run. This is currently an Integration Test, but found missing code coverage in the unit tests. --- .../pipelinerun/pipelinerun_test.go | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go index edbf15bf241..444ef14e60a 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go @@ -1044,6 +1044,188 @@ spec: } } +func TestMissingResultWhenStepErrorIsIgnored(t *testing.T) { + prs := []*v1beta1.PipelineRun{parse.MustParseV1beta1PipelineRun(t, ` +metadata: + name: test-pipeline-missing-results + namespace: foo +spec: + serviceAccountName: test-sa-0 + pipelineSpec: + tasks: + - name: task1 + taskSpec: + results: + - name: result1 + type: string + - name: result2 + type: string + steps: + - name: failing-step + onError: continue + image: busybox + script: 'echo -n 123 | tee $(results.result1.path); exit 1; echo -n 456 | tee $(results.result2.path)' + - name: task2 + runAfter: [ task1 ] + params: + - name: param1 + value: $(tasks.task1.results.result1) + - name: param2 + value: $(tasks.task1.results.result2) + taskSpec: + params: + - name: param1 + type: string + - name: param2 + type: string + steps: + - name: foo + image: busybox + script: 'exit 0' + conditions: + - type: Succeeded + status: "False" + reason: "InvalidTaskResultReference" + message: "Could not find result with name result2 for task task1" + childReferences: + - apiVersion: tekton.dev/v1beta1 + kind: TaskRun + name: pr-test-pipeline-missing-results-0 + pipelineTaskName: test-pipeline-missing-results-task1`)} + + trs := []*v1beta1.TaskRun{mustParseTaskRunWithObjectMeta(t, + taskRunObjectMeta("test-pipeline-missing-results-task1", "foo", + "test-pipeline-missing-results", "test-pipeline", "task1", true), + ` +spec: + serviceAccountName: test-sa + timeout: 1h0m0s +status: + conditions: + - status: "True" + type: Succeeded + taskResults: + - name: result1 + value: 123 +`)} + + expectedPipelineRun := + parse.MustParseV1beta1PipelineRun(t, ` +metadata: + name: test-pipeline-missing-results + namespace: foo + annotations: {} + labels: + tekton.dev/pipeline: test-pipeline-missing-results +spec: + serviceAccountName: test-sa-0 + pipelineSpec: + tasks: + - name: task1 + taskSpec: + results: + - name: result1 + type: string + - name: result2 + type: string + steps: + - name: failing-step + onError: continue + image: busybox + script: 'echo -n 123 | tee $(results.result1.path); exit 1; echo -n 456 | tee $(results.result2.path)' + - name: task2 + runAfter: [ task1 ] + params: + - name: param1 + value: $(tasks.task1.results.result1) + - name: param2 + value: $(tasks.task1.results.result2) + taskSpec: + params: + - name: param1 + type: string + - name: param2 + type: string + steps: + - name: foo + image: busybox + script: 'exit 0' +status: + pipelineSpec: + tasks: + - name: task1 + taskSpec: + results: + - name: result1 + type: string + - name: result2 + type: string + steps: + - name: failing-step + onError: continue + image: busybox + script: 'echo -n 123 | tee $(results.result1.path); exit 1; echo -n 456 | tee $(results.result2.path)' + - name: task2 + runAfter: [ task1 ] + params: + - name: param1 + value: $(tasks.task1.results.result1) + - name: param2 + value: $(tasks.task1.results.result2) + taskSpec: + params: + - name: param1 + type: string + - name: param2 + type: string + steps: + - name: foo + image: busybox + script: 'exit 0' + conditions: + - message: "Could not find result with name result2 for task task1" + reason: InvalidTaskResultReference + status: "False" + type: Succeeded + childReferences: + - apiVersion: tekton.dev/v1beta1 + kind: TaskRun + name: test-pipeline-missing-results-task1 + pipelineTaskName: task1 + provenance: + featureFlags: + RunningInEnvWithInjectedSidecars: true + EnableAPIFields: "stable" + AwaitSidecarReadiness: true + VerificationNoMatchPolicy: "ignore" + EnableProvenanceInStatus: true + ResultExtractionMethod: "termination-message" + MaxResultSize: 4096 +`) + d := test.Data{ + PipelineRuns: prs, + TaskRuns: trs, + } + prt := newPipelineRunTest(t, d) + defer prt.Cancel() + + reconciledRun, clients := prt.reconcileRun("foo", "test-pipeline-missing-results", []string{}, true) + if reconciledRun.Status.CompletionTime == nil { + t.Errorf("Expected a CompletionTime on invalid PipelineRun but was nil") + } + + // The PipelineRun should be marked as failed due to InvalidTaskResultReference. + if d := cmp.Diff(reconciledRun, expectedPipelineRun, ignoreResourceVersion, ignoreLastTransitionTime, ignoreTypeMeta, ignoreStartTime, ignoreCompletionTime); d != "" { + t.Errorf("Expected to see PipelineRun run marked as failed with the reason: InvalidTaskResultReference. Diff %s", diff.PrintWantGot(d)) + } + + // Check that the expected TaskRun was created + taskRuns := getTaskRunsForPipelineRun(prt.TestAssets.Ctx, t, clients, "foo", "test-pipeline-missing-results") + + // We expect only 1 TaskRun to be created, since the PipelineRun should fail before creating the 2nd TaskRun due to the InvalidTaskResultReference + validateTaskRunsCount(t, taskRuns, 1) +} + func TestReconcile_InvalidPipelineRunNames(t *testing.T) { // TestReconcile_InvalidPipelineRunNames runs "Reconcile" on several PipelineRuns that have invalid names. // It verifies that reconcile fails, how it fails and which events are triggered.