Skip to content

Commit b265d8d

Browse files
committed
minimize the diff
Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
1 parent d1ef9cd commit b265d8d

File tree

1 file changed

+166
-29
lines changed

1 file changed

+166
-29
lines changed

pkg/builder/webhook_test.go

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

80-
It("should scaffold a custom defaulter webhook which recovers from panics", func() {
80+
It("should scaffold a custom defaulting webhook if the type implements the CustomDefaulter interface", func() {
81+
By("creating a controller manager")
82+
m, err := manager.New(cfg, manager.Options{})
83+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
84+
85+
By("registering the type in the Scheme")
86+
builder := scheme.Builder{GroupVersion: testDefaulterGVK.GroupVersion()}
87+
builder.Register(&TestDefaulter{}, &TestDefaulterList{})
88+
err = builder.AddToScheme(m.GetScheme())
89+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
90+
91+
err = WebhookManagedBy(m).
92+
For(&TestDefaulter{}).
93+
WithDefaulter(&TestCustomDefaulter{}).
94+
Complete()
95+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
96+
svr := m.GetWebhookServer()
97+
ExpectWithOffset(1, svr).NotTo(BeNil())
98+
99+
reader := strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
100+
"request":{
101+
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
102+
"kind":{
103+
"group":"",
104+
"version":"v1",
105+
"kind":"TestDefaulter"
106+
},
107+
"resource":{
108+
"group":"",
109+
"version":"v1",
110+
"resource":"testdefaulter"
111+
},
112+
"namespace":"default",
113+
"operation":"CREATE",
114+
"object":{
115+
"replica":1
116+
},
117+
"oldObject":null
118+
}
119+
}`)
120+
121+
ctx, cancel := context.WithCancel(context.Background())
122+
cancel()
123+
err = svr.Start(ctx)
124+
if err != nil && !os.IsNotExist(err) {
125+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
126+
}
127+
128+
By("sending a request to a mutating webhook path")
129+
path := generateMutatePath(testDefaulterGVK)
130+
req := httptest.NewRequest("POST", svcBaseAddr+path, reader)
131+
req.Header.Add("Content-Type", "application/json")
132+
w := httptest.NewRecorder()
133+
svr.WebhookMux().ServeHTTP(w, req)
134+
ExpectWithOffset(1, w.Code).To(Equal(http.StatusOK))
135+
By("sanity checking the response contains reasonable fields")
136+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":true`))
137+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"patch":`))
138+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":200`))
139+
140+
By("sending a request to a validating webhook path that doesn't exist")
141+
path = generateValidatePath(testDefaulterGVK)
142+
_, err = reader.Seek(0, 0)
143+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
144+
req = httptest.NewRequest("POST", svcBaseAddr+path, reader)
145+
req.Header.Add("Content-Type", "application/json")
146+
w = httptest.NewRecorder()
147+
svr.WebhookMux().ServeHTTP(w, req)
148+
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
149+
})
150+
151+
It("should scaffold a custom defaulting webhook which recovers from panics", func() {
81152
By("creating a controller manager")
82153
m, err := manager.New(cfg, manager.Options{})
83154
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -114,6 +185,7 @@ func runTests(admissionReviewVersion string) {
114185
"namespace":"default",
115186
"operation":"CREATE",
116187
"object":{
188+
"replica":1,
117189
"panic":true
118190
},
119191
"oldObject":null
@@ -216,7 +288,7 @@ func runTests(admissionReviewVersion string) {
216288
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
217289
})
218290

219-
It("should scaffold a validating webhook with a custom validator", func() {
291+
It("should scaffold a custom validating webhook if the type implements the CustomValidator interface", func() {
220292
By("creating a controller manager")
221293
m, err := manager.New(cfg, manager.Options{})
222294
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -228,11 +300,8 @@ func runTests(admissionReviewVersion string) {
228300
ExpectWithOffset(1, err).NotTo(HaveOccurred())
229301

230302
err = WebhookManagedBy(m).
231-
WithValidator(&TestCustomValidator{}).
232303
For(&TestValidator{}).
233-
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
234-
return admission.DefaultLogConstructor(testingLogger, req)
235-
}).
304+
WithValidator(&TestCustomValidator{}).
236305
Complete()
237306
ExpectWithOffset(1, err).NotTo(HaveOccurred())
238307
svr := m.GetWebhookServer()
@@ -242,17 +311,16 @@ func runTests(admissionReviewVersion string) {
242311
"request":{
243312
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
244313
"kind":{
245-
"group":"foo.test.org",
314+
"group":"",
246315
"version":"v1",
247316
"kind":"TestValidator"
248317
},
249318
"resource":{
250-
"group":"foo.test.org",
319+
"group":"",
251320
"version":"v1",
252321
"resource":"testvalidator"
253322
},
254323
"namespace":"default",
255-
"name":"foo",
256324
"operation":"UPDATE",
257325
"object":{
258326
"replica":1
@@ -290,13 +358,11 @@ func runTests(admissionReviewVersion string) {
290358
By("sanity checking the response contains reasonable field")
291359
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
292360
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":403`))
293-
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"`))
294361
})
295362

296-
It("should scaffold a customValidator webhook which recovers from panics", func() {
363+
It("should scaffold a custom validating webhook which recovers from panics", func() {
297364
By("creating a controller manager")
298365
m, err := manager.New(cfg, manager.Options{})
299-
300366
ExpectWithOffset(1, err).NotTo(HaveOccurred())
301367

302368
By("registering the type in the Scheme")
@@ -306,7 +372,7 @@ func runTests(admissionReviewVersion string) {
306372
ExpectWithOffset(1, err).NotTo(HaveOccurred())
307373

308374
err = WebhookManagedBy(m).
309-
For(&TestValidator{Panic: true}).
375+
For(&TestValidator{}).
310376
WithValidator(&TestCustomValidator{}).
311377
RecoverPanic(true).
312378
Complete()
@@ -322,16 +388,13 @@ func runTests(admissionReviewVersion string) {
322388
"version":"v1",
323389
"kind":"TestValidator"
324390
},
325-
326391
"resource":{
327392
"group":"",
328-
329393
"version":"v1",
330394
"resource":"testvalidator"
331395
},
332396
"namespace":"default",
333397
"operation":"CREATE",
334-
335398
"object":{
336399
"replica":2,
337400
"panic":true
@@ -361,25 +424,84 @@ func runTests(admissionReviewVersion string) {
361424
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
362425
})
363426

364-
It("should send an error when trying to register a webhook with more than one For", func() {
427+
It("should scaffold a validating webhook with a custom validator", func() {
365428
By("creating a controller manager")
366429
m, err := manager.New(cfg, manager.Options{})
367430
ExpectWithOffset(1, err).NotTo(HaveOccurred())
368431

369432
By("registering the type in the Scheme")
370-
builder := scheme.Builder{GroupVersion: testDefaulterGVK.GroupVersion()}
371-
builder.Register(&TestDefaulter{}, &TestDefaulterList{})
433+
builder := scheme.Builder{GroupVersion: testValidatorGVK.GroupVersion()}
434+
builder.Register(&TestValidator{}, &TestValidatorList{})
372435
err = builder.AddToScheme(m.GetScheme())
373436
ExpectWithOffset(1, err).NotTo(HaveOccurred())
374437

375438
err = WebhookManagedBy(m).
376-
For(&TestDefaulter{}).
377-
For(&TestDefaulter{}).
439+
WithValidator(&TestCustomValidator{}).
440+
For(&TestValidator{}).
441+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
442+
return admission.DefaultLogConstructor(testingLogger, req)
443+
}).
378444
Complete()
379-
Expect(err).To(HaveOccurred())
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"`))
380502
})
381503

382-
It("should scaffold a custom validator webhook if the type implements the CustomValidator interface to validate deletes", func() {
504+
It("should scaffold a custom validating webhook if the type implements the CustomValidator interface to validate deletes", func() {
383505
By("creating a controller manager")
384506
ctx, cancel := context.WithCancel(context.Background())
385507

@@ -407,7 +529,6 @@ func runTests(admissionReviewVersion string) {
407529
"group":"",
408530
"version":"v1",
409531
"kind":"TestValidator"
410-
411532
},
412533
"resource":{
413534
"group":"",
@@ -443,14 +564,12 @@ func runTests(admissionReviewVersion string) {
443564
reader = strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
444565
"request":{
445566
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
446-
447567
"kind":{
448568
"group":"",
449569
"version":"v1",
450570
"kind":"TestValidator"
451571
},
452572
"resource":{
453-
454573
"group":"",
455574
"version":"v1",
456575
"resource":"testvalidator"
@@ -474,6 +593,24 @@ func runTests(admissionReviewVersion string) {
474593
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":true`))
475594
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":200`))
476595
})
596+
597+
It("should send an error when trying to register a webhook with more than one For", func() {
598+
By("creating a controller manager")
599+
m, err := manager.New(cfg, manager.Options{})
600+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
601+
602+
By("registering the type in the Scheme")
603+
builder := scheme.Builder{GroupVersion: testDefaulterGVK.GroupVersion()}
604+
builder.Register(&TestDefaulter{}, &TestDefaulterList{})
605+
err = builder.AddToScheme(m.GetScheme())
606+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
607+
608+
err = WebhookManagedBy(m).
609+
For(&TestDefaulter{}).
610+
For(&TestDefaulter{}).
611+
Complete()
612+
Expect(err).To(HaveOccurred())
613+
})
477614
}
478615

479616
// TestDefaulter.
@@ -586,13 +723,13 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
586723
}
587724

588725
d := obj.(*TestDefaulter) //nolint:ifshort
726+
if d.Panic {
727+
panic("fake panic test")
728+
}
589729

590730
if d.Replica < 2 {
591731
d.Replica = 2
592732
}
593-
if d.Panic {
594-
panic("fake panic test")
595-
}
596733
return nil
597734
}
598735

0 commit comments

Comments
 (0)