-
Notifications
You must be signed in to change notification settings - Fork 2k
/
types_test.go
394 lines (372 loc) · 10.1 KB
/
types_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
Copyright 2018 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options
import (
"reflect"
"testing"
)
func TestResourceSetSet(t *testing.T) {
tests := []struct {
Desc string
Value string
Wanted ResourceSet
WantedError bool
}{
{
Desc: "empty resources",
Value: "",
Wanted: ResourceSet{},
WantedError: false,
},
{
Desc: "normal resources",
Value: "configmaps,cronjobs,daemonsets,deployments",
Wanted: ResourceSet(map[string]struct{}{
"configmaps": {},
"cronjobs": {},
"daemonsets": {},
"deployments": {},
}),
WantedError: false,
},
}
for _, test := range tests {
cs := &ResourceSet{}
gotError := cs.Set(test.Value)
if !(((gotError == nil && !test.WantedError) || (gotError != nil && test.WantedError)) && reflect.DeepEqual(*cs, test.Wanted)) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Wanted Error: %v, Got Error: %v", test.Desc, test.Wanted, *cs, test.WantedError, gotError)
}
}
}
func TestNamespaceListSet(t *testing.T) {
tests := []struct {
Desc string
Value string
Wanted NamespaceList
}{
{
Desc: "empty namespacelist",
Value: "",
Wanted: NamespaceList{},
},
{
Desc: "normal namespacelist",
Value: "default, kube-system",
Wanted: NamespaceList([]string{
"default",
"kube-system",
}),
},
}
for _, test := range tests {
ns := &NamespaceList{}
gotError := ns.Set(test.Value)
if gotError != nil || !reflect.DeepEqual(*ns, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Got Error: %v", test.Desc, test.Wanted, *ns, gotError)
}
}
}
func TestNamespaceList_GetNamespaces(t *testing.T) {
tests := []struct {
Desc string
Namespaces NamespaceList
Wanted NamespaceList
}{
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{},
Wanted: NamespaceList{""},
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
Wanted: NamespaceList{""},
},
{
Desc: "general namespaceDenylist",
Namespaces: NamespaceList{"default", "kube-system"},
Wanted: NamespaceList{"default", "kube-system"},
},
}
for i, test := range tests {
ns := &tests[i].Namespaces
allowedNamespaces := ns.GetNamespaces()
if !reflect.DeepEqual(allowedNamespaces, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, allowedNamespaces)
}
}
}
func TestNamespaceList_ExcludeNamespacesFieldSelector(t *testing.T) {
tests := []struct {
Desc string
Namespaces NamespaceList
DeniedNamespaces NamespaceList
Wanted string
}{
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{"default", "kube-system"},
DeniedNamespaces: NamespaceList{},
Wanted: "",
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"some-system"},
Wanted: "metadata.namespace!=some-system",
},
{
Desc: "general case",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system",
},
}
for _, test := range tests {
ns := test.Namespaces
deniedNS := test.DeniedNamespaces
actual := ns.GetExcludeNSFieldSelector(deniedNS)
if !reflect.DeepEqual(actual, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
}
}
}
func TestNodeFieldSelector(t *testing.T) {
tests := []struct {
Desc string
Node NodeType
Wanted string
}{
{
Desc: "empty node name",
Node: "",
Wanted: "",
},
{
Desc: "with node name",
Node: "k8s-node-1",
Wanted: "spec.nodeName=k8s-node-1",
},
}
for _, test := range tests {
node := test.Node
actual := node.GetNodeFieldSelector()
if !reflect.DeepEqual(actual, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
}
}
}
func TestMergeFieldSelectors(t *testing.T) {
tests := []struct {
Desc string
Namespaces NamespaceList
DeniedNamespaces NamespaceList
Node NodeType
Wanted string
}{
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{"default", "kube-system"},
DeniedNamespaces: NamespaceList{},
Node: "",
Wanted: "",
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"some-system"},
Node: "",
Wanted: "metadata.namespace!=some-system",
},
{
Desc: "general case",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
Node: "",
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system",
},
{
Desc: "empty DeniedNamespaces",
Namespaces: NamespaceList{"default", "kube-system"},
DeniedNamespaces: NamespaceList{},
Node: "k8s-node-1",
Wanted: "spec.nodeName=k8s-node-1",
},
{
Desc: "all DeniedNamespaces",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"some-system"},
Node: "k8s-node-1",
Wanted: "metadata.namespace!=some-system,spec.nodeName=k8s-node-1",
},
{
Desc: "general case",
Namespaces: DefaultNamespaces,
DeniedNamespaces: NamespaceList{"case1-system", "case2-system"},
Node: "k8s-node-1",
Wanted: "metadata.namespace!=case1-system,metadata.namespace!=case2-system,spec.nodeName=k8s-node-1",
},
}
for _, test := range tests {
ns := test.Namespaces
deniedNS := test.DeniedNamespaces
selector1 := ns.GetExcludeNSFieldSelector(deniedNS)
selector2 := test.Node.GetNodeFieldSelector()
actual, err := MergeFieldSelectors([]string{selector1, selector2})
if err != nil {
t.Errorf("Test error for Desc: %s. Can't merge field selector %v.", test.Desc, err)
}
if !reflect.DeepEqual(actual, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v.", test.Desc, test.Wanted, actual)
}
}
}
func TestMetricSetSet(t *testing.T) {
tests := []struct {
Desc string
Value string
Wanted MetricSet
}{
{
Desc: "empty metrics",
Value: "",
Wanted: MetricSet{},
},
{
Desc: "normal metrics",
Value: "kube_cronjob_info, kube_cronjob_labels, kube_daemonset_labels",
Wanted: MetricSet(map[string]struct{}{
"kube_cronjob_info": {},
"kube_cronjob_labels": {},
"kube_daemonset_labels": {},
}),
},
{
Desc: "newlines are ignored",
Value: "\n^kube_.+_annotations$,\n ^kube_secret_labels$\n",
Wanted: MetricSet{
"^kube_secret_labels$": struct{}{},
"^kube_.+_annotations$": struct{}{},
},
},
}
for _, test := range tests {
ms := &MetricSet{}
gotError := ms.Set(test.Value)
if gotError != nil || !reflect.DeepEqual(*ms, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Got Error: %v", test.Desc, test.Wanted, *ms, gotError)
}
}
}
func TestLabelsAllowListSet(t *testing.T) {
tests := []struct {
Desc string
Value string
Wanted LabelsAllowList
err bool
}{
{
Desc: "empty labels list",
Value: "",
Wanted: LabelsAllowList{},
},
{
Desc: "[invalid] space delimited",
Value: "cronjobs=[somelabel,label2] cronjobs=[label3,label4]",
Wanted: LabelsAllowList(map[string][]string{}),
err: true,
},
{
Desc: "[invalid] normal missing bracket",
Value: "cronjobs=[somelabel,label2],cronjobs=label3,label4]",
Wanted: LabelsAllowList(map[string][]string{}),
err: true,
},
{
Desc: "[invalid] no comma between metrics",
Value: "cronjobs=[somelabel,label2]cronjobs=[label3,label4]",
Wanted: LabelsAllowList(map[string][]string{}),
err: true,
},
{
Desc: "[invalid] no '=' between name and label list",
Value: "cronjobs[somelabel,label2]cronjobs=[label3,label4]",
Wanted: LabelsAllowList(map[string][]string{}),
err: true,
},
{
Desc: "one resource",
Value: "cronjobs=[somelabel.io,label2/blah]",
Wanted: LabelsAllowList(map[string][]string{
"cronjobs": {
"somelabel.io",
"label2/blah",
}}),
},
{
Desc: "two resources",
Value: "pods=[podsone,pods-two],nodes=[nodesone,nodestwo],namespaces=[nsone,nstwo]",
Wanted: LabelsAllowList(map[string][]string{
"pods": {
"podsone",
"pods-two"},
"nodes": {
"nodesone",
"nodestwo"},
"namespaces": {
"nsone",
"nstwo"}}),
},
{
Desc: "with empty allow labels",
Value: "cronjobs=[somelabel,label2],pods=[]",
Wanted: LabelsAllowList(map[string][]string{
"cronjobs": {
"somelabel",
"label2",
},
"pods": {}}),
},
{
Desc: "with wildcard",
Value: "cronjobs=[*],pods=[*,foo],namespaces=[bar,*]",
Wanted: LabelsAllowList(map[string][]string{
"cronjobs": {
"*",
},
"pods": {
"*",
"foo",
},
"namespaces": {
"bar",
"*"}}),
},
{
Desc: "with key as wildcard",
Value: "*=[*]",
Wanted: LabelsAllowList(map[string][]string{
"*": {
"*",
},
}),
},
}
for _, test := range tests {
lal := &LabelsAllowList{}
gotError := lal.Set(test.Value)
if gotError != nil && !test.err || !reflect.DeepEqual(*lal, test.Wanted) {
t.Errorf("Test error for Desc: %s\n Want: \n%+v\n Got: \n%#+v\n Got Error: %#v", test.Desc, test.Wanted, *lal, gotError)
}
}
}