forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtektonbundles_test.go
348 lines (313 loc) · 10.7 KB
/
tektonbundles_test.go
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//go:build e2e
// +build e2e
/*
Copyright 2020 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 (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/parse"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
knativetest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"sigs.k8s.io/yaml"
)
var resolverFeatureFlags = requireAllGates(map[string]string{
"enable-bundles-resolver": "true",
"enable-api-fields": "beta",
})
// TestTektonBundlesResolver is an integration test which tests a simple, working Tekton bundle using OCI
// images using the remote resolution bundles resolver.
func TestTektonBundlesResolver(t *testing.T) {
ctx := context.Background()
c, namespace := setup(ctx, t, withRegistry, resolverFeatureFlags)
t.Parallel()
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)
taskName := helpers.ObjectNameForTest(t)
pipelineName := helpers.ObjectNameForTest(t)
pipelineRunName := helpers.ObjectNameForTest(t)
repo := getRegistryServiceIP(ctx, t, c, namespace) + ":5000/tektonbundlesresolver"
task := parse.MustParseV1beta1Task(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
steps:
- name: hello
image: mirror.gcr.io/alpine
script: 'echo Hello'
`, taskName, namespace))
pipeline := parse.MustParseV1beta1Pipeline(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
tasks:
- name: hello-world
taskRef:
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
`, pipelineName, namespace, repo, taskName))
setupBundle(ctx, t, c, namespace, repo, task, pipeline)
// Now generate a PipelineRun to invoke this pipeline and task.
pr := parse.MustParseV1PipelineRun(t, fmt.Sprintf(`
metadata:
name: %s
spec:
pipelineRef:
resolver: bundles
params:
- name: bundle
value: %s
- name: name
value: %s
- name: kind
value: pipeline
`, pipelineRunName, repo, pipelineName))
if _, err := c.V1PipelineRunClient.Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun: %s", err)
}
t.Logf("Waiting for PipelineRun in namespace %s to finish", namespace)
if err := WaitForPipelineRunState(ctx, c, pipelineRunName, timeout, PipelineRunSucceed(pipelineRunName), "PipelineRunCompleted", v1Version); err != nil {
t.Errorf("Error waiting for PipelineRun to finish with error: %s", err)
}
trs, err := c.V1TaskRunClient.List(ctx, metav1.ListOptions{})
if err != nil {
t.Errorf("Error retrieving taskrun: %s", err)
}
if len(trs.Items) != 1 {
t.Fatalf("Expected 1 TaskRun but found %d", len(trs.Items))
}
tr := trs.Items[0]
if tr.Status.GetCondition(apis.ConditionSucceeded).IsFalse() {
t.Errorf("Expected TaskRun to succeed but instead found condition: %s", tr.Status.GetCondition(apis.ConditionSucceeded))
}
if tr.Status.PodName == "" {
t.Fatal("Error getting a PodName (empty)")
}
p, err := c.KubeClient.CoreV1().Pods(namespace).Get(ctx, tr.Status.PodName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace)
}
for _, stat := range p.Status.ContainerStatuses {
if strings.Contains(stat.Name, "step-hello") {
req := c.KubeClient.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name})
logContent, err := req.Do(ctx).Raw()
if err != nil {
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace)
}
if !strings.Contains(string(logContent), "Hello") {
t.Fatalf("Expected logs to say hello but received %v", logContent)
}
}
}
}
func tarImageInOCIFormat(namespace string, img v1.Image) ([]byte, error) {
// Write the image in the OCI layout and then tar it up.
dir, err := os.MkdirTemp(os.TempDir(), namespace)
if err != nil {
return nil, err
}
p, err := layout.Write(dir, empty.Index)
if err != nil {
return nil, err
}
if err := p.AppendImage(img); err != nil {
return nil, err
}
// Now that the layout is correct, package this up as a tarball.
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Generate the initial tar header.
header, err := tar.FileInfoHeader(info, path)
if err != nil {
return err
}
// Rewrite the filename with a relative path to the root dir.
header.Name = strings.Replace(path, dir, "", 1)
if err := tw.WriteHeader(header); err != nil {
return err
}
// If not a dir, write file content as is.
if !info.IsDir() {
data, err := os.Open(path)
if err != nil {
return err
}
if _, err := io.Copy(tw, data); err != nil {
return err
}
}
return nil
}); err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, err
}
// Pull out the tar bundle into a bytes object.
return io.ReadAll(&buf)
}
// publishImg will generate a Pod that runs in the namespace to publish an OCI compliant image into the local registry
// that runs in the cluster. We can't speak to the in-cluster registry from the test so we need to run a Pod to do it
// for us.
func publishImg(ctx context.Context, t *testing.T, c *clients, namespace string, img v1.Image, ref name.Reference) {
t.Helper()
podName := "publish-tekton-bundle"
tb, err := tarImageInOCIFormat(namespace, img)
if err != nil {
t.Fatalf("Failed to create OCI tar bundle: %s", err)
}
// Create a configmap to contain the tarball which we will mount in the pod.
cmName := namespace + "uploadimage-cm"
if _, err = c.KubeClient.CoreV1().ConfigMaps(namespace).Create(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: cmName},
BinaryData: map[string][]byte{
"image.tar": tb,
},
}, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create configmap to upload image: %s", err)
}
po, err := c.KubeClient.CoreV1().Pods(namespace).Create(ctx, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
GenerateName: podName,
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{{
Name: "cm",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{LocalObjectReference: corev1.LocalObjectReference{Name: cmName}},
},
}, {
Name: "scratch",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}},
InitContainers: []corev1.Container{{
Name: "untar",
Image: "busybox",
Command: []string{"/bin/sh", "-c"},
Args: []string{"mkdir -p /var/image && tar xvf /var/cm/image.tar -C /var/image"},
VolumeMounts: []corev1.VolumeMount{{
Name: "cm",
MountPath: "/var/cm",
}, {
Name: "scratch",
MountPath: "/var/image",
}},
}},
Containers: []corev1.Container{{
Name: "skopeo",
Image: "gcr.io/tekton-releases/dogfooding/skopeo:latest",
WorkingDir: "/var",
Command: []string{"/bin/sh", "-c"},
Args: []string{"skopeo copy --dest-tls-verify=false oci:image docker://" + ref.String()},
VolumeMounts: []corev1.VolumeMount{{
Name: "scratch",
MountPath: "/var/image",
}},
}},
RestartPolicy: corev1.RestartPolicyNever,
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create the skopeo pod: %v", err)
}
if err := WaitForPodState(ctx, c, po.Name, namespace, func(pod *corev1.Pod) (bool, error) {
return pod.Status.Phase == "Succeeded", nil
}, "PodContainersTerminated"); err != nil {
req := c.KubeClient.CoreV1().Pods(namespace).GetLogs(po.GetName(), &corev1.PodLogOptions{Container: "skopeo"})
logs, err := req.DoRaw(ctx)
if err != nil {
t.Fatalf("Error waiting for Pod %q to terminate: %v", podName, err)
} else {
t.Fatalf("Failed to create image. Pod logs are: \n%s", string(logs))
}
}
}
// setupBundle creates an empty image, provides a reference to the fake registry and pushes the
// bundled task and pipeline within an image into the registry
func setupBundle(ctx context.Context, t *testing.T, c *clients, namespace, repo string, task *v1beta1.Task, pipeline *v1beta1.Pipeline) {
t.Helper()
img := empty.Image
ref, err := name.ParseReference(repo)
if err != nil {
t.Fatalf("Failed to parse %s as an OCI reference: %s", repo, err)
}
var rawTask, rawPipeline []byte
var taskLayer, pipelineLayer v1.Layer
// Write the task and pipeline into an image to the registry in the proper format.
if task != nil {
rawTask, err = yaml.Marshal(task)
if err != nil {
t.Fatalf("Failed to marshal task to yaml: %s", err)
}
taskLayer, err = tarball.LayerFromReader(bytes.NewBuffer(rawTask))
if err != nil {
t.Fatalf("Failed to create oci layer from task: %s", err)
}
img, err = mutate.Append(img, mutate.Addendum{
Layer: taskLayer,
Annotations: map[string]string{
"dev.tekton.image.name": task.Name,
"dev.tekton.image.kind": strings.ToLower(task.Kind),
"dev.tekton.image.apiVersion": task.APIVersion,
},
})
}
if pipeline != nil {
rawPipeline, err = yaml.Marshal(pipeline)
if err != nil {
t.Fatalf("Failed to marshal task to yaml: %s", err)
}
pipelineLayer, err = tarball.LayerFromReader(bytes.NewBuffer(rawPipeline))
if err != nil {
t.Fatalf("Failed to create oci layer from pipeline: %s", err)
}
img, err = mutate.Append(img, mutate.Addendum{
Layer: pipelineLayer,
Annotations: map[string]string{
"dev.tekton.image.name": pipeline.Name,
"dev.tekton.image.kind": strings.ToLower(pipeline.Kind),
"dev.tekton.image.apiVersion": pipeline.APIVersion,
},
})
}
if err != nil {
t.Fatalf("Failed to create an oci image from the task and pipeline layers: %s", err)
}
// Publish this image to the in-cluster registry.
publishImg(ctx, t, c, namespace, img, ref)
}