...

Source file src/k8s.io/component-base/metrics/opts_test.go

Documentation: k8s.io/component-base/metrics

     1  /*
     2  Copyright 2019 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  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  )
    26  
    27  func TestDefaultStabilityLevel(t *testing.T) {
    28  	var tests = []struct {
    29  		name        string
    30  		inputValue  StabilityLevel
    31  		expectValue StabilityLevel
    32  		expectPanic bool
    33  	}{
    34  		{
    35  			name:        "empty should take ALPHA by default",
    36  			inputValue:  "",
    37  			expectValue: ALPHA,
    38  			expectPanic: false,
    39  		},
    40  		{
    41  			name:        "INTERNAL remain unchanged",
    42  			inputValue:  INTERNAL,
    43  			expectValue: INTERNAL,
    44  			expectPanic: false,
    45  		},
    46  		{
    47  			name:        "STABLE remain unchanged",
    48  			inputValue:  STABLE,
    49  			expectValue: STABLE,
    50  			expectPanic: false,
    51  		},
    52  	}
    53  
    54  	for _, tc := range tests {
    55  		tc := tc
    56  		t.Run(tc.name, func(t *testing.T) {
    57  			var stability = tc.inputValue
    58  
    59  			stability.setDefaults()
    60  			assert.Equalf(t, tc.expectValue, stability, "Got %s, expected: %v ", stability, tc.expectValue)
    61  		})
    62  	}
    63  }
    64  
    65  func TestConstrainToAllowedList(t *testing.T) {
    66  	allowList := &MetricLabelAllowList{
    67  		labelToAllowList: map[string]sets.String{
    68  			"label_a": sets.NewString("allow_value1", "allow_value2"),
    69  		},
    70  	}
    71  	labelNameList := []string{"label_a", "label_b"}
    72  	var tests = []struct {
    73  		name                 string
    74  		inputLabelValueList  []string
    75  		outputLabelValueList []string
    76  	}{
    77  		{
    78  			"no unexpected value",
    79  			[]string{"allow_value1", "label_b_value"},
    80  			[]string{"allow_value1", "label_b_value"},
    81  		},
    82  		{
    83  			"with unexpected value",
    84  			[]string{"not_allowed", "label_b_value"},
    85  			[]string{"unexpected", "label_b_value"},
    86  		},
    87  	}
    88  	for _, test := range tests {
    89  		t.Run(test.name, func(t *testing.T) {
    90  			allowList.ConstrainToAllowedList(labelNameList, test.inputLabelValueList)
    91  			if !reflect.DeepEqual(test.inputLabelValueList, test.outputLabelValueList) {
    92  				t.Errorf("Got %v, expected %v", test.inputLabelValueList, test.outputLabelValueList)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestConstrainLabelMap(t *testing.T) {
    99  	allowList := &MetricLabelAllowList{
   100  		labelToAllowList: map[string]sets.String{
   101  			"label_a": sets.NewString("allow_value1", "allow_value2"),
   102  		},
   103  	}
   104  	var tests = []struct {
   105  		name           string
   106  		inputLabelMap  map[string]string
   107  		outputLabelMap map[string]string
   108  	}{
   109  		{
   110  			"no unexpected value",
   111  			map[string]string{
   112  				"label_a": "allow_value1",
   113  				"label_b": "label_b_value",
   114  			},
   115  			map[string]string{
   116  				"label_a": "allow_value1",
   117  				"label_b": "label_b_value",
   118  			},
   119  		},
   120  		{
   121  			"with unexpected value",
   122  			map[string]string{
   123  				"label_a": "not_allowed",
   124  				"label_b": "label_b_value",
   125  			},
   126  			map[string]string{
   127  				"label_a": "unexpected",
   128  				"label_b": "label_b_value",
   129  			},
   130  		},
   131  	}
   132  	for _, test := range tests {
   133  		t.Run(test.name, func(t *testing.T) {
   134  			allowList.ConstrainLabelMap(test.inputLabelMap)
   135  			if !reflect.DeepEqual(test.inputLabelMap, test.outputLabelMap) {
   136  				t.Errorf("Got %v, expected %v", test.inputLabelMap, test.outputLabelMap)
   137  			}
   138  		})
   139  	}
   140  }
   141  

View as plain text