forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatus_test.go
More file actions
299 lines (269 loc) · 10.1 KB
/
status_test.go
File metadata and controls
299 lines (269 loc) · 10.1 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//go:build e2e
// +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"
"crypto/sha256"
"encoding/hex"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
"github.com/tektoncd/pipeline/test/parse"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"sigs.k8s.io/yaml"
)
var (
ignoreFeatureFlags = cmpopts.IgnoreFields(v1beta1.Provenance{}, "FeatureFlags")
)
// TestTaskRunPipelineRunStatus is an integration test that will
// verify a very simple "hello world" TaskRun and PipelineRun failure
// execution lead to the correct TaskRun status.
func TestTaskRunPipelineRunStatus(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)
t.Logf("Creating Task and TaskRun in namespace %s", namespace)
task := parse.MustParseV1beta1Task(t, fmt.Sprintf(`
metadata:
name: %s
spec:
steps:
- name: foo
image: busybox
command: ['ls', '-la']`, helpers.ObjectNameForTest(t)))
if _, err := c.V1beta1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task: %s", err)
}
taskRun := parse.MustParseV1beta1TaskRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
taskRef:
name: %s
serviceAccountName: inexistent`, helpers.ObjectNameForTest(t), task.Name))
if _, err := c.V1beta1TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create TaskRun: %s", err)
}
t.Logf("Waiting for TaskRun in namespace %s to fail", namespace)
if err := WaitForTaskRunState(ctx, c, taskRun.Name, TaskRunFailed(taskRun.Name), "BuildValidationFailed", v1beta1Version); err != nil {
t.Errorf("Error waiting for TaskRun to finish: %s", err)
}
pipeline := parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
spec:
tasks:
- name: foo
taskRef:
name: %s`, helpers.ObjectNameForTest(t), task.Name))
if _, err := c.V1beta1PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", pipeline.Name, err)
}
pipelineRun := parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
name: %s
serviceAccountName: inexistent`, helpers.ObjectNameForTest(t), pipeline.Name))
if _, err := c.V1beta1PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun `%s`: %s", pipelineRun.Name, err)
}
t.Logf("Waiting for PipelineRun in namespace %s to fail", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRun.Name, timeout, PipelineRunFailed(pipelineRun.Name), "BuildValidationFailed", v1beta1Version); err != nil {
t.Errorf("Error waiting for TaskRun to finish: %s", err)
}
}
// TestProvenanceFieldInPipelineRunTaskRunStatus is an integration test that will
// verify if the provenance field in TaskRun and PipelineRun Status is populated
// with correct data.
// [Setup]: PipelineRun refers to a remote/in-cluster pipeline that will be resolved
// by cluster resolver, and the in-cluster pipeline uses a remote/in-cluster task that
// will also be resolved by cluster resolver.
// [Expectation]: PipelineRun status should contain the provenance about the remote pipeline
// i.e. refSource info, and the child TaskRun status should contain the provnenace
// about the remote task i.e. refSource info .
func TestProvenanceFieldInPipelineRunTaskRunStatus(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c, namespace := setup(ctx, t)
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
// example task
taskName := helpers.ObjectNameForTest(t)
exampleTask, err := c.V1beta1TaskClient.Create(ctx, getExampleTask(t, taskName, namespace), metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create Task `%s`: %s", taskName, err)
}
taskSpec, err := yaml.Marshal(exampleTask.Spec)
if err != nil {
t.Fatalf("couldn't marshal task spec: %v", err)
}
expectedTaskRunProvenance := &v1beta1.Provenance{
ConfigSource: &v1beta1.ConfigSource{
URI: fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s@%s", v1beta1.SchemeGroupVersion.String(), namespace, "task", exampleTask.Name, exampleTask.UID),
Digest: map[string]string{"sha256": sha256CheckSum(taskSpec)},
},
RefSource: &v1beta1.RefSource{
URI: fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s@%s", v1beta1.SchemeGroupVersion.String(), namespace, "task", exampleTask.Name, exampleTask.UID),
Digest: map[string]string{"sha256": sha256CheckSum(taskSpec)},
},
}
// example pipeline
pipelineName := helpers.ObjectNameForTest(t)
examplePipeline, err := c.V1beta1PipelineClient.Create(ctx, getExamplePipeline(t, pipelineName, taskName, namespace), metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", pipelineName, err)
}
pipelineSpec, err := yaml.Marshal(examplePipeline.Spec)
if err != nil {
t.Fatalf("couldn't marshal pipeline spec: %v", err)
}
expectedPipelineRunProvenance := &v1beta1.Provenance{
ConfigSource: &v1beta1.ConfigSource{
URI: fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s@%s", v1beta1.SchemeGroupVersion.String(), namespace, "pipeline", examplePipeline.Name, examplePipeline.UID),
Digest: map[string]string{"sha256": sha256CheckSum(pipelineSpec)},
},
RefSource: &v1beta1.RefSource{
URI: fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s@%s", v1beta1.SchemeGroupVersion.String(), namespace, "pipeline", examplePipeline.Name, examplePipeline.UID),
Digest: map[string]string{"sha256": sha256CheckSum(pipelineSpec)},
},
FeatureFlags: &config.FeatureFlags{
EnableAPIFields: config.DefaultEnableAPIFields,
},
}
// pipelinerun
prName := helpers.ObjectNameForTest(t)
if _, err := c.V1beta1PipelineRunClient.Create(ctx, getExamplePipelineRun(t, prName, pipelineName, namespace), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err)
}
t.Logf("Waiting for PipelineRun %s in namespace %s to complete", prName, namespace)
if err := WaitForPipelineRunState(ctx, c, prName, timeout, PipelineRunSucceed(prName), "PipelineRunSuccess", v1beta1Version); err != nil {
t.Fatalf("Error waiting for PipelineRun %s to finish: %s", prName, err)
}
// Get the updated status of the PipelineRun.
pr, err := c.V1beta1PipelineRunClient.Get(ctx, prName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get PipelineRun %q after it completed: %v", prName, err)
}
// check the provenance field in the PipelineRun status
if d := cmp.Diff(expectedPipelineRunProvenance, pr.Status.Provenance, ignoreFeatureFlags); d != "" {
t.Errorf("provenance did not match: %s", diff.PrintWantGot(d))
}
// ensure that FeatureFlags is not nil
if pr.Status.Provenance.FeatureFlags == nil {
t.Error("Expected to get featureflags but got nil")
}
// Get the TaskRun name.
var taskRunName string
for _, cr := range pr.Status.ChildReferences {
if cr.Kind == "TaskRun" {
taskRunName = cr.Name
}
}
if taskRunName == "" {
t.Fatal("PipelineRun does not have expected TaskRun in .status.childReferences")
}
t.Logf("Waiting for TaskRun %s in namespace %s to complete", taskRunName, namespace)
if err := WaitForTaskRunState(ctx, c, taskRunName, TaskRunSucceed(taskRunName), "TaskRunSuccess", v1beta1Version); err != nil {
t.Fatalf("Error waiting for TaskRun %s to finish: %s", taskRunName, err)
}
// Get the TaskRun.
taskRun, err := c.V1beta1TaskRunClient.Get(ctx, taskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get TaskRun %q: %v", taskRunName, err)
}
// check the provenance field in the PipelineRun status
if d := cmp.Diff(expectedTaskRunProvenance, taskRun.Status.Provenance, ignoreFeatureFlags); d != "" {
t.Errorf("provenance did not match: %s", diff.PrintWantGot(d))
}
// ensure that FeatureFlags is not nil
if taskRun.Status.Provenance.FeatureFlags == nil {
t.Error("Expected to get featureflags but got nil")
}
}
func getExampleTask(t *testing.T, taskName, namespace string) *v1beta1.Task {
t.Helper()
return parse.MustParseV1beta1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
params:
- name: HELLO
default: "hello world!"
steps:
- image: ubuntu
script: |
#!/usr/bin/env bash
echo "$(params.HELLO)"
`, taskName, namespace))
}
func getExamplePipeline(t *testing.T, pipelineName, taskName, namespace string) *v1beta1.Pipeline {
t.Helper()
return parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: task1
taskRef:
resolver: cluster
params:
- name: kind
value: task
- name: name
value: %s
- name: namespace
value: %s
`, pipelineName, namespace, taskName, namespace))
}
func getExamplePipelineRun(t *testing.T, prName, pipelineName, namespace string) *v1beta1.PipelineRun {
t.Helper()
return parse.MustParseV1beta1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
pipelineRef:
resolver: cluster
params:
- name: kind
value: pipeline
- name: name
value: %s
- name: namespace
value: %s
`, prName, namespace, pipelineName, namespace))
}
func sha256CheckSum(input []byte) string {
h := sha256.New()
h.Write(input)
return hex.EncodeToString(h.Sum(nil))
}