@@ -17,18 +17,87 @@ limitations under the License.
17
17
package customresourcestate
18
18
19
19
import (
20
+ "encoding/json"
21
+ "reflect"
20
22
"testing"
23
+
24
+ "k8s.io/apimachinery/pkg/runtime/schema"
25
+ "k8s.io/utils/pointer"
21
26
)
22
27
23
28
func TestNewCustomResourceMetrics (t * testing.T ) {
29
+
24
30
tests := []struct {
25
- r Resource
26
- wantErr bool
27
- name string
31
+ r Resource
32
+ wantErr bool
33
+ wantResult * customResourceMetrics
34
+ name string
28
35
}{
29
36
{
30
37
// https://github.com/kubernetes/kube-state-metrics/issues/1886
31
- name : "dynamic metric type (not just hardcoded to gauge)" ,
38
+ name : "cr metric with dynamic metric type" ,
39
+ r : Resource {
40
+ GroupVersionKind : GroupVersionKind {
41
+ Group : "apps" ,
42
+ Version : "v1" ,
43
+ Kind : "Deployment" ,
44
+ },
45
+ Labels : Labels {
46
+ LabelsFromPath : map [string ][]string {
47
+ "name" : {"metadata" , "name" },
48
+ },
49
+ CommonLabels : map [string ]string {
50
+ "hello" : "world" ,
51
+ },
52
+ },
53
+ Metrics : []Generator {
54
+ {
55
+ Name : "test_metrics" ,
56
+ Help : "metrics for testing" ,
57
+ Each : Metric {
58
+ Type : MetricTypeInfo ,
59
+ Info : & MetricInfo {
60
+ MetricMeta : MetricMeta {
61
+ Path : []string {
62
+ "metadata" ,
63
+ "annotations" ,
64
+ },
65
+ },
66
+ LabelFromKey : "test" ,
67
+ },
68
+ },
69
+ },
70
+ },
71
+ },
72
+ wantErr : false ,
73
+ wantResult : & customResourceMetrics {
74
+ MetricNamePrefix : "kube_customresource" ,
75
+ GroupVersionKind : schema.GroupVersionKind {
76
+ Group : "apps" ,
77
+ Version : "v1" ,
78
+ Kind : "Deployment" ,
79
+ },
80
+ ResourceName : "deployments" ,
81
+ Families : []compiledFamily {
82
+ {
83
+ Name : "kube_customresource_test_metrics" ,
84
+ Help : "metrics for testing" ,
85
+ Each : & compiledInfo {},
86
+ Labels : map [string ]string {
87
+ "customresource_group" : "apps" ,
88
+ "customresource_kind" : "Deployment" ,
89
+ "customresource_version" : "v1" ,
90
+ "hello" : "world" ,
91
+ },
92
+ LabelFromPath : map [string ]valuePath {
93
+ "name" : mustCompilePath (t , "metadata" , "name" ),
94
+ },
95
+ },
96
+ },
97
+ },
98
+ },
99
+ {
100
+ name : "cr metric with custom prefix" ,
32
101
r : Resource {
33
102
GroupVersionKind : GroupVersionKind {
34
103
Group : "apps" ,
@@ -39,6 +108,9 @@ func TestNewCustomResourceMetrics(t *testing.T) {
39
108
LabelsFromPath : map [string ][]string {
40
109
"name" : {"metadata" , "name" },
41
110
},
111
+ CommonLabels : map [string ]string {
112
+ "hello" : "world" ,
113
+ },
42
114
},
43
115
Metrics : []Generator {
44
116
{
@@ -58,18 +130,118 @@ func TestNewCustomResourceMetrics(t *testing.T) {
58
130
},
59
131
},
60
132
},
133
+ MetricNamePrefix : pointer .String ("apps_deployment" ),
61
134
},
62
135
wantErr : false ,
136
+ wantResult : & customResourceMetrics {
137
+ MetricNamePrefix : "apps_deployment" ,
138
+ GroupVersionKind : schema.GroupVersionKind {
139
+ Group : "apps" ,
140
+ Version : "v1" ,
141
+ Kind : "Deployment" ,
142
+ },
143
+ ResourceName : "deployments" ,
144
+ Families : []compiledFamily {
145
+ {
146
+ Name : "apps_deployment_test_metrics" ,
147
+ Help : "metrics for testing" ,
148
+ Each : & compiledInfo {},
149
+ Labels : map [string ]string {
150
+ "customresource_group" : "apps" ,
151
+ "customresource_kind" : "Deployment" ,
152
+ "customresource_version" : "v1" ,
153
+ "hello" : "world" ,
154
+ },
155
+ LabelFromPath : map [string ]valuePath {
156
+ "name" : mustCompilePath (t , "metadata" , "name" ),
157
+ },
158
+ },
159
+ },
160
+ },
161
+ },
162
+ {
163
+ name : "cr metric with custom prefix - expect error" ,
164
+ r : Resource {
165
+ GroupVersionKind : GroupVersionKind {
166
+ Group : "apps" ,
167
+ Version : "v1" ,
168
+ Kind : "Deployment" ,
169
+ },
170
+ Labels : Labels {
171
+ LabelsFromPath : map [string ][]string {
172
+ "name" : {"metadata" , "name" },
173
+ },
174
+ CommonLabels : map [string ]string {
175
+ "hello" : "world" ,
176
+ },
177
+ },
178
+ Metrics : []Generator {
179
+ {
180
+ Name : "test_metrics" ,
181
+ Help : "metrics for testing" ,
182
+ Each : Metric {
183
+ Type : MetricTypeInfo ,
184
+ Info : & MetricInfo {
185
+ MetricMeta : MetricMeta {
186
+ Path : []string {
187
+ "metadata" ,
188
+ "annotations" ,
189
+ },
190
+ },
191
+ LabelFromKey : "test" ,
192
+ },
193
+ },
194
+ },
195
+ },
196
+ MetricNamePrefix : pointer .String ("apps_deployment" ),
197
+ },
198
+ wantErr : true ,
199
+ wantResult : & customResourceMetrics {
200
+ GroupVersionKind : schema.GroupVersionKind {
201
+ Group : "apps" ,
202
+ Version : "v1" ,
203
+ Kind : "Deployment" ,
204
+ },
205
+ ResourceName : "deployments" ,
206
+ Families : []compiledFamily {
207
+ {
208
+ Name : "apps_deployment_test_metrics" ,
209
+ Help : "metrics for testing" ,
210
+ Each : & compiledInfo {},
211
+ Labels : map [string ]string {
212
+ "customresource_group" : "apps" ,
213
+ "customresource_kind" : "Deployment" ,
214
+ "customresource_version" : "v1" ,
215
+ "hello" : "world" ,
216
+ },
217
+ LabelFromPath : map [string ]valuePath {
218
+ "name" : mustCompilePath (t , "metadata" , "name" ),
219
+ },
220
+ },
221
+ },
222
+ },
63
223
},
64
224
}
65
225
66
226
for _ , tt := range tests {
67
227
t .Run (tt .name , func (t * testing.T ) {
68
228
v , err := NewCustomResourceMetrics (tt .r )
69
- expectedError := v .(* customResourceMetrics ).Families [0 ].Each .Type () != "info"
70
- if (err != nil ) != tt .wantErr || expectedError {
71
- t .Errorf ("NewCustomResourceMetrics() error = %v, wantErr %v" , err , tt .wantErr )
72
- return
229
+ if err != nil {
230
+ t .Errorf (err .Error ())
231
+ }
232
+
233
+ // convert to JSON for easier nil comparison
234
+ ttWantJSON , _ := json .Marshal (tt .wantResult )
235
+ customResourceMetricJSON , _ := json .Marshal (v .(* customResourceMetrics ))
236
+
237
+ if ! tt .wantErr && ! reflect .DeepEqual (ttWantJSON , customResourceMetricJSON ) {
238
+ t .Errorf ("NewCustomResourceMetrics: error expected %v" , tt .wantErr )
239
+ t .Errorf ("NewCustomResourceMetrics:\n %#v\n is not deep equal\n %#v" , v , tt .wantResult )
240
+ }
241
+
242
+ if tt .wantErr && reflect .DeepEqual (ttWantJSON , customResourceMetricJSON ) {
243
+ t .Errorf ("NewCustomResourceMetrics: error expected %v" , tt .wantErr )
244
+ t .Errorf ("NewCustomResourceMetrics:\n %#v\n is not deep equal\n %#v" , v , tt .wantResult )
73
245
}
74
246
})
75
247
}
0 commit comments