2015-09-30 14:13:00 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2015-10-02 12:10:04 +00:00
|
|
|
"fmt"
|
2015-09-30 14:13:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/prometheus/alertmanager/test"
|
|
|
|
)
|
|
|
|
|
2015-10-02 12:10:04 +00:00
|
|
|
func TestSomething(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := `
|
2015-09-30 14:13:00 +00:00
|
|
|
routes:
|
|
|
|
- send_to: "default"
|
2015-10-08 08:50:37 +00:00
|
|
|
group_wait: 1s
|
|
|
|
group_interval: 1s
|
|
|
|
repeat_interval: 1s
|
2015-09-30 14:13:00 +00:00
|
|
|
|
|
|
|
notification_configs:
|
|
|
|
- name: "default"
|
|
|
|
webhook_configs:
|
2015-10-02 12:10:04 +00:00
|
|
|
- url: 'http://%s'
|
2015-09-30 14:13:00 +00:00
|
|
|
`
|
|
|
|
|
2015-09-30 14:18:44 +00:00
|
|
|
// Create a new acceptance test that instantiates new Alertmanagers
|
|
|
|
// with the given configuration and verifies times with the given
|
|
|
|
// tollerance.
|
2015-09-30 14:13:00 +00:00
|
|
|
at := NewAcceptanceTest(t, &AcceptanceOpts{
|
|
|
|
Tolerance: 150 * time.Millisecond,
|
|
|
|
})
|
|
|
|
|
2015-09-30 14:18:44 +00:00
|
|
|
// Create a collector to which alerts can be written and verified
|
|
|
|
// against a set of expected alert notifications.
|
2015-09-30 14:13:00 +00:00
|
|
|
co := at.Collector("webhook")
|
2015-09-30 14:18:44 +00:00
|
|
|
// Run something that satisfies the webhook interface to which the
|
|
|
|
// Alertmanager pushes as defined by its configuration.
|
2015-10-02 12:10:04 +00:00
|
|
|
wh := NewWebhook(co)
|
|
|
|
|
|
|
|
// Create a new Alertmanager process listening to a random port
|
|
|
|
am := at.Alertmanager(fmt.Sprintf(conf, wh.Address()))
|
2015-09-30 14:13:00 +00:00
|
|
|
|
2015-09-30 14:18:44 +00:00
|
|
|
// Declare pushes to be made to the Alertmanager at the given time.
|
|
|
|
// Times are provided in fractions of seconds.
|
2015-09-30 14:13:00 +00:00
|
|
|
am.Push(At(1), Alert("alertname", "test").Active(1))
|
2015-10-07 14:18:55 +00:00
|
|
|
|
|
|
|
at.Do(At(1.2), func() {
|
|
|
|
am.Terminate()
|
|
|
|
am.Start()
|
|
|
|
})
|
2015-09-30 14:13:00 +00:00
|
|
|
am.Push(At(3.5), Alert("alertname", "test").Active(1, 3))
|
|
|
|
|
2015-09-30 14:18:44 +00:00
|
|
|
// Declare which alerts are expected to arrive at the collector within
|
|
|
|
// the defined time intervals.
|
2015-09-30 14:13:00 +00:00
|
|
|
co.Want(Between(2, 2.5), Alert("alertname", "test").Active(1))
|
|
|
|
co.Want(Between(3, 3.5), Alert("alertname", "test").Active(1))
|
2015-09-30 15:35:33 +00:00
|
|
|
co.Want(Between(4, 4.5), Alert("alertname", "test").Active(1, 3))
|
2015-09-30 14:13:00 +00:00
|
|
|
|
2015-09-30 14:18:44 +00:00
|
|
|
// Start the flow as defined above and run the checks afterwards.
|
2015-09-30 14:13:00 +00:00
|
|
|
at.Run()
|
|
|
|
}
|
2015-09-30 16:02:47 +00:00
|
|
|
|
2015-10-02 12:10:04 +00:00
|
|
|
func TestSilencing(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := `
|
2015-10-01 19:28:18 +00:00
|
|
|
routes:
|
|
|
|
- send_to: "default"
|
2015-10-08 08:50:37 +00:00
|
|
|
group_wait: 1s
|
|
|
|
group_interval: 1s
|
|
|
|
repeat_interval: 1s
|
2015-10-01 19:28:18 +00:00
|
|
|
|
|
|
|
notification_configs:
|
|
|
|
- name: "default"
|
|
|
|
webhook_configs:
|
2015-10-02 12:10:04 +00:00
|
|
|
- url: 'http://%s'
|
2015-10-01 19:28:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
at := NewAcceptanceTest(t, &AcceptanceOpts{
|
|
|
|
Tolerance: 150 * time.Millisecond,
|
|
|
|
})
|
|
|
|
|
|
|
|
co := at.Collector("webhook")
|
2015-10-02 12:10:04 +00:00
|
|
|
wh := NewWebhook(co)
|
2015-10-01 19:28:18 +00:00
|
|
|
|
2015-10-02 12:10:04 +00:00
|
|
|
am := at.Alertmanager(fmt.Sprintf(conf, wh.Address()))
|
2015-10-01 19:28:18 +00:00
|
|
|
|
|
|
|
// No repeat interval is configured. Thus, we receive an alert
|
|
|
|
// notification every second.
|
2015-10-01 20:15:27 +00:00
|
|
|
am.Push(At(1), Alert("alertname", "test1").Active(1))
|
2015-10-01 19:28:18 +00:00
|
|
|
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.
|
2015-10-01 20:22:51 +00:00
|
|
|
am.SetSilence(At(2.5), Silence(2, 4.5).Match("alertname", "test1"))
|
2015-10-01 19:28:18 +00:00
|
|
|
|
|
|
|
co.Want(Between(3, 3.5), Alert("alertname", "test2").Active(1))
|
|
|
|
co.Want(Between(4, 4.5), Alert("alertname", "test2").Active(1))
|
|
|
|
|
2015-10-01 20:22:51 +00:00
|
|
|
// Silence should be over now and we receive both alerts again.
|
2015-10-01 19:28:18 +00:00
|
|
|
|
|
|
|
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.
|
2015-10-01 20:15:27 +00:00
|
|
|
at.Run()
|
2015-10-01 19:28:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 12:10:04 +00:00
|
|
|
func TestBatching(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := `
|
2015-09-30 16:02:47 +00:00
|
|
|
routes:
|
|
|
|
- send_to: "default"
|
2015-10-08 08:50:37 +00:00
|
|
|
group_wait: 1s
|
|
|
|
group_interval: 1s
|
|
|
|
repeat_interval: 5s
|
2015-09-30 16:02:47 +00:00
|
|
|
|
|
|
|
notification_configs:
|
|
|
|
- name: "default"
|
|
|
|
webhook_configs:
|
2015-10-02 12:10:04 +00:00
|
|
|
- url: 'http://%s'
|
2015-09-30 16:02:47 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
at := NewAcceptanceTest(t, &AcceptanceOpts{
|
|
|
|
Tolerance: 150 * time.Millisecond,
|
|
|
|
})
|
|
|
|
|
|
|
|
co := at.Collector("webhook")
|
2015-10-02 12:10:04 +00:00
|
|
|
wh := NewWebhook(co)
|
2015-09-30 16:02:47 +00:00
|
|
|
|
2015-10-02 12:10:04 +00:00
|
|
|
am := at.Alertmanager(fmt.Sprintf(conf, wh.Address()))
|
2015-09-30 16:02:47 +00:00
|
|
|
|
|
|
|
am.Push(At(1.1), Alert("alertname", "test1").Active(1))
|
|
|
|
am.Push(At(1.9), Alert("alertname", "test5").Active(1))
|
|
|
|
am.Push(At(2.3),
|
|
|
|
Alert("alertname", "test2").Active(1.5),
|
|
|
|
Alert("alertname", "test3").Active(1.5),
|
|
|
|
Alert("alertname", "test4").Active(1.6),
|
|
|
|
)
|
|
|
|
|
|
|
|
co.Want(Between(2.0, 2.5),
|
|
|
|
Alert("alertname", "test1").Active(1),
|
|
|
|
Alert("alertname", "test5").Active(1),
|
|
|
|
)
|
|
|
|
// Only expect the new ones with the next group interval.
|
|
|
|
co.Want(Between(3, 3.5),
|
|
|
|
Alert("alertname", "test2").Active(1.5),
|
|
|
|
Alert("alertname", "test3").Active(1.5),
|
|
|
|
Alert("alertname", "test4").Active(1.6),
|
|
|
|
)
|
|
|
|
|
|
|
|
// While no changes happen expect no additional notifications
|
|
|
|
// until the 5s repeat interval has ended.
|
2015-10-01 07:43:51 +00:00
|
|
|
|
|
|
|
// The last three notifications should sent with the first two even
|
|
|
|
// though their repeat interval has not yet passed. This way fragmented
|
2015-09-30 17:03:19 +00:00
|
|
|
// batches are unified and notification noise reduced.
|
|
|
|
co.Want(Between(7, 7.5),
|
2015-09-30 16:02:47 +00:00
|
|
|
Alert("alertname", "test1").Active(1),
|
2015-09-30 16:45:49 +00:00
|
|
|
Alert("alertname", "test5").Active(1),
|
2015-09-30 16:02:47 +00:00
|
|
|
Alert("alertname", "test2").Active(1.5),
|
|
|
|
Alert("alertname", "test3").Active(1.5),
|
|
|
|
Alert("alertname", "test4").Active(1.6),
|
|
|
|
)
|
|
|
|
|
|
|
|
at.Run()
|
|
|
|
}
|