Skip to content

Commit b393468

Browse files
committed
test: add multi-PipelineRun test for relative TaskRefs
Add a unit test that verifies various forms of relative TaskRefs are handled correctly, and that no conflicts occur when assembling URLs if multiple PipelineRuns (each referencing a remote Pipeline with relative TaskRefs) are triggered by the same event. This patch adapts the current unit test implementation to support multiple expected PipelineRuns, and introduces a small wrapper function to save some lines.
1 parent 199e049 commit b393468

File tree

4 files changed

+204
-12
lines changed

4 files changed

+204
-12
lines changed

pkg/resolve/remote_test.go

Lines changed: 133 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,62 @@ func TestRemote(t *testing.T) {
7575
pipelinewithTaskRefYamlB, err := yaml.Marshal(pipelinewithTaskRef)
7676
assert.NilError(t, err)
7777

78+
pipelineRelativeTaskRef := []tektonv1.PipelineTask{
79+
{
80+
Name: remoteTaskName + "-a",
81+
TaskRef: &tektonv1.TaskRef{
82+
Name: remoteTaskName + "-a",
83+
},
84+
},
85+
{
86+
Name: remoteTaskName + "-b",
87+
TaskRef: &tektonv1.TaskRef{
88+
Name: remoteTaskName + "-b",
89+
},
90+
},
91+
{
92+
Name: remoteTaskName + "-c",
93+
TaskRef: &tektonv1.TaskRef{
94+
Name: remoteTaskName + "-c",
95+
},
96+
},
97+
{
98+
Name: remoteTaskName + "-d",
99+
TaskRef: &tektonv1.TaskRef{
100+
Name: remoteTaskName + "-d",
101+
},
102+
},
103+
}
104+
pipelineWithRelativeTaskRef := ttkn.MakePipeline(remotePipelineName, pipelineRelativeTaskRef[:3], map[string]string{
105+
apipac.Task: "./" + remoteTaskName + "-a",
106+
apipac.Task + "-1": "../" + remoteTaskName + "-b",
107+
apipac.Task + "-2": "../../../../" + remoteTaskName + "-c",
108+
})
109+
110+
pipelineWithRelativeTaskRefYamlB, err := yaml.Marshal(pipelineWithRelativeTaskRef)
111+
assert.NilError(t, err)
112+
113+
pipelineWithRelativeTaskRef1 := ttkn.MakePipeline(remotePipelineName, pipelineRelativeTaskRef[1:], map[string]string{
114+
apipac.Task: remoteTaskName + "-b",
115+
apipac.Task + "-1": "utils/" + remoteTaskName + "-c",
116+
apipac.Task + "-2": " " + remoteTaskName + "-d",
117+
})
118+
119+
pipelineWithRelativeTaskRefYamlB1, err := yaml.Marshal(pipelineWithRelativeTaskRef1)
120+
assert.NilError(t, err)
121+
122+
singleRelativeTaskBa, err := ttkn.MakeTaskB(remoteTaskName+"-a", taskFromPipelineSpec)
123+
assert.NilError(t, err)
124+
125+
singleRelativeTaskBb, err := ttkn.MakeTaskB(remoteTaskName+"-b", taskFromPipelineSpec)
126+
assert.NilError(t, err)
127+
128+
singleRelativeTaskBc, err := ttkn.MakeTaskB(remoteTaskName+"-c", taskFromPipelineSpec)
129+
assert.NilError(t, err)
130+
131+
singleRelativeTaskBd, err := ttkn.MakeTaskB(remoteTaskName+"-d", taskFromPipelineSpec)
132+
assert.NilError(t, err)
133+
78134
singleTask := ttkn.MakeTask(remoteTaskName, taskFromPipelineSpec)
79135
singleTaskB, err := yaml.Marshal(singleTask)
80136
assert.NilError(t, err)
@@ -92,7 +148,7 @@ func TestRemote(t *testing.T) {
92148
remoteURLS map[string]map[string]string
93149
expectedLogsSnippets []string
94150
expectedTaskSpec tektonv1.TaskSpec
95-
expectedPipelineRun string
151+
expectedPipelineRun []string
96152
noPipelineRun bool
97153
}{
98154
{
@@ -127,7 +183,66 @@ func TestRemote(t *testing.T) {
127183
fmt.Sprintf("successfully fetched %s from remote https url", remotePipelineURL),
128184
fmt.Sprintf("successfully fetched %s from remote https url", remoteTaskURL),
129185
},
130-
expectedPipelineRun: "remote-pipeline-with-remote-task-from-pipeline.yaml",
186+
expectedPipelineRun: []string{"remote-pipeline-with-remote-task-from-pipeline.yaml"},
187+
},
188+
{
189+
name: "remote pipelines with relative tasks",
190+
pipelineruns: []*tektonv1.PipelineRun{
191+
ttkn.MakePR(randomPipelineRunName, map[string]string{
192+
apipac.Pipeline: remotePipelineURL,
193+
},
194+
tektonv1.PipelineRunSpec{
195+
PipelineRef: &tektonv1.PipelineRef{
196+
Name: remotePipelineName,
197+
},
198+
},
199+
),
200+
ttkn.MakePR(randomPipelineRunName, map[string]string{
201+
apipac.Pipeline: remotePipelineURL + "-1",
202+
},
203+
tektonv1.PipelineRunSpec{
204+
PipelineRef: &tektonv1.PipelineRef{
205+
Name: remotePipelineName,
206+
},
207+
},
208+
),
209+
},
210+
remoteURLS: map[string]map[string]string{
211+
remotePipelineURL: {
212+
"body": string(pipelineWithRelativeTaskRefYamlB),
213+
"code": "200",
214+
},
215+
remotePipelineURL + "-1": {
216+
"body": string(pipelineWithRelativeTaskRefYamlB1),
217+
"code": "200",
218+
},
219+
remoteTaskURL + "-a": {
220+
"body": string(singleRelativeTaskBa),
221+
"code": "200",
222+
},
223+
remoteTaskURL + "-b": {
224+
"body": string(singleRelativeTaskBb),
225+
"code": "200",
226+
},
227+
remoteTaskURL + "-c": {
228+
"body": string(singleRelativeTaskBc),
229+
"code": "200",
230+
},
231+
"http://remote/utils/remote-task-c": {
232+
"body": string(singleRelativeTaskBc),
233+
"code": "200",
234+
},
235+
remoteTaskURL + "-d": {
236+
"body": string(singleRelativeTaskBd),
237+
"code": "200",
238+
},
239+
},
240+
expectedTaskSpec: taskFromPipelineSpec,
241+
expectedLogsSnippets: []string{},
242+
expectedPipelineRun: []string{
243+
"remote-pipeline-with-relative-tasks.yaml",
244+
"remote-pipeline-with-relative-tasks-1.yaml",
245+
},
131246
},
132247
{
133248
name: "remote pipeline with remote task in pipeline overridden from pipelinerun",
@@ -162,7 +277,7 @@ func TestRemote(t *testing.T) {
162277
fmt.Sprintf("successfully fetched %s from remote https url", remotePipelineURL),
163278
fmt.Sprintf("successfully fetched %s from remote https url", taskFromPipelineRunURL),
164279
},
165-
expectedPipelineRun: "remote-pipeline-with-remote-task-from-pipelinerun.yaml",
280+
expectedPipelineRun: []string{"remote-pipeline-with-remote-task-from-pipelinerun.yaml"},
166281
},
167282
{
168283
name: "remote pipelinerun no annotations",
@@ -222,7 +337,7 @@ func TestRemote(t *testing.T) {
222337
fmt.Sprintf("successfully fetched %s from remote https url", remotePipelineURL),
223338
fmt.Sprintf("successfully fetched %s from remote https url", remoteTaskURL),
224339
},
225-
expectedPipelineRun: "skip-fetching-multiple-tasks-of-the-same-name-from-pipelinerun-annotations-and-pipeline-annotation.yaml",
340+
expectedPipelineRun: []string{"skip-fetching-multiple-tasks-of-the-same-name-from-pipelinerun-annotations-and-pipeline-annotation.yaml"},
226341
},
227342
{
228343
name: "skip fetching multiple tasks of the same name from pipelinerun annotations and tektondir",
@@ -258,7 +373,7 @@ func TestRemote(t *testing.T) {
258373
fmt.Sprintf("skipping remote task %s as already fetched task %s for pipelinerun %s", remoteTaskURL, remoteTaskName, randomPipelineRunName),
259374
fmt.Sprintf("overriding task %s coming from .tekton directory by an annotation task for pipelinerun %s", remoteTaskName, randomPipelineRunName),
260375
},
261-
expectedPipelineRun: "skip-fetching-multiple-tasks-of-the-same-name-from-pipelinerun-annotations-and-tektondir.yaml",
376+
expectedPipelineRun: []string{"skip-fetching-multiple-tasks-of-the-same-name-from-pipelinerun-annotations-and-tektondir.yaml"},
262377
},
263378
{
264379
name: "skip fetching multiple pipelines of the same name from pipelinerun annotations and tektondir",
@@ -293,7 +408,7 @@ func TestRemote(t *testing.T) {
293408
fmt.Sprintf("successfully fetched %s from remote https url", remoteTaskURL),
294409
fmt.Sprintf("skipping remote task %s as already fetched task %s for pipelinerun %s", remoteTaskURL, remoteTaskName, randomPipelineRunName),
295410
},
296-
expectedPipelineRun: "skip-fetching-multiple-pipelines-of-the-same-name-from-pipelinerun-annotations-and-tektondir.yaml",
411+
expectedPipelineRun: []string{"skip-fetching-multiple-pipelines-of-the-same-name-from-pipelinerun-annotations-and-tektondir.yaml"},
297412
},
298413
}
299414
for _, tt := range tests {
@@ -334,12 +449,18 @@ func TestRemote(t *testing.T) {
334449
assert.Assert(t, len(ret) == 0, "not expecting any pipelinerun")
335450
return
336451
}
337-
expectedData, err := os.ReadFile("testdata/" + tt.expectedPipelineRun)
338-
assert.NilError(t, err)
339-
pipelineRun := &tektonv1.PipelineRun{}
340-
err = yaml.Unmarshal(expectedData, pipelineRun)
341-
assert.NilError(t, err)
342-
assert.DeepEqual(t, pipelineRun, ret[0])
452+
for i, pr := range ret {
453+
if len(tt.expectedPipelineRun) < len(ret) {
454+
assert.NilError(t, fmt.Errorf("insufficient amount of expectedPipelineRuns was provided, got %d but want %d; or set noPipelineRun to true",
455+
len(tt.expectedPipelineRun), len(ret)))
456+
}
457+
expectedData, err := os.ReadFile("testdata/" + tt.expectedPipelineRun[i])
458+
assert.NilError(t, err)
459+
pipelineRun := &tektonv1.PipelineRun{}
460+
err = yaml.Unmarshal(expectedData, pipelineRun)
461+
assert.NilError(t, err)
462+
assert.DeepEqual(t, pipelineRun, pr)
463+
}
343464
})
344465
}
345466
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: tekton.dev/v1
2+
kind: PipelineRun
3+
metadata:
4+
annotations:
5+
pipelinesascode.tekton.dev/pipeline: http://remote/remote-pipeline-1
6+
generateName: pipelinerun-abc-
7+
spec:
8+
pipelineSpec:
9+
tasks:
10+
- name: remote-task-b
11+
taskSpec:
12+
steps:
13+
- name: step1
14+
image: scratch
15+
command:
16+
- "true"
17+
- name: remote-task-c
18+
taskSpec:
19+
steps:
20+
- name: step1
21+
image: scratch
22+
command:
23+
- "true"
24+
- name: remote-task-d
25+
taskSpec:
26+
steps:
27+
- name: step1
28+
image: scratch
29+
command:
30+
- "true"
31+
finally: []
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: tekton.dev/v1
2+
kind: PipelineRun
3+
metadata:
4+
annotations:
5+
pipelinesascode.tekton.dev/pipeline: http://remote/remote-pipeline
6+
generateName: pipelinerun-abc-
7+
spec:
8+
pipelineSpec:
9+
tasks:
10+
- name: remote-task-a
11+
taskSpec:
12+
steps:
13+
- name: step1
14+
image: scratch
15+
command:
16+
- "true"
17+
- name: remote-task-b
18+
taskSpec:
19+
steps:
20+
- name: step1
21+
image: scratch
22+
command:
23+
- "true"
24+
- name: remote-task-c
25+
taskSpec:
26+
steps:
27+
- name: step1
28+
image: scratch
29+
command:
30+
- "true"
31+
finally: []

