Skip to content

Commit ca78450

Browse files
committed
test: add tests for custom rule group intervals
Specific ones and general/specific combined
1 parent a94fee9 commit ca78450

File tree

1 file changed

+154
-6
lines changed

1 file changed

+154
-6
lines changed

internal/prometheus/storage_test.go

Lines changed: 154 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"testing"
7+
"time"
78

89
"github.com/prometheus/prometheus/model/rulefmt"
910
"github.com/stretchr/testify/assert"
@@ -12,7 +13,29 @@ import (
1213
"github.com/slok/sloth/internal/prometheus"
1314
)
1415

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+
1525
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+
1639
tests := map[string]struct {
1740
slos []prometheus.StorageSLO
1841
expYAML string
@@ -30,10 +53,11 @@ func TestIOWriterGroupedRulesYAMLRepoStore(t *testing.T) {
3053
expErr: true,
3154
},
3255

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+
3458
slos: []prometheus.StorageSLO{
3559
{
36-
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: "2m"},
60+
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: ruleGroupInterval},
3761
Rules: prometheus.SLORules{
3862
SLIErrorRecRules: []rulefmt.Rule{
3963
{
@@ -92,7 +116,7 @@ groups:
92116
"Having a single SLO alert rule should render correctly.": {
93117
slos: []prometheus.StorageSLO{
94118
{
95-
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: "2m"},
119+
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: ruleGroupInterval},
96120
Rules: prometheus.SLORules{
97121
AlertRules: []rulefmt.Rule{
98122
{
@@ -125,7 +149,7 @@ groups:
125149
"Having a single a blank or empty rule_group interval render correctly.": {
126150
slos: []prometheus.StorageSLO{
127151
{
128-
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: ""},
152+
SLO: prometheus.SLO{ID: "test1", RuleGroupInterval: ruleGroupIntervalBlank},
129153
Rules: prometheus.SLORules{
130154
SLIErrorRecRules: []rulefmt.Rule{
131155
{
@@ -155,7 +179,7 @@ groups:
155179
"Having a multiple SLO alert and recording rules should render correctly.": {
156180
slos: []prometheus.StorageSLO{
157181
{
158-
SLO: prometheus.SLO{ID: "testa", RuleGroupInterval: "3m"},
182+
SLO: prometheus.SLO{ID: "testa", RuleGroupInterval: ruleGroupIntervalA},
159183
Rules: prometheus.SLORules{
160184
SLIErrorRecRules: []rulefmt.Rule{
161185
{
@@ -198,7 +222,7 @@ groups:
198222
},
199223
},
200224
{
201-
SLO: prometheus.SLO{ID: "testb", RuleGroupInterval: "1h"},
225+
SLO: prometheus.SLO{ID: "testb", RuleGroupInterval: ruleGroupIntervalB},
202226
Rules: prometheus.SLORules{
203227
SLIErrorRecRules: []rulefmt.Rule{
204228
{
@@ -293,6 +317,130 @@ groups:
293317
test-annot: b-1
294318
`,
295319
},
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+
`},
296444
}
297445

298446
for name, test := range tests {

0 commit comments

Comments
 (0)