...

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

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

     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 feature
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/component-base/metrics/legacyregistry"
    25  	"k8s.io/component-base/metrics/testutil"
    26  )
    27  
    28  var (
    29  	testedMetrics = []string{"kubernetes_feature_enabled"}
    30  )
    31  
    32  func TestObserveHealthcheck(t *testing.T) {
    33  	defer legacyregistry.Reset()
    34  	defer ResetFeatureInfoMetric()
    35  
    36  	testCases := []struct {
    37  		desc    string
    38  		name    string
    39  		stage   string
    40  		enabled bool
    41  		want    string
    42  	}{
    43  		{
    44  			desc:    "test enabled",
    45  			name:    "feature-a",
    46  			stage:   "ALPHA",
    47  			enabled: true,
    48  			want: `
    49         	# HELP kubernetes_feature_enabled [BETA] This metric records the data about the stage and enablement of a k8s feature.
    50          # TYPE kubernetes_feature_enabled gauge
    51          kubernetes_feature_enabled{name="feature-a",stage="ALPHA"} 1
    52  `,
    53  		},
    54  		{
    55  			desc:    "test disabled",
    56  			name:    "feature-b",
    57  			stage:   "BETA",
    58  			enabled: false,
    59  			want: `
    60         	# HELP kubernetes_feature_enabled [BETA] This metric records the data about the stage and enablement of a k8s feature.
    61          # TYPE kubernetes_feature_enabled gauge
    62          kubernetes_feature_enabled{name="feature-b",stage="BETA"} 0
    63  `,
    64  		},
    65  	}
    66  
    67  	for _, test := range testCases {
    68  		t.Run(test.desc, func(t *testing.T) {
    69  			defer ResetFeatureInfoMetric()
    70  			RecordFeatureInfo(context.Background(), test.name, test.stage, test.enabled)
    71  
    72  			if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), testedMetrics...); err != nil {
    73  				t.Fatal(err)
    74  			}
    75  		})
    76  	}
    77  }
    78  

View as plain text