@@ -17,9 +17,11 @@ limitations under the License.
17
17
package workspace
18
18
19
19
import (
20
+ "errors"
20
21
"testing"
21
22
22
23
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
24
+ "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
23
25
corev1 "k8s.io/api/core/v1"
24
26
)
25
27
@@ -131,3 +133,89 @@ func TestValidateBindingsInvalid(t *testing.T) {
131
133
})
132
134
}
133
135
}
136
+
137
+ func TestValidateOnlyOnePVCIsUsed_Valid (t * testing.T ) {
138
+ for _ , tc := range []struct {
139
+ name string
140
+ bindings []v1beta1.WorkspaceBinding
141
+ }{{
142
+ name : "an error is not returned when no bindings are given" ,
143
+ bindings : []v1beta1.WorkspaceBinding {},
144
+ }, {
145
+ name : "an error is not returned when volume claims are not used" ,
146
+ bindings : []v1beta1.WorkspaceBinding {{
147
+ EmptyDir : & corev1.EmptyDirVolumeSource {},
148
+ }, {
149
+ Secret : & corev1.SecretVolumeSource {},
150
+ }},
151
+ }, {
152
+ name : "an error is not returned when one PV claim is used in two bindings" ,
153
+ bindings : []v1beta1.WorkspaceBinding {{
154
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
155
+ ClaimName : "foo" ,
156
+ },
157
+ }, {
158
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
159
+ ClaimName : "foo" ,
160
+ },
161
+ }},
162
+ }, {
163
+ name : "an error is not returned when one PV claim is used in two bindings with different subpaths" ,
164
+ bindings : []v1beta1.WorkspaceBinding {{
165
+ SubPath : "/pathA" ,
166
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
167
+ ClaimName : "foo" ,
168
+ },
169
+ }, {
170
+ SubPath : "/pathB" ,
171
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
172
+ ClaimName : "foo" ,
173
+ },
174
+ }},
175
+ }} {
176
+ t .Run (tc .name , func (t * testing.T ) {
177
+ if err := ValidateOnlyOnePVCIsUsed (tc .bindings ); err != nil {
178
+ t .Errorf ("unexpected error: %v" , err )
179
+ }
180
+ })
181
+ }
182
+ }
183
+
184
+ func TestValidateOnlyOnePVCIsUsed_Invalid (t * testing.T ) {
185
+ validationError := errors .New ("more than one PersistentVolumeClaim is bound" )
186
+ for _ , tc := range []struct {
187
+ name string
188
+ bindings []v1beta1.WorkspaceBinding
189
+ wantErr error
190
+ }{{
191
+ name : "an error is returned when two different PV claims are used" ,
192
+ bindings : []v1beta1.WorkspaceBinding {{
193
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
194
+ ClaimName : "foo" ,
195
+ },
196
+ }, {
197
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
198
+ ClaimName : "bar" ,
199
+ },
200
+ }},
201
+ wantErr : validationError ,
202
+ }, {
203
+ name : "an error is returned when a PVC and volume claim template are mixed" ,
204
+ bindings : []v1beta1.WorkspaceBinding {{
205
+ PersistentVolumeClaim : & corev1.PersistentVolumeClaimVolumeSource {
206
+ ClaimName : "foo" ,
207
+ },
208
+ }, {
209
+ Name : "bar" ,
210
+ VolumeClaimTemplate : & corev1.PersistentVolumeClaim {},
211
+ }},
212
+ wantErr : validationError ,
213
+ }} {
214
+ t .Run (tc .name , func (t * testing.T ) {
215
+ err := ValidateOnlyOnePVCIsUsed (tc .bindings )
216
+ if err == nil || (tc .wantErr .Error () != err .Error ()) {
217
+ t .Errorf ("expected %v received %v" , tc .wantErr , err )
218
+ }
219
+ })
220
+ }
221
+ }
0 commit comments