Skip to content

Commit

Permalink
Merge pull request #1165 from cloudflare/tests
Browse files Browse the repository at this point in the history
Add more test coverage
  • Loading branch information
prymitive authored Oct 25, 2024
2 parents e233a20 + 8cd062f commit 95af10f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ func checkQueryLabels(labelName, labelValue string, src utils.Source) (problems
}

func textForProblem(label string, src utils.Source, severity Severity) exprProblem {
// FIXME add query fragment to the details

switch {
case src.Operation == "absent":
return exprProblem{
Expand Down
69 changes: 69 additions & 0 deletions internal/checks/alerts_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,75 @@ func TestTemplateCheck(t *testing.T) {
}
},
},
{
description: "multiple or",
content: `
- alert: Prefix_Advertised_On_Very_Few_Routers
expr: >
avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case!~".*offpeak.*|.*multicolo.*|.*aggregate.*|.*test.*|.*tier1.*|.*regional.*|.*brat.*|.*utopia.*|.*byoip.*",prefix!~"141.101.112.0/20|190.93.240.0/20"})
< 0.5 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*multicolo.*"})
< 0.4 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*aggregate.*"} OR router_anycast_prefix_enabled{prefix=~"141.101.112.0/20|190.93.240.0/20"})
< 20 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*offpeak.*"})
< 8 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*tier1.*"})
< on() group_left() count(colo_router_tier:disabled_pops:max{tier="1",router=~"edge.*"}) * 0.4 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*regional.*"})
< 0.1 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*brat.*",cidr_use_case!~".*tier1.*",plan=~".*(free|pro).*"})
< 0.1 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*utopia.*"})
< 5 > 0
annotations:
dashboard: 'Prefix is {{ $labels.prefix }}'
`,
checker: newTemplateCheck,
prometheus: noProm,
problems: noProblems,
},
{
description: "multiple or / missing group_left()",
content: `
- alert: Prefix_Advertised_On_Very_Few_Routers
expr: >
avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case!~".*offpeak.*|.*multicolo.*|.*aggregate.*|.*test.*|.*tier1.*|.*regional.*|.*brat.*|.*utopia.*|.*byoip.*",prefix!~"141.101.112.0/20|190.93.240.0/20"})
< 0.5 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*multicolo.*"})
< 0.4 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*aggregate.*"} OR router_anycast_prefix_enabled{prefix=~"141.101.112.0/20|190.93.240.0/20"})
< 20 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*offpeak.*"})
< 8 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*tier1.*"})
< on() count(colo_router_tier:disabled_pops:max{tier="1",router=~"edge.*"}) * 0.4 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*regional.*"})
< 0.1 > 0
or avg without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*brat.*",cidr_use_case!~".*tier1.*",plan=~".*(free|pro).*"})
< 0.1 > 0
or sum without(router, colo_id, instance) (router_anycast_prefix_enabled{cidr_use_case=~".*utopia.*"})
< 5 > 0
annotations:
dashboard: 'Prefix is {{ $labels.prefix }}'
`,
checker: newTemplateCheck,
prometheus: noProm,
problems: func(_ string) []checks.Problem {
return []checks.Problem{
{
Lines: parser.LineRange{
First: 21,
Last: 21,
},
Reporter: checks.TemplateCheckName,
Text: "Template is using `prefix` label but the query removes it.",
Details: checks.TemplateCheckAggregationDetails,
Severity: checks.Bug,
},
}
},
},
}
runTests(t, testCases)
}

0 comments on commit 95af10f

Please sign in to comment.