sigs.k8s.io/kueue@v0.6.2/pkg/util/testing/metrics/metrics_test.go (about)

     1  /*
     2  Copyright 2023 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  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-cmp/cmp/cmpopts"
    24  	"github.com/prometheus/client_golang/prometheus"
    25  )
    26  
    27  func getTestGaugeVec() *prometheus.GaugeVec {
    28  	ret := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"l1", "l2"})
    29  	ret.With(prometheus.Labels{"l1": "l1v1", "l2": "l2v1"}).Set(1)
    30  	ret.With(prometheus.Labels{"l1": "l1v1", "l2": "l2v2"}).Set(2)
    31  	ret.With(prometheus.Labels{"l1": "l1v1", "l2": "l2v3"}).Set(3)
    32  	ret.With(prometheus.Labels{"l1": "l1v2", "l2": "l2v1"}).Set(4)
    33  	ret.With(prometheus.Labels{"l1": "l1v2", "l2": "l2v2"}).Set(5)
    34  	return ret
    35  }
    36  
    37  func TestCollect(t *testing.T) {
    38  	cases := map[string]struct {
    39  		vec    *prometheus.GaugeVec
    40  		labels map[string]string
    41  		want   []GaugeDataPoint
    42  	}{
    43  		"nil": {
    44  			vec:    nil,
    45  			labels: nil,
    46  			want:   nil,
    47  		},
    48  		"empty": {
    49  			vec:    prometheus.NewGaugeVec(prometheus.GaugeOpts{}, nil),
    50  			labels: nil,
    51  			want:   []GaugeDataPoint{},
    52  		},
    53  		"filter l1": {
    54  			vec:    getTestGaugeVec(),
    55  			labels: map[string]string{"l1": "l1v1"},
    56  			want: []GaugeDataPoint{
    57  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v1"}, Value: 1},
    58  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v2"}, Value: 2},
    59  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v3"}, Value: 3},
    60  			},
    61  		},
    62  		"filter l2": {
    63  			vec:    getTestGaugeVec(),
    64  			labels: map[string]string{"l2": "l2v1"},
    65  			want: []GaugeDataPoint{
    66  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v1"}, Value: 1},
    67  				{Labels: map[string]string{"l1": "l1v2", "l2": "l2v1"}, Value: 4},
    68  			},
    69  		},
    70  		"filter both": {
    71  			vec:    getTestGaugeVec(),
    72  			labels: map[string]string{"l1": "l1v2", "l2": "l2v1"},
    73  			want: []GaugeDataPoint{
    74  				{Labels: map[string]string{"l1": "l1v2", "l2": "l2v1"}, Value: 4},
    75  			},
    76  		},
    77  		"filter no match": {
    78  			vec:    getTestGaugeVec(),
    79  			labels: map[string]string{"l3": "l3v1"},
    80  			want:   []GaugeDataPoint{},
    81  		},
    82  		"empty filter": {
    83  			vec:    getTestGaugeVec(),
    84  			labels: nil,
    85  			want: []GaugeDataPoint{
    86  				{Labels: map[string]string{"l1": "l1v2", "l2": "l2v1"}, Value: 4},
    87  				{Labels: map[string]string{"l1": "l1v2", "l2": "l2v2"}, Value: 5},
    88  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v1"}, Value: 1},
    89  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v2"}, Value: 2},
    90  				{Labels: map[string]string{"l1": "l1v1", "l2": "l2v3"}, Value: 3},
    91  			},
    92  		},
    93  	}
    94  
    95  	for name, tc := range cases {
    96  		t.Run(name, func(t *testing.T) {
    97  			got := CollectFilteredGaugeVec(tc.vec, tc.labels)
    98  			if diff := cmp.Diff(tc.want, got, cmpopts.SortSlices(func(a, b GaugeDataPoint) bool { return a.Less(&b) })); len(diff) != 0 {
    99  				t.Errorf("Unexpected data points (-want,+got):\n%s", diff)
   100  			}
   101  		})
   102  
   103  	}
   104  }