-
Notifications
You must be signed in to change notification settings - Fork 6
/
kove_test.go
389 lines (340 loc) · 10.9 KB
/
kove_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
package main
import (
"fmt"
"testing"
diff "github.com/r3labs/diff/v2"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/prometheus/client_golang/prometheus/testutil"
)
var (
emptyMap = make(map[string]string)
annotationsTeam = map[string]string{"company.domain/team": "test"}
)
func initConfig() {
cp := "example/config/config.yaml"
configPath = &cp
conf = getConfig()
}
func TestContains(t *testing.T) {
tests := map[string]struct {
input1 []string
input2 string
want bool
}{
"single item": {input1: []string{"string1"}, input2: "string1", want: true},
"multiple items": {input1: []string{"string1", "string2"}, input2: "string1", want: true},
"not present": {input1: []string{"string1", "string2"}, input2: "not present", want: false},
"empty list": {input1: []string{}, input2: "string1", want: false},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := contains(tc.input1, tc.input2)
require.Equal(t, tc.want, got)
})
}
}
func TestLegitimateChange(t *testing.T) {
tests := map[string]struct {
oldResource *unstructured.Unstructured
newResource *unstructured.Unstructured
want bool
}{
"same resource": {
oldResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, false),
newResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, false),
want: false,
},
"rename": {
oldResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, false),
newResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test2", "2", emptyMap, emptyMap, false),
want: true,
},
"only resource version": {
oldResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, false),
newResource: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "2", emptyMap, emptyMap, false),
want: false,
},
}
initConfig()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
objDiff, _ := diff.Diff(tc.oldResource, tc.newResource)
got := legitimateChange(objDiff)
require.Equal(t, tc.want, got)
})
}
}
func TestHasOwnerRefs(t *testing.T) {
tests := map[string]struct {
obj *unstructured.Unstructured
want bool
}{
"no owner": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, false),
want: false,
},
"with owner": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", emptyMap, emptyMap, true),
want: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := hasOwnerRefs(tc.obj)
require.Equal(t, tc.want, got)
})
}
}
func TestEvaluate(t *testing.T) {
tests := map[string]struct {
obj *unstructured.Unstructured
previousViolations int
resetCount bool // Should the violation counter metric be reset after the test run.
want int
}{
"success": {
obj: newUnstructured("extensions/v1beta1", "deployment", "testEvaluate", "testEvaluate", "1", annotationsTeam, getChartLabels("4.0.0"), false),
previousViolations: 0,
resetCount: true,
want: 0,
},
"failure": {
obj: newUnstructured("extensions/v1beta1", "deployment", "testEvaluate", "testEvaluate", "1", annotationsTeam, getChartLabels("3.0.0"), false),
previousViolations: 0,
resetCount: true,
want: 1,
},
}
initConfig()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
evaluate(tc.obj, tc.previousViolations)
got := getNumberOfViolations()
if tc.resetCount {
// Reset counter for next test
violation.Reset()
}
require.Equal(t, tc.want, got)
})
}
}
func TestOnAdd(t *testing.T) {
tests := map[string]struct {
obj *unstructured.Unstructured
existing bool
resetCount bool // Should the violation counter metric be reset after the test run.
want int
}{
"add good": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("4.0.0"), false),
resetCount: true,
want: 0,
},
"add bad": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
resetCount: true,
want: 1,
},
"add bad child object": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), true),
resetCount: true,
want: 0,
},
}
initConfig()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
onAdd(tc.obj)
wg.Wait()
got := getNumberOfViolations()
if tc.resetCount {
// Reset counter for next test
violation.Reset()
}
require.Equal(t, tc.want, got)
})
}
}
func TestOnUpdate(t *testing.T) {
tests := map[string]struct {
oldObj *unstructured.Unstructured
newObj *unstructured.Unstructured
resetCount bool // Should the violation counter metric be reset after the test run.
want int
}{
"both good": {
oldObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("4.0.0"), false),
newObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("4.0.1"), false),
resetCount: true,
want: 0,
},
"good then bad": {
oldObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("4.0.0"), false),
newObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
resetCount: true,
want: 1,
},
"both bad": {
oldObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
newObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.1"), false),
resetCount: true,
want: 1,
},
"bad then good": {
oldObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
newObj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("4.0.0"), false),
resetCount: true,
want: 0,
},
}
initConfig()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
onAdd(tc.oldObj)
wg.Wait()
onUpdate(tc.oldObj, tc.newObj)
wg.Wait()
got := getNumberOfViolations()
if tc.resetCount {
// Reset counter for next test
violation.Reset()
}
require.Equal(t, tc.want, got)
})
}
}
func TestOnDelete(t *testing.T) {
tests := map[string]struct {
obj *unstructured.Unstructured
resetCount bool // Should the violation counter metric be reset after the test run.
want int
}{
"delete bad": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
resetCount: true,
want: 0,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
onAdd(tc.obj)
wg.Wait()
onDelete(tc.obj)
wg.Wait()
got := getNumberOfViolations()
if tc.resetCount {
// Reset counter for next test
violation.Reset()
}
require.Equal(t, tc.want, got)
})
}
}
func TestDeleteAllMetricsForObject(t *testing.T) {
tests := map[string]struct {
obj *unstructured.Unstructured
metricCount int // Number of metrics to create
resetCount bool // Should the violation counter metric be reset after the test run.
want int
}{
"delete with single": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
metricCount: 1,
resetCount: true,
want: 0,
},
"delete with multiple for same object": {
obj: newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false),
metricCount: 4,
resetCount: true,
want: 0,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
metricsToCreate := tc.metricCount
for metricsToCreate != 0 {
registerViolation(
tc.obj.GetName(),
tc.obj.GetNamespace(),
tc.obj.GetKind(),
tc.obj.GetAPIVersion(),
fmt.Sprintf("ruleset-%d", metricsToCreate),
fmt.Sprintf("data-%d", metricsToCreate),
)
metricsToCreate -= 1
}
deleteAllMetricsForObject(tc.obj)
got := getNumberOfViolations()
if tc.resetCount {
// Reset counter for next test
violation.Reset()
}
require.Equal(t, tc.want, got)
})
}
t.Run("delete with multiple for different objects", func(t *testing.T) {
objToDelete := newUnstructured("extensions/v1beta1", "deployment", "test", "test", "1", annotationsTeam, getChartLabels("3.0.0"), false)
otherObj := newUnstructured("extensions/v1beta1", "deployment", "other", "other", "1", annotationsTeam, getChartLabels("3.0.0"), false)
i := 0
metricsToCreate := 3
for i < metricsToCreate {
registerViolation(
objToDelete.GetName(),
objToDelete.GetNamespace(),
objToDelete.GetKind(),
objToDelete.GetAPIVersion(),
fmt.Sprintf("ruleset-%d", i),
fmt.Sprintf("data-%d", i),
)
registerViolation(
otherObj.GetName(),
otherObj.GetNamespace(),
otherObj.GetKind(),
otherObj.GetAPIVersion(),
fmt.Sprintf("ruleset-%d", i),
fmt.Sprintf("data-%d", i),
)
i += 1
}
deleteAllMetricsForObject(objToDelete)
got := getNumberOfViolations()
violation.Reset()
require.Equal(t, metricsToCreate, got)
})
}
func getNumberOfViolations() int {
return testutil.CollectAndCount(violation)
}
func newUnstructured(apiVersion, kind, namespace, name, resourceVersion string, annotations, labels map[string]string, setOwnerRef bool) *unstructured.Unstructured {
ret := unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
"resourceVersion": resourceVersion,
"annotations": annotations,
"labels": labels,
},
"spec": "test",
},
}
if setOwnerRef {
var ownerReferences []metav1.OwnerReference
ownerReferences = append(ownerReferences, metav1.OwnerReference{
APIVersion: apiVersion,
Kind: kind,
Name: name,
UID: "ad834522-d9a5-4841-beac-991ff3798c00",
})
ret.SetOwnerReferences(ownerReferences)
}
return &ret
}
func getChartLabels(vers string) map[string]string {
return map[string]string{"helm.sh/chart": fmt.Sprintf("specific-chart-name-%s", vers)}
}