cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/alerts_comparison_test.go
187lines · 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 newComparisonCheck(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 11 | return checks.NewComparisonCheck() |
| 12 | } |
| 13 | |
| 14 | func TestComparisonCheck(t *testing.T) { |
| 15 | testCases := []checkTest{ |
| 16 | { |
| 17 | description: "ignores recording rules", |
| 18 | content: "- record: foo\n expr: up == 0\n", |
| 19 | checker: newComparisonCheck, |
| 20 | prometheus: noProm, |
| 21 | }, |
| 22 | { |
| 23 | description: "ignores rules with syntax errors", |
| 24 | content: "- alert: Foo Is Down\n expr: sum(\n", |
| 25 | checker: newComparisonCheck, |
| 26 | prometheus: noProm, |
| 27 | }, |
| 28 | { |
| 29 | description: "alert expr with > condition", |
| 30 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} > 0\n", |
| 31 | checker: newComparisonCheck, |
| 32 | prometheus: noProm, |
| 33 | }, |
| 34 | { |
| 35 | description: "alert expr with >= condition", |
| 36 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} >= 1\n", |
| 37 | checker: newComparisonCheck, |
| 38 | prometheus: noProm, |
| 39 | }, |
| 40 | { |
| 41 | description: "alert expr with == condition", |
| 42 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} == 1\n", |
| 43 | checker: newComparisonCheck, |
| 44 | prometheus: noProm, |
| 45 | }, |
| 46 | { |
| 47 | description: "alert expr without any condition", |
| 48 | content: "- alert: Foo Is Down\n expr: up{job=\"foo\"}\n", |
| 49 | checker: newComparisonCheck, |
| 50 | prometheus: noProm, |
| 51 | problems: true, |
| 52 | }, |
| 53 | { |
| 54 | description: "deep level comparison", |
| 55 | content: ` |
| 56 | - alert: High_UDP_Receive_Errors |
| 57 | expr: quantile_over_time(0.7,(irate(udp_packets_drops[2m]))[10m:2m]) > 200 |
| 58 | AND ON (instance) |
| 59 | (rate(node_netstat_Udp_RcvbufErrors[5m])+rate(node_netstat_Udp6_RcvbufErrors[5m])) > 200 |
| 60 | `, |
| 61 | checker: newComparisonCheck, |
| 62 | prometheus: noProm, |
| 63 | }, |
| 64 | { |
| 65 | description: "deep level without comparison", |
| 66 | content: ` |
| 67 | - alert: High_UDP_Receive_Errors |
| 68 | expr: quantile_over_time(0.7,(irate(udp_packets_drops[2m]))[10m:2m]) |
| 69 | AND ON (instance) |
| 70 | rate(node_netstat_Udp_RcvbufErrors[5m])+rate(node_netstat_Udp6_RcvbufErrors[5m]) |
| 71 | `, |
| 72 | checker: newComparisonCheck, |
| 73 | prometheus: noProm, |
| 74 | problems: true, |
| 75 | }, |
| 76 | { |
| 77 | description: "alert unless condition", |
| 78 | content: "- alert: Foo Is Down\n for: 10m\n expr: foo unless bar\n", |
| 79 | checker: newComparisonCheck, |
| 80 | prometheus: noProm, |
| 81 | }, |
| 82 | { |
| 83 | description: "alert expr with bool", |
| 84 | content: "- alert: Error rate is high\n expr: rate(error_count[5m]) > bool 5\n", |
| 85 | checker: newComparisonCheck, |
| 86 | prometheus: noProm, |
| 87 | problems: true, |
| 88 | }, |
| 89 | { |
| 90 | description: "alert expr with bool and condition", |
| 91 | content: "- alert: Error rate is high\n expr: rate(error_count[5m]) > bool 5 == 1\n", |
| 92 | checker: newComparisonCheck, |
| 93 | prometheus: noProm, |
| 94 | }, |
| 95 | { |
| 96 | description: "alert on absent", |
| 97 | content: "- alert: Foo Is Missing\n expr: absent(foo)\n", |
| 98 | checker: newComparisonCheck, |
| 99 | prometheus: noProm, |
| 100 | }, |
| 101 | { |
| 102 | description: "absent or absent", |
| 103 | content: "- alert: Foo Is Missing\n expr: absent(foo) or absent(bar)\n", |
| 104 | checker: newComparisonCheck, |
| 105 | prometheus: noProm, |
| 106 | }, |
| 107 | { |
| 108 | description: "absent or absent or absent", |
| 109 | content: "- alert: Foo Is Missing\n expr: absent(foo) or absent(bar) or absent(bob{job=\"xx\"})\n", |
| 110 | checker: newComparisonCheck, |
| 111 | prometheus: noProm, |
| 112 | }, |
| 113 | { |
| 114 | description: "alert on absent_over_time", |
| 115 | content: "- alert: Foo Is Missing\n expr: absent_over_time(foo[5m])\n", |
| 116 | checker: newComparisonCheck, |
| 117 | prometheus: noProm, |
| 118 | }, |
| 119 | { |
| 120 | description: "(foo > 1) > bool 1", |
| 121 | content: "- alert: Foo Is Missing\n expr: (foo > 1) > bool 1\n", |
| 122 | checker: newComparisonCheck, |
| 123 | prometheus: noProm, |
| 124 | }, |
| 125 | { |
| 126 | description: "vector(0) or (foo > 0)", |
| 127 | content: "- alert: Foo Is Down\n expr: (foo > 0) or vector(0)\n", |
| 128 | checker: newComparisonCheck, |
| 129 | prometheus: noProm, |
| 130 | problems: true, |
| 131 | }, |
| 132 | { |
| 133 | description: "(foo > 0) or vector(0)", |
| 134 | content: "- alert: Foo Is Down\n expr: (foo > 0) or vector(0)\n", |
| 135 | checker: newComparisonCheck, |
| 136 | prometheus: noProm, |
| 137 | problems: true, |
| 138 | }, |
| 139 | { |
| 140 | description: "absent(foo) or vector(0)", |
| 141 | content: "- alert: Foo Is Down\n expr: (foo > 0) or vector(0)\n", |
| 142 | checker: newComparisonCheck, |
| 143 | prometheus: noProm, |
| 144 | problems: true, |
| 145 | }, |
| 146 | { |
| 147 | description: "(foo or vector(0)) / bar > 0", |
| 148 | content: "- alert: Foo Is Missing\n expr: (foo or vector(0)) / bar > 0\n", |
| 149 | checker: newComparisonCheck, |
| 150 | prometheus: noProm, |
| 151 | }, |
| 152 | { |
| 153 | // FIXME this should warn because missing foo makes this `0 / <something> <= 0`, so <something> > 0 makes it `0 <= 0`. |
| 154 | description: "(foo or vector(0)) / bar <= 0", |
| 155 | content: "- alert: Foo Is Missing\n expr: (foo or vector(0)) / bar <= 0\n", |
| 156 | checker: newComparisonCheck, |
| 157 | prometheus: noProm, |
| 158 | }, |
| 159 | { |
| 160 | description: "max() * group_right label_replace(...)", |
| 161 | content: ` |
| 162 | - alert: Foo Is Missing |
| 163 | expr: | |
| 164 | max(kernel_xfs_corruption_errors > 0) by (instance) |
| 165 | * on (instance) group_right |
| 166 | label_replace(salt_highstate_runner_configured_minions, "instance", "$1", "minion", "(.+)") |
| 167 | `, |
| 168 | checker: newComparisonCheck, |
| 169 | prometheus: noProm, |
| 170 | }, |
| 171 | { |
| 172 | description: "vector(0)", |
| 173 | content: "- alert: Foo Is Down\n expr: vector(0)\n", |
| 174 | checker: newComparisonCheck, |
| 175 | prometheus: noProm, |
| 176 | problems: true, |
| 177 | }, |
| 178 | { |
| 179 | description: "alert on absent(vector(1))", |
| 180 | content: "- alert: Foo Is Missing\n expr: absent(vector(1))\n", |
| 181 | checker: newComparisonCheck, |
| 182 | prometheus: noProm, |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | runTests(t, testCases) |
| 187 | } |
| 188 | |