From c5cbc7cd1cc41f286ba85de5fd5d41c8cd441126 Mon Sep 17 00:00:00 2001 From: jbpratt Date: Thu, 17 Mar 2022 05:08:44 -0500 Subject: [PATCH] refactor create helpers in reconcile pr tests Refactor the pipelineRun and taskRun create helpers to parse yaml. Requires passing `*testing.T` through all calls. continued work on #4610 Signed-off-by: jbpratt --- .../pipelinerun/pipelinerun_test.go | 109 ++++++++++++------ 1 file changed, 71 insertions(+), 38 deletions(-) diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go index 84490326595..52948d82e08 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go @@ -1550,7 +1550,7 @@ func TestReconcileOnCompletedPipelineRun(t *testing.T) { }} ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline} ts := []*v1beta1.Task{simpleHelloWorldTask} - trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(taskRunName, "foo", + trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(t, taskRunName, "foo", "test-pipeline-run-completed", "test-pipeline", "", apis.Condition{ Type: apis.ConditionSucceeded, @@ -1618,10 +1618,12 @@ func TestReconcileOnCancelledPipelineRunDeprecated(t *testing.T) { // TestReconcileOnCancelledPipelineRunDeprecated runs "Reconcile" on a PipelineRun that has been cancelled. // It verifies that reconcile is successful, the pipeline status updated and events generated. // This test uses the deprecated status "PipelineRunCancelled" in PipelineRunSpec. - prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelledDeprecated)} + prs := []*v1beta1.PipelineRun{ + createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelledDeprecated), + } ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline} ts := []*v1beta1.Task{simpleHelloWorldTask} - trs := []*v1beta1.TaskRun{createHelloWorldTaskRun("test-pipeline-run-cancelled-hello-world", "foo", + trs := []*v1beta1.TaskRun{createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-hello-world", "foo", "test-pipeline-run-cancelled", "test-pipeline")} d := test.Data{ @@ -1663,10 +1665,12 @@ func getConfigMapsWithEnabledAlphaAPIFields() []*corev1.ConfigMap { func TestReconcileOnCancelledPipelineRun(t *testing.T) { // TestReconcileOnCancelledPipelineRun runs "Reconcile" on a PipelineRun that has been cancelled. // It verifies that reconcile is successful, the pipeline status updated and events generated. - prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelled)} + prs := []*v1beta1.PipelineRun{ + createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelled), + } ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline} ts := []*v1beta1.Task{simpleHelloWorldTask} - trs := []*v1beta1.TaskRun{createHelloWorldTaskRun("test-pipeline-run-cancelled-hello-world", "foo", + trs := []*v1beta1.TaskRun{createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-hello-world", "foo", "test-pipeline-run-cancelled", "test-pipeline")} cms := getConfigMapsWithEnabledAlphaAPIFields() @@ -1932,7 +1936,13 @@ func TestReconcileForCustomTaskWithPipelineRunTimedOut(t *testing.T) { func TestReconcileOnCancelledRunFinallyPipelineRun(t *testing.T) { // TestReconcileOnCancelledRunFinallyPipelineRun runs "Reconcile" on a PipelineRun that has been gracefully cancelled. // It verifies that reconcile is successful, the pipeline status updated and events generated. - prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled-run-finally", v1beta1.PipelineRunSpecStatusCancelledRunFinally)} + prs := []*v1beta1.PipelineRun{ + createCancelledPipelineRun( + t, + "test-pipeline-run-cancelled-run-finally", + v1beta1.PipelineRunSpecStatusCancelledRunFinally, + ), + } ps := []*v1beta1.Pipeline{ parse.MustParsePipeline(t, ` metadata: @@ -1996,7 +2006,13 @@ spec: func TestReconcileOnCancelledRunFinallyPipelineRunWithFinalTask(t *testing.T) { // TestReconcileOnCancelledRunFinallyPipelineRunWithFinalTask runs "Reconcile" on a PipelineRun that has been gracefully cancelled. // It verifies that reconcile is successful, final tasks run, the pipeline status updated and events generated. - prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled-run-finally", v1beta1.PipelineRunSpecStatusCancelledRunFinally)} + prs := []*v1beta1.PipelineRun{ + createCancelledPipelineRun( + t, + "test-pipeline-run-cancelled-run-finally", + v1beta1.PipelineRunSpecStatusCancelledRunFinally, + ), + } ps := []*v1beta1.Pipeline{ parse.MustParsePipeline(t, ` metadata: @@ -2118,13 +2134,13 @@ func TestReconcileOnCancelledRunFinallyPipelineRunWithRunningFinalTask(t *testin simpleSomeTask, } trs := []*v1beta1.TaskRun{ - createHelloWorldTaskRunWithStatus("test-pipeline-run-cancelled-run-finally-hello-world", "foo", + createHelloWorldTaskRunWithStatus(t, "test-pipeline-run-cancelled-run-finally-hello-world", "foo", "test-pipeline-run-cancelled-run-finally", "test-pipeline", "my-pod-name", apis.Condition{ Type: apis.ConditionSucceeded, Status: corev1.ConditionTrue, }), - createHelloWorldTaskRun("test-pipeline-run-cancelled-run-finally-final-task", "foo", + createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-run-finally-final-task", "foo", "test-pipeline-run-cancelled-run-finally", "test-pipeline"), } cms := getConfigMapsWithEnabledAlphaAPIFields() @@ -2224,7 +2240,7 @@ func TestReconcileOnCancelledRunFinallyPipelineRunWithFinalTaskAndRetries(t *tes }} // TaskRun exists for DAG task "hello-world-1" that has failed with reason of cancellation - trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus("test-pipeline-run-cancelled-run-finally-hello-world", "foo", + trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(t, "test-pipeline-run-cancelled-run-finally-hello-world", "foo", "test-pipeline-run-cancelled-run-finally", "test-pipeline", "my-pod-name", apis.Condition{ Type: apis.ConditionSucceeded, @@ -2305,6 +2321,7 @@ func TestReconcileCancelledRunFinallyFailsTaskRunCancellation(t *testing.T) { } trs := []*v1beta1.TaskRun{ getTaskRun( + t, "test-pipeline-fails-to-cancelhello-world-1", prName, "test-pipeline", @@ -2412,6 +2429,7 @@ func TestReconcileTaskResolutionError(t *testing.T) { }} trs := []*v1beta1.TaskRun{ getTaskRun( + t, "test-pipeline-fails-task-resolutionhello-world-1", prName, "test-pipeline", @@ -2555,6 +2573,7 @@ func TestReconcileOnStoppedRunFinallyPipelineRunWithRunningTask(t *testing.T) { ts := []*v1beta1.Task{simpleHelloWorldTask} trs := []*v1beta1.TaskRun{ getTaskRun( + t, "test-pipeline-run-stopped-run-finally-hello-world", "test-pipeline-run-stopped-run-finally", "test-pipeline", @@ -2650,6 +2669,7 @@ func TestReconcileOnStoppedPipelineRunWithCompletedTask(t *testing.T) { ts := []*v1beta1.Task{simpleHelloWorldTask} trs := []*v1beta1.TaskRun{ getTaskRun( + t, "test-pipeline-run-stopped-hello-world", "test-pipeline-run-stopped", "test-pipeline", @@ -6952,6 +6972,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { trs: []*v1beta1.TaskRun{ getTaskRun( + t, "task-run-dag-task", "pipeline-run-dag-task-failing", "pipeline-dag-task-failing", @@ -6959,6 +6980,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { corev1.ConditionFalse, ), getTaskRun( + t, "task-run-final-task", "pipeline-run-dag-task-failing", "pipeline-dag-task-failing", @@ -7016,6 +7038,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { trs: []*v1beta1.TaskRun{ getTaskRun( + t, "task-run-dag-task", "pipeline-run-with-dag-successful-but-final-failing", "pipeline-with-dag-successful-but-final-failing", @@ -7023,6 +7046,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { "", ), getTaskRun( + t, "task-run-final-task", "pipeline-run-with-dag-successful-but-final-failing", "pipeline-with-dag-successful-but-final-failing", @@ -7080,6 +7104,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { trs: []*v1beta1.TaskRun{ getTaskRun( + t, "task-run-dag-task", "pipeline-run-with-dag-and-final-failing", "pipeline-with-dag-and-final-failing", @@ -7087,6 +7112,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { corev1.ConditionFalse, ), getTaskRun( + t, "task-run-final-task", "pipeline-run-with-dag-and-final-failing", "pipeline-with-dag-and-final-failing", @@ -7151,6 +7177,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { trs: []*v1beta1.TaskRun{ getTaskRun( + t, "task-run-dag-task-1", "pipeline-run-with-dag-running", "pipeline-with-dag-running", @@ -7158,6 +7185,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { corev1.ConditionFalse, ), getTaskRun( + t, "task-run-dag-task-2", "pipeline-run-with-dag-running", "pipeline-with-dag-running", @@ -7216,6 +7244,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) { trs: []*v1beta1.TaskRun{ getTaskRun( + t, "task-run-dag-task-1", "pipeline-run-dag-task-running", "pipeline-dag-task-running", @@ -7328,8 +7357,8 @@ func getPipeline(p string, spec v1beta1.PipelineSpec) []*v1beta1.Pipeline { return ps } -func getTaskRun(tr, pr, p, t string, status corev1.ConditionStatus) *v1beta1.TaskRun { - return createHelloWorldTaskRunWithStatusTaskLabel(tr, "foo", pr, p, "", t, +func getTaskRun(t *testing.T, tr, pr, p, tl string, status corev1.ConditionStatus) *v1beta1.TaskRun { + return createHelloWorldTaskRunWithStatusTaskLabel(t, tr, "foo", pr, p, "", tl, apis.Condition{ Type: apis.ConditionSucceeded, Status: status, @@ -8424,8 +8453,12 @@ func taskRunObjectMeta(trName, ns, prName, pipelineName, pipelineTaskName string return om } -func createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName string, condition apis.Condition) *v1beta1.TaskRun { - p := createHelloWorldTaskRun(trName, ns, prName, pName) +func createHelloWorldTaskRunWithStatus( + t *testing.T, + trName, ns, prName, pName, podName string, + condition apis.Condition, +) *v1beta1.TaskRun { + p := createHelloWorldTaskRun(t, trName, ns, prName, pName) p.Status = v1beta1.TaskRunStatus{ Status: duckv1beta1.Status{ Conditions: duckv1beta1.Conditions{condition}, @@ -8437,37 +8470,37 @@ func createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName string return p } -func createHelloWorldTaskRunWithStatusTaskLabel(trName, ns, prName, pName, podName, taskLabel string, condition apis.Condition) *v1beta1.TaskRun { - p := createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName, condition) +func createHelloWorldTaskRunWithStatusTaskLabel( + t *testing.T, + trName, ns, prName, pName, podName, taskLabel string, + condition apis.Condition, +) *v1beta1.TaskRun { + p := createHelloWorldTaskRunWithStatus(t, trName, ns, prName, pName, podName, condition) p.Labels[pipeline.PipelineTaskLabelKey] = taskLabel return p } -func createHelloWorldTaskRun(trName, ns, prName, pName string) *v1beta1.TaskRun { - return &v1beta1.TaskRun{ - ObjectMeta: metav1.ObjectMeta{ - Name: trName, - Namespace: ns, - OwnerReferences: []metav1.OwnerReference{{ - Kind: "kind", - Name: "name", - }}, - Labels: map[string]string{ - pipeline.PipelineLabelKey: pName, - pipeline.PipelineRunLabelKey: prName, - }, - }, - Spec: v1beta1.TaskRunSpec{ - TaskRef: &v1beta1.TaskRef{ - Name: "hello-world", - }, - ServiceAccountName: "test-sa", - }, - } +func createHelloWorldTaskRun(t *testing.T, trName, ns, prName, pName string) *v1beta1.TaskRun { + return parse.MustParseTaskRun(t, fmt.Sprintf(` +metadata: + name: %s + namespace: %s + labels: + %s: %s + %s: %s +spec: + taskRef: + name: hello-world + serviceAccountName: test-sa +`, trName, ns, pipeline.PipelineLabelKey, pName, pipeline.PipelineRunLabelKey, prName)) } -func createCancelledPipelineRun(t *testing.T, prName string, specStatus v1beta1.PipelineRunSpecStatus) *v1beta1.PipelineRun { +func createCancelledPipelineRun( + t *testing.T, + prName string, + specStatus v1beta1.PipelineRunSpecStatus, +) *v1beta1.PipelineRun { return parse.MustParsePipelineRun(t, fmt.Sprintf(` metadata: name: %s