Skip to content

Commit ba3080c

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 ba3080c

File tree

1 file changed

+366
-3
lines changed

1 file changed

+366
-3
lines changed

pkg/reconciler/pipelinerun/pipelinerun_test.go

+366-3
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,24 @@ func getTaskRunByName(t *testing.T, taskRuns map[string]*v1beta1.TaskRun, expect
178178
// getTaskRunsForPipelineRun returns the set of TaskRuns associated with the input PipelineRun.
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 {
181+
labelSelector := pipeline.PipelineRunLabelKey + "=" + prName
182+
return getTaskRuns(ctx, t, clients, namespace, labelSelector)
183+
}
184+
185+
// getTaskRunsForPipelineTask returns the set of TaskRuns associated with the input PipelineRun and PipelineTask
186+
// It will fatal the test if an error occurred.
187+
func getTaskRunsForPipelineTask(ctx context.Context, t *testing.T, clients test.Clients, namespace string, prName string, ptLabel string) map[string]*v1beta1.TaskRun {
188+
labelSelector := pipeline.PipelineRunLabelKey + "=" + prName + "," + pipeline.PipelineTaskLabelKey + "=" + ptLabel
189+
return getTaskRuns(ctx, t, clients, namespace, labelSelector)
190+
}
191+
192+
// getTaskRuns returns the set of TaskRuns.
193+
// It will fatal the test if an error occurred.
194+
func getTaskRuns(ctx context.Context, t *testing.T, clients test.Clients, namespace string, labelSelector string) map[string]*v1beta1.TaskRun {
181195
t.Helper()
182196

183197
opt := metav1.ListOptions{
184-
LabelSelector: pipeline.PipelineRunLabelKey + "=" + prName,
198+
LabelSelector: labelSelector,
185199
}
186200

187201
taskRuns, err := clients.Pipeline.TektonV1beta1().TaskRuns(namespace).List(ctx, opt)
@@ -202,8 +216,6 @@ func getTaskRunsForPipelineRun(ctx context.Context, t *testing.T, clients test.C
202216
// Task that has not been started yet. It verifies that the TaskRun is created,
203217
// it checks the resulting API actions, status and events.
204218
func TestReconcile(t *testing.T) {
205-
names.TestingSeed()
206-
207219
namespace := "foo"
208220
prName := "test-pipeline-run-success"
209221
trName := "test-pipeline-run-success-unit-test-1"
@@ -10849,3 +10861,354 @@ spec:
1084910861
t.Errorf("Expected PipelineRun to be create run failed, but condition reason is %s", reconciledRun.Status.GetCondition(apis.ConditionSucceeded))
1085010862
}
1085110863
}
10864+
10865+
func TestReconciler_PipelineTaskMatrixResultsWithArrayIndexing(t *testing.T) {
10866+
names.TestingSeed()
10867+
task := parse.MustParseV1beta1Task(t, `
10868+
metadata:
10869+
name: mytask
10870+
namespace: foo
10871+
spec:
10872+
params:
10873+
- name: platform
10874+
default: mac
10875+
steps:
10876+
- name: echo
10877+
image: alpine
10878+
script: |
10879+
echo "$(params.platform)"
10880+
`)
10881+
taskwithresults := parse.MustParseV1beta1Task(t, `
10882+
metadata:
10883+
name: taskwithresults
10884+
namespace: foo
10885+
spec:
10886+
results:
10887+
- name: platforms
10888+
type: array
10889+
steps:
10890+
- name: produce-a-list-of-platforms
10891+
image: bash:latest
10892+
script: |
10893+
#!/usr/bin/env bash
10894+
echo -n "[\"linux\",\"mac\",\"windows\"]" | tee $(results.platforms.path)
10895+
`)
10896+
10897+
cms := []*corev1.ConfigMap{withEnabledAlphaAPIFields(newFeatureFlagsConfigMap())}
10898+
cms = append(cms, withMaxMatrixCombinationsCount(newDefaultsConfigMap(), 10))
10899+
tests := []struct {
10900+
name string
10901+
pName string
10902+
p *v1beta1.Pipeline
10903+
tr *v1beta1.TaskRun
10904+
expectedTaskRuns []*v1beta1.TaskRun
10905+
expectedPipelineRun *v1beta1.PipelineRun
10906+
}{{
10907+
name: "indexing results in params",
10908+
pName: "p-dag",
10909+
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
10910+
metadata:
10911+
name: %s
10912+
namespace: foo
10913+
spec:
10914+
tasks:
10915+
- name: pt-with-result
10916+
params:
10917+
- name: platforms
10918+
type: array
10919+
taskRef:
10920+
name: taskwithresults
10921+
- name: echo-platforms
10922+
params:
10923+
- name: platforms
10924+
value:
10925+
- $(tasks.pt-with-result.results.platforms[0])
10926+
- $(tasks.pt-with-result.results.platforms[1])
10927+
- $(tasks.pt-with-result.results.platforms[2])
10928+
taskRef:
10929+
name: mytask
10930+
`, "p-dag")),
10931+
tr: mustParseTaskRunWithObjectMeta(t,
10932+
taskRunObjectMeta("pr-pt-with-result", "foo",
10933+
"pr", "p-dag", "pt-with-result", false),
10934+
`
10935+
spec:
10936+
serviceAccountName: test-sa
10937+
taskRef:
10938+
name: taskwithresults
10939+
status:
10940+
conditions:
10941+
- type: Succeeded
10942+
status: "True"
10943+
reason: Succeeded
10944+
message: All Tasks have completed executing
10945+
taskResults:
10946+
- name: platforms
10947+
value:
10948+
- linux
10949+
- mac
10950+
- windows
10951+
`),
10952+
expectedTaskRuns: []*v1beta1.TaskRun{
10953+
mustParseTaskRunWithObjectMeta(t,
10954+
taskRunObjectMeta("pr-echo-platforms", "foo",
10955+
"pr", "p-dag", "echo-platforms", false),
10956+
`
10957+
spec:
10958+
params:
10959+
- name: platforms
10960+
value:
10961+
- linux
10962+
- mac
10963+
- windows
10964+
serviceAccountName: test-sa
10965+
taskRef:
10966+
name: mytask
10967+
kind: Task
10968+
labels:
10969+
tekton.dev/memberOf: tasks
10970+
tekton.dev/pipeline: p-dag
10971+
`),
10972+
},
10973+
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
10974+
metadata:
10975+
name: pr
10976+
namespace: foo
10977+
annotations: {}
10978+
labels:
10979+
tekton.dev/pipeline: p-dag
10980+
spec:
10981+
serviceAccountName: test-sa
10982+
pipelineRef:
10983+
name: p-dag
10984+
status:
10985+
pipelineSpec:
10986+
tasks:
10987+
- name: pt-with-result
10988+
params:
10989+
- name: platforms
10990+
type: array
10991+
taskRef:
10992+
name: taskwithresults
10993+
kind: Task
10994+
- name: echo-platforms
10995+
taskRef:
10996+
name: mytask
10997+
kind: Task
10998+
params:
10999+
- name: platforms
11000+
value:
11001+
- $(tasks.pt-with-result.results.platforms[0])
11002+
- $(tasks.pt-with-result.results.platforms[1])
11003+
- $(tasks.pt-with-result.results.platforms[2])
11004+
conditions:
11005+
- type: Succeeded
11006+
status: "Unknown"
11007+
reason: "Running"
11008+
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
11009+
childReferences:
11010+
- apiVersion: tekton.dev/v1beta1
11011+
kind: TaskRun
11012+
name: pr-pt-with-result
11013+
pipelineTaskName: pt-with-result
11014+
- apiVersion: tekton.dev/v1beta1
11015+
kind: TaskRun
11016+
name: pr-echo-platforms
11017+
pipelineTaskName: echo-platforms
11018+
`),
11019+
}, {
11020+
name: "indexing results in matrix.params",
11021+
pName: "p-dag-2",
11022+
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
11023+
metadata:
11024+
name: %s
11025+
namespace: foo
11026+
spec:
11027+
tasks:
11028+
- name: pt-with-result
11029+
params:
11030+
- name: platforms
11031+
type: array
11032+
taskRef:
11033+
name: taskwithresults
11034+
- name: echo-platforms
11035+
matrix:
11036+
params:
11037+
- name: platform
11038+
value:
11039+
- $(tasks.pt-with-result.results.platforms[0])
11040+
- $(tasks.pt-with-result.results.platforms[1])
11041+
- $(tasks.pt-with-result.results.platforms[2])
11042+
taskRef:
11043+
name: mytask
11044+
`, "p-dag-2")),
11045+
tr: mustParseTaskRunWithObjectMeta(t,
11046+
taskRunObjectMeta("pr-pt-with-result", "foo",
11047+
"pr", "p-dag-2", "pt-with-result", false),
11048+
`
11049+
spec:
11050+
serviceAccountName: test-sa
11051+
taskRef:
11052+
name: taskwithresults
11053+
status:
11054+
conditions:
11055+
- type: Succeeded
11056+
status: "True"
11057+
reason: Succeeded
11058+
message: All Tasks have completed executing
11059+
taskResults:
11060+
- name: platforms
11061+
value:
11062+
- linux
11063+
- mac
11064+
- windows
11065+
`),
11066+
expectedTaskRuns: []*v1beta1.TaskRun{
11067+
mustParseTaskRunWithObjectMeta(t,
11068+
taskRunObjectMeta("pr-echo-platforms-0", "foo",
11069+
"pr", "p-dag-2", "echo-platforms", false),
11070+
`
11071+
spec:
11072+
params:
11073+
- name: platform
11074+
value: linux
11075+
serviceAccountName: test-sa
11076+
taskRef:
11077+
name: mytask
11078+
kind: Task
11079+
labels:
11080+
tekton.dev/memberOf: tasks
11081+
tekton.dev/pipeline: p-dag-2
11082+
`),
11083+
mustParseTaskRunWithObjectMeta(t,
11084+
taskRunObjectMeta("pr-echo-platforms-1", "foo",
11085+
"pr", "p-dag-2", "echo-platforms", false),
11086+
`
11087+
spec:
11088+
params:
11089+
- name: platform
11090+
value: mac
11091+
serviceAccountName: test-sa
11092+
taskRef:
11093+
name: mytask
11094+
kind: Task
11095+
labels:
11096+
tekton.dev/memberOf: tasks
11097+
tekton.dev/pipeline: p-dag-2
11098+
`),
11099+
mustParseTaskRunWithObjectMeta(t,
11100+
taskRunObjectMeta("pr-echo-platforms-2", "foo",
11101+
"pr", "p-dag-2", "echo-platforms", false),
11102+
`
11103+
spec:
11104+
params:
11105+
- name: platform
11106+
value: windows
11107+
serviceAccountName: test-sa
11108+
taskRef:
11109+
name: mytask
11110+
kind: Task
11111+
labels:
11112+
tekton.dev/memberOf: tasks
11113+
tekton.dev/pipeline: p-dag-2
11114+
`),
11115+
},
11116+
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
11117+
metadata:
11118+
name: pr
11119+
namespace: foo
11120+
annotations: {}
11121+
labels:
11122+
tekton.dev/pipeline: p-dag-2
11123+
spec:
11124+
serviceAccountName: test-sa
11125+
pipelineRef:
11126+
name: p-dag-2
11127+
status:
11128+
pipelineSpec:
11129+
tasks:
11130+
- name: pt-with-result
11131+
params:
11132+
- name: platforms
11133+
type: array
11134+
taskRef:
11135+
name: taskwithresults
11136+
kind: Task
11137+
- name: echo-platforms
11138+
taskRef:
11139+
name: mytask
11140+
kind: Task
11141+
matrix:
11142+
params:
11143+
- name: platform
11144+
value:
11145+
- $(tasks.pt-with-result.results.platforms[0])
11146+
- $(tasks.pt-with-result.results.platforms[1])
11147+
- $(tasks.pt-with-result.results.platforms[2])
11148+
conditions:
11149+
- type: Succeeded
11150+
status: "Unknown"
11151+
reason: "Running"
11152+
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
11153+
childReferences:
11154+
- apiVersion: tekton.dev/v1beta1
11155+
kind: TaskRun
11156+
name: pr-pt-with-result
11157+
pipelineTaskName: pt-with-result
11158+
- apiVersion: tekton.dev/v1beta1
11159+
kind: TaskRun
11160+
name: pr-echo-platforms-0
11161+
pipelineTaskName: echo-platforms
11162+
- apiVersion: tekton.dev/v1beta1
11163+
kind: TaskRun
11164+
name: pr-echo-platforms-1
11165+
pipelineTaskName: echo-platforms
11166+
- apiVersion: tekton.dev/v1beta1
11167+
kind: TaskRun
11168+
name: pr-echo-platforms-2
11169+
pipelineTaskName: echo-platforms
11170+
`),
11171+
}}
11172+
for _, tt := range tests {
11173+
t.Run(tt.pName, func(t *testing.T) {
11174+
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
11175+
metadata:
11176+
name: pr
11177+
namespace: foo
11178+
spec:
11179+
serviceAccountName: test-sa
11180+
pipelineRef:
11181+
name: %s
11182+
`, tt.pName))
11183+
d := test.Data{
11184+
PipelineRuns: []*v1beta1.PipelineRun{pr},
11185+
Pipelines: []*v1beta1.Pipeline{tt.p},
11186+
Tasks: []*v1beta1.Task{task, taskwithresults},
11187+
ConfigMaps: cms,
11188+
}
11189+
if tt.tr != nil {
11190+
d.TaskRuns = []*v1beta1.TaskRun{tt.tr}
11191+
}
11192+
prt := newPipelineRunTest(t, d)
11193+
defer prt.Cancel()
11194+
_, clients := prt.reconcileRun("foo", "pr", []string{}, false)
11195+
11196+
taskRuns := getTaskRunsForPipelineTask(prt.TestAssets.Ctx, t, clients, pr.Namespace, pr.Name, "echo-platforms")
11197+
validateTaskRunsCount(t, taskRuns, len(tt.expectedTaskRuns))
11198+
for _, expectedTaskRun := range tt.expectedTaskRuns {
11199+
trName := expectedTaskRun.Name
11200+
actual := getTaskRunByName(t, taskRuns, trName)
11201+
if d := cmp.Diff(expectedTaskRun, actual, ignoreResourceVersion, ignoreTypeMeta); d != "" {
11202+
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRun.Name, diff.PrintWantGot(d))
11203+
}
11204+
}
11205+
pipelineRun, err := clients.Pipeline.TektonV1beta1().PipelineRuns("foo").Get(prt.TestAssets.Ctx, "pr", metav1.GetOptions{})
11206+
if err != nil {
11207+
t.Fatalf("Got an error getting reconciled run out of fake client: %s", err)
11208+
}
11209+
if d := cmp.Diff(tt.expectedPipelineRun, pipelineRun, ignoreResourceVersion, ignoreTypeMeta, ignoreLastTransitionTime, ignoreStartTime, ignoreFinallyStartTime, cmpopts.EquateEmpty()); d != "" {
11210+
t.Errorf("expected PipelineRun was not created. Diff %s", diff.PrintWantGot(d))
11211+
}
11212+
})
11213+
}
11214+
}

0 commit comments

Comments
 (0)