-
Notifications
You must be signed in to change notification settings - Fork 8
/
controller_test.go
491 lines (389 loc) · 12.3 KB
/
controller_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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package main
import (
"fmt"
"math/rand"
"os"
"testing"
"gopkg.in/yaml.v2"
"time"
corev1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/prometheus/prometheus/pkg/rulefmt"
. "github.com/smartystreets/goconvey/convey"
)
const(
myAnno = "nordstrom.net/prometheus2alerts"
)
var (
c *Controller
events *ConfigMapEventContainer
testRules []byte
testRuleGroup []byte
testRuleGroups []byte
configmapDataBlockRules corev1.ConfigMap
configmapDataBlockRulesGroup corev1.ConfigMap
configmapDataBlockRulesGroups corev1.ConfigMap
configmapDataBlockAllThree corev1.ConfigMap
configmapNoAnnotation corev1.ConfigMap
)
type ConfigMapEventContainer struct {
Events []ConfigMapEvent
}
type ConfigMapEvent struct {
CMName string
CMNamespace string
Eventtype string
Message string
Reason string
}
func TestMain(m *testing.M) {
rsource := rand.NewSource(time.Now().UnixNano())
testRulesObj := validRulesArray()
testRuleGroupObj := rulefmt.RuleGroup{
Name: "TestGroup",
Rules: testRulesObj,
}
testRuleGroupsObj := rulefmt.RuleGroups{
Groups: []rulefmt.RuleGroup{
testRuleGroupObj,
},
}
var err error
testRules, err = yaml.Marshal(testRulesObj)
if err != nil {
fmt.Println(err)
}
testRuleGroup, err = yaml.Marshal(testRuleGroupObj)
if err != nil {
fmt.Println(err)
}
testRuleGroups, err = yaml.Marshal(testRuleGroupsObj)
if err != nil {
fmt.Println(err)
}
configmapDataBlockRules = corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: "rules",
Namespace: "default",
Annotations: map[string]string{myAnno: "true"},
},
Data: nil,
}
configmapDataBlockRulesGroup = corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: "rules-group",
Namespace: "default",
Annotations: map[string]string{myAnno: "true"},
},
Data: nil,
}
configmapDataBlockRulesGroups = corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: "rules-groups",
Namespace: "default",
Annotations: map[string]string{myAnno: "true"},
},
Data: nil,
}
configmapDataBlockAllThree = corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: "rules-groups-all-three",
Namespace: "default",
Annotations: map[string]string{myAnno: "true"},
},
Data: nil,
}
configmapNoAnnotation = configmapDataBlockRules
configmapNoAnnotation.ObjectMeta.Annotations = nil
configmapDataBlockRules.Data = map[string]string{
"rules": string(testRules),
}
configmapDataBlockRulesGroup.Data = map[string]string{
"rules": string(testRuleGroup),
}
configmapDataBlockRulesGroups.Data = map[string]string{
"rules": string(testRuleGroup),
}
configmapDataBlockAllThree.Data = map[string]string{
"aaa": string(testRules),
"bbb": string(testRuleGroup),
"ccc": string(testRuleGroups),
}
configmapNoAnnotation.Data = map[string]string{
"rules": string(testRules),
}
anno := myAnno
events = &ConfigMapEventContainer{}
// just what we need
c = &Controller{
kubeclientset: nil,
configmapsLister: nil,
configmapsSynced: nil,
workqueue: nil,
recorder: nil,
resourceVersionMap: make(map[string]string),
interestingAnnotation: &anno,
reloadEndpoint: nil,
rulesPath: nil,
randSrc: &rsource,
configmapEventRecorderFunc: events.Add,
}
os.Exit(m.Run())
}
func TestExtractRuleGroups(t *testing.T) {
Convey("Test extractRuleGroups, a properly formatted rule groups", t, func() {
Convey("Positive test case", func() {
err, rulegroups := c.extractRuleGroups(string(testRuleGroups))
So(err, ShouldBeNil)
So(rulegroups.Groups[0].Name, ShouldEqual, "TestGroup")
So(rulegroups.Groups[0].Rules[0].Record, ShouldEqual, "job:http_inprogress_requests:sum")
})
Convey( "Negative test case", func() {
err, rulegroups := c.extractRuleGroups(string(testRuleGroup))
So(err, ShouldNotBeNil)
So(len(rulegroups.Groups), ShouldEqual, 0)
})
})
}
func TestExtractRuleGroupAsRuleGroups(t *testing.T) {
Convey("Test extractRuleGroupAsRuleGroups, a properly formatted rule group", t, func() {
Convey("Positive test case", func() {
err, rulegroups := c.extractRuleGroupAsRuleGroups(string(testRuleGroup))
So(err, ShouldBeNil)
So(rulegroups.Groups[0].Name, ShouldEqual, "TestGroup")
So(rulegroups.Groups[0].Rules[0].Record, ShouldEqual, "job:http_inprogress_requests:sum")
})
Convey("Negative test case", func() {
err, rulegroups := c.extractRuleGroupAsRuleGroups(string(testRuleGroups))
So(err, ShouldNotBeNil)
So(len(rulegroups.Groups), ShouldEqual, 0)
})
})
}
func TestExtractRulesAsRuleGroups(t *testing.T) {
Convey("Test extractRuleGroupAsRuleGroups, a properly formatted rule group", t, func() {
Convey("Positive test case", func() {
err, rulegroups := c.extractRulesAsRuleGroups("aaaa-bbbb", "cccc", string(testRules))
So(err, ShouldBeNil)
So(rulegroups.Groups[0].Name, ShouldEqual, "aaaa-bbbb-cccc")
So(rulegroups.Groups[0].Rules[0].Record, ShouldEqual, "job:http_inprogress_requests:sum")
})
Convey("Negative test case", func() {
err, rulegroups := c.extractRulesAsRuleGroups("aaaa-bbbb", "cccc", string(testRuleGroup))
So(err, ShouldNotBeNil)
So(len(rulegroups.Groups), ShouldEqual, 0)
})
})
}
func TestExtractValues(t *testing.T) {
Convey("Presented with a configmap, the proper values should be extracted regardless of formats, where the format is acceptable", t, func() {
mrg := c.extractValues(&configmapDataBlockAllThree)
So(len(mrg.Values), ShouldEqual, 3)
So(countMultiRuleGroupsRules(mrg), ShouldEqual, 6)
})
Convey("Presented with a configmap in the wrong format there should be a warning on the event stream for the configmap", t, func() {
events.Clear()
cm := corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: "rules",
Namespace: "default",
Annotations: map[string]string{myAnno: "true"},
},
Data: nil,
}
cm.Data = map[string]string{
// spacing inside of `` is sacrosanct do not over-format (line 2 should be two spaces indented)
"test": `- record: job:http_inprogress_requests:sum
expr: sum(http_inprogress_requests) by (job)`,
"test2": `failNotARule`,
"test3": `- record: failNoExpression`,
}
mrg := c.extractValues(&cm)
So(len(mrg.Values), ShouldEqual, 1)
So(countMultiRuleGroupsRules(mrg), ShouldEqual, 1)
// 1 key accepted at all
So(events.CountNormals(), ShouldEqual, 1)
// 1 rule validation failure (test3 record rule)
// 1 "key has no rules failure" (test3)
// 1 key not parsable failure (test2)
So(events.CountWarnings(), ShouldEqual, 3)
})
}
func TestIsRuleConfigMap(t *testing.T) {
Convey("Should correctly detect a properly annotated configmap", t, func() {
ok := c.isRuleConfigMap(&configmapDataBlockRules)
ok2 := c.isRuleConfigMap(&configmapNoAnnotation)
So(ok, ShouldBeTrue)
So(ok2, ShouldBeFalse)
})
}
func TestHaveRuleConfigMapsChanged(t *testing.T) {
Convey("Should detect if configmaps have changed", t, func() {
cm1 := configmapDataBlockAllThree
cm2 := configmapDataBlockAllThree
cm1.ResourceVersion = "0000000001"
cm2.ResourceVersion = "0000000001"
list := corev1.ConfigMapList{Items: []corev1.ConfigMap{cm1, cm2}}
// initial read should be positive
changed := c.haveConfigMapsChanged(&list)
So(changed, ShouldBeTrue)
// run it again, should be false (no changes)
changed = c.haveConfigMapsChanged(&list)
So(changed, ShouldBeFalse)
// tweak then one last time, RV will change on one should be true
list.Items[1].ResourceVersion = "0000000002"
changed = c.haveConfigMapsChanged(&list)
So(changed, ShouldBeTrue)
})
}
func TestValidateRuleGroups(t *testing.T) {
}
func TestRemoveRules(t *testing.T) {
Convey("Should properly remove rules from a rule group by index", t, func() {
rgtest := createRuleGroup()
c.removeRules(&rgtest, []int{0,2})
So(len(rgtest.Rules), ShouldEqual, 1)
So(rgtest.Rules[0].Record, ShouldEqual, "test1")
rgtest2 := createRuleGroup()
So(len(rgtest2.Rules), ShouldEqual, 3)
c.removeRules(&rgtest2, []int{1})
So(len(rgtest2.Rules), ShouldEqual, 2)
So(rgtest2.Rules[0].Record, ShouldEqual, "test0")
So(rgtest2.Rules[1].Record, ShouldEqual, "test2")
rgtest3 := createRuleGroup()
So(len(rgtest3.Rules), ShouldEqual, 3)
c.removeRules(&rgtest3, []int{1,2})
So(len(rgtest3.Rules), ShouldEqual, 1)
So(rgtest3.Rules[0].Record, ShouldEqual, "test0")
})
}
func TestDecomposeMultiRuleGroupIntoRuleGroups(t *testing.T) {
Convey("Should properly return a single ruleGroups from a MRG", t, func() {
mrgs := createMultiRuleGroups()
// prelim confirmation
So(len(mrgs.Values), ShouldEqual, 2)
So(countMultiRuleGroupsRules(mrgs), ShouldEqual, 9)
rgs := c.decomposeMultiRuleGroupIntoRuleGroups(&mrgs)
So(len(rgs.Groups), ShouldEqual, 3)
So(countRuleGroupsRules(*rgs), ShouldEqual, 9)
})
}
func TestSaltRuleGroupNames(t *testing.T) {
Convey("Group names should be unique after salting", t, func() {
rgs := createRuleGroups()
// prelim confirm
So(len(rgs.Groups), ShouldEqual, 3)
So(countRuleGroupsRules(rgs), ShouldEqual, 9)
So(rgs.Groups[0].Name, ShouldEqual, rgs.Groups[1].Name)
rgs2 := c.saltRuleGroupNames(&rgs)
So(rgs2.Groups[0].Name, ShouldNotEqual, rgs2.Groups[1].Name)
So(rgs2.Groups[0].Name, ShouldNotEqual, rgs2.Groups[2].Name)
So(rgs2.Groups[1].Name, ShouldNotEqual, rgs2.Groups[2].Name)
})
}
func validRulesArray() []rulefmt.Rule {
return []rulefmt.Rule {
rulefmt.Rule{
Record: "job:http_inprogress_requests:sum",
Expr: "sum(http_inprogress_requests) by (job)",
},
rulefmt.Rule{
Alert: "HighErrorRate",
Expr: "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
Labels: map[string]string {
"severity": "Page",
},
Annotations: map[string]string {
"summary": "High request latency",
},
},
}
}
func createRuleGroup() rulefmt.RuleGroup {
rg := rulefmt.RuleGroup{
Name: "Test",
Interval: 0,
Rules: nil,
}
rg.Rules = append(rg.Rules, rulefmt.Rule{
Record: "test0",
Expr: "sum(http_inprogress_requests) by (job, x)",
For: 0,
Labels: nil,
Annotations: nil,
})
rg.Rules = append(rg.Rules, rulefmt.Rule{
Record: "test1",
Expr: "sum(http_inprogress_requests) by (job, y)",
For: 0,
Labels: nil,
Annotations: nil,
})
rg.Rules = append(rg.Rules, rulefmt.Rule{
Record: "test2",
Expr: "sum(http_inprogress_requests) by (job, z)",
For: 0,
Labels: nil,
Annotations: nil,
})
return rg
}
func createRuleGroups() rulefmt.RuleGroups {
rgs := rulefmt.RuleGroups{}
rgs.Groups = append(rgs.Groups, createRuleGroup())
rgs.Groups = append(rgs.Groups, createRuleGroup())
rgs.Groups = append(rgs.Groups, createRuleGroup())
return rgs
}
// returns a mrg of 2 rulegroups, one which has 2 rg the other has 1
// total of 3 rg and 9 rules
func createMultiRuleGroups() MultiRuleGroups {
rgs0 := rulefmt.RuleGroups{}
rgs0.Groups = append(rgs0.Groups, createRuleGroup())
rgs0.Groups = append(rgs0.Groups, createRuleGroup())
rgs1 := rulefmt.RuleGroups{}
rgs1.Groups = append(rgs1.Groups, createRuleGroup())
mgs := MultiRuleGroups{}
mgs.Values = []rulefmt.RuleGroups{rgs0, rgs1}
return mgs
}
func countMultiRuleGroupsRules(mrgs MultiRuleGroups) int {
count := 0
for _, rgs := range mrgs.Values {
for _, rg := range rgs.Groups {
count += len(rg.Rules)
}
}
return count
}
func countRuleGroupsRules(rgs rulefmt.RuleGroups) int {
count := 0
for _, rg := range rgs.Groups {
count += len(rg.Rules)
}
return count
}
func (ce *ConfigMapEventContainer) Clear() {
ce.Events = make([]ConfigMapEvent,0)
}
func (ce *ConfigMapEventContainer) Add(cm *corev1.ConfigMap, eventtype, reason, msg string) {
ce.Events = append(ce.Events, ConfigMapEvent{ cm.Name, cm.Namespace, eventtype, msg, reason})
}
func (ce *ConfigMapEventContainer) CountWarnings() int {
count := 0
for _, v := range ce.Events {
if v.Eventtype == corev1.EventTypeWarning {
count++
}
}
return count
}
func (ce *ConfigMapEventContainer) CountNormals() int {
count := 0
for _, v := range ce.Events {
if v.Eventtype == corev1.EventTypeNormal {
count++
}
}
return count
}