1
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