Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caryttd committed Aug 28, 2023
1 parent c5403aa commit c6bc779
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/checks/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ func simpleProm(name, uri string, timeout time.Duration, required bool) *promapi
)
}

func doubleProm(name, uri string, timeout time.Duration, required bool) *promapi.FailoverGroup {
return promapi.NewFailoverGroup(
name,
[]*promapi.Prometheus{
promapi.NewPrometheus(name, uri, map[string]string{"X-Debug": "1"}, timeout, 16, 1000, nil),
promapi.NewPrometheus(name+"2", uri, map[string]string{"X-Debug": "1"}, timeout, 16, 1000, nil),
},
required,
"up",
nil,
nil,
nil,
)
}

func newDoubleProm(uri string) *promapi.FailoverGroup {
return doubleProm("prom", uri, time.Second*5, true)
}

func newSimpleProm(uri string) *promapi.FailoverGroup {
return simpleProm("prom", uri, time.Second*5, true)
}
Expand Down
79 changes: 79 additions & 0 deletions internal/checks/promql_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import (
"github.com/cloudflare/pint/internal/promapi"
)

var (
respondWithInternalErrorThenGoodData func(writer responseWriter) responseWriter
respondWithEmptyDataThenGoodData func(writer responseWriter) responseWriter
)

func init() {
respondWithInternalErrorThenGoodDataState := 0
respondWithInternalErrorThenGoodData = func(goodResponse responseWriter) responseWriter {
respondWithInternalErrorThenGoodDataState++
if respondWithInternalErrorThenGoodDataState == 0 {
return promError{code: 500, errorType: v1.ErrServer, err: "internal error"}
}
return goodResponse
}
respondWithEmptyDataThenGoodDataState := 0
respondWithEmptyDataThenGoodData = func(goodResponse responseWriter) responseWriter {
respondWithEmptyDataThenGoodDataState++
if respondWithEmptyDataThenGoodDataState == 0 {
return metadataResponse{metadata: map[string][]v1.Metadata{}}
}
return goodResponse
}
}

func newCounterCheck(prom *promapi.FailoverGroup) checks.RuleChecker {
return checks.NewCounterCheck(prom)
}
Expand Down Expand Up @@ -202,6 +226,19 @@ func TestCounterCheck(t *testing.T) {
},
},
},
{
description: "empty data from Prometheus API",
content: "- alert: my alert\n expr: irate(foo[5m])\n",
checker: newCounterCheck,
prometheus: newSimpleProm,
problems: noProblems,
mocks: []*prometheusMock{
{
conds: []requestCondition{requireMetadataPath},
resp: metadataResponse{metadata: map[string][]v1.Metadata{}},
},
},
},
{
description: "500 error from Prometheus API",
content: "- record: foo\n expr: rate(foo[5m])\n",
Expand All @@ -225,6 +262,48 @@ func TestCounterCheck(t *testing.T) {
},
},
},
{
description: "500 error from first Prometheus API - use counter with rate",
content: "- record: foo\n expr: rate(foo[5m])\n",
checker: newCounterCheck,
prometheus: newDoubleProm,
problems: noProblems,

mocks: []*prometheusMock{
{
conds: []requestCondition{requireMetadataPath},
resp: respondWithInternalErrorThenGoodData(metadataResponse{metadata: map[string][]v1.Metadata{
"foo": {{Type: "counter"}},
}}),
},
},
},
{
description: "empty data from first Prometheus API - use counter with delta",
content: "- record: foo\n expr: delta(foo[5m])\n",
checker: newCounterCheck,
prometheus: newDoubleProm,
problems: func(uri string) []checks.Problem {
return []checks.Problem{
{
Fragment: "foo",
Lines: []int{2},
Reporter: "promql/counter",
Text: CounterMustUseFuncTextForRecordingRule("foo"),
Severity: checks.Warning,
},
}
},

mocks: []*prometheusMock{
{
conds: []requestCondition{requireMetadataPath},
resp: respondWithEmptyDataThenGoodData(metadataResponse{metadata: map[string][]v1.Metadata{
"foo": {{Type: "counter"}},
}}),
},
},
},
}

runTests(t, testCases)
Expand Down

0 comments on commit c6bc779

Please # to comment.