@@ -49,6 +49,10 @@ const (
4949 svcBaseAddr = "http://svc-name.svc-ns.svc"
5050
5151 customPath = "/custom-path"
52+
53+ userAgentHeader = "User-Agent"
54+ userAgentCtxKey = "UserAgent"
55+ userAgentValue = "test"
5256)
5357
5458var _ = Describe ("webhook" , func () {
@@ -315,6 +319,9 @@ func runTests(admissionReviewVersion string) {
315319 WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
316320 return admission .DefaultLogConstructor (testingLogger , req )
317321 }).
322+ WithContextFunc (func (ctx context.Context , request * http.Request ) context.Context {
323+ return context .WithValue (ctx , userAgentCtxKey , request .Header .Get (userAgentHeader ))
324+ }).
318325 Complete ()
319326 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
320327 svr := m .GetWebhookServer ()
@@ -344,6 +351,30 @@ func runTests(admissionReviewVersion string) {
344351 }
345352 }
346353}` )
354+ readerWithCxt := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
355+ "request":{
356+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
357+ "kind":{
358+ "group":"foo.test.org",
359+ "version":"v1",
360+ "kind":"TestValidator"
361+ },
362+ "resource":{
363+ "group":"foo.test.org",
364+ "version":"v1",
365+ "resource":"testvalidator"
366+ },
367+ "namespace":"default",
368+ "name":"foo",
369+ "operation":"UPDATE",
370+ "object":{
371+ "replica":1
372+ },
373+ "oldObject":{
374+ "replica":1
375+ }
376+ }
377+ }` )
347378
348379 ctx , cancel := context .WithCancel (specCtx )
349380 cancel ()
@@ -373,6 +404,20 @@ func runTests(admissionReviewVersion string) {
373404 ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
374405 ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
375406 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"` ))
407+
408+ By ("sending a request to a validating webhook with context header validation" )
409+ path = generateValidatePath (testValidatorGVK )
410+ _ , err = readerWithCxt .Seek (0 , 0 )
411+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
412+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , readerWithCxt )
413+ req .Header .Add ("Content-Type" , "application/json" )
414+ req .Header .Add (userAgentHeader , userAgentValue )
415+ w = httptest .NewRecorder ()
416+ svr .WebhookMux ().ServeHTTP (w , req )
417+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
418+ By ("sanity checking the response contains reasonable field" )
419+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
420+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
376421 })
377422
378423 It ("should scaffold a custom validating webhook with a custom path" , func (specCtx SpecContext ) {
@@ -1009,6 +1054,7 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
10091054 if d .Replica < 2 {
10101055 d .Replica = 2
10111056 }
1057+
10121058 return nil
10131059}
10141060
@@ -1035,6 +1081,7 @@ func (*TestCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
10351081 if v .Replica < 0 {
10361082 return nil , errors .New ("number of replica should be greater than or equal to 0" )
10371083 }
1084+
10381085 return nil , nil
10391086}
10401087
@@ -1056,6 +1103,12 @@ func (*TestCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj r
10561103 if v .Replica < old .Replica {
10571104 return nil , fmt .Errorf ("new replica %v should not be fewer than old replica %v" , v .Replica , old .Replica )
10581105 }
1106+
1107+ userAgent , ok := ctx .Value (userAgentCtxKey ).(string )
1108+ if ok && userAgent != userAgentValue {
1109+ return nil , fmt .Errorf ("expected %s value is %q in TestCustomValidator got %q" , userAgentCtxKey , userAgentValue , userAgent )
1110+ }
1111+
10591112 return nil , nil
10601113}
10611114
0 commit comments