k8s.io/apiserver@v0.31.1/pkg/admission/metrics/testutil_test.go (about)

     1  /*
     2  Copyright 2017 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 metrics
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/component-base/metrics/legacyregistry"
    23  	"k8s.io/component-base/metrics/testutil"
    24  )
    25  
    26  // expectFindMetric find a metric with the given name nad labels or reports a fatal test error.
    27  func expectFindMetric(t *testing.T, name string, expectedLabels map[string]string) {
    28  	metrics, err := legacyregistry.DefaultGatherer.Gather()
    29  	if err != nil {
    30  		t.Fatalf("Failed to gather metrics: %s", err)
    31  	}
    32  
    33  	if len(metrics) == 0 {
    34  		t.Fatalf("No metric found with name %s and labels %#+v", name, expectedLabels)
    35  	}
    36  
    37  	for _, mf := range metrics {
    38  		if mf.GetName() == name {
    39  			for _, metric := range mf.GetMetric() {
    40  				if testutil.LabelsMatch(metric, expectedLabels) {
    41  					gotLabelCount := len(metric.GetLabel())
    42  					wantLabelCount := len(expectedLabels)
    43  					if wantLabelCount != gotLabelCount {
    44  						t.Errorf("Got metric with %d labels, but wanted %d labels. Wanted %#+v for %s",
    45  							gotLabelCount, wantLabelCount, expectedLabels, metric.String())
    46  						continue
    47  					}
    48  				}
    49  			}
    50  		}
    51  	}
    52  }
    53  
    54  // expectHistogramCountTotal ensures that the sum of counts of metrics matching the labelFilter is as
    55  // expected.
    56  func expectHistogramCountTotal(t *testing.T, name string, labelFilter map[string]string, wantCount int) {
    57  	metrics, err := legacyregistry.DefaultGatherer.Gather()
    58  	if err != nil {
    59  		t.Fatalf("Failed to gather metrics: %s", err)
    60  	}
    61  
    62  	counterSum := 0
    63  	for _, mf := range metrics {
    64  		if mf.GetName() != name {
    65  			continue // Ignore other metrics.
    66  		}
    67  		for _, metric := range mf.GetMetric() {
    68  			if !testutil.LabelsMatch(metric, labelFilter) {
    69  				continue
    70  			}
    71  			counterSum += int(metric.GetHistogram().GetSampleCount())
    72  		}
    73  	}
    74  	if wantCount != counterSum {
    75  		t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter)
    76  		for _, mf := range metrics {
    77  			if mf.GetName() == name {
    78  				for _, metric := range mf.GetMetric() {
    79  					t.Logf("\tnear match: %s", metric.String())
    80  				}
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  // expectCounterValue ensures that the counts of metrics matching the labelFilter is as
    87  // expected.
    88  func expectCounterValue(t *testing.T, name string, labelFilter map[string]string, wantCount int) {
    89  	metrics, err := legacyregistry.DefaultGatherer.Gather()
    90  	if err != nil {
    91  		t.Fatalf("Failed to gather metrics: %s", err)
    92  	}
    93  
    94  	counterSum := 0
    95  	for _, mf := range metrics {
    96  		if mf.GetName() != name {
    97  			continue // Ignore other metrics.
    98  		}
    99  		for _, metric := range mf.GetMetric() {
   100  			if !testutil.LabelsMatch(metric, labelFilter) {
   101  				continue
   102  			}
   103  			counterSum += int(metric.GetCounter().GetValue())
   104  		}
   105  	}
   106  	if wantCount != counterSum {
   107  		t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter)
   108  		for _, mf := range metrics {
   109  			if mf.GetName() == name {
   110  				for _, metric := range mf.GetMetric() {
   111  					t.Logf("\tnear match: %s", metric.String())
   112  				}
   113  			}
   114  		}
   115  	}
   116  }