Skip to content

Commit c32ad24

Browse files
authored
Merge pull request #2945 from sbueringer/pr-cleanup-tests
🌱 Cleanup defaulter & validator unit tests
2 parents c6f06dc + 4fa5868 commit c32ad24

File tree

1 file changed

+17
-160
lines changed

1 file changed

+17
-160
lines changed

pkg/builder/webhook_test.go

Lines changed: 17 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runTests(admissionReviewVersion string) {
7777
close(stop)
7878
})
7979

80-
It("should scaffold a custom defaulting webhook if the type implements the CustomDefaulter interface", func() {
80+
It("should scaffold a custom defaulting webhook", func() {
8181
By("creating a controller manager")
8282
m, err := manager.New(cfg, manager.Options{})
8383
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -91,6 +91,9 @@ func runTests(admissionReviewVersion string) {
9191
err = WebhookManagedBy(m).
9292
For(&TestDefaulter{}).
9393
WithDefaulter(&TestCustomDefaulter{}).
94+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
95+
return admission.DefaultLogConstructor(testingLogger, req)
96+
}).
9497
Complete()
9598
ExpectWithOffset(1, err).NotTo(HaveOccurred())
9699
svr := m.GetWebhookServer()
@@ -100,16 +103,17 @@ func runTests(admissionReviewVersion string) {
100103
"request":{
101104
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
102105
"kind":{
103-
"group":"",
106+
"group":"foo.test.org",
104107
"version":"v1",
105108
"kind":"TestDefaulter"
106109
},
107110
"resource":{
108-
"group":"",
111+
"group":"foo.test.org",
109112
"version":"v1",
110113
"resource":"testdefaulter"
111114
},
112115
"namespace":"default",
116+
"name":"foo",
113117
"operation":"CREATE",
114118
"object":{
115119
"replica":1
@@ -136,6 +140,7 @@ func runTests(admissionReviewVersion string) {
136140
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":true`))
137141
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"patch":`))
138142
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":200`))
143+
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Defaulting object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
139144

140145
By("sending a request to a validating webhook path that doesn't exist")
141146
path = generateValidatePath(testDefaulterGVK)
@@ -212,83 +217,7 @@ func runTests(admissionReviewVersion string) {
212217
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
213218
})
214219

215-
It("should scaffold a defaulting webhook with a custom defaulter", func() {
216-
By("creating a controller manager")
217-
m, err := manager.New(cfg, manager.Options{})
218-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
219-
220-
By("registering the type in the Scheme")
221-
builder := scheme.Builder{GroupVersion: testDefaulterGVK.GroupVersion()}
222-
builder.Register(&TestDefaulter{}, &TestDefaulterList{})
223-
err = builder.AddToScheme(m.GetScheme())
224-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
225-
226-
err = WebhookManagedBy(m).
227-
WithDefaulter(&TestCustomDefaulter{}).
228-
For(&TestDefaulter{}).
229-
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
230-
return admission.DefaultLogConstructor(testingLogger, req)
231-
}).
232-
Complete()
233-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
234-
svr := m.GetWebhookServer()
235-
ExpectWithOffset(1, svr).NotTo(BeNil())
236-
237-
reader := strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
238-
"request":{
239-
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
240-
"kind":{
241-
"group":"foo.test.org",
242-
"version":"v1",
243-
"kind":"TestDefaulter"
244-
},
245-
"resource":{
246-
"group":"foo.test.org",
247-
"version":"v1",
248-
"resource":"testdefaulter"
249-
},
250-
"namespace":"default",
251-
"name":"foo",
252-
"operation":"CREATE",
253-
"object":{
254-
"replica":1
255-
},
256-
"oldObject":null
257-
}
258-
}`)
259-
260-
ctx, cancel := context.WithCancel(context.Background())
261-
cancel()
262-
err = svr.Start(ctx)
263-
if err != nil && !os.IsNotExist(err) {
264-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
265-
}
266-
267-
By("sending a request to a mutating webhook path")
268-
path := generateMutatePath(testDefaulterGVK)
269-
req := httptest.NewRequest("POST", svcBaseAddr+path, reader)
270-
req.Header.Add("Content-Type", "application/json")
271-
w := httptest.NewRecorder()
272-
svr.WebhookMux().ServeHTTP(w, req)
273-
ExpectWithOffset(1, w.Code).To(Equal(http.StatusOK))
274-
By("sanity checking the response contains reasonable fields")
275-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":true`))
276-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"patch":`))
277-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":200`))
278-
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Defaulting object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
279-
280-
By("sending a request to a validating webhook path that doesn't exist")
281-
path = generateValidatePath(testDefaulterGVK)
282-
_, err = reader.Seek(0, 0)
283-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
284-
req = httptest.NewRequest("POST", svcBaseAddr+path, reader)
285-
req.Header.Add("Content-Type", "application/json")
286-
w = httptest.NewRecorder()
287-
svr.WebhookMux().ServeHTTP(w, req)
288-
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
289-
})
290-
291-
It("should scaffold a custom validating webhook if the type implements the CustomValidator interface", func() {
220+
It("should scaffold a custom validating webhook", func() {
292221
By("creating a controller manager")
293222
m, err := manager.New(cfg, manager.Options{})
294223
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -302,6 +231,9 @@ func runTests(admissionReviewVersion string) {
302231
err = WebhookManagedBy(m).
303232
For(&TestValidator{}).
304233
WithValidator(&TestCustomValidator{}).
234+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
235+
return admission.DefaultLogConstructor(testingLogger, req)
236+
}).
305237
Complete()
306238
ExpectWithOffset(1, err).NotTo(HaveOccurred())
307239
svr := m.GetWebhookServer()
@@ -311,16 +243,17 @@ func runTests(admissionReviewVersion string) {
311243
"request":{
312244
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
313245
"kind":{
314-
"group":"",
246+
"group":"foo.test.org",
315247
"version":"v1",
316248
"kind":"TestValidator"
317249
},
318250
"resource":{
319-
"group":"",
251+
"group":"foo.test.org",
320252
"version":"v1",
321253
"resource":"testvalidator"
322254
},
323255
"namespace":"default",
256+
"name":"foo",
324257
"operation":"UPDATE",
325258
"object":{
326259
"replica":1
@@ -358,6 +291,7 @@ func runTests(admissionReviewVersion string) {
358291
By("sanity checking the response contains reasonable field")
359292
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
360293
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":403`))
294+
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
361295
})
362296

363297
It("should scaffold a custom validating webhook which recovers from panics", func() {
@@ -424,84 +358,7 @@ func runTests(admissionReviewVersion string) {
424358
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
425359
})
426360

427-
It("should scaffold a validating webhook with a custom validator", func() {
428-
By("creating a controller manager")
429-
m, err := manager.New(cfg, manager.Options{})
430-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
431-
432-
By("registering the type in the Scheme")
433-
builder := scheme.Builder{GroupVersion: testValidatorGVK.GroupVersion()}
434-
builder.Register(&TestValidator{}, &TestValidatorList{})
435-
err = builder.AddToScheme(m.GetScheme())
436-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
437-
438-
err = WebhookManagedBy(m).
439-
WithValidator(&TestCustomValidator{}).
440-
For(&TestValidator{}).
441-
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
442-
return admission.DefaultLogConstructor(testingLogger, req)
443-
}).
444-
Complete()
445-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
446-
svr := m.GetWebhookServer()
447-
ExpectWithOffset(1, svr).NotTo(BeNil())
448-
449-
reader := strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
450-
"request":{
451-
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
452-
"kind":{
453-
"group":"foo.test.org",
454-
"version":"v1",
455-
"kind":"TestValidator"
456-
},
457-
"resource":{
458-
"group":"foo.test.org",
459-
"version":"v1",
460-
"resource":"testvalidator"
461-
},
462-
"namespace":"default",
463-
"name":"foo",
464-
"operation":"UPDATE",
465-
"object":{
466-
"replica":1
467-
},
468-
"oldObject":{
469-
"replica":2
470-
}
471-
}
472-
}`)
473-
474-
ctx, cancel := context.WithCancel(context.Background())
475-
cancel()
476-
err = svr.Start(ctx)
477-
if err != nil && !os.IsNotExist(err) {
478-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
479-
}
480-
481-
By("sending a request to a mutating webhook path that doesn't exist")
482-
path := generateMutatePath(testValidatorGVK)
483-
req := httptest.NewRequest("POST", svcBaseAddr+path, reader)
484-
req.Header.Add("Content-Type", "application/json")
485-
w := httptest.NewRecorder()
486-
svr.WebhookMux().ServeHTTP(w, req)
487-
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
488-
489-
By("sending a request to a validating webhook path")
490-
path = generateValidatePath(testValidatorGVK)
491-
_, err = reader.Seek(0, 0)
492-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
493-
req = httptest.NewRequest("POST", svcBaseAddr+path, reader)
494-
req.Header.Add("Content-Type", "application/json")
495-
w = httptest.NewRecorder()
496-
svr.WebhookMux().ServeHTTP(w, req)
497-
ExpectWithOffset(1, w.Code).To(Equal(http.StatusOK))
498-
By("sanity checking the response contains reasonable field")
499-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
500-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":403`))
501-
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
502-
})
503-
504-
It("should scaffold a custom validating webhook if the type implements the CustomValidator interface to validate deletes", func() {
361+
It("should scaffold a custom validating webhook to validate deletes", func() {
505362
By("creating a controller manager")
506363
ctx, cancel := context.WithCancel(context.Background())
507364

0 commit comments

Comments
 (0)