@@ -20,6 +20,7 @@ package test
2020
2121import (
2222 "context"
23+ "strings"
2324 "testing"
2425 "time"
2526
@@ -29,6 +30,7 @@ import (
2930 "github.com/tektoncd/pipeline/test/diff"
3031 corev1 "k8s.io/api/core/v1"
3132 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+ "k8s.io/apimachinery/pkg/runtime"
3234 "knative.dev/pkg/apis"
3335 duckv1 "knative.dev/pkg/apis/duck/v1"
3436 knativetest "knative.dev/pkg/test"
@@ -51,7 +53,8 @@ func TestCustomTask(t *testing.T) {
5153 c , namespace := setup (ctx , t , requireAnyGate (supportedFeatureGates ))
5254 knativetest .CleanupOnInterrupt (func () { tearDown (ctx , t , c , namespace ) }, t .Logf )
5355 defer tearDown (ctx , t , c , namespace )
54-
56+ customTaskRawSpec := []byte (`{"field1":123,"field2":"value"}` )
57+ metadataLabel := map [string ]string {"test-label" : "test" }
5558 // Create a PipelineRun that runs a Custom Task.
5659 pipelineRunName := "custom-task-pipeline"
5760 if _ , err := c .PipelineRunClient .Create (
@@ -61,43 +64,49 @@ func TestCustomTask(t *testing.T) {
6164 Spec : v1beta1.PipelineRunSpec {
6265 PipelineSpec : & v1beta1.PipelineSpec {
6366 Tasks : []v1beta1.PipelineTask {{
64- Name : "custom-task" ,
67+ Name : "custom-task-ref " ,
6568 TaskRef : & v1beta1.TaskRef {
6669 APIVersion : apiVersion ,
6770 Kind : kind ,
6871 },
6972 }, {
7073 Name : "custom-task-spec" ,
7174 TaskSpec : & v1beta1.EmbeddedTask {
72- EmbeddedSpec : v1beta1.EmbeddedSpec {
73- TypeMeta : runtime.TypeMeta {
74- APIVersion : apiVersion ,
75- Kind : kind ,
76- },
77- Spec : runtime.RawExtension {
78- Raw : customTaskRawSpec ,
79- },
75+ TypeMeta : runtime.TypeMeta {
76+ APIVersion : apiVersion ,
77+ Kind : kind ,
78+ },
79+ Metadata : v1beta1.PipelineTaskMetadata {Labels : metadataLabel },
80+ Spec : runtime.RawExtension {
81+ Raw : customTaskRawSpec ,
8082 },
8183 },
8284 }, {
8385 Name : "result-consumer" ,
8486 Params : []v1beta1.Param {{
85- Name : "input-result-from-custom-task" , Value : * v1beta1 .NewArrayOrString ("$(tasks.custom-task.results.runResult)" ),
87+ Name : "input-result-from-custom-task-ref" , Value : * v1beta1 .NewArrayOrString ("$(tasks.custom-task-ref.results.runResult)" ),
88+ }, {
89+ Name : "input-result-from-custom-task-spec" , Value : * v1beta1 .NewArrayOrString ("$(tasks.custom-task-spec.results.runResult)" ),
8690 }},
8791 TaskSpec : & v1beta1.EmbeddedTask {TaskSpec : v1beta1.TaskSpec {
8892 Params : []v1beta1.ParamSpec {{
89- Name : "input-result-from-custom-task" , Type : v1beta1 .ParamTypeString ,
93+ Name : "input-result-from-custom-task-ref" , Type : v1beta1 .ParamTypeString ,
94+ }, {
95+ Name : "input-result-from-custom-task-spec" , Type : v1beta1 .ParamTypeString ,
9096 }},
9197 Steps : []v1beta1.Step {{Container : corev1.Container {
9298 Image : "ubuntu" ,
9399 Command : []string {"/bin/bash" },
94- Args : []string {"-c" , "echo $(input-result-from-custom-task)" },
100+ Args : []string {"-c" , "echo $(input-result-from-custom-task-ref) $(input-result-from-custom-task-spec )" },
95101 }}},
96102 }},
97103 }},
98104 Results : []v1beta1.PipelineResult {{
99- Name : "prResult" ,
100- Value : "$(tasks.custom-task.results.runResult)" ,
105+ Name : "prResult-ref" ,
106+ Value : "$(tasks.custom-task-ref.results.runResult)" ,
107+ }, {
108+ Name : "prResult-spec" ,
109+ Value : "$(tasks.custom-task-spec.results.runResult)" ,
101110 }},
102111 },
103112 },
@@ -118,53 +127,58 @@ func TestCustomTask(t *testing.T) {
118127 }
119128
120129 // Get the Run name.
121- if len (pr .Status .Runs ) != 1 {
122- t .Fatalf ("PipelineRun had unexpected .status.runs; got %d, want 1" , len (pr .Status .Runs ))
123- }
124- var runName string
125- for k := range pr .Status .Runs {
126- runName = k
127- break
128- }
129-
130- // Get the Run.
131- r , err := c .RunClient .Get (ctx , runName , metav1.GetOptions {})
132- if err != nil {
133- t .Fatalf ("Failed to get Run %q: %v" , runName , err )
134- }
135- if r .IsDone () {
136- t .Fatalf ("Run unexpectedly done: %v" , r .Status .GetCondition (apis .ConditionSucceeded ))
137- }
138-
139- // Simulate a Custom Task controller updating the Run to done/successful.
140- r .Status = v1alpha1.RunStatus {
141- Status : duckv1.Status {
142- Conditions : duckv1.Conditions {{
143- Type : apis .ConditionSucceeded ,
144- Status : corev1 .ConditionTrue ,
145- }},
146- },
147- RunStatusFields : v1alpha1.RunStatusFields {
148- Results : []v1alpha1.RunResult {{
149- Name : "runResult" ,
150- Value : "aResultValue" ,
151- }},
152- },
153- }
154-
155- if _ , err := c .RunClient .UpdateStatus (ctx , r , metav1.UpdateOptions {}); err != nil {
156- t .Fatalf ("Failed to update Run to successful: %v" , err )
157- }
158-
159- // Get the Run.
160- r , err = c .RunClient .Get (ctx , runName , metav1.GetOptions {})
161- if err != nil {
162- t .Fatalf ("Failed to get Run %q: %v" , runName , err )
163- }
164- if ! r .IsDone () {
165- t .Fatalf ("Run unexpectedly not done after update (UpdateStatus didn't work): %v" , r .Status )
130+ if len (pr .Status .Runs ) != 2 {
131+ t .Fatalf ("PipelineRun had unexpected .status.runs; got %d, want 2" , len (pr .Status .Runs ))
132+ }
133+
134+ for runName := range pr .Status .Runs {
135+ // Get the Run.
136+ r , err := c .RunClient .Get (ctx , runName , metav1.GetOptions {})
137+ if err != nil {
138+ t .Fatalf ("Failed to get Run %q: %v" , runName , err )
139+ }
140+ if r .IsDone () {
141+ t .Fatalf ("Run unexpectedly done: %v" , r .Status .GetCondition (apis .ConditionSucceeded ))
142+ }
143+
144+ // Simulate a Custom Task controller updating the Run to done/successful.
145+ r .Status = v1alpha1.RunStatus {
146+ Status : duckv1.Status {
147+ Conditions : duckv1.Conditions {{
148+ Type : apis .ConditionSucceeded ,
149+ Status : corev1 .ConditionTrue ,
150+ }},
151+ },
152+ RunStatusFields : v1alpha1.RunStatusFields {
153+ Results : []v1alpha1.RunResult {{
154+ Name : "runResult" ,
155+ Value : "aResultValue" ,
156+ }},
157+ },
158+ }
159+
160+ if _ , err := c .RunClient .UpdateStatus (ctx , r , metav1.UpdateOptions {}); err != nil {
161+ t .Fatalf ("Failed to update Run to successful: %v" , err )
162+ }
163+
164+ // Get the Run.
165+ r , err = c .RunClient .Get (ctx , runName , metav1.GetOptions {})
166+
167+ if strings .Contains (runName , "custom-task-spec" ) {
168+ if d := cmp .Diff (customTaskRawSpec , r .Spec .Spec .Spec .Raw ); d != "" {
169+ t .Fatalf ("Unexpected value of Spec.Raw: %s" , diff .PrintWantGot (d ))
170+ }
171+ if d := cmp .Diff (metadataLabel , r .Spec .Spec .Metadata .Labels ); d != "" {
172+ t .Fatalf ("Unexpected value of Metadata.Labels: %s" , diff .PrintWantGot (d ))
173+ }
174+ }
175+ if err != nil {
176+ t .Fatalf ("Failed to get Run %q: %v" , runName , err )
177+ }
178+ if ! r .IsDone () {
179+ t .Fatalf ("Run unexpectedly not done after update (UpdateStatus didn't work): %v" , r .Status )
180+ }
166181 }
167-
168182 // Wait for the PipelineRun to become done/successful.
169183 if err := WaitForPipelineRunState (ctx , c , pipelineRunName , time .Minute , PipelineRunSucceed (pipelineRunName ), "PipelineRunCompleted" ); err != nil {
170184 t .Fatalf ("Waiting for PipelineRun to complete successfully: %v" , err )
@@ -194,7 +208,9 @@ func TestCustomTask(t *testing.T) {
194208
195209 // Validate the task's result reference to the custom task's result was resolved.
196210 expectedTaskRunParams := []v1beta1.Param {{
197- Name : "input-result-from-custom-task" , Value : * v1beta1 .NewArrayOrString ("aResultValue" ),
211+ Name : "input-result-from-custom-task-ref" , Value : * v1beta1 .NewArrayOrString ("aResultValue" ),
212+ }, {
213+ Name : "input-result-from-custom-task-spec" , Value : * v1beta1 .NewArrayOrString ("aResultValue" ),
198214 }}
199215
200216 if d := cmp .Diff (expectedTaskRunParams , taskRun .Spec .Params ); d != "" {
@@ -204,12 +220,15 @@ func TestCustomTask(t *testing.T) {
204220 // Validate that the pipeline's result reference to the custom task's result was resolved.
205221
206222 expectedPipelineResults := []v1beta1.PipelineRunResult {{
207- Name : "prResult" ,
223+ Name : "prResult-ref" ,
224+ Value : "aResultValue" ,
225+ }, {
226+ Name : "prResult-spec" ,
208227 Value : "aResultValue" ,
209228 }}
210229
211- if len (pr .Status .PipelineResults ) == 0 {
212- t .Fatalf ("Expected PipelineResults but there are none" )
230+ if len (pr .Status .PipelineResults ) != 2 {
231+ t .Fatalf ("Expected 2 PipelineResults but there are %d." , len ( pr . Status . PipelineResults ) )
213232 }
214233 if d := cmp .Diff (expectedPipelineResults , pr .Status .PipelineResults ); d != "" {
215234 t .Fatalf ("Unexpected PipelineResults: %s" , diff .PrintWantGot (d ))
0 commit comments