cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/alerts_annotation_test.go
192lines · modecode
| 1 | package checks_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/cloudflare/pint/internal/checks" |
| 7 | "github.com/cloudflare/pint/internal/promapi" |
| 8 | ) |
| 9 | |
| 10 | func TestAnnotationCheck(t *testing.T) { |
| 11 | testCases := []checkTest{ |
| 12 | { |
| 13 | description: "ignores recording rules", |
| 14 | content: "- record: foo\n expr: sum(foo) without(\n", |
| 15 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 16 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 17 | }, |
| 18 | prometheus: noProm, |
| 19 | }, |
| 20 | { |
| 21 | description: "doesn't ignore rules with syntax errors", |
| 22 | content: "- alert: foo\n expr: sum(foo) without(\n", |
| 23 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 24 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 25 | }, |
| 26 | prometheus: noProm, |
| 27 | problems: true, |
| 28 | }, |
| 29 | { |
| 30 | description: "no annotations / required", |
| 31 | content: "- alert: foo\n expr: sum(foo)\n", |
| 32 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 33 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 34 | }, |
| 35 | prometheus: noProm, |
| 36 | problems: true, |
| 37 | }, |
| 38 | { |
| 39 | description: "empty annotations / required", |
| 40 | content: ` |
| 41 | - alert: foo |
| 42 | expr: sum(foo) |
| 43 | annotations: |
| 44 | foo: bar |
| 45 | severity: |
| 46 | level: warning |
| 47 | `, |
| 48 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 49 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, nil, nil, true, "", checks.Bug) |
| 50 | }, |
| 51 | prometheus: noProm, |
| 52 | problems: true, |
| 53 | }, |
| 54 | { |
| 55 | description: "no annotations / not required", |
| 56 | content: "- alert: foo\n expr: sum(foo)\n", |
| 57 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 58 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, false, "", checks.Warning) |
| 59 | }, |
| 60 | prometheus: noProm, |
| 61 | }, |
| 62 | { |
| 63 | description: "missing annotation / required", |
| 64 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n foo: bar\n", |
| 65 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 66 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 67 | }, |
| 68 | prometheus: noProm, |
| 69 | problems: true, |
| 70 | }, |
| 71 | { |
| 72 | description: "missing annotation / not required", |
| 73 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n foo: bar\n", |
| 74 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 75 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, false, "", checks.Warning) |
| 76 | }, |
| 77 | prometheus: noProm, |
| 78 | }, |
| 79 | { |
| 80 | description: "wrong annotation value / required", |
| 81 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n severity: bar\n", |
| 82 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 83 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 84 | }, |
| 85 | prometheus: noProm, |
| 86 | problems: true, |
| 87 | }, |
| 88 | { |
| 89 | description: "wrong annotation value / not required", |
| 90 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n severity: bar\n", |
| 91 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 92 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical"), nil, false, "", checks.Warning) |
| 93 | }, |
| 94 | prometheus: noProm, |
| 95 | problems: true, |
| 96 | }, |
| 97 | { |
| 98 | description: "valid annotation / required", |
| 99 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n severity: info\n", |
| 100 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 101 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical|info|debug"), nil, true, "", checks.Warning) |
| 102 | }, |
| 103 | prometheus: noProm, |
| 104 | }, |
| 105 | { |
| 106 | description: "valid annotation / not required", |
| 107 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n severity: info\n", |
| 108 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 109 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("severity"), nil, checks.MustTemplatedRegexp("critical|info|debug"), nil, false, "", checks.Warning) |
| 110 | }, |
| 111 | prometheus: noProm, |
| 112 | }, |
| 113 | { |
| 114 | description: "templated annotation value / passing", |
| 115 | content: "- alert: foo\n expr: sum(foo)\n for: 5m\n annotations:\n for: 5m\n", |
| 116 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 117 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("for"), nil, checks.MustTemplatedRegexp("{{ $for }}"), nil, true, "", checks.Bug) |
| 118 | }, |
| 119 | prometheus: noProm, |
| 120 | }, |
| 121 | { |
| 122 | description: "templated annotation value / passing", |
| 123 | content: "- alert: foo\n expr: sum(foo)\n for: 5m\n annotations:\n for: 4m\n", |
| 124 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 125 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("for"), nil, checks.MustTemplatedRegexp("{{ $for }}"), nil, true, "", checks.Bug) |
| 126 | }, |
| 127 | prometheus: noProm, |
| 128 | problems: true, |
| 129 | }, |
| 130 | { |
| 131 | description: "valid annotation key regex / required", |
| 132 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n annotation_1: info\n", |
| 133 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 134 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("annotation_.*"), nil, checks.MustTemplatedRegexp("critical|info|debug"), nil, true, "", checks.Warning) |
| 135 | }, |
| 136 | prometheus: noProm, |
| 137 | }, |
| 138 | { |
| 139 | description: "valid annotation key regex / not required", |
| 140 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n annotation_1: info\n", |
| 141 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 142 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("annotation_.*"), nil, checks.MustTemplatedRegexp("critical|info|debug"), nil, false, "", checks.Warning) |
| 143 | }, |
| 144 | prometheus: noProm, |
| 145 | }, |
| 146 | { |
| 147 | description: "wrong annotation key regex value / required", |
| 148 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n annotation_1: bar\n", |
| 149 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 150 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("annotation_.*"), nil, checks.MustTemplatedRegexp("critical"), nil, true, "", checks.Warning) |
| 151 | }, |
| 152 | prometheus: noProm, |
| 153 | problems: true, |
| 154 | }, |
| 155 | { |
| 156 | description: "wrong annotation key regex value / not required", |
| 157 | content: "- alert: foo\n expr: sum(foo)\n annotations:\n annotation_1: bar\n", |
| 158 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 159 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("annotation_.*"), nil, checks.MustTemplatedRegexp("critical"), nil, false, "", checks.Warning) |
| 160 | }, |
| 161 | prometheus: noProm, |
| 162 | problems: true, |
| 163 | }, |
| 164 | { |
| 165 | description: "invalid value / token / valueRe", |
| 166 | content: "- alert: foo\n expr: rate(foo[1m])\n annotations:\n components: api db\n", |
| 167 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 168 | return checks.NewAnnotationCheck(checks.MustTemplatedRegexp("components"), checks.MustRawTemplatedRegexp("\\w+"), checks.MustTemplatedRegexp("api|memcached"), nil, false, "rule comment", checks.Bug) |
| 169 | }, |
| 170 | prometheus: noProm, |
| 171 | problems: true, |
| 172 | }, |
| 173 | { |
| 174 | description: "invalid value / token / values", |
| 175 | content: "- alert: foo\n expr: rate(foo[1m])\n annotations:\n components: api db\n", |
| 176 | checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 177 | return checks.NewAnnotationCheck( |
| 178 | checks.MustTemplatedRegexp("components"), |
| 179 | checks.MustRawTemplatedRegexp("\\w+"), |
| 180 | nil, |
| 181 | []string{"api", "memcached", "storage", "prometheus", "kvm", "mysql", "memsql", "haproxy", "postgresql"}, |
| 182 | false, |
| 183 | "rule comment", |
| 184 | checks.Bug, |
| 185 | ) |
| 186 | }, |
| 187 | prometheus: noProm, |
| 188 | problems: true, |
| 189 | }, |
| 190 | } |
| 191 | runTests(t, testCases) |
| 192 | } |