|
| 1 | +/* |
| 2 | +Copyright 2020 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 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/tektoncd/pipeline/pkg/apis/config" |
| 25 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" |
| 26 | + "github.com/tektoncd/pipeline/test/diff" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "knative.dev/pkg/apis" |
| 30 | + logtesting "knative.dev/pkg/logging/testing" |
| 31 | +) |
| 32 | + |
| 33 | +func TestPipelineRef_Invalid(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + ref *v1beta1.PipelineRef |
| 37 | + wantErr *apis.FieldError |
| 38 | + withContext func(context.Context) context.Context |
| 39 | + }{{ |
| 40 | + name: "use of bundle without the feature flag set", |
| 41 | + ref: &v1beta1.PipelineRef{ |
| 42 | + Name: "my-pipeline", |
| 43 | + Bundle: "docker.io/foo", |
| 44 | + }, |
| 45 | + wantErr: apis.ErrGeneric("bundle requires \"enable-tekton-oci-bundles\" feature gate to be true but it is false"), |
| 46 | + }, { |
| 47 | + name: "bundle missing name", |
| 48 | + ref: &v1beta1.PipelineRef{ |
| 49 | + Bundle: "docker.io/foo", |
| 50 | + }, |
| 51 | + wantErr: apis.ErrMissingField("name"), |
| 52 | + withContext: enableTektonOCIBundles(t), |
| 53 | + }, { |
| 54 | + name: "invalid bundle reference", |
| 55 | + ref: &v1beta1.PipelineRef{ |
| 56 | + Name: "my-pipeline", |
| 57 | + Bundle: "not a valid reference", |
| 58 | + }, |
| 59 | + wantErr: apis.ErrInvalidValue("invalid bundle reference", "bundle", "could not parse reference: not a valid reference"), |
| 60 | + withContext: enableTektonOCIBundles(t), |
| 61 | + }, { |
| 62 | + name: "pipelineRef without Pipeline Name", |
| 63 | + ref: &v1beta1.PipelineRef{}, |
| 64 | + wantErr: apis.ErrMissingField("name"), |
| 65 | + }, { |
| 66 | + name: "pipelineref resolver disallowed without alpha feature gate", |
| 67 | + ref: &v1beta1.PipelineRef{ |
| 68 | + ResolverRef: v1beta1.ResolverRef{ |
| 69 | + Resolver: "foo", |
| 70 | + }, |
| 71 | + }, |
| 72 | + wantErr: apis.ErrGeneric("resolver requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\""), |
| 73 | + }, { |
| 74 | + name: "pipelineref resource disallowed without alpha feature gate", |
| 75 | + ref: &v1beta1.PipelineRef{ |
| 76 | + ResolverRef: v1beta1.ResolverRef{ |
| 77 | + Resource: []v1beta1.ResolverParam{}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + wantErr: apis.ErrMissingField("resolver").Also(apis.ErrGeneric("resource requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"")), |
| 81 | + }, { |
| 82 | + name: "pipelineref resource disallowed without resolver", |
| 83 | + ref: &v1beta1.PipelineRef{ |
| 84 | + ResolverRef: v1beta1.ResolverRef{ |
| 85 | + Resource: []v1beta1.ResolverParam{}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + wantErr: apis.ErrMissingField("resolver"), |
| 89 | + withContext: enableAlphaAPIFields, |
| 90 | + }, { |
| 91 | + name: "pipelineref resolver disallowed in conjunction with pipelineref name", |
| 92 | + ref: &v1beta1.PipelineRef{ |
| 93 | + Name: "foo", |
| 94 | + ResolverRef: v1beta1.ResolverRef{ |
| 95 | + Resolver: "bar", |
| 96 | + }, |
| 97 | + }, |
| 98 | + wantErr: apis.ErrMultipleOneOf("name", "resolver"), |
| 99 | + withContext: enableAlphaAPIFields, |
| 100 | + }, { |
| 101 | + name: "pipelineref resolver disallowed in conjunction with pipelineref bundle", |
| 102 | + ref: &v1beta1.PipelineRef{ |
| 103 | + Bundle: "foo", |
| 104 | + ResolverRef: v1beta1.ResolverRef{ |
| 105 | + Resolver: "baz", |
| 106 | + }, |
| 107 | + }, |
| 108 | + wantErr: apis.ErrMultipleOneOf("bundle", "resolver"), |
| 109 | + withContext: enableAlphaAPIFields, |
| 110 | + }, { |
| 111 | + name: "pipelineref resource disallowed in conjunction with taskref name", |
| 112 | + ref: &v1beta1.PipelineRef{ |
| 113 | + Name: "bar", |
| 114 | + ResolverRef: v1beta1.ResolverRef{ |
| 115 | + Resource: []v1beta1.ResolverParam{{ |
| 116 | + Name: "foo", |
| 117 | + Value: "bar", |
| 118 | + }}, |
| 119 | + }, |
| 120 | + }, |
| 121 | + wantErr: apis.ErrMultipleOneOf("name", "resource").Also(apis.ErrMissingField("resolver")), |
| 122 | + withContext: enableAlphaAPIFields, |
| 123 | + }, { |
| 124 | + name: "pipelineref resource disallowed in conjunction with taskref bundle", |
| 125 | + ref: &v1beta1.PipelineRef{ |
| 126 | + Bundle: "bar", |
| 127 | + ResolverRef: v1beta1.ResolverRef{ |
| 128 | + Resource: []v1beta1.ResolverParam{{ |
| 129 | + Name: "foo", |
| 130 | + Value: "bar", |
| 131 | + }}, |
| 132 | + }, |
| 133 | + }, |
| 134 | + wantErr: apis.ErrMultipleOneOf("bundle", "resource").Also(apis.ErrMissingField("resolver")), |
| 135 | + withContext: enableAlphaAPIFields, |
| 136 | + }} |
| 137 | + |
| 138 | + for _, tc := range tests { |
| 139 | + t.Run(tc.name, func(t *testing.T) { |
| 140 | + ctx := context.Background() |
| 141 | + if tc.withContext != nil { |
| 142 | + ctx = tc.withContext(ctx) |
| 143 | + } |
| 144 | + err := tc.ref.Validate(ctx) |
| 145 | + if d := cmp.Diff(tc.wantErr.Error(), err.Error()); d != "" { |
| 146 | + t.Error(diff.PrintWantGot(d)) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestPipelineRef_Valid(t *testing.T) { |
| 153 | + tests := []struct { |
| 154 | + name string |
| 155 | + ref *v1beta1.PipelineRef |
| 156 | + wc func(context.Context) context.Context |
| 157 | + }{{ |
| 158 | + name: "no pipelineRef", |
| 159 | + ref: nil, |
| 160 | + }, { |
| 161 | + name: "alpha feature: valid resolver", |
| 162 | + ref: &v1beta1.PipelineRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git"}}, |
| 163 | + wc: enableAlphaAPIFields, |
| 164 | + }, { |
| 165 | + name: "alpha feature: valid resolver with resource parameters", |
| 166 | + ref: &v1beta1.PipelineRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git", Resource: []v1beta1.ResolverParam{{ |
| 167 | + Name: "repo", |
| 168 | + Value: "https://github.com/tektoncd/pipeline.git", |
| 169 | + }, { |
| 170 | + Name: "branch", |
| 171 | + Value: "baz", |
| 172 | + }}}}, |
| 173 | + wc: enableAlphaAPIFields, |
| 174 | + }} |
| 175 | + |
| 176 | + for _, ts := range tests { |
| 177 | + t.Run(ts.name, func(t *testing.T) { |
| 178 | + ctx := context.Background() |
| 179 | + if ts.wc != nil { |
| 180 | + ctx = ts.wc(ctx) |
| 181 | + } |
| 182 | + if err := ts.ref.Validate(ctx); err != nil { |
| 183 | + t.Error(err) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func enableTektonOCIBundles(t *testing.T) func(context.Context) context.Context { |
| 190 | + return func(ctx context.Context) context.Context { |
| 191 | + s := config.NewStore(logtesting.TestLogger(t)) |
| 192 | + s.OnConfigChanged(&corev1.ConfigMap{ |
| 193 | + ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName()}, |
| 194 | + Data: map[string]string{ |
| 195 | + "enable-tekton-oci-bundles": "true", |
| 196 | + }, |
| 197 | + }) |
| 198 | + return s.ToContext(ctx) |
| 199 | + } |
| 200 | +} |
0 commit comments