Skip to content

Commit 2b83efd

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 2b83efd

File tree

1 file changed

+350
-0
lines changed

1 file changed

+350
-0
lines changed

pkg/reconciler/pipelinerun/pipelinerun_test.go

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

0 commit comments

Comments
 (0)