2020-04-06 11:44:03 +00:00
|
|
|
package notifier
|
2020-02-21 21:15:05 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-10 16:58:17 +00:00
|
|
|
"context"
|
2020-02-21 21:15:05 +00:00
|
|
|
"encoding/json"
|
2024-08-06 13:37:25 +00:00
|
|
|
"fmt"
|
2020-02-21 21:15:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-06-21 10:32:46 +00:00
|
|
|
"strconv"
|
2020-02-21 21:15:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2022-02-02 12:11:41 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2024-09-09 11:34:48 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
2020-02-21 21:15:05 +00:00
|
|
|
)
|
|
|
|
|
2021-08-31 09:28:02 +00:00
|
|
|
func TestAlertManager_Addr(t *testing.T) {
|
|
|
|
const addr = "http://localhost"
|
2022-04-09 06:21:16 +00:00
|
|
|
am, err := NewAlertManager(addr, nil, promauth.HTTPClientConfig{}, nil, 0)
|
2022-02-02 12:11:41 +00:00
|
|
|
if err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
2021-08-31 09:28:02 +00:00
|
|
|
if am.Addr() != addr {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected to have %q; got %q", addr, am.Addr())
|
2021-08-31 09:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 21:15:05 +00:00
|
|
|
func TestAlertManager_Send(t *testing.T) {
|
2020-06-29 19:21:03 +00:00
|
|
|
const baUser, baPass = "foo", "bar"
|
2023-04-27 11:02:21 +00:00
|
|
|
const headerKey, headerValue = "TenantID", "foo"
|
2020-02-21 21:15:05 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("should not be called")
|
2020-02-21 21:15:05 +00:00
|
|
|
})
|
|
|
|
c := -1
|
2020-04-06 11:44:03 +00:00
|
|
|
mux.HandleFunc(alertManagerPath, func(w http.ResponseWriter, r *http.Request) {
|
2020-06-29 19:21:03 +00:00
|
|
|
user, pass, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unauthorized request")
|
2020-06-29 19:21:03 +00:00
|
|
|
}
|
|
|
|
if user != baUser || pass != baPass {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("wrong creds %q:%q; expected %q:%q", user, pass, baUser, baPass)
|
2020-06-29 19:21:03 +00:00
|
|
|
}
|
2020-02-21 21:15:05 +00:00
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected POST method got %s", r.Method)
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 1:
|
2024-08-06 13:37:25 +00:00
|
|
|
if r.Header.Get(headerKey) != headerValue {
|
|
|
|
t.Fatalf("expected header %q to be set to %q; got %q instead", headerKey, headerValue, r.Header.Get(headerKey))
|
|
|
|
}
|
2020-02-21 21:15:05 +00:00
|
|
|
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 {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("can not unmarshal data into alert %s", err)
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
if len(a) != 1 {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 1 alert in array got %d", len(a))
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
if a[0].GeneratorURL != "0/0" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 0/0 as generatorURL got %s", a[0].GeneratorURL)
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
if a[0].StartsAt.IsZero() {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected non-zero start time")
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
if a[0].EndAt.IsZero() {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected non-zero end time")
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
if len(a[0].Labels) != 1 {
|
|
|
|
t.Fatalf("expected 1 labels got %d", len(a[0].Labels))
|
|
|
|
}
|
|
|
|
if len(a[0].Annotations) != 2 {
|
|
|
|
t.Fatalf("expected 2 annotations got %d", len(a[0].Annotations))
|
|
|
|
}
|
2024-08-06 13:37:25 +00:00
|
|
|
if r.Header.Get(headerKey) != "bar" {
|
|
|
|
t.Fatalf("expected header %q to be set to %q; got %q instead", headerKey, headerValue, r.Header.Get(headerKey))
|
|
|
|
}
|
2023-04-27 11:02:21 +00:00
|
|
|
case 3:
|
2024-09-09 11:34:48 +00:00
|
|
|
var a []struct {
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
|
|
|
t.Fatalf("can not unmarshal data into alert %s", err)
|
|
|
|
}
|
|
|
|
if len(a) != 1 {
|
|
|
|
t.Fatalf("expected 1 alert in array got %d", len(a))
|
|
|
|
}
|
|
|
|
if len(a[0].Labels) != 3 {
|
|
|
|
t.Fatalf("expected 3 labels got %d", len(a[0].Labels))
|
|
|
|
}
|
|
|
|
if a[0].Labels["env"] != "prod" {
|
|
|
|
t.Fatalf("expected env label to be prod during relabeling, got %s", a[0].Labels["env"])
|
|
|
|
}
|
|
|
|
if r.Header.Get(headerKey) != "bar" {
|
|
|
|
t.Fatalf("expected header %q to be set to %q; got %q instead", headerKey, "bar", r.Header.Get(headerKey))
|
2023-04-27 11:02:21 +00:00
|
|
|
}
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
2022-02-02 12:11:41 +00:00
|
|
|
|
|
|
|
aCfg := promauth.HTTPClientConfig{
|
|
|
|
BasicAuth: &promauth.BasicAuthConfig{
|
|
|
|
Username: baUser,
|
|
|
|
Password: promauth.NewSecret(baPass),
|
|
|
|
},
|
2024-08-06 13:37:25 +00:00
|
|
|
Headers: []string{fmt.Sprintf("%s:%s", headerKey, headerValue)},
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
parsedConfigs, err := promrelabel.ParseRelabelConfigsData([]byte(`
|
|
|
|
- action: drop
|
|
|
|
if: '{tenant="0"}'
|
|
|
|
regex: ".*"
|
|
|
|
- target_label: "env"
|
|
|
|
replacement: "prod"
|
|
|
|
if: '{tenant="1"}'
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error when parse relabeling config: %s", err)
|
|
|
|
}
|
2022-02-02 12:11:41 +00:00
|
|
|
am, err := NewAlertManager(srv.URL+alertManagerPath, func(alert Alert) string {
|
2020-06-21 10:32:46 +00:00
|
|
|
return strconv.FormatUint(alert.GroupID, 10) + "/" + strconv.FormatUint(alert.ID, 10)
|
2024-09-09 11:34:48 +00:00
|
|
|
}, aCfg, parsedConfigs, 0)
|
2022-02-02 12:11:41 +00:00
|
|
|
if err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
|
|
|
|
if err := am.Send(context.Background(), []Alert{{Labels: map[string]string{"a": "b"}}}, nil); err == nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected connection error got nil")
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
|
|
|
|
if err := am.Send(context.Background(), []Alert{{Labels: map[string]string{"a": "b"}}}, nil); err == nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected wrong http code error got nil")
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
|
2020-05-10 16:58:17 +00:00
|
|
|
if err := am.Send(context.Background(), []Alert{{
|
|
|
|
GroupID: 0,
|
2020-03-13 10:19:31 +00:00
|
|
|
Name: "alert0",
|
|
|
|
Start: time.Now().UTC(),
|
|
|
|
End: time.Now().UTC(),
|
2024-09-09 11:34:48 +00:00
|
|
|
Labels: map[string]string{"alertname": "alert0"},
|
|
|
|
Annotations: map[string]string{"a": "b", "c": "d"},
|
2024-08-06 13:37:25 +00:00
|
|
|
}}, map[string]string{headerKey: "bar"}); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
|
|
|
|
if err := am.Send(context.Background(), []Alert{
|
|
|
|
// drop tenant0 alert message during relabeling
|
|
|
|
{
|
|
|
|
Name: "alert1",
|
|
|
|
Labels: map[string]string{"rule": "test", "tenant": "0"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alert2",
|
|
|
|
Labels: map[string]string{"rule": "test", "tenant": "1"},
|
|
|
|
},
|
|
|
|
}, map[string]string{headerKey: "bar"}); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2023-04-27 11:02:21 +00:00
|
|
|
}
|
2024-09-09 11:34:48 +00:00
|
|
|
|
|
|
|
if c != 3 {
|
|
|
|
t.Fatalf("expected 3 calls(count from zero) to server got %d", c)
|
|
|
|
}
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|