From 7b0820a205a748ed1035b8d17db1d8cdae651f67 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 1 Oct 2015 21:28:18 +0200 Subject: [PATCH] Add silencing test --- main.go | 2 +- test/acceptance.go | 31 ++++++++++++------ test/acceptance/simple_test.go | 57 ++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 2d788d53..94bb2123 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/test/acceptance.go b/test/acceptance.go index ed392007..5eddab59 100644 --- a/test/acceptance.go +++ b/test/acceptance.go @@ -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), }) diff --git a/test/acceptance/simple_test.go b/test/acceptance/simple_test.go index be6e0119..7f5ecbfc 100644 --- a/test/acceptance/simple_test.go +++ b/test/acceptance/simple_test.go @@ -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"