Skip to content

Commit

Permalink
Add Unit Tests for Array Results using [] notation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EmmaMunley committed Apr 26, 2023
1 parent 8e8c163 commit bf5ce34
Showing 1 changed file with 347 additions and 0 deletions.
347 changes: 347 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10849,3 +10849,350 @@ spec:
t.Errorf("Expected PipelineRun to be create run failed, but condition reason is %s", reconciledRun.Status.GetCondition(apis.ConditionSucceeded))
}
}

func TestReconciler_PipelineTaskMatrixResultsWithArrayIndexing(t *testing.T) {
names.TestingSeed()
task := parse.MustParseV1beta1Task(t, `
metadata:
name: mytask
namespace: foo
spec:
params:
- name: platform
default: mac
steps:
- name: echo
image: alpine
script: |
echo "$(params.platform)"
`)
taskwithresults := parse.MustParseV1beta1Task(t, `
metadata:
name: taskwithresults
namespace: foo
spec:
results:
- name: platforms
type: array
steps:
- name: produce-a-list-of-platforms
image: bash:latest
script: |
#!/usr/bin/env bash
echo -n "[\"linux\",\"mac\",\"windows\"]" | tee $(results.platforms.path)
`)

cms := []*corev1.ConfigMap{withEnabledAlphaAPIFields(newFeatureFlagsConfigMap())}
cms = append(cms, withMaxMatrixCombinationsCount(newDefaultsConfigMap(), 10))
tests := []struct {
name string
pName string
p *v1beta1.Pipeline
tr *v1beta1.TaskRun
expectedTaskRuns []*v1beta1.TaskRun
expectedPipelineRun *v1beta1.PipelineRun
}{{
name: "indexing results in params",
pName: "p-dag",
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: foo
spec:
tasks:
- name: pt-with-result
params:
- name: platforms
type: array
taskRef:
name: taskwithresults
- name: echo-platforms
params:
- name: platforms
value:
- $(tasks.pt-with-result.results.platforms[0])
- $(tasks.pt-with-result.results.platforms[1])
- $(tasks.pt-with-result.results.platforms[2])
taskRef:
name: mytask
`, "p-dag")),
tr: mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-pt-with-result", "foo",
"pr", "p-dag", "pt-with-result", false),
`
spec:
serviceAccountName: test-sa
taskRef:
name: taskwithresults
status:
conditions:
- type: Succeeded
status: "True"
reason: Succeeded
message: All Tasks have completed executing
taskResults:
- name: platforms
value:
- linux
- mac
- windows
`),
expectedTaskRuns: []*v1beta1.TaskRun{
mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-echo-platforms", "foo",
"pr", "p", "echo-platforms", false),
`
spec:
params:
- name: platforms
value:
- linux
- mac
- windows
serviceAccountName: test-sa
taskRef:
name: mytask
kind: Task
`),
},
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
metadata:
name: pr
namespace: foo
annotations: {}
labels:
tekton.dev/pipeline: p-dag
spec:
serviceAccountName: test-sa
pipelineRef:
name: p-dag
status:
pipelineSpec:
tasks:
- name: pt-with-result
params:
- name: platforms
type: array
taskRef:
name: taskwithresults
kind: Task
- name: echo-platforms
taskRef:
name: mytask
kind: Task
params:
- name: platforms
value:
- $(tasks.pt-with-result.results.platforms[0])
- $(tasks.pt-with-result.results.platforms[1])
- $(tasks.pt-with-result.results.platforms[2])
conditions:
- type: Succeeded
status: "Unknown"
reason: "Running"
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-pt-with-result
pipelineTaskName: pt-with-result
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-echo-platforms
pipelineTaskName: echo-platforms
`),
}, {
name: "indexing results in matrix.params",
pName: "p-dag-2",
p: parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: foo
spec:
tasks:
- name: pt-with-result
params:
- name: platforms
type: array
taskRef:
name: taskwithresults
- name: echo-platforms
matrix:
params:
- name: platform
value:
- $(tasks.pt-with-result.results.platforms[0])
- $(tasks.pt-with-result.results.platforms[1])
- $(tasks.pt-with-result.results.platforms[2])
taskRef:
name: mytask
`, "p-dag-2")),
tr: mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-pt-with-result", "foo",
"pr", "p-dag-2", "pt-with-result", false),
`
spec:
serviceAccountName: test-sa
taskRef:
name: taskwithresults
status:
conditions:
- type: Succeeded
status: "True"
reason: Succeeded
message: All Tasks have completed executing
taskResults:
- name: platforms
value:
- linux
- mac
- windows
`),
expectedTaskRuns: []*v1beta1.TaskRun{
mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-echo-platforms-0", "foo",
"pr", "p", "echo-platforms", false),
`
spec:
params:
- name: platform
value: linux
serviceAccountName: test-sa
taskRef:
name: mytask
kind: Task
`),
mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-echo-platforms-1", "foo",
"pr", "p", "echo-platforms", false),
`
spec:
params:
- name: platform
value: mac
serviceAccountName: test-sa
taskRef:
name: mytask
kind: Task
`),
mustParseTaskRunWithObjectMeta(t,
taskRunObjectMeta("pr-echo-platforms-2", "foo",
"pr", "p", "echo-platforms", false),
`
spec:
params:
- name: platform
value: windows
serviceAccountName: test-sa
taskRef:
name: mytask
kind: Task
`),
},
expectedPipelineRun: parse.MustParseV1beta1PipelineRun(t, `
metadata:
name: pr
namespace: foo
annotations: {}
labels:
tekton.dev/pipeline: p-dag-2
spec:
serviceAccountName: test-sa
pipelineRef:
name: p-dag-2
status:
pipelineSpec:
tasks:
- name: pt-with-result
params:
- name: platforms
type: array
taskRef:
name: taskwithresults
kind: Task
- name: echo-platforms
taskRef:
name: mytask
kind: Task
matrix:
params:
- name: platform
value:
- $(tasks.pt-with-result.results.platforms[0])
- $(tasks.pt-with-result.results.platforms[1])
- $(tasks.pt-with-result.results.platforms[2])
conditions:
- type: Succeeded
status: "Unknown"
reason: "Running"
message: "Tasks Completed: 1 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
childReferences:
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-pt-with-result
pipelineTaskName: pt-with-result
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-echo-platforms-0
pipelineTaskName: echo-platforms
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-echo-platforms-1
pipelineTaskName: echo-platforms
- apiVersion: tekton.dev/v1beta1
kind: TaskRun
name: pr-echo-platforms-2
pipelineTaskName: echo-platforms
`),
}}
for _, tt := range tests {
t.Run(tt.pName, func(t *testing.T) {
pr := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: pr
namespace: foo
spec:
serviceAccountName: test-sa
pipelineRef:
name: %s
`, tt.pName))
d := test.Data{
PipelineRuns: []*v1beta1.PipelineRun{pr},
Pipelines: []*v1beta1.Pipeline{tt.p},
Tasks: []*v1beta1.Task{task, taskwithresults},
ConfigMaps: cms,
}
if tt.tr != nil {
d.TaskRuns = []*v1beta1.TaskRun{tt.tr}
}
prt := newPipelineRunTest(t, d)
defer prt.Cancel()
_, clients := prt.reconcileRun("foo", "pr", []string{}, false)
taskRuns, err := clients.Pipeline.TektonV1beta1().TaskRuns("foo").List(prt.TestAssets.Ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/pipelineRun=pr,tekton.dev/pipeline=%s,tekton.dev/pipelineTask=echo-platforms", tt.pName),
Limit: 1,
})
if err != nil {
t.Fatalf("Failure to list TaskRun's %s", err)
}
if len(taskRuns.Items) != len(tt.expectedTaskRuns) {
t.Fatalf("Expected %d TaskRuns got %d", len(tt.expectedTaskRuns), len(taskRuns.Items))
}
for i := range taskRuns.Items {
expectedTaskRun := tt.expectedTaskRuns[i]
expectedTaskRun.Labels["tekton.dev/pipeline"] = tt.pName
expectedTaskRun.Labels["tekton.dev/memberOf"] = "tasks"
if d := cmp.Diff(expectedTaskRun, &taskRuns.Items[i], ignoreResourceVersion, ignoreTypeMeta); d != "" {
t.Errorf("expected to see TaskRun %v created. Diff %s", tt.expectedTaskRuns[i].Name, diff.PrintWantGot(d))
}
}
pipelineRun, err := clients.Pipeline.TektonV1beta1().PipelineRuns("foo").Get(prt.TestAssets.Ctx, "pr", metav1.GetOptions{})
if err != nil {
t.Fatalf("Got an error getting reconciled run out of fake client: %s", err)
}
if d := cmp.Diff(tt.expectedPipelineRun, pipelineRun, ignoreResourceVersion, ignoreTypeMeta, ignoreLastTransitionTime, ignoreStartTime, ignoreFinallyStartTime, cmpopts.EquateEmpty()); d != "" {
t.Errorf("expected PipelineRun was not created. Diff %s", diff.PrintWantGot(d))
}
})
}
}

0 comments on commit bf5ce34

Please sign in to comment.