forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdag_test.go
More file actions
254 lines (234 loc) · 6.87 KB
/
dag_test.go
File metadata and controls
254 lines (234 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// +build e2e
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"context"
"fmt"
"math"
"sort"
"strings"
"testing"
"time"
"github.com/tektoncd/pipeline/test/parse"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
)
const sleepDuration = 15 * time.Second
// TestDAGPipelineRun creates a graph of arbitrary Tasks, then looks at the corresponding
// TaskRun start times to ensure they were run in the order intended, which is:
// |
// pipeline-task-1
// / \
// pipeline-task-2-parallel-1 pipeline-task-2-parallel-2
// \ /
// pipeline-task-3
// |
// pipeline-task-4
func TestDAGPipelineRun(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
echoTask := parse.MustParseTask(t, fmt.Sprintf(`
metadata:
name: echo-task
namespace: %s
spec:
resources:
inputs:
- name: repo
type: git
outputs:
- name: repo
type: git
params:
- name: text
type: string
description: 'The text that should be echoed'
steps:
- image: busybox
script: 'echo $(params["text"])'
- image: busybox
# Sleep for N seconds so that we can check that tasks that
# should be run in parallel have overlap.
script: 'sleep %d'
- image: busybox
script: 'ln -s $(resources.inputs.repo.path) $(resources.outputs.repo.path)'
`, namespace, int(sleepDuration.Seconds())))
if _, err := c.TaskClient.Create(ctx, echoTask, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create echo Task: %s", err)
}
// Create the repo PipelineResource (doesn't really matter which repo we use)
repoResource := parse.MustParsePipelineResource(t, `
metadata:
name: repo
spec:
type: git
params:
- name: Url
value: https://github.com/githubtraining/example-basic
`)
if _, err := c.PipelineResourceClient.Create(ctx, repoResource, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create simple repo PipelineResource: %s", err)
}
// Intentionally declaring Tasks in a mixed up order to ensure the order
// of execution isn't at all dependent on the order they are declared in
pipeline := parse.MustParsePipeline(t, fmt.Sprintf(`
metadata:
name: dag-pipeline
namespace: %s
spec:
resources:
- name: repo
type: git
tasks:
- name: pipeline-task-3
params:
- name: text
value: wow
resources:
inputs:
- from:
- pipeline-task-2-parallel-1
- pipeline-task-2-parallel-2
name: repo
resource: repo
outputs:
- name: repo
resource: repo
taskRef:
name: echo-task
- name: pipeline-task-2-parallel-2
params:
- name: text
value: such parallel
resources:
inputs:
- from:
- pipeline-task-1
name: repo
resource: repo
outputs:
- name: repo
resource: repo
taskRef:
name: echo-task
- name: pipeline-task-4
params:
- name: text
value: very cloud native
resources:
inputs:
- name: repo
resource: repo
outputs:
- name: repo
resource: repo
runAfter:
- pipeline-task-3
taskRef:
name: echo-task
- name: pipeline-task-2-parallel-1
params:
- name: text
value: much graph
resources:
inputs:
- from:
- pipeline-task-1
name: repo
resource: repo
outputs:
- name: repo
resource: repo
taskRef:
name: echo-task
- name: pipeline-task-1
params:
- name: text
value: how to ci/cd?
resources:
inputs:
- name: repo
resource: repo
outputs:
- name: repo
resource: repo
taskRef:
name: echo-task
`, namespace))
if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag-pipeline: %s", err)
}
pipelineRun := parse.MustParsePipelineRun(t, fmt.Sprintf(`
metadata:
name: dag-pipeline-run
namespace: %s
spec:
pipelineRef:
name: dag-pipeline
resources:
- name: repo
resourceRef:
name: repo
`, namespace))
if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create dag-pipeline-run PipelineRun: %s", err)
}
t.Logf("Waiting for DAG pipeline to complete")
if err := WaitForPipelineRunState(ctx, c, "dag-pipeline-run", timeout, PipelineRunSucceed("dag-pipeline-run"), "PipelineRunSuccess"); err != nil {
t.Fatalf("Error waiting for PipelineRun to finish: %s", err)
}
verifyExpectedOrder(ctx, t, c.TaskRunClient)
}
func verifyExpectedOrder(ctx context.Context, t *testing.T, c clientset.TaskRunInterface) {
t.Logf("Verifying order of execution")
taskRunsResp, err := c.List(ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("Couldn't get TaskRuns (so that we could check when they executed): %v", err)
}
taskRuns := taskRunsResp.Items
if len(taskRuns) != 5 {
t.Fatalf("Expected 5 TaskRuns to have executed but got start times for %d TaskRuns", len(taskRuns))
}
sort.Slice(taskRuns, func(i, j int) bool {
it := taskRuns[i].Status.StartTime.Time
jt := taskRuns[j].Status.StartTime.Time
return it.Before(jt)
})
wantPrefixes := []string{
"dag-pipeline-run-pipeline-task-1",
// Could be task-2-parallel-1 or task-2-parallel-2
"dag-pipeline-run-pipeline-task-2-parallel",
"dag-pipeline-run-pipeline-task-2-parallel",
"dag-pipeline-run-pipeline-task-3",
"dag-pipeline-run-pipeline-task-4",
}
for i, wp := range wantPrefixes {
if !strings.HasPrefix(taskRuns[i].Name, wp) {
t.Errorf("Expected task %q to execute first, but %q was first", wp, taskRuns[0].Name)
}
}
// Check that the two tasks that can run in parallel did
s1 := taskRuns[1].Status.StartTime.Time
s2 := taskRuns[2].Status.StartTime.Time
absDiff := time.Duration(math.Abs(float64(s2.Sub(s1))))
if absDiff > sleepDuration {
t.Errorf("Expected parallel tasks to execute more or less at the same time, but they were %v apart", absDiff)
}
}