Skip to content

Commit ec45cd7

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

File tree

1 file changed

+166
-30
lines changed

1 file changed

+166
-30
lines changed

pkg/builder/webhook_test.go

Lines changed: 166 additions & 30 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())
@@ -91,7 +162,6 @@ func runTests(admissionReviewVersion string) {
91162
err = WebhookManagedBy(m).
92163
For(&TestDefaulter{}).
93164
WithDefaulter(&TestCustomDefaulter{}).
94-
RecoverPanic(true).
95165
// RecoverPanic defaults to true.
96166
Complete()
97167
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -114,6 +184,7 @@ func runTests(admissionReviewVersion string) {
114184
"namespace":"default",
115185
"operation":"CREATE",
116186
"object":{
187+
"replica":1,
117188
"panic":true
118189
},
119190
"oldObject":null
@@ -216,7 +287,7 @@ func runTests(admissionReviewVersion string) {
216287
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
217288
})
218289

219-
It("should scaffold a validating webhook with a custom validator", func() {
290+
It("should scaffold a custom validating webhook if the type implements the CustomValidator interface", func() {
220291
By("creating a controller manager")
221292
m, err := manager.New(cfg, manager.Options{})
222293
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -228,11 +299,8 @@ func runTests(admissionReviewVersion string) {
228299
ExpectWithOffset(1, err).NotTo(HaveOccurred())
229300

230301
err = WebhookManagedBy(m).
231-
WithValidator(&TestCustomValidator{}).
232302
For(&TestValidator{}).
233-
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
234-
return admission.DefaultLogConstructor(testingLogger, req)
235-
}).
303+
WithValidator(&TestCustomValidator{}).
236304
Complete()
237305
ExpectWithOffset(1, err).NotTo(HaveOccurred())
238306
svr := m.GetWebhookServer()
@@ -242,17 +310,16 @@ func runTests(admissionReviewVersion string) {
242310
"request":{
243311
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
244312
"kind":{
245-
"group":"foo.test.org",
313+
"group":"",
246314
"version":"v1",
247315
"kind":"TestValidator"
248316
},
249317
"resource":{
250-
"group":"foo.test.org",
318+
"group":"",
251319
"version":"v1",
252320
"resource":"testvalidator"
253321
},
254322
"namespace":"default",
255-
"name":"foo",
256323
"operation":"UPDATE",
257324
"object":{
258325
"replica":1
@@ -290,13 +357,11 @@ func runTests(admissionReviewVersion string) {
290357
By("sanity checking the response contains reasonable field")
291358
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
292359
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"`))
294360
})
295361

296-
It("should scaffold a customValidator webhook which recovers from panics", func() {
362+
It("should scaffold a custom validating webhook which recovers from panics", func() {
297363
By("creating a controller manager")
298364
m, err := manager.New(cfg, manager.Options{})
299-
300365
ExpectWithOffset(1, err).NotTo(HaveOccurred())
301366

302367
By("registering the type in the Scheme")
@@ -306,7 +371,7 @@ func runTests(admissionReviewVersion string) {
306371
ExpectWithOffset(1, err).NotTo(HaveOccurred())
307372

308373
err = WebhookManagedBy(m).
309-
For(&TestValidator{Panic: true}).
374+
For(&TestValidator{}).
310375
WithValidator(&TestCustomValidator{}).
311376
RecoverPanic(true).
312377
Complete()
@@ -322,16 +387,13 @@ func runTests(admissionReviewVersion string) {
322387
"version":"v1",
323388
"kind":"TestValidator"
324389
},
325-
326390
"resource":{
327391
"group":"",
328-
329392
"version":"v1",
330393
"resource":"testvalidator"
331394
},
332395
"namespace":"default",
333396
"operation":"CREATE",
334-
335397
"object":{
336398
"replica":2,
337399
"panic":true
@@ -361,25 +423,84 @@ func runTests(admissionReviewVersion string) {
361423
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
362424
})
363425

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

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

375437
err = WebhookManagedBy(m).
376-
For(&TestDefaulter{}).
377-
For(&TestDefaulter{}).
438+
WithValidator(&TestCustomValidator{}).
439+
For(&TestValidator{}).
440+
WithLogConstructor(func(base logr.Logger, req *admission.Request) logr.Logger {
441+
return admission.DefaultLogConstructor(testingLogger, req)
442+
}).
378443
Complete()
379-
Expect(err).To(HaveOccurred())
444+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
445+
svr := m.GetWebhookServer()
446+
ExpectWithOffset(1, svr).NotTo(BeNil())
447+
448+
reader := strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
449+
"request":{
450+
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
451+
"kind":{
452+
"group":"foo.test.org",
453+
"version":"v1",
454+
"kind":"TestValidator"
455+
},
456+
"resource":{
457+
"group":"foo.test.org",
458+
"version":"v1",
459+
"resource":"testvalidator"
460+
},
461+
"namespace":"default",
462+
"name":"foo",
463+
"operation":"UPDATE",
464+
"object":{
465+
"replica":1
466+
},
467+
"oldObject":{
468+
"replica":2
469+
}
470+
}
471+
}`)
472+
473+
ctx, cancel := context.WithCancel(context.Background())
474+
cancel()
475+
err = svr.Start(ctx)
476+
if err != nil && !os.IsNotExist(err) {
477+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
478+
}
479+
480+
By("sending a request to a mutating webhook path that doesn't exist")
481+
path := generateMutatePath(testValidatorGVK)
482+
req := httptest.NewRequest("POST", svcBaseAddr+path, reader)
483+
req.Header.Add("Content-Type", "application/json")
484+
w := httptest.NewRecorder()
485+
svr.WebhookMux().ServeHTTP(w, req)
486+
ExpectWithOffset(1, w.Code).To(Equal(http.StatusNotFound))
487+
488+
By("sending a request to a validating webhook path")
489+
path = generateValidatePath(testValidatorGVK)
490+
_, err = reader.Seek(0, 0)
491+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
492+
req = httptest.NewRequest("POST", svcBaseAddr+path, reader)
493+
req.Header.Add("Content-Type", "application/json")
494+
w = httptest.NewRecorder()
495+
svr.WebhookMux().ServeHTTP(w, req)
496+
ExpectWithOffset(1, w.Code).To(Equal(http.StatusOK))
497+
By("sanity checking the response contains reasonable field")
498+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
499+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":403`))
500+
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"`))
380501
})
381502

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

@@ -407,7 +528,6 @@ func runTests(admissionReviewVersion string) {
407528
"group":"",
408529
"version":"v1",
409530
"kind":"TestValidator"
410-
411531
},
412532
"resource":{
413533
"group":"",
@@ -443,14 +563,12 @@ func runTests(admissionReviewVersion string) {
443563
reader = strings.NewReader(admissionReviewGV + admissionReviewVersion + `",
444564
"request":{
445565
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
446-
447566
"kind":{
448567
"group":"",
449568
"version":"v1",
450569
"kind":"TestValidator"
451570
},
452571
"resource":{
453-
454572
"group":"",
455573
"version":"v1",
456574
"resource":"testvalidator"
@@ -474,6 +592,24 @@ func runTests(admissionReviewVersion string) {
474592
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":true`))
475593
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":200`))
476594
})
595+
596+
It("should send an error when trying to register a webhook with more than one For", func() {
597+
By("creating a controller manager")
598+
m, err := manager.New(cfg, manager.Options{})
599+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
600+
601+
By("registering the type in the Scheme")
602+
builder := scheme.Builder{GroupVersion: testDefaulterGVK.GroupVersion()}
603+
builder.Register(&TestDefaulter{}, &TestDefaulterList{})
604+
err = builder.AddToScheme(m.GetScheme())
605+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
606+
607+
err = WebhookManagedBy(m).
608+
For(&TestDefaulter{}).
609+
For(&TestDefaulter{}).
610+
Complete()
611+
Expect(err).To(HaveOccurred())
612+
})
477613
}
478614

479615
// TestDefaulter.
@@ -586,13 +722,13 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
586722
}
587723

588724
d := obj.(*TestDefaulter) //nolint:ifshort
725+
if d.Panic {
726+
panic("fake panic test")
727+
}
589728

590729
if d.Replica < 2 {
591730
d.Replica = 2
592731
}
593-
if d.Panic {
594-
panic("fake panic test")
595-
}
596732
return nil
597733
}
598734

0 commit comments

Comments
 (0)