pkg/test/tekton/genz.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
knativeapi "knative.dev/pkg/apis"
1111
knativeduckv1 "knative.dev/pkg/apis/duck/v1"
12+
"sigs.k8s.io/yaml"
1213
)
1314

1415
func MakePrTrStatus(ptaskname, displayName string, completionmn int) *tektonv1.PipelineRunTaskRunStatus {
@@ -149,6 +150,14 @@ func MakeTask(name string, taskSpec tektonv1.TaskSpec) *tektonv1.Task {
149150
}
150151
}
151152

153+
func MakeTaskB(name string, taskSpec tektonv1.TaskSpec) ([]byte, error) {
154+
objB, err := yaml.Marshal(MakeTask(name, taskSpec))
155+
if err != nil {
156+
return nil, err
157+
}
158+
return objB, nil
159+
}
160+
152161
func MakeTaskRunCompletion(clock *clockwork.FakeClock, name, namespace, runstatus string, annotation map[string]string, taskStatus tektonv1.TaskRunStatusFields, conditions knativeduckv1.Conditions, timeshift int) *tektonv1.TaskRun {
153162
starttime := time.Duration((timeshift - 5*-1) * int(time.Minute))
154163
endtime := time.Duration((timeshift * -1) * int(time.Minute))

0 commit comments

Comments
 (0)