bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/sched/notification_test.go (about) 1 package sched 2 3 import ( 4 "testing" 5 6 "bosun.org/cmd/bosun/conf" 7 "bosun.org/cmd/bosun/conf/rule" 8 "bosun.org/models" 9 ) 10 11 func TestActionNotificationGrouping(t *testing.T) { 12 defer setup()() 13 c, err := rule.NewConf("", conf.EnabledBackends{}, nil, ` 14 template t{ 15 subject = 2 16 body = 2 17 } 18 notification n1 { 19 print = true 20 } 21 notification n2{ 22 print = true 23 } 24 notification n3{ 25 print = true 26 runOnActions = true 27 } 28 notification n4{ 29 print = true 30 runOnActions = false 31 } 32 alert a { 33 template = t 34 warnNotification = n1 35 critNotification = n2 36 warnNotification = n4 37 crit = 1 38 warn = 1 39 } 40 alert b{ 41 template = t 42 warnNotification = n2 43 critNotification = n3 44 crit = 1 45 warn = 1 46 } 47 lookup byHost{ 48 entry host=a{ 49 main_contact = n2 50 } 51 entry host=b{ 52 main_contact = n3 53 } 54 } 55 alert c{ 56 template = t 57 warnNotification = n1 58 warnNotification = lookup("byHost", "main_contact") 59 warn = 1 60 } 61 `) 62 if err != nil { 63 t.Fatal(err) 64 } 65 s, err := initSched(&conf.SystemConf{}, c) 66 if err != nil { 67 t.Fatal(err) 68 } 69 awarn := models.AlertKey("a{host=w}") 70 acrit := models.AlertKey("a{host=c}") 71 bwarn := models.AlertKey("b{host=w}") 72 bcrit := models.AlertKey("b{host=c}") 73 cA := models.AlertKey("c{host=a}") 74 cB := models.AlertKey("c{host=b}") 75 da := s.DataAccess.State() 76 da.UpdateIncidentState(&models.IncidentState{AlertKey: awarn, Alert: awarn.Name(), Tags: awarn.Group().Tags(), WorstStatus: models.StWarning, Events: []models.Event{{Status: models.StWarning}}}) 77 da.UpdateIncidentState(&models.IncidentState{AlertKey: acrit, Alert: acrit.Name(), Tags: acrit.Group().Tags(), WorstStatus: models.StCritical, Events: []models.Event{{Status: models.StCritical}}}) 78 da.UpdateIncidentState(&models.IncidentState{AlertKey: bwarn, Alert: bwarn.Name(), Tags: bwarn.Group().Tags(), WorstStatus: models.StWarning, Events: []models.Event{{Status: models.StWarning}}}) 79 da.UpdateIncidentState(&models.IncidentState{AlertKey: bcrit, Alert: bcrit.Name(), Tags: bcrit.Group().Tags(), WorstStatus: models.StCritical, Events: []models.Event{{Status: models.StCritical}}}) 80 da.UpdateIncidentState(&models.IncidentState{AlertKey: cA, Alert: cA.Name(), Tags: cA.Group().Tags(), WorstStatus: models.StWarning, Events: []models.Event{{Status: models.StWarning}}}) 81 da.UpdateIncidentState(&models.IncidentState{AlertKey: cB, Alert: cB.Name(), Tags: cB.Group().Tags(), WorstStatus: models.StWarning, Events: []models.Event{{Status: models.StWarning}}}) 82 83 groups, err := s.groupActionNotifications(models.ActionClose, []models.AlertKey{awarn, acrit, bwarn, bcrit, cA, cB}) 84 if err != nil { 85 t.Fatal(err) 86 } 87 expect := func(not string, aks ...models.AlertKey) { 88 n := c.Notifications[not] 89 key := notificationGroupKey{notification: n, template: c.Templates["t"]} 90 actualAks, ok := groups[key] 91 if !ok { 92 t.Fatalf("Notification %s not present in groupings.", not) 93 } 94 if len(actualAks) != len(aks) { 95 t.Fatalf("Count mismatch for grouping %s. %d != %d.", not, len(actualAks), len(aks)) 96 } 97 for i, ak := range aks { 98 if actualAks[i].AlertKey != ak { 99 t.Fatalf("Alert key mismatch at index %d. %s != %s.", i, actualAks[i].AlertKey, ak) 100 } 101 } 102 } 103 expect("n1", awarn, cA, cB) 104 expect("n2", acrit, bwarn, cA) 105 expect("n3", bcrit, cB) 106 }