Skip to content

Commit

Permalink
Relax result type validation to avoid nightly build failure
Browse files Browse the repository at this point in the history
This commit fixes the string result type validation, PR tektoncd#4779 adds
result type and asumes that the default type should be string via
mutating webhook. PR tektoncd#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.
  • Loading branch information
Yongxuanzhang committed Jun 29, 2022
1 parent 7d7d96f commit ae4a766
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
1 change: 0 additions & 1 deletion pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/result_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type TaskResult struct {

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

// TaskRunResult used to describe the results of a task
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1beta1/result_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (tr TaskResult) Validate(ctx context.Context) (errs *apis.FieldError) {
return errs.Also(ValidateEnabledAPIFields(ctx, "results type", config.AlphaAPIFields))
}

// Resources created before the result. Type was introduced may not have Type set
// and should be considered valid
if tr.Type == "" {
return nil
}

// By default the result type is string
if tr.Type != ResultsTypeString {
return apis.ErrInvalidValue(tr.Type, "type", fmt.Sprintf("type must be string"))
}
Expand Down
147 changes: 147 additions & 0 deletions pkg/apis/pipeline/v1beta1/result_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
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 v1beta1_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
"knative.dev/pkg/apis"
)

func TestResultsValidate(t *testing.T) {
tests := []struct {
name string
Result v1beta1.TaskResult
apiFields string
}{{
name: "valid result type empty",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Description: "my great result",
},
apiFields: "stable",
}, {
name: "valid result type string",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "string",
Description: "my great result",
},

apiFields: "stable",
}, {
name: "valid result type array",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "array",
Description: "my great result",
},

apiFields: "alpha",
}, {
name: "valid result type object",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "array",
Description: "my great result",
},

apiFields: "alpha",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := getContextBasedOnFeatureFlag(tt.apiFields)
if err := tt.Result.Validate(ctx); err != nil {
t.Errorf("TaskSpec.Validate() = %v", err)
}
})
}
}

func TestResultsValidateError(t *testing.T) {
tests := []struct {
name string
Result v1beta1.TaskResult
apiFields string
expectedError apis.FieldError
}{{
name: "invalid result type in stable",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "wrong",
Description: "my great result",
},
apiFields: "stable",
expectedError: apis.FieldError{
Message: `invalid value: wrong`,
Paths: []string{"type"},
Details: "type must be string",
},
}, {
name: "invalid result type in alpha",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "wrong",
Description: "my great result",
},
apiFields: "alpha",
expectedError: apis.FieldError{
Message: `invalid value: wrong`,
Paths: []string{"type"},
Details: "type must be string",
},
}, {
name: "invalid array result type in stable",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "array",
Description: "my great result",
},
apiFields: "stable",
expectedError: apis.FieldError{
Message: "results type requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"",
},
}, {
name: "invalid object result type in stable",
Result: v1beta1.TaskResult{
Name: "MY-RESULT",
Type: "object",
Description: "my great result",
},
apiFields: "stable",
expectedError: apis.FieldError{
Message: "results type requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"",
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := getContextBasedOnFeatureFlag(tt.apiFields)
err := tt.Result.Validate(ctx)
if err == nil {
t.Fatalf("Expected an error, got nothing for %v", tt.Result)
}
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
}

})
}
}
3 changes: 1 addition & 2 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2243,8 +2243,7 @@
"properties": {
"description": {
"description": "Description is a human-readable description of the result",
"type": "string",
"default": ""
"type": "string"
},
"name": {
"description": "Name the given name",
Expand Down

0 comments on commit ae4a766

Please sign in to comment.