2020-04-27 21:19:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-05-10 16:58:17 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2020-04-27 21:19:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandler(t *testing.T) {
|
2020-06-01 10:46:37 +00:00
|
|
|
ar := &AlertingRule{
|
2020-04-27 21:19:27 +00:00
|
|
|
Name: "alert",
|
|
|
|
alerts: map[uint64]*notifier.Alert{
|
|
|
|
0: {},
|
|
|
|
},
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
g := &Group{
|
|
|
|
Name: "group",
|
2020-06-01 10:46:37 +00:00
|
|
|
Rules: []Rule{ar},
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
m := &manager{groups: make(map[uint64]*Group)}
|
|
|
|
m.groups[0] = g
|
|
|
|
rh := &requestHandler{m: m}
|
|
|
|
|
2020-04-27 21:19:27 +00:00
|
|
|
getResp := func(url string, to interface{}, code int) {
|
|
|
|
t.Helper()
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected err %s", err)
|
|
|
|
}
|
|
|
|
if code != resp.StatusCode {
|
|
|
|
t.Errorf("unexpected status code %d want %d", resp.StatusCode, code)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
t.Errorf("err closing body %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if to != nil {
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(to); err != nil {
|
|
|
|
t.Errorf("unexpected err %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }))
|
|
|
|
defer ts.Close()
|
|
|
|
t.Run("/api/v1/alerts", func(t *testing.T) {
|
|
|
|
lr := listAlertsResponse{}
|
|
|
|
getResp(ts.URL+"/api/v1/alerts", &lr, 200)
|
|
|
|
if length := len(lr.Data.Alerts); length != 1 {
|
|
|
|
t.Errorf("expected 1 alert got %d", length)
|
|
|
|
}
|
|
|
|
})
|
2020-06-01 10:46:37 +00:00
|
|
|
t.Run("/api/v1/groups", func(t *testing.T) {
|
|
|
|
lr := listGroupsResponse{}
|
|
|
|
getResp(ts.URL+"/api/v1/groups", &lr, 200)
|
|
|
|
if length := len(lr.Data.Groups); length != 1 {
|
|
|
|
t.Errorf("expected 1 group got %d", length)
|
|
|
|
}
|
|
|
|
})
|
2020-05-10 16:58:17 +00:00
|
|
|
t.Run("/api/v1/0/0/status", func(t *testing.T) {
|
2020-04-27 21:19:27 +00:00
|
|
|
alert := &APIAlert{}
|
2020-05-10 16:58:17 +00:00
|
|
|
getResp(ts.URL+"/api/v1/0/0/status", alert, 200)
|
2020-06-01 10:46:37 +00:00
|
|
|
expAlert := ar.newAlertAPI(*ar.alerts[0])
|
2020-04-27 21:19:27 +00:00
|
|
|
if !reflect.DeepEqual(alert, expAlert) {
|
|
|
|
t.Errorf("expected %v is equal to %v", alert, expAlert)
|
|
|
|
}
|
|
|
|
})
|
2020-05-10 16:58:17 +00:00
|
|
|
t.Run("/api/v1/0/1/status", func(t *testing.T) {
|
|
|
|
getResp(ts.URL+"/api/v1/0/1/status", nil, 404)
|
2020-04-27 21:19:27 +00:00
|
|
|
})
|
2020-05-10 16:58:17 +00:00
|
|
|
t.Run("/api/v1/1/0/status", func(t *testing.T) {
|
|
|
|
getResp(ts.URL+"/api/v1/1/0/status", nil, 404)
|
2020-04-27 21:19:27 +00:00
|
|
|
})
|
|
|
|
t.Run("/", func(t *testing.T) {
|
|
|
|
getResp(ts.URL, nil, 200)
|
|
|
|
})
|
|
|
|
}
|