Skip to content

Commit

Permalink
test: add test for matrix params with string value containing variabl…
Browse files Browse the repository at this point in the history
…e reference

fix #8324
  • Loading branch information
l-qing committed Jan 15, 2025
1 parent f18a2b1 commit def2c31
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 7 deletions.
13 changes: 11 additions & 2 deletions pkg/apis/pipeline/v1/matrix_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ func TestPipelineTask_CountCombinations(t *testing.T) {
}{{
name: "combinations count is zero",
matrix: &v1.Matrix{
Params: v1.Params{{}}},
Params: v1.Params{}},
want: 0,
}, {
name: "combinations count is one from one parameter",
Expand Down Expand Up @@ -915,11 +915,20 @@ func TestPipelineTask_CountCombinations(t *testing.T) {
}},
}},
want: 7,
}, {
name: "matrix params with string value containing variable reference",
matrix: &v1.Matrix{
Params: v1.Params{{
Name: "GOARCH", Value: v1.ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}},
}, {
Name: "version", Value: v1.ParamValue{StringVal: "$(tasks.platforms.results.str[*])"}},
}},
want: 3,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if d := cmp.Diff(tt.want, tt.matrix.CountCombinations()); d != "" {
t.Errorf("Matrix.CountCombinations() errors diff %s", diff.PrintWantGot(d))
t.Errorf("%s Matrix.CountCombinations() errors diff %s", tt.name, diff.PrintWantGot(d))
}
})
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8942,8 +8942,14 @@ spec:
script: |
echo "$(params.platform)"
- name: b-task
taskRef:
name: mytask
taskSpec:
params:
- name: platform
steps:
- name: echo
image: alpine
script: |
echo "$(params.platform)"
matrix:
params:
- name: platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3801,10 +3801,10 @@ func TestResolvePipelineRunTask_WithMatrix(t *testing.T) {
name: "task with matrix - whole array results",
pt: pts[2],
want: &ResolvedPipelineTask{
TaskRunNames: nil,
TaskRunNames: []string{"pipelinerun-pipelinetask-with-whole-array-results"},
TaskRuns: nil,
PipelineTask: &pts[2],
ResolvedTask: nil,
ResolvedTask: rtr,
},
pst: pipelineRunState,
}} {
Expand Down
182 changes: 181 additions & 1 deletion test/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
"github.com/tektoncd/pipeline/test/parse"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
Expand Down Expand Up @@ -68,11 +69,14 @@ spec:
default: ""
- name: package
default: ""
results:
- name: str
type: string
steps:
- name: echo
image: mirror.gcr.io/alpine
script: |
echo "$(params.GOARCH) and $(params.version)"
echo -n "$(params.GOARCH) and $(params.version)" | tee $(results.str.path)
`, namespace))

task1withresults := parse.MustParseV1Task(t, fmt.Sprintf(`
Expand Down Expand Up @@ -107,6 +111,22 @@ spec:
echo -n "[\"go1.17\",\"go1.18.1\"]" | tee $(results.versions.path)
`, namespace))

task3printer := parse.MustParseV1Task(t, fmt.Sprintf(`
metadata:
name: printer
namespace: %s
spec:
params:
- name: platform
value: "default-platform"
steps:
- name: produce-a-list-of-versions
image: mirror.gcr.io/bash
script: |
#!/usr/bin/env bash
echo "platform: $(params.platform)"
`, namespace))

if _, err := c.V1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", task.Name, err)
}
Expand All @@ -116,6 +136,9 @@ spec:
if _, err := c.V1TaskClient.Create(ctx, task2withresults, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", task2withresults.Name, err)
}
if _, err := c.V1TaskClient.Create(ctx, task3printer, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", task3printer.Name, err)
}

pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(`
metadata:
Expand Down Expand Up @@ -157,6 +180,13 @@ spec:
params:
- name: GOARCH
value: I-do-not-exist
- name: printer-matrix
taskRef:
name: printer
matrix:
params:
- name: platform
value: $(tasks.matrix-include.results.str[*])
`, helpers.ObjectNameForTest(t), namespace))

pipelineRun := parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
Expand Down Expand Up @@ -208,6 +238,11 @@ spec:
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
Results: []v1.TaskRunResult{{
Name: "str",
Type: v1.ResultsTypeString,
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.17"},
}},
},
},
}, {
Expand Down Expand Up @@ -240,6 +275,11 @@ spec:
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
Results: []v1.TaskRunResult{{
Name: "str",
Type: v1.ResultsTypeString,
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.17"},
}},
},
},
}, {
Expand Down Expand Up @@ -269,6 +309,11 @@ spec:
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
Results: []v1.TaskRunResult{{
Name: "str",
Type: v1.ResultsTypeString,
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.18.1"},
}},
},
},
}, {
Expand Down Expand Up @@ -298,6 +343,11 @@ spec:
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
Results: []v1.TaskRunResult{{
Name: "str",
Type: v1.ResultsTypeString,
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.18.1"},
}},
},
},
}, {
Expand All @@ -319,6 +369,136 @@ spec:
Reason: "Succeeded",
Message: "All Steps have completed executing",
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
Results: []v1.TaskRunResult{{
Name: "str",
Type: v1.ResultsTypeString,
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "I-do-not-exist and "},
}},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-printer-matrix-0",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "platform",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.17"},
}},
ServiceAccountName: "default",
TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-printer-matrix-1",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "platform",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.17"},
}},
ServiceAccountName: "default",
TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-printer-matrix-2",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "platform",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.18.1"},
}},
ServiceAccountName: "default",
TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-printer-matrix-3",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "platform",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.18.1"},
}},
ServiceAccountName: "default",
TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-printer-matrix-4",
},
Spec: v1.TaskRunSpec{
Params: v1.Params{{
Name: "platform",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "I-do-not-exist and "},
}},
ServiceAccountName: "default",
TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
Artifacts: &v1.Artifacts{},
},
Expand Down

0 comments on commit def2c31

Please sign in to comment.