@@ -24,16 +24,21 @@ import (
24
24
"net/http"
25
25
"os"
26
26
"path"
27
+ "regexp"
27
28
"strings"
28
29
"testing"
29
30
31
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
32
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
33
+ "k8s.io/component-base/zpages/features"
30
34
"k8s.io/klog/v2/ktesting"
31
35
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
32
36
kubeschedulertesting "k8s.io/kubernetes/cmd/kube-scheduler/app/testing"
33
37
"k8s.io/kubernetes/test/integration/framework"
34
38
)
35
39
36
- func TestHealthEndpoints (t * testing.T ) {
40
+ func TestEndpointHandlers (t * testing.T ) {
41
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ComponentFlagz , true )
37
42
server , configStr , _ , err := startTestAPIServer (t )
38
43
if err != nil {
39
44
t .Fatalf ("Failed to start kube-apiserver server: %v" , err )
@@ -64,52 +69,64 @@ func TestHealthEndpoints(t *testing.T) {
64
69
}()
65
70
66
71
tests := []struct {
67
- name string
68
- path string
69
- useBrokenConfig bool
70
- wantResponseCode int
72
+ name string
73
+ path string
74
+ useBrokenConfig bool
75
+ requestHeader map [string ]string
76
+ wantResponseCode int
77
+ wantResponseBodyRegx string
71
78
}{
72
79
{
73
- "/healthz" ,
74
- "/healthz" ,
75
- false ,
76
- http .StatusOK ,
80
+ name : "/healthz" ,
81
+ path : "/healthz" ,
82
+ useBrokenConfig : false ,
83
+ wantResponseCode : http .StatusOK ,
77
84
},
78
85
{
79
- "/livez" ,
80
- "/livez" ,
81
- false ,
82
- http .StatusOK ,
86
+ name : "/livez" ,
87
+ path : "/livez" ,
88
+ useBrokenConfig : false ,
89
+ wantResponseCode : http .StatusOK ,
83
90
},
84
91
{
85
- "/livez with ping check" ,
86
- "/livez/ping" ,
87
- false ,
88
- http .StatusOK ,
92
+ name : "/livez with ping check" ,
93
+ path : "/livez/ping" ,
94
+ useBrokenConfig : false ,
95
+ wantResponseCode : http .StatusOK ,
89
96
},
90
97
{
91
- "/readyz" ,
92
- "/readyz" ,
93
- false ,
94
- http .StatusOK ,
98
+ name : "/readyz" ,
99
+ path : "/readyz" ,
100
+ useBrokenConfig : false ,
101
+ wantResponseCode : http .StatusOK ,
95
102
},
96
103
{
97
- "/readyz with sched-handler-sync" ,
98
- "/readyz/sched-handler-sync" ,
99
- false ,
100
- http .StatusOK ,
104
+ name : "/readyz with sched-handler-sync" ,
105
+ path : "/readyz/sched-handler-sync" ,
106
+ useBrokenConfig : false ,
107
+ wantResponseCode : http .StatusOK ,
101
108
},
102
109
{
103
- "/readyz with shutdown" ,
104
- "/readyz/shutdown" ,
105
- false ,
106
- http .StatusOK ,
110
+ name : "/readyz with shutdown" ,
111
+ path : "/readyz/shutdown" ,
112
+ useBrokenConfig : false ,
113
+ wantResponseCode : http .StatusOK ,
107
114
},
108
115
{
109
- "/readyz with broken apiserver" ,
110
- "/readyz" ,
111
- true ,
112
- http .StatusInternalServerError ,
116
+ name : "/readyz with broken apiserver" ,
117
+ path : "/readyz" ,
118
+ useBrokenConfig : true ,
119
+ wantResponseCode : http .StatusInternalServerError ,
120
+ },
121
+ {
122
+ name : "/flagz" ,
123
+ path : "/flagz" ,
124
+ requestHeader : map [string ]string {"Accept" : "text/plain" },
125
+ wantResponseCode : http .StatusOK ,
126
+ wantResponseBodyRegx : `^\n` +
127
+ `kube-scheduler flags\n` +
128
+ `Warning: This endpoint is not meant to be machine parseable, ` +
129
+ `has no formatting compatibility guarantees and is for debugging purposes only.` ,
113
130
},
114
131
}
115
132
@@ -141,6 +158,11 @@ func TestHealthEndpoints(t *testing.T) {
141
158
if err != nil {
142
159
t .Fatalf ("failed to request: %v" , err )
143
160
}
161
+ if tt .requestHeader != nil {
162
+ for k , v := range tt .requestHeader {
163
+ req .Header .Set (k , v )
164
+ }
165
+ }
144
166
r , err := client .Do (req )
145
167
if err != nil {
146
168
t .Fatalf ("failed to GET %s from component: %v" , tt .path , err )
@@ -156,6 +178,15 @@ func TestHealthEndpoints(t *testing.T) {
156
178
if got , expected := r .StatusCode , tt .wantResponseCode ; got != expected {
157
179
t .Fatalf ("expected http %d at %s of component, got: %d %q" , expected , tt .path , got , string (body ))
158
180
}
181
+ if tt .wantResponseBodyRegx != "" {
182
+ matched , err := regexp .MatchString (tt .wantResponseBodyRegx , string (body ))
183
+ if err != nil {
184
+ t .Fatalf ("failed to compile regex: %v" , err )
185
+ }
186
+ if ! matched {
187
+ t .Fatalf ("response body does not match regex.\n Expected:\n %s\n \n Got:\n %s" , tt .wantResponseBodyRegx , string (body ))
188
+ }
189
+ }
159
190
})
160
191
}
161
192
}
0 commit comments