Skip to content

Commit

Permalink
refactor create helpers in reconcile pr tests
Browse files Browse the repository at this point in the history
Refactor the pipelineRun and taskRun create helpers to parse yaml.
Requires passing `*testing.T` through all calls.

continued work on tektoncd#4610

Signed-off-by: jbpratt <[email protected]>
  • Loading branch information
jbpratt committed Mar 17, 2022
1 parent 6738db7 commit c5cbc7c
Showing 1 changed file with 71 additions and 38 deletions.
109 changes: 71 additions & 38 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2305,6 +2321,7 @@ func TestReconcileCancelledRunFinallyFailsTaskRunCancellation(t *testing.T) {
}
trs := []*v1beta1.TaskRun{
getTaskRun(
t,
"test-pipeline-fails-to-cancelhello-world-1",
prName,
"test-pipeline",
Expand Down Expand Up @@ -2412,6 +2429,7 @@ func TestReconcileTaskResolutionError(t *testing.T) {
}}
trs := []*v1beta1.TaskRun{
getTaskRun(
t,
"test-pipeline-fails-task-resolutionhello-world-1",
prName,
"test-pipeline",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -6952,13 +6972,15 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {

trs: []*v1beta1.TaskRun{
getTaskRun(
t,
"task-run-dag-task",
"pipeline-run-dag-task-failing",
"pipeline-dag-task-failing",
"dag-task-1",
corev1.ConditionFalse,
),
getTaskRun(
t,
"task-run-final-task",
"pipeline-run-dag-task-failing",
"pipeline-dag-task-failing",
Expand Down Expand Up @@ -7016,13 +7038,15 @@ 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",
"dag-task-1",
"",
),
getTaskRun(
t,
"task-run-final-task",
"pipeline-run-with-dag-successful-but-final-failing",
"pipeline-with-dag-successful-but-final-failing",
Expand Down Expand Up @@ -7080,13 +7104,15 @@ 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",
"dag-task-1",
corev1.ConditionFalse,
),
getTaskRun(
t,
"task-run-final-task",
"pipeline-run-with-dag-and-final-failing",
"pipeline-with-dag-and-final-failing",
Expand Down Expand Up @@ -7151,13 +7177,15 @@ 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",
"dag-task-1",
corev1.ConditionFalse,
),
getTaskRun(
t,
"task-run-dag-task-2",
"pipeline-run-with-dag-running",
"pipeline-with-dag-running",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand All @@ -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
Expand Down

0 comments on commit c5cbc7c

Please sign in to comment.