@@ -17,10 +17,15 @@ import (
1717)
1818
1919var _ = Describe ("validatingHandler" , func () {
20- Describe ("Handle" , func () {
21- It ("should return 200 in response when create succeeds" , func () {
2220
23- handler := createSucceedingValidatingHandler ()
21+ decoder , _ := NewDecoder (scheme .Scheme )
22+
23+ Context ("when dealing with successful results" , func () {
24+
25+ f := & fakeValidator {ErrorToReturn : nil }
26+ handler := validatingHandler {validator : f , decoder : decoder }
27+
28+ It ("should return 200 in response when create succeeds" , func () {
2429
2530 response := handler .Handle (context .TODO (), Request {
2631 AdmissionRequest : v1beta1.AdmissionRequest {
@@ -31,87 +36,77 @@ var _ = Describe("validatingHandler", func() {
3136 },
3237 },
3338 })
39+
3440 Expect (response .Allowed ).Should (BeTrue ())
3541 Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
3642 })
3743
38- It ("should return 400 in response when create fails on decode" , func () {
39- //TODO
40- })
41-
42- It ("should return response built with the Status object when ValidateCreate returns APIStatus error" , func () {
43-
44- handler , expectedError := createValidatingHandlerWhichReturnsStatusError ()
44+ It ("should return 200 in response when update succeeds" , func () {
4545
4646 response := handler .Handle (context .TODO (), Request {
4747 AdmissionRequest : v1beta1.AdmissionRequest {
48- Operation : v1beta1 .Create ,
48+ Operation : v1beta1 .Update ,
4949 Object : runtime.RawExtension {
5050 Raw : []byte ("{}" ),
5151 Object : handler .validator ,
5252 },
53+ OldObject : runtime.RawExtension {
54+ Raw : []byte ("{}" ),
55+ Object : handler .validator ,
56+ },
5357 },
5458 })
55- Expect (response .Allowed ).Should (BeFalse ())
56-
57- apiStatus , ok := expectedError .(apierrs.APIStatus )
58- Expect (ok ).Should (BeTrue ())
59- Expect (response .Result .Code ).Should (Equal (apiStatus .Status ().Code ))
60- Expect (* response .Result ).Should (Equal (apiStatus .Status ()))
61-
59+ Expect (response .Allowed ).Should (BeTrue ())
60+ Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
6261 })
6362
64- It ("should return 403 response when ValidateCreate returns non-APIStatus error" , func () {
65-
66- handler , expectedError := createValidatingHandlerWhichReturnsRegularError ()
63+ It ("should return 200 in response when delete succeeds" , func () {
6764
6865 response := handler .Handle (context .TODO (), Request {
6966 AdmissionRequest : v1beta1.AdmissionRequest {
70- Operation : v1beta1 .Create ,
71- Object : runtime.RawExtension {
67+ Operation : v1beta1 .Delete ,
68+ OldObject : runtime.RawExtension {
7269 Raw : []byte ("{}" ),
7370 Object : handler .validator ,
7471 },
7572 },
7673 })
77- Expect (response .Allowed ).Should (BeFalse ())
78- Expect (response .Result .Code ).Should (Equal (int32 (http .StatusForbidden )))
79- Expect (string (response .Result .Reason )).Should (Equal (expectedError .Error ()))
80-
74+ Expect (response .Allowed ).Should (BeTrue ())
75+ Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
8176 })
8277
83- It ( "should return 200 in response when update succeeds" , func () {
78+ })
8479
85- handler := createSucceedingValidatingHandler ()
80+ Context ("when dealing with Status errors" , func () {
81+
82+ expectedError := & apierrs.StatusError {
83+ ErrStatus : v1.Status {
84+ Message : "some message" ,
85+ Code : http .StatusUnprocessableEntity ,
86+ },
87+ }
88+ f := & fakeValidator {ErrorToReturn : expectedError }
89+ handler := validatingHandler {validator : f , decoder : decoder }
90+
91+ It ("should propagate the Status from ValidateCreate's return value to the HTTP response" , func () {
8692
8793 response := handler .Handle (context .TODO (), Request {
8894 AdmissionRequest : v1beta1.AdmissionRequest {
89- Operation : v1beta1 .Update ,
95+ Operation : v1beta1 .Create ,
9096 Object : runtime.RawExtension {
9197 Raw : []byte ("{}" ),
9298 Object : handler .validator ,
9399 },
94- OldObject : runtime.RawExtension {
95- Raw : []byte ("{}" ),
96- Object : handler .validator ,
97- },
98100 },
99101 })
100- Expect (response .Allowed ).Should (BeTrue ())
101- Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
102- })
103102
104- It ( "should return 400 in response when update fails on decoding new object" , func () {
105- //TODO
106- } )
103+ Expect ( response . Allowed ). Should ( BeFalse ())
104+ Expect ( response . Result . Code ). Should ( Equal ( expectedError . Status (). Code ))
105+ Expect ( * response . Result ). Should ( Equal ( expectedError . Status ()) )
107106
108- It ("should return 400 in response when update fails on decoding old object" , func () {
109- //TODO
110107 })
111108
112- It ("should return response built with the Status object when ValidateUpdate returns APIStatus error" , func () {
113-
114- handler , expectedError := createValidatingHandlerWhichReturnsStatusError ()
109+ It ("should propagate the Status from ValidateUpdate's return value to the HTTP response" , func () {
115110
116111 response := handler .Handle (context .TODO (), Request {
117112 AdmissionRequest : v1beta1.AdmissionRequest {
@@ -126,85 +121,78 @@ var _ = Describe("validatingHandler", func() {
126121 },
127122 },
128123 })
129- Expect (response .Allowed ).Should (BeFalse ())
130124
131- apiStatus , ok := expectedError .(apierrs.APIStatus )
132- Expect (ok ).Should (BeTrue ())
133- Expect (response .Result .Code ).Should (Equal (apiStatus .Status ().Code ))
134- Expect (* response .Result ).Should (Equal (apiStatus .Status ()))
125+ Expect (response .Allowed ).Should (BeFalse ())
126+ Expect (response .Result .Code ).Should (Equal (expectedError .Status ().Code ))
127+ Expect (* response .Result ).Should (Equal (expectedError .Status ()))
135128
136129 })
137130
138- It ("should return 403 response when ValidateUpdate returns non-APIStatus error" , func () {
139-
140- handler , expectedError := createValidatingHandlerWhichReturnsRegularError ()
131+ It ("should propagate the Status from ValidateDelete's return value to the HTTP response" , func () {
141132
142133 response := handler .Handle (context .TODO (), Request {
143134 AdmissionRequest : v1beta1.AdmissionRequest {
144- Operation : v1beta1 .Update ,
145- Object : runtime.RawExtension {
146- Raw : []byte ("{}" ),
147- Object : handler .validator ,
148- },
135+ Operation : v1beta1 .Delete ,
149136 OldObject : runtime.RawExtension {
150137 Raw : []byte ("{}" ),
151138 Object : handler .validator ,
152139 },
153140 },
154141 })
142+
155143 Expect (response .Allowed ).Should (BeFalse ())
156- Expect (response .Result .Code ).Should (Equal (int32 ( http . StatusForbidden ) ))
157- Expect (string ( response .Result . Reason )) .Should (Equal (expectedError .Error ()))
144+ Expect (response .Result .Code ).Should (Equal (expectedError . Status (). Code ))
145+ Expect (* response .Result ) .Should (Equal (expectedError .Status ()))
158146
159147 })
160148
161- It ("should return 200 in response when delete succeeds" , func () {
149+ })
150+ Context ("when dealing with non-status errors" , func () {
162151
163- handler := createSucceedingValidatingHandler ()
152+ expectedError := goerrors .New ("some error" )
153+ f := & fakeValidator {ErrorToReturn : expectedError }
154+ handler := validatingHandler {validator : f , decoder : decoder }
155+
156+ It ("should return 403 response when ValidateCreate with error message embedded" , func () {
164157
165158 response := handler .Handle (context .TODO (), Request {
166159 AdmissionRequest : v1beta1.AdmissionRequest {
167- Operation : v1beta1 .Delete ,
168- OldObject : runtime.RawExtension {
160+ Operation : v1beta1 .Create ,
161+ Object : runtime.RawExtension {
169162 Raw : []byte ("{}" ),
170163 Object : handler .validator ,
171164 },
172165 },
173166 })
174- Expect (response .Allowed ).Should (BeTrue ())
175- Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
176- } )
167+ Expect (response .Allowed ).Should (BeFalse ())
168+ Expect (response .Result .Code ).Should (Equal (int32 (http .StatusForbidden )))
169+ Expect ( string ( response . Result . Reason )). Should ( Equal ( expectedError . Error ()) )
177170
178- It ("should return 400 in response when delete fails on decode" , func () {
179- //TODO
180171 })
181172
182- It ("should return response built with the Status object when ValidateDelete returns APIStatus error" , func () {
183-
184- handler , expectedError := createValidatingHandlerWhichReturnsStatusError ()
173+ It ("should return 403 response when ValidateUpdate returns non-APIStatus error" , func () {
185174
186175 response := handler .Handle (context .TODO (), Request {
187176 AdmissionRequest : v1beta1.AdmissionRequest {
188- Operation : v1beta1 .Delete ,
177+ Operation : v1beta1 .Update ,
178+ Object : runtime.RawExtension {
179+ Raw : []byte ("{}" ),
180+ Object : handler .validator ,
181+ },
189182 OldObject : runtime.RawExtension {
190183 Raw : []byte ("{}" ),
191184 Object : handler .validator ,
192185 },
193186 },
194187 })
195188 Expect (response .Allowed ).Should (BeFalse ())
196-
197- apiStatus , ok := expectedError .(apierrs.APIStatus )
198- Expect (ok ).Should (BeTrue ())
199- Expect (response .Result .Code ).Should (Equal (apiStatus .Status ().Code ))
200- Expect (* response .Result ).Should (Equal (apiStatus .Status ()))
189+ Expect (response .Result .Code ).Should (Equal (int32 (http .StatusForbidden )))
190+ Expect (string (response .Result .Reason )).Should (Equal (expectedError .Error ()))
201191
202192 })
203193
204194 It ("should return 403 response when ValidateDelete returns non-APIStatus error" , func () {
205195
206- handler , expectedError := createValidatingHandlerWhichReturnsRegularError ()
207-
208196 response := handler .Handle (context .TODO (), Request {
209197 AdmissionRequest : v1beta1.AdmissionRequest {
210198 Operation : v1beta1 .Delete ,
@@ -219,7 +207,17 @@ var _ = Describe("validatingHandler", func() {
219207 Expect (string (response .Result .Reason )).Should (Equal (expectedError .Error ()))
220208
221209 })
210+
222211 })
212+
213+ PIt ("should return 400 in response when create fails on decode" , func () {})
214+
215+ PIt ("should return 400 in response when update fails on decoding new object" , func () {})
216+
217+ PIt ("should return 400 in response when update fails on decoding old object" , func () {})
218+
219+ PIt ("should return 400 in response when delete fails on decode" , func () {})
220+
223221})
224222
225223type fakeValidator struct {
@@ -253,28 +251,3 @@ func (v *fakeValidator) GroupVersionKind() schema.GroupVersionKind {
253251}
254252
255253func (v * fakeValidator ) SetGroupVersionKind (gvk schema.GroupVersionKind ) {}
256-
257- func createSucceedingValidatingHandler () * validatingHandler {
258- decoder , _ := NewDecoder (scheme .Scheme )
259- f := & fakeValidator {ErrorToReturn : nil }
260- return & validatingHandler {f , decoder }
261- }
262-
263- func createValidatingHandlerWhichReturnsRegularError () (validatingHandler , error ) {
264- decoder , _ := NewDecoder (scheme .Scheme )
265- errToReturn := goerrors .New ("some error" )
266- f := & fakeValidator {ErrorToReturn : errToReturn }
267- return validatingHandler {f , decoder }, errToReturn
268- }
269-
270- func createValidatingHandlerWhichReturnsStatusError () (validatingHandler , error ) {
271- decoder , _ := NewDecoder (scheme .Scheme )
272- errToReturn := & apierrs.StatusError {
273- ErrStatus : v1.Status {
274- Message : "some message" ,
275- Code : http .StatusUnprocessableEntity ,
276- },
277- }
278- f := & fakeValidator {ErrorToReturn : errToReturn }
279- return validatingHandler {f , decoder }, errToReturn
280- }
0 commit comments