这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cmd/bosun/sched/check_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package sched

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"bosun.org/cmd/bosun/conf"
"bosun.org/cmd/bosun/expr"
Expand Down Expand Up @@ -73,3 +78,52 @@ func TestCheckFlapping(t *testing.T) {
t.Fatal("expected notification")
}
}

func TestCheckSilence(t *testing.T) {
s := new(Schedule)
done := make(chan bool, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
done <- true
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
c, err := conf.New("", fmt.Sprintf(`
template t {
subject = "test"
body = "test"
}
notification n {
post = http://%s/
}
alert a {
template = t
warnNotification = n
warn = 1
}
`, u.Host))
if err != nil {
t.Fatal(err)
}
err = s.Init(c)
if err != nil {
t.Fatal(err)
}
_, err = s.AddSilence(time.Now().Add(-time.Hour), time.Now().Add(time.Hour), "a", "", false, true, "")
if err != nil {
t.Fatal(err)
}
_, err = s.Check(nil, time.Now())
if err != nil {
t.Fatal(err)
}
s.CheckNotifications()
select {
case <-done:
t.Fatal("silenced notification was sent")
case <-time.After(time.Second * 2):
// Timeout *probably* means the silence worked
}
}
6 changes: 5 additions & 1 deletion cmd/bosun/sched/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ func (s *Schedule) sendNotifications(silenced map[expr.AlertKey]Silence) {
ustates := make(States)
for _, st := range states {
ak := st.AlertKey()
_, silenced := silenced[ak]
if st.Last().Status == StUnknown {
if _, ok := silenced[ak]; ok {
if silenced {
log.Println("silencing unknown", ak)
continue
}
ustates[ak] = st
}
if silenced {
log.Println("silencing", ak)
} else {
s.notify(st, n)
}
Expand Down