...

Source file src/k8s.io/component-base/metrics/prometheus/slis/metrics_test.go

Documentation: k8s.io/component-base/metrics/prometheus/slis

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package slis
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/component-base/metrics"
    25  	"k8s.io/component-base/metrics/testutil"
    26  )
    27  
    28  var (
    29  	testedMetrics = []string{"kubernetes_healthcheck", "kubernetes_healthchecks_total"}
    30  )
    31  
    32  func TestObserveHealthcheck(t *testing.T) {
    33  	registry := metrics.NewKubeRegistry()
    34  	defer registry.Reset()
    35  	defer ResetHealthMetrics()
    36  	Register(registry)
    37  	initialState := Error
    38  	healthcheckName := "healthcheck-a"
    39  	initialOutput := `
    40          # HELP kubernetes_healthcheck [STABLE] This metric records the result of a single healthcheck.
    41          # TYPE kubernetes_healthcheck gauge
    42          kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 0
    43          # HELP kubernetes_healthchecks_total [STABLE] This metric records the results of all healthcheck.
    44          # TYPE kubernetes_healthchecks_total counter
    45          kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
    46  `
    47  	testCases := []struct {
    48  		desc     string
    49  		name     string
    50  		hcType   string
    51  		hcStatus HealthcheckStatus
    52  		want     string
    53  	}{
    54  		{
    55  			desc:     "test success",
    56  			name:     healthcheckName,
    57  			hcType:   "healthz",
    58  			hcStatus: Success,
    59  			want: `
    60          # HELP kubernetes_healthcheck [STABLE] This metric records the result of a single healthcheck.
    61          # TYPE kubernetes_healthcheck gauge
    62          kubernetes_healthcheck{name="healthcheck-a",type="healthz"} 1
    63          # HELP kubernetes_healthchecks_total [STABLE] This metric records the results of all healthcheck.
    64          # TYPE kubernetes_healthchecks_total counter
    65          kubernetes_healthchecks_total{name="healthcheck-a",status="error",type="healthz"} 1
    66          kubernetes_healthchecks_total{name="healthcheck-a",status="success",type="healthz"} 1
    67  `,
    68  		},
    69  	}
    70  
    71  	for _, test := range testCases {
    72  		t.Run(test.desc, func(t *testing.T) {
    73  			defer ResetHealthMetrics()
    74  			// let's first record an error as initial state
    75  			err := ObserveHealthcheck(context.Background(), test.name, test.hcType, initialState)
    76  			if err != nil {
    77  				t.Errorf("unexpected err: %v", err)
    78  			}
    79  			if err := testutil.GatherAndCompare(registry, strings.NewReader(initialOutput), testedMetrics...); err != nil {
    80  				t.Fatal(err)
    81  			}
    82  			// now record that we successfully purge state
    83  			err = ObserveHealthcheck(context.Background(), test.name, test.hcType, test.hcStatus)
    84  			if err != nil {
    85  				t.Errorf("unexpected err: %v", err)
    86  			}
    87  			if err := testutil.GatherAndCompare(registry, strings.NewReader(test.want), testedMetrics...); err != nil {
    88  				t.Fatal(err)
    89  			}
    90  		})
    91  	}
    92  }
    93  

View as plain text