2020-04-27 23:19:27 +02:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
2020-05-10 18:58:17 +02:00
|
|
|
"context"
|
2020-04-27 23:19:27 +02:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-06-21 12:32:46 +02:00
|
|
|
"strconv"
|
2020-04-27 23:19:27 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertManager_Send(t *testing.T) {
|
2020-06-29 21:21:03 +02:00
|
|
|
const baUser, baPass = "foo", "bar"
|
2020-04-27 23:19:27 +02:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
t.Errorf("should not be called")
|
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc(alertManagerPath, func(w http.ResponseWriter, r *http.Request) {
|
2020-06-29 21:21:03 +02:00
|
|
|
user, pass, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("unauthorized request")
|
|
|
|
}
|
|
|
|
if user != baUser || pass != baPass {
|
|
|
|
t.Errorf("wrong creds %q:%q; expected %q:%q",
|
|
|
|
user, pass, baUser, baPass)
|
|
|
|
}
|
2020-04-27 23:19:27 +02:00
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
t.Errorf("expected POST method got %s", r.Method)
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 1:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
case 2:
|
|
|
|
var a []struct {
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
StartsAt time.Time `json:"startsAt"`
|
|
|
|
EndAt time.Time `json:"endsAt"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
|
|
|
GeneratorURL string `json:"generatorURL"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
|
|
|
t.Errorf("can not unmarshal data into alert %s", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(a) != 1 {
|
|
|
|
t.Errorf("expected 1 alert in array got %d", len(a))
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
if a[0].GeneratorURL != "0/0" {
|
2020-07-02 17:05:36 +02:00
|
|
|
t.Errorf("expected 0/0 as generatorURL got %s", a[0].GeneratorURL)
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
if a[0].Labels["alertname"] != "alert0" {
|
2020-07-02 17:05:36 +02:00
|
|
|
t.Errorf("expected alert0 as alert name got %s", a[0].Labels["alertname"])
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
if a[0].StartsAt.IsZero() {
|
2020-07-02 17:05:36 +02:00
|
|
|
t.Errorf("expected non-zero start time")
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
if a[0].EndAt.IsZero() {
|
2020-07-02 17:05:36 +02:00
|
|
|
t.Errorf("expected non-zero end time")
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
2020-06-29 21:21:03 +02:00
|
|
|
am := NewAlertManager(srv.URL, baUser, baPass, func(alert Alert) string {
|
2020-06-21 12:32:46 +02:00
|
|
|
return strconv.FormatUint(alert.GroupID, 10) + "/" + strconv.FormatUint(alert.ID, 10)
|
2020-04-27 23:19:27 +02:00
|
|
|
}, srv.Client())
|
2020-05-10 18:58:17 +02:00
|
|
|
if err := am.Send(context.Background(), []Alert{{}, {}}); err == nil {
|
2020-04-27 23:19:27 +02:00
|
|
|
t.Error("expected connection error got nil")
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
if err := am.Send(context.Background(), []Alert{}); err == nil {
|
2020-04-27 23:19:27 +02:00
|
|
|
t.Error("expected wrong http code error got nil")
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
if err := am.Send(context.Background(), []Alert{{
|
|
|
|
GroupID: 0,
|
2020-04-27 23:19:27 +02:00
|
|
|
Name: "alert0",
|
|
|
|
Start: time.Now().UTC(),
|
|
|
|
End: time.Now().UTC(),
|
|
|
|
Annotations: map[string]string{"a": "b", "c": "d", "e": "f"},
|
|
|
|
}}); err != nil {
|
|
|
|
t.Errorf("unexpected error %s", err)
|
|
|
|
}
|
|
|
|
if c != 2 {
|
|
|
|
t.Errorf("expected 2 calls(count from zero) to server got %d", c)
|
|
|
|
}
|
|
|
|
}
|