@@ -26,6 +26,7 @@ import (
26
26
"reflect"
27
27
"testing"
28
28
29
+ "k8s.io/apimachinery/pkg/util/json"
29
30
"k8s.io/apimachinery/pkg/util/sets"
30
31
"k8s.io/apiserver/pkg/server/healthz"
31
32
"k8s.io/client-go/informers"
@@ -135,31 +136,36 @@ func TestNewWithDelegate(t *testing.T) {
135
136
// Wait for the hooks to finish before checking the response
136
137
<- delegatePostStartHookChan
137
138
<- wrappingPostStartHookChan
138
-
139
- checkPath (server .URL , http .StatusOK , `{
140
- "paths": [
141
- "/apis",
142
- "/bar",
143
- "/foo",
144
- "/healthz",
145
- "/healthz/delegate-health",
146
- "/healthz/log",
147
- "/healthz/ping",
148
- "/healthz/poststarthook/delegate-post-start-hook",
149
- "/healthz/poststarthook/generic-apiserver-start-informers",
150
- "/healthz/poststarthook/wrapping-post-start-hook",
151
- "/healthz/wrapping-health",
152
- "/metrics",
153
- "/readyz",
154
- "/readyz/delegate-health",
155
- "/readyz/log",
156
- "/readyz/ping",
157
- "/readyz/poststarthook/delegate-post-start-hook",
158
- "/readyz/poststarthook/generic-apiserver-start-informers",
159
- "/readyz/poststarthook/wrapping-post-start-hook",
160
- "/readyz/shutdown"
161
- ]
162
- }` , t )
139
+ expectedPaths := []string {
140
+ "/apis" ,
141
+ "/bar" ,
142
+ "/foo" ,
143
+ "/healthz" ,
144
+ "/healthz/delegate-health" ,
145
+ "/healthz/log" ,
146
+ "/healthz/ping" ,
147
+ "/healthz/poststarthook/delegate-post-start-hook" ,
148
+ "/healthz/poststarthook/generic-apiserver-start-informers" ,
149
+ "/healthz/poststarthook/wrapping-post-start-hook" ,
150
+ "/healthz/wrapping-health" ,
151
+ "/livez" ,
152
+ "/livez/delegate-health" ,
153
+ "/livez/log" ,
154
+ "/livez/ping" ,
155
+ "/livez/poststarthook/delegate-post-start-hook" ,
156
+ "/livez/poststarthook/generic-apiserver-start-informers" ,
157
+ "/livez/poststarthook/wrapping-post-start-hook" ,
158
+ "/metrics" ,
159
+ "/readyz" ,
160
+ "/readyz/delegate-health" ,
161
+ "/readyz/log" ,
162
+ "/readyz/ping" ,
163
+ "/readyz/poststarthook/delegate-post-start-hook" ,
164
+ "/readyz/poststarthook/generic-apiserver-start-informers" ,
165
+ "/readyz/poststarthook/wrapping-post-start-hook" ,
166
+ "/readyz/shutdown" ,
167
+ }
168
+ checkExpectedPathsAtRoot (server .URL , expectedPaths , t )
163
169
checkPath (server .URL + "/healthz" , http .StatusInternalServerError , `[+]ping ok
164
170
[+]log ok
165
171
[-]wrapping-health failed: reason withheld
@@ -181,22 +187,57 @@ healthz check failed
181
187
}
182
188
183
189
func checkPath (url string , expectedStatusCode int , expectedBody string , t * testing.T ) {
184
- resp , err := http .Get (url )
185
- if err != nil {
186
- t .Fatal (err )
187
- }
188
- dump , _ := httputil .DumpResponse (resp , true )
189
- t .Log (string (dump ))
190
+ t .Run (url , func (t * testing.T ) {
191
+ resp , err := http .Get (url )
192
+ if err != nil {
193
+ t .Fatal (err )
194
+ }
195
+ dump , _ := httputil .DumpResponse (resp , true )
196
+ t .Log (string (dump ))
190
197
191
- body , err := ioutil .ReadAll (resp .Body )
192
- if err != nil {
193
- t .Fatal (err )
194
- }
198
+ body , err := ioutil .ReadAll (resp .Body )
199
+ if err != nil {
200
+ t .Fatal (err )
201
+ }
195
202
196
- if e , a := expectedBody , string (body ); e != a {
197
- t .Errorf ("%q expected %v, got %v" , url , e , a )
198
- }
199
- if e , a := expectedStatusCode , resp .StatusCode ; e != a {
200
- t .Errorf ("%q expected %v, got %v" , url , e , a )
201
- }
203
+ if e , a := expectedBody , string (body ); e != a {
204
+ t .Errorf ("%q expected %v, got %v" , url , e , a )
205
+ }
206
+ if e , a := expectedStatusCode , resp .StatusCode ; e != a {
207
+ t .Errorf ("%q expected %v, got %v" , url , e , a )
208
+ }
209
+ })
210
+ }
211
+
212
+ func checkExpectedPathsAtRoot (url string , expectedPaths []string , t * testing.T ) {
213
+ t .Run (url , func (t * testing.T ) {
214
+ resp , err := http .Get (url )
215
+ if err != nil {
216
+ t .Fatal (err )
217
+ }
218
+ dump , _ := httputil .DumpResponse (resp , true )
219
+ t .Log (string (dump ))
220
+
221
+ body , err := ioutil .ReadAll (resp .Body )
222
+ if err != nil {
223
+ t .Fatal (err )
224
+ }
225
+ var result map [string ]interface {}
226
+ json .Unmarshal (body , & result )
227
+ paths , ok := result ["paths" ].([]interface {})
228
+ if ! ok {
229
+ t .Errorf ("paths not found" )
230
+ }
231
+ pathset := sets .NewString ()
232
+ for _ , p := range paths {
233
+ pathset .Insert (p .(string ))
234
+ }
235
+ expectedset := sets .NewString (expectedPaths ... )
236
+ for _ , p := range pathset .Difference (expectedset ) {
237
+ t .Errorf ("Got %v path, which we did not expect" , p )
238
+ }
239
+ for _ , p := range expectedset .Difference (pathset ) {
240
+ t .Errorf (" Expected %v path which we did not get" , p )
241
+ }
242
+ })
202
243
}
0 commit comments