@@ -17,14 +17,15 @@ limitations under the License.
17
17
package v1beta1
18
18
19
19
import (
20
+ "context"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
24
+ admissionv1 "k8s.io/api/admission/v1"
23
25
corev1 "k8s.io/api/core/v1"
24
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
27
"k8s.io/utils/pointer"
26
-
27
- utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
28
+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28
29
)
29
30
30
31
func TestMachineDefault (t * testing.T ) {
@@ -39,7 +40,10 @@ func TestMachineDefault(t *testing.T) {
39
40
Version : pointer .String ("1.17.5" ),
40
41
},
41
42
}
42
- t .Run ("for Machine" , utildefaulting .DefaultValidateTest (m ))
43
+ scheme , err := SchemeBuilder .Build ()
44
+ g .Expect (err ).ToNot (HaveOccurred ())
45
+ validator := MachineValidator (scheme )
46
+ t .Run ("for Machine" , defaultDefaulterTestCustomValidator (m , validator ))
43
47
m .Default ()
44
48
45
49
g .Expect (m .Labels [ClusterNameLabel ]).To (Equal (m .Spec .ClusterName ))
@@ -75,15 +79,25 @@ func TestMachineBootstrapValidation(t *testing.T) {
75
79
for _ , tt := range tests {
76
80
t .Run (tt .name , func (t * testing.T ) {
77
81
g := NewWithT (t )
82
+ scheme , err := SchemeBuilder .Build ()
83
+ g .Expect (err ).ToNot (HaveOccurred ())
84
+ validator := MachineValidator (scheme )
85
+
86
+ ctx := admission .NewContextWithRequest (context .Background (), admission.Request {
87
+ AdmissionRequest : admissionv1.AdmissionRequest {
88
+ Operation : admissionv1 .Create ,
89
+ },
90
+ })
91
+
78
92
m := & Machine {
79
93
Spec : MachineSpec {Bootstrap : tt .bootstrap },
80
94
}
81
95
if tt .expectErr {
82
- g .Expect (m .ValidateCreate ()).NotTo (Succeed ())
83
- g .Expect (m .ValidateUpdate (m )).NotTo (Succeed ())
96
+ g .Expect (validator .ValidateCreate (ctx , m )).NotTo (Succeed ())
97
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).NotTo (Succeed ())
84
98
} else {
85
- g .Expect (m .ValidateCreate ()).To (Succeed ())
86
- g .Expect (m .ValidateUpdate (m )).To (Succeed ())
99
+ g .Expect (validator .ValidateCreate (ctx , m )).To (Succeed ())
100
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).To (Succeed ())
87
101
}
88
102
})
89
103
}
@@ -130,18 +144,27 @@ func TestMachineNamespaceValidation(t *testing.T) {
130
144
for _ , tt := range tests {
131
145
t .Run (tt .name , func (t * testing.T ) {
132
146
g := NewWithT (t )
147
+ scheme , err := SchemeBuilder .Build ()
148
+ g .Expect (err ).ToNot (HaveOccurred ())
149
+ validator := MachineValidator (scheme )
150
+
151
+ ctx := admission .NewContextWithRequest (context .Background (), admission.Request {
152
+ AdmissionRequest : admissionv1.AdmissionRequest {
153
+ Operation : admissionv1 .Create ,
154
+ },
155
+ })
133
156
134
157
m := & Machine {
135
158
ObjectMeta : metav1.ObjectMeta {Namespace : tt .namespace },
136
159
Spec : MachineSpec {Bootstrap : tt .bootstrap , InfrastructureRef : tt .infraRef },
137
160
}
138
161
139
162
if tt .expectErr {
140
- g .Expect (m .ValidateCreate ()).NotTo (Succeed ())
141
- g .Expect (m .ValidateUpdate (m )).NotTo (Succeed ())
163
+ g .Expect (validator .ValidateCreate (ctx , m )).NotTo (Succeed ())
164
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).NotTo (Succeed ())
142
165
} else {
143
- g .Expect (m .ValidateCreate ()).To (Succeed ())
144
- g .Expect (m .ValidateUpdate (m )).To (Succeed ())
166
+ g .Expect (validator .ValidateCreate (ctx , m )).To (Succeed ())
167
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).To (Succeed ())
145
168
}
146
169
})
147
170
}
@@ -171,6 +194,15 @@ func TestMachineClusterNameImmutable(t *testing.T) {
171
194
for _ , tt := range tests {
172
195
t .Run (tt .name , func (t * testing.T ) {
173
196
g := NewWithT (t )
197
+ scheme , err := SchemeBuilder .Build ()
198
+ g .Expect (err ).ToNot (HaveOccurred ())
199
+ validator := MachineValidator (scheme )
200
+
201
+ ctx := admission .NewContextWithRequest (context .Background (), admission.Request {
202
+ AdmissionRequest : admissionv1.AdmissionRequest {
203
+ Operation : admissionv1 .Create ,
204
+ },
205
+ })
174
206
175
207
newMachine := & Machine {
176
208
Spec : MachineSpec {
@@ -186,9 +218,9 @@ func TestMachineClusterNameImmutable(t *testing.T) {
186
218
}
187
219
188
220
if tt .expectErr {
189
- g .Expect (newMachine .ValidateUpdate (oldMachine )).NotTo (Succeed ())
221
+ g .Expect (validator .ValidateUpdate (ctx , oldMachine , newMachine )).NotTo (Succeed ())
190
222
} else {
191
- g .Expect (newMachine .ValidateUpdate (oldMachine )).To (Succeed ())
223
+ g .Expect (validator .ValidateUpdate (ctx , oldMachine , newMachine )).To (Succeed ())
192
224
}
193
225
})
194
226
}
@@ -230,6 +262,15 @@ func TestMachineVersionValidation(t *testing.T) {
230
262
for _ , tt := range tests {
231
263
t .Run (tt .name , func (t * testing.T ) {
232
264
g := NewWithT (t )
265
+ scheme , err := SchemeBuilder .Build ()
266
+ g .Expect (err ).ToNot (HaveOccurred ())
267
+ validator := MachineValidator (scheme )
268
+
269
+ ctx := admission .NewContextWithRequest (context .Background (), admission.Request {
270
+ AdmissionRequest : admissionv1.AdmissionRequest {
271
+ Operation : admissionv1 .Create ,
272
+ },
273
+ })
233
274
234
275
m := & Machine {
235
276
Spec : MachineSpec {
@@ -239,12 +280,50 @@ func TestMachineVersionValidation(t *testing.T) {
239
280
}
240
281
241
282
if tt .expectErr {
242
- g .Expect (m .ValidateCreate ()).NotTo (Succeed ())
243
- g .Expect (m .ValidateUpdate (m )).NotTo (Succeed ())
283
+ g .Expect (validator .ValidateCreate (ctx , m )).NotTo (Succeed ())
284
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).NotTo (Succeed ())
244
285
} else {
245
- g .Expect (m .ValidateCreate ()).To (Succeed ())
246
- g .Expect (m .ValidateUpdate (m )).To (Succeed ())
286
+ g .Expect (validator .ValidateCreate (ctx , m )).To (Succeed ())
287
+ g .Expect (validator .ValidateUpdate (ctx , m , m )).To (Succeed ())
247
288
}
248
289
})
249
290
}
250
291
}
292
+
293
+ // defaultDefaulterTestCustomVAlidator returns a new testing function to be used in tests to
294
+ // make sure defaulting webhooks also pass validation tests on create, update and delete.
295
+ // Note: The difference to util/defaulting.DefaultValidateTest is that this function takes an additional
296
+ // CustomValidator as the validation is not implemented on the object directly.
297
+ func defaultDefaulterTestCustomValidator (object admission.Defaulter , customValidator admission.CustomValidator ) func (* testing.T ) {
298
+ return func (t * testing.T ) {
299
+ t .Helper ()
300
+
301
+ createCopy := object .DeepCopyObject ().(admission.Defaulter )
302
+ updateCopy := object .DeepCopyObject ().(admission.Defaulter )
303
+ deleteCopy := object .DeepCopyObject ().(admission.Defaulter )
304
+ defaultingUpdateCopy := updateCopy .DeepCopyObject ().(admission.Defaulter )
305
+
306
+ ctx := admission .NewContextWithRequest (context .Background (), admission.Request {
307
+ AdmissionRequest : admissionv1.AdmissionRequest {
308
+ Operation : admissionv1 .Create ,
309
+ },
310
+ })
311
+
312
+ t .Run ("validate-on-create" , func (t * testing.T ) {
313
+ g := NewWithT (t )
314
+ createCopy .Default ()
315
+ g .Expect (customValidator .ValidateCreate (ctx , createCopy )).To (Succeed ())
316
+ })
317
+ t .Run ("validate-on-update" , func (t * testing.T ) {
318
+ g := NewWithT (t )
319
+ defaultingUpdateCopy .Default ()
320
+ updateCopy .Default ()
321
+ g .Expect (customValidator .ValidateUpdate (ctx , defaultingUpdateCopy , updateCopy )).To (Succeed ())
322
+ })
323
+ t .Run ("validate-on-delete" , func (t * testing.T ) {
324
+ g := NewWithT (t )
325
+ deleteCopy .Default ()
326
+ g .Expect (customValidator .ValidateDelete (ctx , deleteCopy )).To (Succeed ())
327
+ })
328
+ }
329
+ }
0 commit comments