bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/database/test/errors_test.go (about) 1 package dbtest 2 3 import ( 4 "testing" 5 ) 6 7 func TestErrors_RoundTrip(t *testing.T) { 8 ed := testData.Errors() 9 alert := "abcdefg" 10 11 // make sure we can mark success and error 12 check(t, ed.MarkAlertSuccess(alert)) 13 failing, err := ed.IsAlertFailing(alert) 14 check(t, err) 15 if failing { 16 t.Fatal("Alert should not be failing") 17 } 18 check(t, ed.MarkAlertFailure(alert, "Something bad happened")) 19 failing, err = ed.IsAlertFailing(alert) 20 check(t, err) 21 if !failing { 22 t.Fatal("Alert should be failing") 23 } 24 25 // generate a sequence of errors. We should have groups of 2/1/1 (oldest to newest) 26 check(t, ed.MarkAlertFailure(alert, "Something bad happened")) 27 check(t, ed.MarkAlertSuccess(alert)) 28 check(t, ed.MarkAlertFailure(alert, "Something bad happened")) 29 check(t, ed.MarkAlertFailure(alert, "Something different bad happened")) 30 31 failingCount, events, err := ed.GetFailingAlertCounts() 32 check(t, err) 33 if failingCount != 1 { 34 t.Fatalf("Expected 1 failing alert. Got %d", failingCount) 35 } 36 if events != 4 { 37 t.Fatalf("Expected 1 error events. Got %d", events) 38 } 39 40 fullData, err := ed.GetFullErrorHistory() 41 check(t, err) 42 if len(fullData) != 1 { 43 t.Fatalf("Expected data for 1 alert. See %d", len(fullData)) 44 } 45 ad := fullData[alert] 46 if len(ad) != 3 { 47 t.Fatalf("Expected data for alert to have 3 entries. See %d", len(ad)) 48 } 49 if ad[0].Count != 1 { 50 t.Fatalf("Expected first entry to have length 1. Found %d.", ad[0].Count) 51 } 52 if ad[1].Count != 1 { 53 t.Fatalf("Expected second entry to have length 1. Found %d.", ad[1].Count) 54 } 55 if ad[2].Count != 2 { 56 t.Fatalf("Expected third entry to have length 2. Found %d.", ad[2].Count) 57 } 58 59 check(t, ed.ClearAlert(alert)) 60 failingCount, events, err = ed.GetFailingAlertCounts() 61 check(t, err) 62 if failingCount != 0 { 63 t.Fatalf("Expected 0 failing alert. Got %d", failingCount) 64 } 65 if events != 0 { 66 t.Fatalf("Expected 0 error events. Got %d", events) 67 } 68 69 check(t, ed.MarkAlertFailure(alert, "Something bad happened")) 70 check(t, ed.ClearAll()) 71 failingCount, events, err = ed.GetFailingAlertCounts() 72 check(t, err) 73 if failingCount != 0 { 74 t.Fatalf("Expected 0 failing alert. Got %d", failingCount) 75 } 76 if events != 0 { 77 t.Fatalf("Expected 0 error events. Got %d", events) 78 } 79 }