Skip to content

Commit a59ccb5

Browse files
Yongxuanzhangtekton-robot
authored andcommitted
Relax result type validation to avoid nightly build failure
This commit fixes the string result type validation, PR #4779 adds result type and asumes that the default type should be string via mutating webhook. PR #4818 adds the validation for this. However, resources that did already exist in etcd didn't get the default. So this commit relaxes the validation for empty result type.
1 parent 13f9c0f commit a59ccb5

File tree

5 files changed

+156
-4
lines changed

5 files changed

+156
-4
lines changed

pkg/apis/pipeline/v1beta1/openapi_generated.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/pipeline/v1beta1/result_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type TaskResult struct {
2525

2626
// Description is a human-readable description of the result
2727
// +optional
28-
Description string `json:"description"`
28+
Description string `json:"description,omitempty"`
2929
}
3030

3131
// TaskRunResult used to describe the results of a task

pkg/apis/pipeline/v1beta1/result_validation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func (tr TaskResult) Validate(ctx context.Context) (errs *apis.FieldError) {
3131
return errs.Also(ValidateEnabledAPIFields(ctx, "results type", config.AlphaAPIFields))
3232
}
3333

34+
// Resources created before the result. Type was introduced may not have Type set
35+
// and should be considered valid
36+
if tr.Type == "" {
37+
return nil
38+
}
39+
40+
// By default the result type is string
3441
if tr.Type != ResultsTypeString {
3542
return apis.ErrInvalidValue(tr.Type, "type", fmt.Sprintf("type must be string"))
3643
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright 2019 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
"github.com/google/go-cmp/cmp/cmpopts"
24+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
25+
"github.com/tektoncd/pipeline/test/diff"
26+
"knative.dev/pkg/apis"
27+
)
28+
29+
func TestResultsValidate(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
Result v1beta1.TaskResult
33+
apiFields string
34+
}{{
35+
name: "valid result type empty",
36+
Result: v1beta1.TaskResult{
37+
Name: "MY-RESULT",
38+
Description: "my great result",
39+
},
40+
apiFields: "stable",
41+
}, {
42+
name: "valid result type string",
43+
Result: v1beta1.TaskResult{
44+
Name: "MY-RESULT",
45+
Type: "string",
46+
Description: "my great result",
47+
},
48+
49+
apiFields: "stable",
50+
}, {
51+
name: "valid result type array",
52+
Result: v1beta1.TaskResult{
53+
Name: "MY-RESULT",
54+
Type: "array",
55+
Description: "my great result",
56+
},
57+
58+
apiFields: "alpha",
59+
}, {
60+
name: "valid result type object",
61+
Result: v1beta1.TaskResult{
62+
Name: "MY-RESULT",
63+
Type: "array",
64+
Description: "my great result",
65+
},
66+
67+
apiFields: "alpha",
68+
}}
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
ctx := getContextBasedOnFeatureFlag(tt.apiFields)
72+
if err := tt.Result.Validate(ctx); err != nil {
73+
t.Errorf("TaskSpec.Validate() = %v", err)
74+
}
75+
})
76+
}
77+
}
78+
79+
func TestResultsValidateError(t *testing.T) {
80+
tests := []struct {
81+
name string
82+
Result v1beta1.TaskResult
83+
apiFields string
84+
expectedError apis.FieldError
85+
}{{
86+
name: "invalid result type in stable",
87+
Result: v1beta1.TaskResult{
88+
Name: "MY-RESULT",
89+
Type: "wrong",
90+
Description: "my great result",
91+
},
92+
apiFields: "stable",
93+
expectedError: apis.FieldError{
94+
Message: `invalid value: wrong`,
95+
Paths: []string{"type"},
96+
Details: "type must be string",
97+
},
98+
}, {
99+
name: "invalid result type in alpha",
100+
Result: v1beta1.TaskResult{
101+
Name: "MY-RESULT",
102+
Type: "wrong",
103+
Description: "my great result",
104+
},
105+
apiFields: "alpha",
106+
expectedError: apis.FieldError{
107+
Message: `invalid value: wrong`,
108+
Paths: []string{"type"},
109+
Details: "type must be string",
110+
},
111+
}, {
112+
name: "invalid array result type in stable",
113+
Result: v1beta1.TaskResult{
114+
Name: "MY-RESULT",
115+
Type: "array",
116+
Description: "my great result",
117+
},
118+
apiFields: "stable",
119+
expectedError: apis.FieldError{
120+
Message: "results type requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"",
121+
},
122+
}, {
123+
name: "invalid object result type in stable",
124+
Result: v1beta1.TaskResult{
125+
Name: "MY-RESULT",
126+
Type: "object",
127+
Description: "my great result",
128+
},
129+
apiFields: "stable",
130+
expectedError: apis.FieldError{
131+
Message: "results type requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"",
132+
},
133+
}}
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
ctx := getContextBasedOnFeatureFlag(tt.apiFields)
137+
err := tt.Result.Validate(ctx)
138+
if err == nil {
139+
t.Fatalf("Expected an error, got nothing for %v", tt.Result)
140+
}
141+
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
142+
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
143+
}
144+
145+
})
146+
}
147+
}

pkg/apis/pipeline/v1beta1/swagger.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,8 +2447,7 @@
24472447
"properties": {
24482448
"description": {
24492449
"description": "Description is a human-readable description of the result",
2450-
"type": "string",
2451-
"default": ""
2450+
"type": "string"
24522451
},
24532452
"name": {
24542453
"description": "Name the given name",

0 commit comments

Comments
 (0)