|
| 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 | +} |
0 commit comments