Skip to content

Commit 3c6354c

Browse files
jbpratttekton-robot
authored andcommitted
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 tektoncd#4610 Signed-off-by: jbpratt <[email protected]>
1 parent e4ec917 commit 3c6354c

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

pkg/reconciler/pipelinerun/pipelinerun_test.go

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ func TestReconcileOnCompletedPipelineRun(t *testing.T) {
15501550
}}
15511551
ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline}
15521552
ts := []*v1beta1.Task{simpleHelloWorldTask}
1553-
trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(taskRunName, "foo",
1553+
trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(t, taskRunName, "foo",
15541554
"test-pipeline-run-completed", "test-pipeline", "",
15551555
apis.Condition{
15561556
Type: apis.ConditionSucceeded,
@@ -1621,7 +1621,7 @@ func TestReconcileOnCancelledPipelineRunDeprecated(t *testing.T) {
16211621
prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelledDeprecated)}
16221622
ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline}
16231623
ts := []*v1beta1.Task{simpleHelloWorldTask}
1624-
trs := []*v1beta1.TaskRun{createHelloWorldTaskRun("test-pipeline-run-cancelled-hello-world", "foo",
1624+
trs := []*v1beta1.TaskRun{createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-hello-world", "foo",
16251625
"test-pipeline-run-cancelled", "test-pipeline")}
16261626

16271627
d := test.Data{
@@ -1666,7 +1666,7 @@ func TestReconcileOnCancelledPipelineRun(t *testing.T) {
16661666
prs := []*v1beta1.PipelineRun{createCancelledPipelineRun(t, "test-pipeline-run-cancelled", v1beta1.PipelineRunSpecStatusCancelled)}
16671667
ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline}
16681668
ts := []*v1beta1.Task{simpleHelloWorldTask}
1669-
trs := []*v1beta1.TaskRun{createHelloWorldTaskRun("test-pipeline-run-cancelled-hello-world", "foo",
1669+
trs := []*v1beta1.TaskRun{createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-hello-world", "foo",
16701670
"test-pipeline-run-cancelled", "test-pipeline")}
16711671
cms := getConfigMapsWithEnabledAlphaAPIFields()
16721672

@@ -2118,13 +2118,13 @@ func TestReconcileOnCancelledRunFinallyPipelineRunWithRunningFinalTask(t *testin
21182118
simpleSomeTask,
21192119
}
21202120
trs := []*v1beta1.TaskRun{
2121-
createHelloWorldTaskRunWithStatus("test-pipeline-run-cancelled-run-finally-hello-world", "foo",
2121+
createHelloWorldTaskRunWithStatus(t, "test-pipeline-run-cancelled-run-finally-hello-world", "foo",
21222122
"test-pipeline-run-cancelled-run-finally", "test-pipeline", "my-pod-name",
21232123
apis.Condition{
21242124
Type: apis.ConditionSucceeded,
21252125
Status: corev1.ConditionTrue,
21262126
}),
2127-
createHelloWorldTaskRun("test-pipeline-run-cancelled-run-finally-final-task", "foo",
2127+
createHelloWorldTaskRun(t, "test-pipeline-run-cancelled-run-finally-final-task", "foo",
21282128
"test-pipeline-run-cancelled-run-finally", "test-pipeline"),
21292129
}
21302130
cms := getConfigMapsWithEnabledAlphaAPIFields()
@@ -2224,7 +2224,7 @@ func TestReconcileOnCancelledRunFinallyPipelineRunWithFinalTaskAndRetries(t *tes
22242224
}}
22252225

22262226
// TaskRun exists for DAG task "hello-world-1" that has failed with reason of cancellation
2227-
trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus("test-pipeline-run-cancelled-run-finally-hello-world", "foo",
2227+
trs := []*v1beta1.TaskRun{createHelloWorldTaskRunWithStatus(t, "test-pipeline-run-cancelled-run-finally-hello-world", "foo",
22282228
"test-pipeline-run-cancelled-run-finally", "test-pipeline", "my-pod-name",
22292229
apis.Condition{
22302230
Type: apis.ConditionSucceeded,
@@ -2305,6 +2305,7 @@ func TestReconcileCancelledRunFinallyFailsTaskRunCancellation(t *testing.T) {
23052305
}
23062306
trs := []*v1beta1.TaskRun{
23072307
getTaskRun(
2308+
t,
23082309
"test-pipeline-fails-to-cancelhello-world-1",
23092310
prName,
23102311
"test-pipeline",
@@ -2412,6 +2413,7 @@ func TestReconcileTaskResolutionError(t *testing.T) {
24122413
}}
24132414
trs := []*v1beta1.TaskRun{
24142415
getTaskRun(
2416+
t,
24152417
"test-pipeline-fails-task-resolutionhello-world-1",
24162418
prName,
24172419
"test-pipeline",
@@ -2555,6 +2557,7 @@ func TestReconcileOnStoppedRunFinallyPipelineRunWithRunningTask(t *testing.T) {
25552557
ts := []*v1beta1.Task{simpleHelloWorldTask}
25562558
trs := []*v1beta1.TaskRun{
25572559
getTaskRun(
2560+
t,
25582561
"test-pipeline-run-stopped-run-finally-hello-world",
25592562
"test-pipeline-run-stopped-run-finally",
25602563
"test-pipeline",
@@ -2650,6 +2653,7 @@ func TestReconcileOnStoppedPipelineRunWithCompletedTask(t *testing.T) {
26502653
ts := []*v1beta1.Task{simpleHelloWorldTask}
26512654
trs := []*v1beta1.TaskRun{
26522655
getTaskRun(
2656+
t,
26532657
"test-pipeline-run-stopped-hello-world",
26542658
"test-pipeline-run-stopped",
26552659
"test-pipeline",
@@ -6952,13 +6956,15 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {
69526956

69536957
trs: []*v1beta1.TaskRun{
69546958
getTaskRun(
6959+
t,
69556960
"task-run-dag-task",
69566961
"pipeline-run-dag-task-failing",
69576962
"pipeline-dag-task-failing",
69586963
"dag-task-1",
69596964
corev1.ConditionFalse,
69606965
),
69616966
getTaskRun(
6967+
t,
69626968
"task-run-final-task",
69636969
"pipeline-run-dag-task-failing",
69646970
"pipeline-dag-task-failing",
@@ -7016,13 +7022,15 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {
70167022

70177023
trs: []*v1beta1.TaskRun{
70187024
getTaskRun(
7025+
t,
70197026
"task-run-dag-task",
70207027
"pipeline-run-with-dag-successful-but-final-failing",
70217028
"pipeline-with-dag-successful-but-final-failing",
70227029
"dag-task-1",
70237030
"",
70247031
),
70257032
getTaskRun(
7033+
t,
70267034
"task-run-final-task",
70277035
"pipeline-run-with-dag-successful-but-final-failing",
70287036
"pipeline-with-dag-successful-but-final-failing",
@@ -7080,13 +7088,15 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {
70807088

70817089
trs: []*v1beta1.TaskRun{
70827090
getTaskRun(
7091+
t,
70837092
"task-run-dag-task",
70847093
"pipeline-run-with-dag-and-final-failing",
70857094
"pipeline-with-dag-and-final-failing",
70867095
"dag-task-1",
70877096
corev1.ConditionFalse,
70887097
),
70897098
getTaskRun(
7099+
t,
70907100
"task-run-final-task",
70917101
"pipeline-run-with-dag-and-final-failing",
70927102
"pipeline-with-dag-and-final-failing",
@@ -7151,13 +7161,15 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {
71517161

71527162
trs: []*v1beta1.TaskRun{
71537163
getTaskRun(
7164+
t,
71547165
"task-run-dag-task-1",
71557166
"pipeline-run-with-dag-running",
71567167
"pipeline-with-dag-running",
71577168
"dag-task-1",
71587169
corev1.ConditionFalse,
71597170
),
71607171
getTaskRun(
7172+
t,
71617173
"task-run-dag-task-2",
71627174
"pipeline-run-with-dag-running",
71637175
"pipeline-with-dag-running",
@@ -7216,6 +7228,7 @@ func TestReconcilePipeline_FinalTasks(t *testing.T) {
72167228

72177229
trs: []*v1beta1.TaskRun{
72187230
getTaskRun(
7231+
t,
72197232
"task-run-dag-task-1",
72207233
"pipeline-run-dag-task-running",
72217234
"pipeline-dag-task-running",
@@ -7328,8 +7341,8 @@ func getPipeline(p string, spec v1beta1.PipelineSpec) []*v1beta1.Pipeline {
73287341
return ps
73297342
}
73307343

7331-
func getTaskRun(tr, pr, p, t string, status corev1.ConditionStatus) *v1beta1.TaskRun {
7332-
return createHelloWorldTaskRunWithStatusTaskLabel(tr, "foo", pr, p, "", t,
7344+
func getTaskRun(t *testing.T, tr, pr, p, tl string, status corev1.ConditionStatus) *v1beta1.TaskRun {
7345+
return createHelloWorldTaskRunWithStatusTaskLabel(t, tr, "foo", pr, p, "", tl,
73337346
apis.Condition{
73347347
Type: apis.ConditionSucceeded,
73357348
Status: status,
@@ -8422,8 +8435,12 @@ func taskRunObjectMeta(trName, ns, prName, pipelineName, pipelineTaskName string
84228435
return om
84238436
}
84248437

8425-
func createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName string, condition apis.Condition) *v1beta1.TaskRun {
8426-
p := createHelloWorldTaskRun(trName, ns, prName, pName)
8438+
func createHelloWorldTaskRunWithStatus(
8439+
t *testing.T,
8440+
trName, ns, prName, pName, podName string,
8441+
condition apis.Condition,
8442+
) *v1beta1.TaskRun {
8443+
p := createHelloWorldTaskRun(t, trName, ns, prName, pName)
84278444
p.Status = v1beta1.TaskRunStatus{
84288445
Status: duckv1beta1.Status{
84298446
Conditions: duckv1beta1.Conditions{condition},
@@ -8435,34 +8452,30 @@ func createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName string
84358452
return p
84368453
}
84378454

8438-
func createHelloWorldTaskRunWithStatusTaskLabel(trName, ns, prName, pName, podName, taskLabel string, condition apis.Condition) *v1beta1.TaskRun {
8439-
p := createHelloWorldTaskRunWithStatus(trName, ns, prName, pName, podName, condition)
8455+
func createHelloWorldTaskRunWithStatusTaskLabel(
8456+
t *testing.T,
8457+
trName, ns, prName, pName, podName, taskLabel string,
8458+
condition apis.Condition,
8459+
) *v1beta1.TaskRun {
8460+
p := createHelloWorldTaskRunWithStatus(t, trName, ns, prName, pName, podName, condition)
84408461
p.Labels[pipeline.PipelineTaskLabelKey] = taskLabel
84418462

84428463
return p
84438464
}
84448465

8445-
func createHelloWorldTaskRun(trName, ns, prName, pName string) *v1beta1.TaskRun {
8446-
return &v1beta1.TaskRun{
8447-
ObjectMeta: metav1.ObjectMeta{
8448-
Name: trName,
8449-
Namespace: ns,
8450-
OwnerReferences: []metav1.OwnerReference{{
8451-
Kind: "kind",
8452-
Name: "name",
8453-
}},
8454-
Labels: map[string]string{
8455-
pipeline.PipelineLabelKey: pName,
8456-
pipeline.PipelineRunLabelKey: prName,
8457-
},
8458-
},
8459-
Spec: v1beta1.TaskRunSpec{
8460-
TaskRef: &v1beta1.TaskRef{
8461-
Name: "hello-world",
8462-
},
8463-
ServiceAccountName: "test-sa",
8464-
},
8465-
}
8466+
func createHelloWorldTaskRun(t *testing.T, trName, ns, prName, pName string) *v1beta1.TaskRun {
8467+
return parse.MustParseTaskRun(t, fmt.Sprintf(`
8468+
metadata:
8469+
name: %s
8470+
namespace: %s
8471+
labels:
8472+
%s: %s
8473+
%s: %s
8474+
spec:
8475+
taskRef:
8476+
name: hello-world
8477+
serviceAccountName: test-sa
8478+
`, trName, ns, pipeline.PipelineLabelKey, pName, pipeline.PipelineRunLabelKey, prName))
84668479
}
84678480

84688481
func createCancelledPipelineRun(t *testing.T, prName string, specStatus v1beta1.PipelineRunSpecStatus) *v1beta1.PipelineRun {

0 commit comments

Comments
 (0)