Add silencing test

This commit is contained in:
Fabian Reinartz 2015-10-01 21:28:18 +02:00
parent 38b324eab2
commit 7b0820a205
3 changed files with 80 additions and 10 deletions

View File

@ -89,7 +89,7 @@ func main() {
router := route.New()
NewAPI(router.WithPrefix("/api"), alerts, silences)
NewAPI(router.WithPrefix("/api/v1"), alerts, silences)
go http.ListenAndServe(*listenAddress, router)

View File

@ -52,16 +52,27 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
return test
}
func freeAddress() string {
// Let the OS allocate a free address, close it and hope
// it is still free when starting Alertmanager.
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
l.Close()
var freeAdresses []string
return l.Addr().String()
func freeAddress() string {
if len(freeAdresses) == 0 {
for i := 0; i < 100; i++ {
// Let the OS allocate a free address, close it and hope
// it is still free when starting Alertmanager.
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
defer l.Close()
freeAdresses = append(freeAdresses, l.Addr().String())
}
}
next := freeAdresses[0]
freeAdresses = freeAdresses[1:]
return next
}
// Alertmanager returns a new structure that allows starting an instance
@ -85,6 +96,8 @@ func (t *AcceptanceTest) Alertmanager() *Alertmanager {
am.addr = freeAddress()
t.Logf("AM on %s", am.addr)
client, err := alertmanager.New(alertmanager.Config{
Address: fmt.Sprintf("http://%s", am.addr),
})

View File

@ -57,6 +57,63 @@ func TestSomething(t *testing.T) {
at.Run()
}
var silenceConfig = `
routes:
- send_to: "default"
group_wait: 1s
group_interval: 1s
notification_configs:
- name: "default"
send_resolved: true
webhook_configs:
- url: 'http://localhost:8090'
`
func TestSilencing(t *testing.T) {
t.Parallel()
at := NewAcceptanceTest(t, &AcceptanceOpts{
Tolerance: 150 * time.Millisecond,
Config: somethingConfig,
})
am := at.Alertmanager()
co := at.Collector("webhook")
go NewWebhook(":8090", co).Run()
// No repeat interval is configured. Thus, we receive an alert
// notification every second.
am.Push(At(1), Alert("alertname", "test122").Active(1))
am.Push(At(1), Alert("alertname", "test2").Active(1))
co.Want(Between(2, 2.5),
Alert("alertname", "test1").Active(1),
Alert("alertname", "test2").Active(1),
)
// Add a silence that affects the first alert.
sil := Silence(2, 4.5).Match("alertname", "test1")
am.SetSilence(At(2.5), sil)
co.Want(Between(3, 3.5), Alert("alertname", "test2").Active(1))
co.Want(Between(4, 4.5), Alert("alertname", "test2").Active(1))
// Remove the silence so in the next interval we receive both
// alerts again.
am.DelSilence(At(4.5), sil)
co.Want(Between(5, 5.5),
Alert("alertname", "test1").Active(1),
Alert("alertname", "test2").Active(1),
)
// Start the flow as defined above and run the checks afterwards.
// at.Run()
}
var batchConfig = `
routes:
- send_to: "default"