Skip to content

Commit 332fdb2

Browse files
committed
Add Unit Tests for Array Results using [] notation
This commit adds test coverage for a pipeline task that emit an array of results and test string replacements from the array of results using indexing. This addresses issue: #6574.
1 parent 8e8c163 commit 332fdb2

File tree

1 file changed

+365
-3
lines changed

1 file changed

+365
-3
lines changed

pkg/reconciler/pipelinerun/pipelinerun_test.go

+365-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,25 @@ func getTaskRunByName(t *testing.T, taskRuns map[string]*v1beta1.TaskRun, expect
179179
// It will fatal the test if an error occurred.
180180
func getTaskRunsForPipelineRun(ctx context.Context, t *testing.T, clients test.Clients, namespace string, prName string) map[string]*v1beta1.TaskRun {
181181
t.Helper()
182+
labelSelector := pipeline.PipelineRunLabelKey + "=" + prName
183+
return getTaskRuns(ctx, t, clients, namespace, labelSelector)
184+
}
185+
186+
// getTaskRunsForPipelineTask returns the set of TaskRuns associated with the input PipelineRun and PipelineTask
187+
// It will fatal the test if an error occurred.
188+
func getTaskRunsForPipelineTask(ctx context.Context, t *testing.T, clients test.Clients, namespace string, prName string, ptLabel string) map[string]*v1beta1.TaskRun {
189+
t.Helper()
190+
labelSelector := pipeline.PipelineRunLabelKey + "=" + prName + "," + pipeline.PipelineTaskLabelKey + "=" + ptLabel
191+
return getTaskRuns(ctx, t, clients, namespace, labelSelector)
192+
}
193+
194+
// getTaskRuns returns the set of TaskRuns matching the label selector.
195+
// It will fatal the test if an error occurred.
196+
func getTaskRuns(ctx context.Context, t *testing.T, clients test.Clients, namespace string, labelSelector string) map[string]*v1beta1.TaskRun {
197+
t.Helper()
182198

183199
opt := metav1.ListOptions{
184-
LabelSelector: pipeline.PipelineRunLabelKey + "=" + prName,
200+
LabelSelector: labelSelector,
185201
}
186202

187203
taskRuns, err := clients.Pipeline.TektonV1beta1().TaskRuns(namespace).List(ctx, opt)
@@ -202,8 +218,6 @@ func getTaskRunsForPipelineRun(ctx context.Context, t *testing.T, clients test.C
202218
// Task that has not been started yet. It verifies that the TaskRun is created,
203219
// it checks the resulting API actions, status and events.
204220
func TestReconcile(t *testing.T) {
205-
names.TestingSeed()
206-
207221
namespace := "foo"
208222
prName := "test-pipeline-run-success"
209223
trName := "test-pipeline-run-success-unit-test-1"
@@ -9306,6 +9320,354 @@ spec:
93069320
}
93079321
}
93089322

9323+
func TestReconciler_PipelineTaskMatrixResultsWithArrayIndexing(t *testing.T) {
9324+
names.TestingSeed()
9325+
task := parse.MustParseV1beta1Task(t, `
9326+
metadata:
9327+
name: mytask
9328+
namespace: foo
9329+
spec:
9330+
params:
9331+
- name: platform
9332+
default: mac
9333+
steps:
9334+
- name: echo
9335+
image: alpine
9336+
script: |
9337+
echo "$(params.platform)"
9338+
`)
9339+
taskwithresults := parse.MustParseV1beta1Task(t, `
9340+
metadata:
9341+
name: taskwithresults
9342+
namespace: foo
9343+
spec:
9344+
results:
9345+
- name: platforms
9346+
type: array
9347+
steps:
9348+
- name: produce-a-list-of-platforms
9349+
image: bash:latest
9350+
script: |
9351+
#!/usr/bin/env bash
9352+
echo -n "[\"linux\",\"mac\",\"windows\"]" | tee $(results.platforms.path)
9353+
`)
9354+
9355+
cms := []*corev1.ConfigMap{withEnabledAlphaAPIFields(newFeatureFlagsConfigMap())}
9356+
cms = append(cms, withMaxMatrixCombinationsCount(newDefaultsConfigMap(), 10))
9357+
tests := []struct {
9358+
name string
9359+
pName string
9360+
p *v1beta1.Pipeline
9361+
tr *v1beta1.TaskRun
9362+
expectedTaskRuns []*v1beta1.TaskRun
9363+
expectedPipelineRun *v1beta1.PipelineRun
9364+
}{{
9365+
name: "indexing results in params",
9366+
pName: "p-dag",
9367+
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
9368+
metadata:
9369+
name: %s
9370+
namespace: foo
9371+
spec:
9372+
tasks:
9373+
- name: pt-with-result
9374+
params:
9375+
- name: platforms
9376+
type: array
9377+
taskRef:
9378+
name: taskwithresults
9379+
- name: echo-platforms
9380+
params:
9381+
- name: platforms
9382+
value:
9383+
- $(tasks.pt-with-result.results.platforms[0])
9384+
- $(tasks.pt-with-result.results.platforms[1])
9385+
- $(tasks.pt-with-result.results.platforms[2])
9386+
taskRef:
9387+
name: mytask
9388+
`, "p-dag")),
9389+
tr: mustParseTaskRunWithObjectMeta(t,
9390+
taskRunObjectMeta("pr-pt-with-result", "foo",
9391+
"pr", "p-dag", "pt-with-result", false),
9392+
`
9393+
spec:
9394+
serviceAccountName: test-sa
9395+
taskRef:
9396+
name: taskwithresults
9397+
status:
9398+
conditions:
9399+
- type: Succeeded
9400+
status: "True"
9401+
reason: Succeeded
9402+
message: All Tasks have completed executing
9403+
taskResults:
9404+
- name: platforms
9405+
value:
9406+
- linux
9407+
- mac
9408+
- windows
9409+
`),
9410+
expectedTaskRuns: []*v1beta1.TaskRun{
9411+
mustParseTaskRunWithObjectMeta(t,
9412+
taskRunObjectMeta("pr-echo-platforms", "foo",
9413+
"pr", "p-dag", "echo-platforms", false),
9414+
`
9415+
spec:
9416+
params:
9417+
- name: platforms
9418+
value:
9419+
- linux
9420+
- mac
9421+
- windows
9422+
serviceAccountName: test-sa
9423+
taskRef:
9424+
name: mytask
9425+
kind: Task
9426+
labels:
9427+
tekton.dev/memberOf: tasks
9428+
tekton.dev/pipeline: p-dag
9429+
`),
9430+
},
9431+
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
9432+
metadata:
9433+
name: pr
9434+
namespace: foo
9435+
annotations: {}
9436+
labels:
9437+
tekton.dev/pipeline: p-dag
9438+
spec:
9439+
serviceAccountName: test-sa
9440+
pipelineRef:
9441+
name: p-dag
9442+
status:
9443+
pipelineSpec:
9444+
tasks:
9445+
- name: pt-with-result
9446+
params:
9447+
- name: platforms
9448+
type: array
9449+
taskRef:
9450+
name: taskwithresults
9451+
kind: Task
9452+
- name: echo-platforms
9453+
taskRef:
9454+
name: mytask
9455+
kind: Task
9456+
params:
9457+
- name: platforms
9458+
value:
9459+
- $(tasks.pt-with-result.results.platforms[0])
9460+
- $(tasks.pt-with-result.results.platforms[1])
9461+
- $(tasks.pt-with-result.results.platforms[2])
9462+
conditions:
9463+
- type: Succeeded
9464+
status: "Unknown"
9465+
reason: "Running"
9466+
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
9467+
childReferences:
9468+
- apiVersion: tekton.dev/v1beta1
9469+
kind: TaskRun
9470+
name: pr-pt-with-result
9471+
pipelineTaskName: pt-with-result
9472+
- apiVersion: tekton.dev/v1beta1
9473+
kind: TaskRun
9474+
name: pr-echo-platforms
9475+
pipelineTaskName: echo-platforms
9476+
`),
9477+
}, {
9478+
name: "indexing results in matrix.params",
9479+
pName: "p-dag-2",
9480+
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
9481+
metadata:
9482+
name: %s
9483+
namespace: foo
9484+
spec:
9485+
tasks:
9486+
- name: pt-with-result
9487+
params:
9488+
- name: platforms
9489+
type: array
9490+
taskRef:
9491+
name: taskwithresults
9492+
- name: echo-platforms
9493+
matrix:
9494+
params:
9495+
- name: platform
9496+
value:
9497+
- $(tasks.pt-with-result.results.platforms[0])
9498+
- $(tasks.pt-with-result.results.platforms[1])
9499+
- $(tasks.pt-with-result.results.platforms[2])
9500+
taskRef:
9501+
name: mytask
9502+
`, "p-dag-2")),
9503+
tr: mustParseTaskRunWithObjectMeta(t,
9504+
taskRunObjectMeta("pr-pt-with-result", "foo",
9505+
"pr", "p-dag-2", "pt-with-result", false),
9506+
`
9507+
spec:
9508+
serviceAccountName: test-sa
9509+
taskRef:
9510+
name: taskwithresults
9511+
status:
9512+
conditions:
9513+
- type: Succeeded
9514+
status: "True"
9515+
reason: Succeeded
9516+
message: All Tasks have completed executing
9517+
taskResults:
9518+
- name: platforms
9519+
value:
9520+
- linux
9521+
- mac
9522+
- windows
9523+
`),
9524+
expectedTaskRuns: []*v1beta1.TaskRun{
9525+
mustParseTaskRunWithObjectMeta(t,
9526+
taskRunObjectMeta("pr-echo-platforms-0", "foo",
9527+
"pr", "p-dag-2", "echo-platforms", false),
9528+
`
9529+
spec:
9530+
params:
9531+
- name: platform
9532+
value: linux
9533+
serviceAccountName: test-sa
9534+
taskRef:
9535+
name: mytask
9536+
kind: Task
9537+
labels:
9538+
tekton.dev/memberOf: tasks
9539+
tekton.dev/pipeline: p-dag-2
9540+
`),
9541+
mustParseTaskRunWithObjectMeta(t,
9542+
taskRunObjectMeta("pr-echo-platforms-1", "foo",
9543+
"pr", "p-dag-2", "echo-platforms", false),
9544+
`
9545+
spec:
9546+
params:
9547+
- name: platform
9548+
value: mac
9549+
serviceAccountName: test-sa
9550+
taskRef:
9551+
name: mytask
9552+
kind: Task
9553+
labels:
9554+
tekton.dev/memberOf: tasks
9555+
tekton.dev/pipeline: p-dag-2
9556+
`),
9557+
mustParseTaskRunWithObjectMeta(t,
9558+
taskRunObjectMeta("pr-echo-platforms-2", "foo",
9559+
"pr", "p-dag-2", "echo-platforms", false),
9560+
`
9561+
spec:
9562+
params:
9563+
- name: platform
9564+
value: windows
9565+
serviceAccountName: test-sa
9566+
taskRef:
9567+
name: mytask
9568+
kind: Task
9569+
labels:
9570+
tekton.dev/memberOf: tasks
9571+
tekton.dev/pipeline: p-dag-2
9572+
`),
9573+
},
9574+
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
9575+
metadata:
9576+
name: pr
9577+
namespace: foo
9578+
annotations: {}
9579+
labels:
9580+
tekton.dev/pipeline: p-dag-2
9581+
spec:
9582+
serviceAccountName: test-sa
9583+
pipelineRef:
9584+
name: p-dag-2
9585+
status:
9586+
pipelineSpec:
9587+
tasks:
9588+
- name: pt-with-result
9589+
params:
9590+
- name: platforms
9591+
type: array
9592+
taskRef:
9593+
name: taskwithresults
9594+
kind: Task
9595+
- name: echo-platforms
9596+
taskRef:
9597+
name: mytask
9598+
kind: Task
9599+
matrix:
9600+
params:
9601+
- name: platform
9602+
value:
9603+
- $(tasks.pt-with-result.results.platforms[0])
9604+
- $(tasks.pt-with-result.results.platforms[1])
9605+
- $(tasks.pt-with-result.results.platforms[2])
9606+
conditions:
9607+
- type: Succeeded
9608+
status: "Unknown"
9609+
reason: "Running"
9610+
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
9611+
childReferences:
9612+
- apiVersion: tekton.dev/v1beta1
9613+
kind: TaskRun
9614+
name: pr-pt-with-result
9615+
pipelineTaskName: pt-with-result
9616+
- apiVersion: tekton.dev/v1beta1
9617+
kind: TaskRun
9618+
name: pr-echo-platforms-0
9619+
pipelineTaskName: echo-platforms
9620+
- apiVersion: tekton.dev/v1beta1
9621+
kind: TaskRun
9622+
name: pr-echo-platforms-1
9623+
pipelineTaskName: echo-platforms
9624+
- apiVersion: tekton.dev/v1beta1
9625+
kind: TaskRun
9626+
name: pr-echo-platforms-2
9627+
pipelineTaskName: echo-platforms
9628+
`),
9629+
}}
9630+
for _, tt := range tests {
9631+
t.Run(tt.pName, func(t *testing.T) {
9632+
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
9633+
metadata:
9634+
name: pr
9635+
namespace: foo
9636+
spec:
9637+
serviceAccountName: test-sa
9638+
pipelineRef:
9639+
name: %s
9640+
`, tt.pName))
9641+
d := test.Data{
9642+
PipelineRuns: []*v1beta1.PipelineRun{pr},
9643+
Pipelines: []*v1beta1.Pipeline{tt.p},
9644+
Tasks: []*v1beta1.Task{task, taskwithresults},
9645+
ConfigMaps: cms,
9646+
}
9647+
if tt.tr != nil {
9648+
d.TaskRuns = []*v1beta1.TaskRun{tt.tr}
9649+
}
9650+
prt := newPipelineRunTest(t, d)
9651+
defer prt.Cancel()
9652+
pipelineRun, clients := prt.reconcileRun(pr.Namespace, pr.Name, []string{} /* wantEvents*/, false /* permanentError*/)
9653+
9654+
taskRuns := getTaskRunsForPipelineTask(prt.TestAssets.Ctx, t, clients, pr.Namespace, pr.Name, "echo-platforms")
9655+
validateTaskRunsCount(t, taskRuns, len(tt.expectedTaskRuns))
9656+
for _, expectedTaskRun := range tt.expectedTaskRuns {
9657+
trName := expectedTaskRun.Name
9658+
actual := getTaskRunByName(t, taskRuns, trName)
9659+
if d := cmp.Diff(expectedTaskRun, actual, ignoreResourceVersion, ignoreTypeMeta); d != "" {
9660+
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRun.Name, diff.PrintWantGot(d))
9661+
}
9662+
}
9663+
9664+
if d := cmp.Diff(tt.expectedPipelineRun, pipelineRun, ignoreResourceVersion, ignoreTypeMeta, ignoreLastTransitionTime, ignoreStartTime, ignoreFinallyStartTime, cmpopts.EquateEmpty()); d != "" {
9665+
t.Errorf("expected PipelineRun was not created. Diff %s", diff.PrintWantGot(d))
9666+
}
9667+
})
9668+
}
9669+
}
9670+
93099671
func TestReconciler_PipelineTaskMatrixWithRetries(t *testing.T) {
93109672
names.TestingSeed()
93119673

0 commit comments

Comments
 (0)