@@ -39,6 +39,7 @@ import (
39
39
"github.com/google/go-containerregistry/pkg/v1/layout"
40
40
"github.com/google/go-containerregistry/pkg/v1/mutate"
41
41
"github.com/google/go-containerregistry/pkg/v1/tarball"
42
+ "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
42
43
"github.com/tektoncd/pipeline/pkg/pod"
43
44
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun"
44
45
corev1 "k8s.io/api/core/v1"
@@ -49,7 +50,7 @@ import (
49
50
"sigs.k8s.io/yaml"
50
51
)
51
52
52
- var requireFeatureFlags = requireAnyGate (map [string ]string {
53
+ var bundleFeatureFlags = requireAnyGate (map [string ]string {
53
54
"enable-tekton-oci-bundles" : "true" ,
54
55
"enable-api-fields" : "alpha" ,
55
56
})
@@ -62,7 +63,7 @@ var resolverFeatureFlags = requireAnyGate(map[string]string{
62
63
// images.
63
64
func TestTektonBundlesSimpleWorkingExample (t * testing.T ) {
64
65
ctx := context .Background ()
65
- c , namespace := setup (ctx , t , withRegistry , requireFeatureFlags )
66
+ c , namespace := setup (ctx , t , withRegistry , bundleFeatureFlags )
66
67
67
68
t .Parallel ()
68
69
@@ -102,47 +103,7 @@ spec:
102
103
bundle: %s
103
104
` , pipelineName , namespace , taskName , repo ))
104
105
105
- // Write the task and pipeline into an image to the registry in the proper format.
106
- rawTask , err := yaml .Marshal (task )
107
- if err != nil {
108
- t .Fatalf ("Failed to marshal task to yaml: %s" , err )
109
- }
110
-
111
- rawPipeline , err := yaml .Marshal (pipeline )
112
- if err != nil {
113
- t .Fatalf ("Failed to marshal task to yaml: %s" , err )
114
- }
115
-
116
- img := empty .Image
117
- taskLayer , err := tarball .LayerFromReader (bytes .NewBuffer (rawTask ))
118
- if err != nil {
119
- t .Fatalf ("Failed to create oci layer from task: %s" , err )
120
- }
121
- pipelineLayer , err := tarball .LayerFromReader (bytes .NewBuffer (rawPipeline ))
122
- if err != nil {
123
- t .Fatalf ("Failed to create oci layer from pipeline: %s" , err )
124
- }
125
- img , err = mutate .Append (img , mutate.Addendum {
126
- Layer : taskLayer ,
127
- Annotations : map [string ]string {
128
- "dev.tekton.image.name" : taskName ,
129
- "dev.tekton.image.kind" : strings .ToLower (task .Kind ),
130
- "dev.tekton.image.apiVersion" : task .APIVersion ,
131
- },
132
- }, mutate.Addendum {
133
- Layer : pipelineLayer ,
134
- Annotations : map [string ]string {
135
- "dev.tekton.image.name" : pipelineName ,
136
- "dev.tekton.image.kind" : strings .ToLower (pipeline .Kind ),
137
- "dev.tekton.image.apiVersion" : pipeline .APIVersion ,
138
- },
139
- })
140
- if err != nil {
141
- t .Fatalf ("Failed to create an oci image from the task and pipeline layers: %s" , err )
142
- }
143
-
144
- // Publish this image to the in-cluster registry.
145
- publishImg (ctx , t , c , namespace , img , ref )
106
+ setupBundle (ctx , t , c , namespace , repo , task , pipeline )
146
107
147
108
// Now generate a PipelineRun to invoke this pipeline and task.
148
109
pr := parse .MustParsePipelineRun (t , fmt .Sprintf (`
@@ -212,10 +173,6 @@ func TestTektonBundlesResolver(t *testing.T) {
212
173
pipelineName := helpers .ObjectNameForTest (t )
213
174
pipelineRunName := helpers .ObjectNameForTest (t )
214
175
repo := fmt .Sprintf ("%s:5000/tektonbundlesresolver" , getRegistryServiceIP (ctx , t , c , namespace ))
215
- ref , err := name .ParseReference (repo )
216
- if err != nil {
217
- t .Fatalf ("Failed to parse %s as an OCI reference: %s" , repo , err )
218
- }
219
176
220
177
task := parse .MustParseTask (t , fmt .Sprintf (`
221
178
metadata:
@@ -244,47 +201,7 @@ spec:
244
201
value: %s
245
202
` , pipelineName , namespace , repo , taskName ))
246
203
247
- // Write the task and pipeline into an image to the registry in the proper format.
248
- rawTask , err := yaml .Marshal (task )
249
- if err != nil {
250
- t .Fatalf ("Failed to marshal task to yaml: %s" , err )
251
- }
252
-
253
- rawPipeline , err := yaml .Marshal (pipeline )
254
- if err != nil {
255
- t .Fatalf ("Failed to marshal task to yaml: %s" , err )
256
- }
257
-
258
- img := empty .Image
259
- taskLayer , err := tarball .LayerFromReader (bytes .NewBuffer (rawTask ))
260
- if err != nil {
261
- t .Fatalf ("Failed to create oci layer from task: %s" , err )
262
- }
263
- pipelineLayer , err := tarball .LayerFromReader (bytes .NewBuffer (rawPipeline ))
264
- if err != nil {
265
- t .Fatalf ("Failed to create oci layer from pipeline: %s" , err )
266
- }
267
- img , err = mutate .Append (img , mutate.Addendum {
268
- Layer : taskLayer ,
269
- Annotations : map [string ]string {
270
- "dev.tekton.image.name" : taskName ,
271
- "dev.tekton.image.kind" : strings .ToLower (task .Kind ),
272
- "dev.tekton.image.apiVersion" : task .APIVersion ,
273
- },
274
- }, mutate.Addendum {
275
- Layer : pipelineLayer ,
276
- Annotations : map [string ]string {
277
- "dev.tekton.image.name" : pipelineName ,
278
- "dev.tekton.image.kind" : strings .ToLower (pipeline .Kind ),
279
- "dev.tekton.image.apiVersion" : pipeline .APIVersion ,
280
- },
281
- })
282
- if err != nil {
283
- t .Fatalf ("Failed to create an oci image from the task and pipeline layers: %s" , err )
284
- }
285
-
286
- // Publish this image to the in-cluster registry.
287
- publishImg (ctx , t , c , namespace , img , ref )
204
+ setupBundle (ctx , t , c , namespace , repo , task , pipeline )
288
205
289
206
// Now generate a PipelineRun to invoke this pipeline and task.
290
207
pr := parse .MustParsePipelineRun (t , fmt .Sprintf (`
@@ -348,7 +265,7 @@ spec:
348
265
// TestTektonBundlesUsingRegularImage is an integration test which passes a non-Tekton bundle as a task reference.
349
266
func TestTektonBundlesUsingRegularImage (t * testing.T ) {
350
267
ctx := context .Background ()
351
- c , namespace := setup (ctx , t , withRegistry , requireFeatureFlags )
268
+ c , namespace := setup (ctx , t , withRegistry , bundleFeatureFlags )
352
269
353
270
t .Parallel ()
354
271
@@ -377,32 +294,7 @@ spec:
377
294
bundle: registry
378
295
` , pipelineName , namespace , taskName ))
379
296
380
- // Write the pipeline into an image to the registry in the proper format. We don't write the task because we are
381
- // using an non Tekton Bundle.
382
- rawPipeline , err := yaml .Marshal (pipeline )
383
- if err != nil {
384
- t .Fatalf ("Failed to marshal task to yaml: %s" , err )
385
- }
386
-
387
- img := empty .Image
388
- pipelineLayer , err := tarball .LayerFromReader (bytes .NewBuffer (rawPipeline ))
389
- if err != nil {
390
- t .Fatalf ("Failed to create oci layer from pipeline: %s" , err )
391
- }
392
- img , err = mutate .Append (img , mutate.Addendum {
393
- Layer : pipelineLayer ,
394
- Annotations : map [string ]string {
395
- "dev.tekton.image.name" : pipelineName ,
396
- "dev.tekton.image.kind" : strings .ToLower (pipeline .Kind ),
397
- "dev.tekton.image.apiVersion" : pipeline .APIVersion ,
398
- },
399
- })
400
- if err != nil {
401
- t .Fatalf ("Failed to create an oci image from the task and pipeline layers: %s" , err )
402
- }
403
-
404
- // Publish this image to the in-cluster registry.
405
- publishImg (ctx , t , c , namespace , img , ref )
297
+ setupBundle (ctx , t , c , namespace , repo , nil , pipeline )
406
298
407
299
// Now generate a PipelineRun to invoke this pipeline and task.
408
300
pr := parse .MustParsePipelineRun (t , fmt .Sprintf (`
@@ -431,7 +323,7 @@ spec:
431
323
// task reference.
432
324
func TestTektonBundlesUsingImproperFormat (t * testing.T ) {
433
325
ctx := context .Background ()
434
- c , namespace := setup (ctx , t , withRegistry , requireFeatureFlags )
326
+ c , namespace := setup (ctx , t , withRegistry , bundleFeatureFlags )
435
327
436
328
t .Parallel ()
437
329
@@ -671,3 +563,62 @@ func publishImg(ctx context.Context, t *testing.T, c *clients, namespace string,
671
563
}
672
564
}
673
565
}
566
+
567
+ // setupBundle creates an empty image, provides a reference to the fake registry and pushes the
568
+ // bundled task and pipeline within an image into the registry
569
+ func setupBundle (ctx context.Context , t * testing.T , c * clients , namespace , repo string , task * v1beta1.Task , pipeline * v1beta1.Pipeline ) {
570
+ t .Helper ()
571
+ img := empty .Image
572
+ ref , err := name .ParseReference (repo )
573
+ if err != nil {
574
+ t .Fatalf ("Failed to parse %s as an OCI reference: %s" , repo , err )
575
+ }
576
+
577
+ var rawTask , rawPipeline []byte
578
+ var taskLayer , pipelineLayer v1.Layer
579
+ // Write the task and pipeline into an image to the registry in the proper format.
580
+ if task != nil {
581
+ rawTask , err = yaml .Marshal (task )
582
+ if err != nil {
583
+ t .Fatalf ("Failed to marshal task to yaml: %s" , err )
584
+ }
585
+ taskLayer , err = tarball .LayerFromReader (bytes .NewBuffer (rawTask ))
586
+ if err != nil {
587
+ t .Fatalf ("Failed to create oci layer from task: %s" , err )
588
+ }
589
+ img , err = mutate .Append (img , mutate.Addendum {
590
+ Layer : taskLayer ,
591
+ Annotations : map [string ]string {
592
+ "dev.tekton.image.name" : task .Name ,
593
+ "dev.tekton.image.kind" : strings .ToLower (task .Kind ),
594
+ "dev.tekton.image.apiVersion" : task .APIVersion ,
595
+ },
596
+ })
597
+ }
598
+
599
+ if pipeline != nil {
600
+ rawPipeline , err = yaml .Marshal (pipeline )
601
+ if err != nil {
602
+ t .Fatalf ("Failed to marshal task to yaml: %s" , err )
603
+ }
604
+ pipelineLayer , err = tarball .LayerFromReader (bytes .NewBuffer (rawPipeline ))
605
+ if err != nil {
606
+ t .Fatalf ("Failed to create oci layer from pipeline: %s" , err )
607
+ }
608
+ img , err = mutate .Append (img , mutate.Addendum {
609
+ Layer : pipelineLayer ,
610
+ Annotations : map [string ]string {
611
+ "dev.tekton.image.name" : pipeline .Name ,
612
+ "dev.tekton.image.kind" : strings .ToLower (pipeline .Kind ),
613
+ "dev.tekton.image.apiVersion" : pipeline .APIVersion ,
614
+ },
615
+ })
616
+ }
617
+
618
+ if err != nil {
619
+ t .Fatalf ("Failed to create an oci image from the task and pipeline layers: %s" , err )
620
+ }
621
+
622
+ // Publish this image to the in-cluster registry.
623
+ publishImg (ctx , t , c , namespace , img , ref )
624
+ }
0 commit comments