forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkspace_validation_test.go
193 lines (185 loc) · 4.77 KB
/
workspace_validation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2022 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 v1_test
import (
"context"
"testing"
cfgtesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestWorkspaceBindingValidateValid(t *testing.T) {
for _, tc := range []struct {
name string
binding *v1.WorkspaceBinding
wc func(context.Context) context.Context
}{{
name: "Valid PVC",
binding: &v1.WorkspaceBinding{
Name: "beth",
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "pool-party",
},
},
}, {
name: "Valid volumeClaimTemplate",
binding: &v1.WorkspaceBinding{
Name: "beth",
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "mypvc",
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"storage": resource.MustParse("1Gi"),
},
},
},
},
},
}, {
name: "Valid emptyDir",
binding: &v1.WorkspaceBinding{
Name: "beth",
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}, {
name: "Valid configMap",
binding: &v1.WorkspaceBinding{
Name: "beth",
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "a-configmap-name",
},
},
},
}, {
name: "Valid secret",
binding: &v1.WorkspaceBinding{
Name: "beth",
Secret: &corev1.SecretVolumeSource{
SecretName: "my-secret",
},
},
}, {
name: "Valid projected",
binding: &v1.WorkspaceBinding{
Name: "beth",
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{{
ConfigMap: &corev1.ConfigMapProjection{
LocalObjectReference: corev1.LocalObjectReference{
Name: "a-configmap-name",
},
},
}, {
Secret: &corev1.SecretProjection{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-secret",
},
},
}},
},
},
}, {
name: "Valid csi",
binding: &v1.WorkspaceBinding{
Name: "beth",
CSI: &corev1.CSIVolumeSource{
Driver: "my-csi",
},
},
}} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
if tc.wc != nil {
ctx = tc.wc(ctx)
}
if err := tc.binding.Validate(ctx); err != nil {
t.Errorf("didnt expect error for valid binding but got: %v", err)
}
})
}
}
func TestWorkspaceBindingValidateInvalid(t *testing.T) {
for _, tc := range []struct {
name string
binding *v1.WorkspaceBinding
wc func(context.Context) context.Context
}{{
name: "no binding provided",
binding: nil,
}, {
name: "Provided both pvc and emptydir",
binding: &v1.WorkspaceBinding{
Name: "beth",
EmptyDir: &corev1.EmptyDirVolumeSource{},
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "pool-party",
},
},
}, {
name: "Provided neither pvc nor emptydir",
binding: &v1.WorkspaceBinding{
Name: "beth",
},
}, {
name: "Provided pvc without claim name",
binding: &v1.WorkspaceBinding{
Name: "beth",
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{},
},
}, {
name: "Provide configmap without a name",
binding: &v1.WorkspaceBinding{
Name: "beth",
ConfigMap: &corev1.ConfigMapVolumeSource{},
},
}, {
name: "Provide secret without a secretName",
binding: &v1.WorkspaceBinding{
Name: "beth",
Secret: &corev1.SecretVolumeSource{},
},
}, {
name: "Provide projected without sources",
binding: &v1.WorkspaceBinding{
Name: "beth",
Projected: &corev1.ProjectedVolumeSource{},
},
wc: cfgtesting.EnableBetaAPIFields,
}, {
name: "Provide csi without a driver",
binding: &v1.WorkspaceBinding{
Name: "beth",
CSI: &corev1.CSIVolumeSource{
Driver: "",
},
},
wc: cfgtesting.EnableBetaAPIFields,
}} {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
if tc.wc != nil {
ctx = tc.wc(ctx)
}
if err := tc.binding.Validate(ctx); err == nil {
t.Errorf("expected error for invalid binding but didn't get any!")
}
})
}
}