Skip to content

Commit

Permalink
Add tests for alertstate
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksontj committed Aug 19, 2023
1 parent 952d867 commit efba2fe
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions pkg/alertbackfill/alertstate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package alertbackfill

import (
"strconv"
"testing"
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
)

func TestGenerateAlertStateMatrix(t *testing.T) {
start := model.Time(0).Add(time.Minute)
tests := []struct {
in model.Matrix
matchers []*labels.Matcher
alertLabels labels.Labels
step time.Duration

out model.Matrix
}{
{
in: model.Matrix{
&model.SampleStream{
Metric: model.Metric{
"__name__": "prometheus_build_info",
"job": "prometheus",
"replica": "a",
},
Values: []model.SamplePair{
model.SamplePair{start.Add(time.Minute * 0), 1},
model.SamplePair{start.Add(time.Minute * 1), 1},
model.SamplePair{start.Add(time.Minute * 2), 1},
model.SamplePair{start.Add(time.Minute * 3), 1},
model.SamplePair{start.Add(time.Minute * 4), 1},
// Have a gap!
model.SamplePair{start.Add(time.Minute * 6), 1},
},
},
&model.SampleStream{
Metric: model.Metric{
"__name__": "prometheus_build_info",
"job": "prometheus",
"replica": "b",
},
Values: []model.SamplePair{
model.SamplePair{start.Add(time.Minute * 0), 1},
model.SamplePair{start.Add(time.Minute * 1), 1},
model.SamplePair{start.Add(time.Minute * 2), 1},
model.SamplePair{start.Add(time.Minute * 3), 1},
model.SamplePair{start.Add(time.Minute * 4), 1},
},
},
},
matchers: []*labels.Matcher{
labels.MustNewMatcher(labels.MatchEqual, model.MetricNameLabel, "ALERTS_FOR_STATE"),
labels.MustNewMatcher(labels.MatchEqual, model.AlertNameLabel, "testalert"),
labels.MustNewMatcher(labels.MatchEqual, "replica", "a"),
},
alertLabels: labels.Labels{
labels.Label{"severity", "page"},
},
step: time.Minute,
out: model.Matrix{
&model.SampleStream{
Metric: model.Metric{
"__name__": "ALERTS_FOR_STATE",
"alertname": "testalert",
"job": "prometheus",
"replica": "a",
"severity": "page",
},
Values: []model.SamplePair{
model.SamplePair{start.Add(time.Minute * 0), model.SampleValue(start.Unix())},
model.SamplePair{start.Add(time.Minute * 1), model.SampleValue(start.Unix())},
model.SamplePair{start.Add(time.Minute * 2), model.SampleValue(start.Unix())},
model.SamplePair{start.Add(time.Minute * 3), model.SampleValue(start.Unix())},
model.SamplePair{start.Add(time.Minute * 4), model.SampleValue(start.Unix())},
model.SamplePair{start.Add(time.Minute * 6), model.SampleValue(start.Add(time.Minute * 6).Unix())},
},
},
},
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
out := GenerateAlertStateMatrix(test.in, test.matchers, test.alertLabels, test.step)

if test.out.String() != out.String() {
t.Fatalf("mismatch in series expected=%v actual=%v", test.out, out)
}

for i, sampleStream := range out {
for _, matcher := range test.matchers {
if !matcher.Matches(string(sampleStream.Metric[model.LabelName(matcher.Name)])) {
t.Fatalf("out series=%d label %s=%s doesn't match matcher %v", i, matcher.Name, sampleStream.Metric[model.LabelName(matcher.Name)], matcher.Value)
}
}
}
})
}
}

0 comments on commit efba2fe

Please sign in to comment.