4
4
"bytes"
5
5
"context"
6
6
"testing"
7
+ "time"
7
8
8
9
"github.com/prometheus/prometheus/model/rulefmt"
9
10
"github.com/stretchr/testify/assert"
@@ -12,7 +13,29 @@ import (
12
13
"github.com/slok/sloth/internal/prometheus"
13
14
)
14
15
16
+ func parseDuration (durationStr string , t * testing.T ) time.Duration {
17
+ duration , err := time .ParseDuration (durationStr )
18
+ if err != nil {
19
+ t .Errorf ("could not parse duration: %v" , err )
20
+ return 0
21
+ }
22
+ return duration
23
+ }
24
+
15
25
func TestIOWriterGroupedRulesYAMLRepoStore (t * testing.T ) {
26
+ // set intervals ahead of time
27
+ ruleGroupInterval := parseDuration ("2m" , t )
28
+ // 0s = default/blank
29
+ ruleGroupIntervalBlank := parseDuration ("0s" , t )
30
+ // A/B for multiple group test
31
+ ruleGroupIntervalA := parseDuration ("3m" , t )
32
+ ruleGroupIntervalB := parseDuration ("1h" , t )
33
+ // for individual settings
34
+ sliErrorRulesInterval := parseDuration ("4m" , t )
35
+ metadataRulesInterval := parseDuration ("5m" , t )
36
+ alertRulesInterval := parseDuration ("6m" , t )
37
+ // need test for mix of rulegroupinterval and individual
38
+
16
39
tests := map [string ]struct {
17
40
slos []prometheus.StorageSLO
18
41
expYAML string
@@ -30,10 +53,11 @@ func TestIOWriterGroupedRulesYAMLRepoStore(t *testing.T) {
30
53
expErr : true ,
31
54
},
32
55
33
- "Having a single SLI recording rule should render correctly." : {
56
+ "Having a single SLI recording rule with the generic rule_group interval should render correctly." : {
57
+
34
58
slos : []prometheus.StorageSLO {
35
59
{
36
- SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : "2m" },
60
+ SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : ruleGroupInterval },
37
61
Rules : prometheus.SLORules {
38
62
SLIErrorRecRules : []rulefmt.Rule {
39
63
{
@@ -92,7 +116,7 @@ groups:
92
116
"Having a single SLO alert rule should render correctly." : {
93
117
slos : []prometheus.StorageSLO {
94
118
{
95
- SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : "2m" },
119
+ SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : ruleGroupInterval },
96
120
Rules : prometheus.SLORules {
97
121
AlertRules : []rulefmt.Rule {
98
122
{
@@ -125,7 +149,7 @@ groups:
125
149
"Having a single a blank or empty rule_group interval render correctly." : {
126
150
slos : []prometheus.StorageSLO {
127
151
{
128
- SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : "" },
152
+ SLO : prometheus.SLO {ID : "test1" , RuleGroupInterval : ruleGroupIntervalBlank },
129
153
Rules : prometheus.SLORules {
130
154
SLIErrorRecRules : []rulefmt.Rule {
131
155
{
@@ -155,7 +179,7 @@ groups:
155
179
"Having a multiple SLO alert and recording rules should render correctly." : {
156
180
slos : []prometheus.StorageSLO {
157
181
{
158
- SLO : prometheus.SLO {ID : "testa" , RuleGroupInterval : "3m" },
182
+ SLO : prometheus.SLO {ID : "testa" , RuleGroupInterval : ruleGroupIntervalA },
159
183
Rules : prometheus.SLORules {
160
184
SLIErrorRecRules : []rulefmt.Rule {
161
185
{
@@ -198,7 +222,7 @@ groups:
198
222
},
199
223
},
200
224
{
201
- SLO : prometheus.SLO {ID : "testb" , RuleGroupInterval : "1h" },
225
+ SLO : prometheus.SLO {ID : "testb" , RuleGroupInterval : ruleGroupIntervalB },
202
226
Rules : prometheus.SLORules {
203
227
SLIErrorRecRules : []rulefmt.Rule {
204
228
{
@@ -293,6 +317,130 @@ groups:
293
317
test-annot: b-1
294
318
` ,
295
319
},
320
+ "Having a mix of rule group intervals should render correctly." : {
321
+
322
+ slos : []prometheus.StorageSLO {
323
+ {
324
+ SLO : prometheus.SLO {ID : "testa" , SLIErrorRulesInterval : sliErrorRulesInterval , MetadataRulesInterval : metadataRulesInterval , AlertRulesInterval : alertRulesInterval },
325
+ Rules : prometheus.SLORules {
326
+ SLIErrorRecRules : []rulefmt.Rule {
327
+ {
328
+ Record : "test:record-a1" ,
329
+ Expr : "test-expr-a1" ,
330
+ Labels : map [string ]string {"test-label" : "a-1" },
331
+ },
332
+ },
333
+ MetadataRecRules : []rulefmt.Rule {
334
+ {
335
+ Record : "test:record-a3" ,
336
+ Expr : "test-expr-a3" ,
337
+ Labels : map [string ]string {"test-label" : "a-3" },
338
+ },
339
+ },
340
+ AlertRules : []rulefmt.Rule {
341
+ {
342
+ Alert : "testAlertA1" ,
343
+ Expr : "test-expr-a1" ,
344
+ Labels : map [string ]string {"test-label" : "a-1" },
345
+ Annotations : map [string ]string {"test-annot" : "a-1" },
346
+ },
347
+ },
348
+ },
349
+ },
350
+ },
351
+ expYAML : `
352
+ ---
353
+ # Code generated by Sloth (dev): https://github.com/slok/sloth.
354
+ # DO NOT EDIT.
355
+
356
+ groups:
357
+ - name: sloth-slo-sli-recordings-testa
358
+ interval: 4m
359
+ rules:
360
+ - record: test:record-a1
361
+ expr: test-expr-a1
362
+ labels:
363
+ test-label: a-1
364
+ - name: sloth-slo-meta-recordings-testa
365
+ interval: 5m
366
+ rules:
367
+ - record: test:record-a3
368
+ expr: test-expr-a3
369
+ labels:
370
+ test-label: a-3
371
+ - name: sloth-slo-alerts-testa
372
+ interval: 6m
373
+ rules:
374
+ - alert: testAlertA1
375
+ expr: test-expr-a1
376
+ labels:
377
+ test-label: a-1
378
+ annotations:
379
+ test-annot: a-1
380
+ ` },
381
+ "Having a mix of rule group intervals and the overarching rule_group interval should render correctly." : {
382
+
383
+ slos : []prometheus.StorageSLO {
384
+ {
385
+ SLO : prometheus.SLO {ID : "testa" , SLIErrorRulesInterval : sliErrorRulesInterval , MetadataRulesInterval : metadataRulesInterval , RuleGroupInterval : ruleGroupInterval },
386
+ // in this case we use the broad RuleGroupInterval to set a 2m interval for the alert rules
387
+ // that dont have an explicit one set
388
+ Rules : prometheus.SLORules {
389
+ SLIErrorRecRules : []rulefmt.Rule {
390
+ {
391
+ Record : "test:record-a1" ,
392
+ Expr : "test-expr-a1" ,
393
+ Labels : map [string ]string {"test-label" : "a-1" },
394
+ },
395
+ },
396
+ MetadataRecRules : []rulefmt.Rule {
397
+ {
398
+ Record : "test:record-a3" ,
399
+ Expr : "test-expr-a3" ,
400
+ Labels : map [string ]string {"test-label" : "a-3" },
401
+ },
402
+ },
403
+ AlertRules : []rulefmt.Rule {
404
+ {
405
+ Alert : "testAlertA1" ,
406
+ Expr : "test-expr-a1" ,
407
+ Labels : map [string ]string {"test-label" : "a-1" },
408
+ Annotations : map [string ]string {"test-annot" : "a-1" },
409
+ },
410
+ },
411
+ },
412
+ },
413
+ },
414
+ expYAML : `
415
+ ---
416
+ # Code generated by Sloth (dev): https://github.com/slok/sloth.
417
+ # DO NOT EDIT.
418
+
419
+ groups:
420
+ - name: sloth-slo-sli-recordings-testa
421
+ interval: 4m
422
+ rules:
423
+ - record: test:record-a1
424
+ expr: test-expr-a1
425
+ labels:
426
+ test-label: a-1
427
+ - name: sloth-slo-meta-recordings-testa
428
+ interval: 5m
429
+ rules:
430
+ - record: test:record-a3
431
+ expr: test-expr-a3
432
+ labels:
433
+ test-label: a-3
434
+ - name: sloth-slo-alerts-testa
435
+ interval: 2m
436
+ rules:
437
+ - alert: testAlertA1
438
+ expr: test-expr-a1
439
+ labels:
440
+ test-label: a-1
441
+ annotations:
442
+ test-annot: a-1
443
+ ` },
296
444
}
297
445
298
446
for name , test := range tests {
0 commit comments