Skip to content

Commit bf5ce34

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 bf5ce34

File tree

1 file changed

+347
-0
lines changed

1 file changed

+347
-0
lines changed

pkg/reconciler/pipelinerun/pipelinerun_test.go

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

0 commit comments

Comments
 (0)