cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/alerts_count_test.go
152lines · modecode
| 1 | package checks_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/cloudflare/pint/internal/checks" |
| 11 | |
| 12 | "github.com/rs/zerolog" |
| 13 | ) |
| 14 | |
| 15 | func TestAlertsCheck(t *testing.T) { |
| 16 | zerolog.SetGlobalLevel(zerolog.FatalLevel) |
| 17 | |
| 18 | content := "- alert: Foo Is Down\n expr: up{job=\"foo\"} == 0\n" |
| 19 | |
| 20 | now := time.Now() |
| 21 | |
| 22 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | err := r.ParseForm() |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | query := r.Form.Get("query") |
| 28 | if query != `up{job="foo"} == 0` { |
| 29 | t.Fatalf("Prometheus got invalid query: %s", query) |
| 30 | } |
| 31 | |
| 32 | switch r.URL.Path { |
| 33 | case "/empty/api/v1/query_range": |
| 34 | w.WriteHeader(200) |
| 35 | w.Header().Set("Content-Type", "application/json") |
| 36 | _, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[]}}`)) |
| 37 | case "/alerts/api/v1/query_range": |
| 38 | w.WriteHeader(200) |
| 39 | out := fmt.Sprintf(`{ |
| 40 | "status":"success", |
| 41 | "data":{ |
| 42 | "resultType":"matrix", |
| 43 | "result":[ |
| 44 | {"metric":{"instance":"1"},"values":[ |
| 45 | [%d,"0"], |
| 46 | [%d,"0"], |
| 47 | [%d,"0"], |
| 48 | [%d,"0"], |
| 49 | [%d,"0"] |
| 50 | ]}, |
| 51 | {"metric":{"instance":"2"},"values":[ |
| 52 | [%d,"0"], |
| 53 | [%d,"0"], |
| 54 | [%d,"0"], |
| 55 | [%d,"0"], |
| 56 | [%d,"0"] |
| 57 | ]} |
| 58 | ] |
| 59 | } |
| 60 | }`, |
| 61 | now.AddDate(0, 0, -1).Unix(), |
| 62 | now.AddDate(0, 0, -1).Add(time.Minute).Unix(), |
| 63 | now.AddDate(0, 0, -1).Add(time.Minute*2).Unix(), |
| 64 | now.AddDate(0, 0, -1).Add(time.Minute*60).Unix(), |
| 65 | now.AddDate(0, 0, -1).Add(time.Minute*61).Unix(), |
| 66 | |
| 67 | now.AddDate(0, 0, -1).Add(time.Minute*6).Unix(), |
| 68 | now.AddDate(0, 0, -1).Add(time.Minute*12).Unix(), |
| 69 | now.AddDate(0, 0, -1).Add(time.Minute*18).Unix(), |
| 70 | now.AddDate(0, 0, -1).Add(time.Minute*24).Unix(), |
| 71 | now.AddDate(0, 0, -1).Add(time.Minute*30).Unix(), |
| 72 | ) |
| 73 | _, _ = w.Write([]byte(out)) |
| 74 | default: |
| 75 | w.WriteHeader(400) |
| 76 | w.Header().Set("Content-Type", "application/json") |
| 77 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`)) |
| 78 | } |
| 79 | })) |
| 80 | defer srv.Close() |
| 81 | |
| 82 | testCases := []checkTest{ |
| 83 | { |
| 84 | description: "ignores recording rules", |
| 85 | content: "- record: foo\n expr: up == 0\n", |
| 86 | checker: checks.NewAlertsCheck("prom", "http://localhost", time.Second*5, time.Hour*24, time.Minute, time.Minute*5), |
| 87 | }, |
| 88 | { |
| 89 | description: "ignores rules with syntax errors", |
| 90 | content: "- alert: Foo Is Down\n expr: sum(\n", |
| 91 | checker: checks.NewAlertsCheck("prom", "http://localhost", time.Second*5, time.Hour*24, time.Minute, time.Minute*5), |
| 92 | }, |
| 93 | { |
| 94 | description: "bad request", |
| 95 | content: content, |
| 96 | checker: checks.NewAlertsCheck("prom", srv.URL+"/400/", time.Second*5, time.Hour*24, time.Minute, time.Minute*5), |
| 97 | problems: []checks.Problem{ |
| 98 | { |
| 99 | Fragment: `up{job="foo"} == 0`, |
| 100 | Lines: []int{2}, |
| 101 | Reporter: "alerts/count", |
| 102 | Text: "query using prom failed with: bad_data: unhandled path", |
| 103 | Severity: checks.Bug, |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | { |
| 108 | description: "empty response", |
| 109 | content: content, |
| 110 | checker: checks.NewAlertsCheck("prom", srv.URL+"/empty/", time.Second*5, time.Hour*24, time.Minute, time.Minute*5), |
| 111 | problems: []checks.Problem{ |
| 112 | { |
| 113 | Fragment: `up{job="foo"} == 0`, |
| 114 | Lines: []int{2}, |
| 115 | Reporter: "alerts/count", |
| 116 | Text: "query using prom would trigger 0 alert(s) in the last 1d", |
| 117 | Severity: checks.Information, |
| 118 | }, |
| 119 | }, |
| 120 | }, |
| 121 | { |
| 122 | description: "multiple alerts", |
| 123 | content: content, |
| 124 | checker: checks.NewAlertsCheck("prom", srv.URL+"/alerts/", time.Second*5, time.Hour*24, time.Minute, time.Minute*5), |
| 125 | problems: []checks.Problem{ |
| 126 | { |
| 127 | Fragment: `up{job="foo"} == 0`, |
| 128 | Lines: []int{2}, |
| 129 | Reporter: "alerts/count", |
| 130 | Text: "query using prom would trigger 7 alert(s) in the last 1d", |
| 131 | Severity: checks.Information, |
| 132 | }, |
| 133 | }, |
| 134 | }, |
| 135 | { |
| 136 | description: "for: 10m", |
| 137 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} == 0\n", |
| 138 | checker: checks.NewAlertsCheck("prom", srv.URL+"/alerts/", time.Second*5, time.Hour*24, time.Minute*6, time.Minute*10), |
| 139 | problems: []checks.Problem{ |
| 140 | { |
| 141 | Fragment: `up{job="foo"} == 0`, |
| 142 | Lines: []int{2, 3}, |
| 143 | Reporter: "alerts/count", |
| 144 | Text: "query using prom would trigger 1 alert(s) in the last 1d", |
| 145 | Severity: checks.Information, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | } |
| 150 | |
| 151 | runTests(t, testCases) |
| 152 | } |
| 153 | |