@@ -23,9 +23,13 @@ import (
23
23
"net/http/httptest"
24
24
"net/url"
25
25
"reflect"
26
+ "strings"
26
27
"testing"
27
28
28
29
"k8s.io/apimachinery/pkg/util/sets"
30
+ "k8s.io/apiserver/pkg/endpoints/metrics"
31
+ "k8s.io/component-base/metrics/legacyregistry"
32
+ "k8s.io/component-base/metrics/testutil"
29
33
)
30
34
31
35
func TestInstallHandler (t * testing.T ) {
@@ -232,6 +236,35 @@ func TestGetExcludedChecks(t *testing.T) {
232
236
}
233
237
}
234
238
239
+ func TestMetrics (t * testing.T ) {
240
+ mux := http .NewServeMux ()
241
+ InstallHandler (mux )
242
+ InstallLivezHandler (mux )
243
+ InstallReadyzHandler (mux )
244
+ metrics .Register ()
245
+ metrics .Reset ()
246
+
247
+ paths := []string {"/healthz" , "/livez" , "/readyz" }
248
+ for _ , path := range paths {
249
+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("http://example.com%s" , path ), nil )
250
+ if err != nil {
251
+ t .Errorf ("%v" , err )
252
+ }
253
+ mux .ServeHTTP (httptest .NewRecorder (), req )
254
+ }
255
+
256
+ expected := strings .NewReader (`
257
+ # HELP apiserver_request_total [ALPHA] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, client, and HTTP response contentType and code.
258
+ # TYPE apiserver_request_total counter
259
+ apiserver_request_total{client="unknown",code="200",component="",contentType="text/plain; charset=utf-8",dry_run="",group="",resource="",scope="",subresource="/healthz",verb="GET",version=""} 1
260
+ apiserver_request_total{client="unknown",code="200",component="",contentType="text/plain; charset=utf-8",dry_run="",group="",resource="",scope="",subresource="/livez",verb="GET",version=""} 1
261
+ apiserver_request_total{client="unknown",code="200",component="",contentType="text/plain; charset=utf-8",dry_run="",group="",resource="",scope="",subresource="/readyz",verb="GET",version=""} 1
262
+ ` )
263
+ if err := testutil .GatherAndCompare (legacyregistry .DefaultGatherer , expected , "apiserver_request_total" ); err != nil {
264
+ t .Error (err )
265
+ }
266
+ }
267
+
235
268
func createGetRequestWithUrl (rawUrlString string ) * http.Request {
236
269
url , _ := url .Parse (rawUrlString )
237
270
return & http.Request {
0 commit comments