@@ -17,10 +17,15 @@ import (
17
17
)
18
18
19
19
var _ = Describe ("validatingHandler" , func () {
20
- Describe ("Handle" , func () {
21
- It ("should return 200 in response when create succeeds" , func () {
22
20
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 () {
24
29
25
30
response := handler .Handle (context .TODO (), Request {
26
31
AdmissionRequest : v1beta1.AdmissionRequest {
@@ -31,87 +36,77 @@ var _ = Describe("validatingHandler", func() {
31
36
},
32
37
},
33
38
})
39
+
34
40
Expect (response .Allowed ).Should (BeTrue ())
35
41
Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
36
42
})
37
43
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 () {
45
45
46
46
response := handler .Handle (context .TODO (), Request {
47
47
AdmissionRequest : v1beta1.AdmissionRequest {
48
- Operation : v1beta1 .Create ,
48
+ Operation : v1beta1 .Update ,
49
49
Object : runtime.RawExtension {
50
50
Raw : []byte ("{}" ),
51
51
Object : handler .validator ,
52
52
},
53
+ OldObject : runtime.RawExtension {
54
+ Raw : []byte ("{}" ),
55
+ Object : handler .validator ,
56
+ },
53
57
},
54
58
})
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 )))
62
61
})
63
62
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 () {
67
64
68
65
response := handler .Handle (context .TODO (), Request {
69
66
AdmissionRequest : v1beta1.AdmissionRequest {
70
- Operation : v1beta1 .Create ,
71
- Object : runtime.RawExtension {
67
+ Operation : v1beta1 .Delete ,
68
+ OldObject : runtime.RawExtension {
72
69
Raw : []byte ("{}" ),
73
70
Object : handler .validator ,
74
71
},
75
72
},
76
73
})
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 )))
81
76
})
82
77
83
- It ( "should return 200 in response when update succeeds" , func () {
78
+ })
84
79
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 () {
86
92
87
93
response := handler .Handle (context .TODO (), Request {
88
94
AdmissionRequest : v1beta1.AdmissionRequest {
89
- Operation : v1beta1 .Update ,
95
+ Operation : v1beta1 .Create ,
90
96
Object : runtime.RawExtension {
91
97
Raw : []byte ("{}" ),
92
98
Object : handler .validator ,
93
99
},
94
- OldObject : runtime.RawExtension {
95
- Raw : []byte ("{}" ),
96
- Object : handler .validator ,
97
- },
98
100
},
99
101
})
100
- Expect (response .Allowed ).Should (BeTrue ())
101
- Expect (response .Result .Code ).Should (Equal (int32 (http .StatusOK )))
102
- })
103
102
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 ()) )
107
106
108
- It ("should return 400 in response when update fails on decoding old object" , func () {
109
- //TODO
110
107
})
111
108
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 () {
115
110
116
111
response := handler .Handle (context .TODO (), Request {
117
112
AdmissionRequest : v1beta1.AdmissionRequest {
@@ -126,85 +121,78 @@ var _ = Describe("validatingHandler", func() {
126
121
},
127
122
},
128
123
})
129
- Expect (response .Allowed ).Should (BeFalse ())
130
124
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 ()))
135
128
136
129
})
137
130
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 () {
141
132
142
133
response := handler .Handle (context .TODO (), Request {
143
134
AdmissionRequest : v1beta1.AdmissionRequest {
144
- Operation : v1beta1 .Update ,
145
- Object : runtime.RawExtension {
146
- Raw : []byte ("{}" ),
147
- Object : handler .validator ,
148
- },
135
+ Operation : v1beta1 .Delete ,
149
136
OldObject : runtime.RawExtension {
150
137
Raw : []byte ("{}" ),
151
138
Object : handler .validator ,
152
139
},
153
140
},
154
141
})
142
+
155
143
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 ()))
158
146
159
147
})
160
148
161
- It ("should return 200 in response when delete succeeds" , func () {
149
+ })
150
+ Context ("when dealing with non-status errors" , func () {
162
151
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 () {
164
157
165
158
response := handler .Handle (context .TODO (), Request {
166
159
AdmissionRequest : v1beta1.AdmissionRequest {
167
- Operation : v1beta1 .Delete ,
168
- OldObject : runtime.RawExtension {
160
+ Operation : v1beta1 .Create ,
161
+ Object : runtime.RawExtension {
169
162
Raw : []byte ("{}" ),
170
163
Object : handler .validator ,
171
164
},
172
165
},
173
166
})
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 ()) )
177
170
178
- It ("should return 400 in response when delete fails on decode" , func () {
179
- //TODO
180
171
})
181
172
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 () {
185
174
186
175
response := handler .Handle (context .TODO (), Request {
187
176
AdmissionRequest : v1beta1.AdmissionRequest {
188
- Operation : v1beta1 .Delete ,
177
+ Operation : v1beta1 .Update ,
178
+ Object : runtime.RawExtension {
179
+ Raw : []byte ("{}" ),
180
+ Object : handler .validator ,
181
+ },
189
182
OldObject : runtime.RawExtension {
190
183
Raw : []byte ("{}" ),
191
184
Object : handler .validator ,
192
185
},
193
186
},
194
187
})
195
188
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 ()))
201
191
202
192
})
203
193
204
194
It ("should return 403 response when ValidateDelete returns non-APIStatus error" , func () {
205
195
206
- handler , expectedError := createValidatingHandlerWhichReturnsRegularError ()
207
-
208
196
response := handler .Handle (context .TODO (), Request {
209
197
AdmissionRequest : v1beta1.AdmissionRequest {
210
198
Operation : v1beta1 .Delete ,
@@ -219,7 +207,17 @@ var _ = Describe("validatingHandler", func() {
219
207
Expect (string (response .Result .Reason )).Should (Equal (expectedError .Error ()))
220
208
221
209
})
210
+
222
211
})
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
+
223
221
})
224
222
225
223
type fakeValidator struct {
@@ -253,28 +251,3 @@ func (v *fakeValidator) GroupVersionKind() schema.GroupVersionKind {
253
251
}
254
252
255
253
func (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