mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-30 15:22:07 +00:00
[vmalert] add tests to webserver (#413)
This commit is contained in:
parent
7c9405f53d
commit
8fca5f2819
2 changed files with 79 additions and 6 deletions
|
@ -58,13 +58,14 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type listAlertsResponse struct {
|
||||||
|
Data struct {
|
||||||
|
Alerts []*apiAlert `json:"alerts"`
|
||||||
|
} `json:"data"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
func (rh *requestHandler) list() ([]byte, error) {
|
func (rh *requestHandler) list() ([]byte, error) {
|
||||||
type listAlertsResponse struct {
|
|
||||||
Data struct {
|
|
||||||
Alerts []*apiAlert `json:"alerts"`
|
|
||||||
} `json:"data"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
}
|
|
||||||
lr := listAlertsResponse{Status: "success"}
|
lr := listAlertsResponse{Status: "success"}
|
||||||
for _, g := range rh.groups {
|
for _, g := range rh.groups {
|
||||||
for _, r := range g.Rules {
|
for _, r := range g.Rules {
|
||||||
|
|
72
app/vmalert/web_test.go
Normal file
72
app/vmalert/web_test.go
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandler(t *testing.T) {
|
||||||
|
rule := &Rule{
|
||||||
|
Name: "alert",
|
||||||
|
alerts: map[uint64]*notifier.Alert{
|
||||||
|
0: {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
rh := &requestHandler{
|
||||||
|
groups: []Group{{
|
||||||
|
Name: "group",
|
||||||
|
Rules: []*Rule{rule},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("/api/v1/group/0/status", func(t *testing.T) {
|
||||||
|
alert := &apiAlert{}
|
||||||
|
getResp(ts.URL+"/api/v1/group/0/status", alert, 200)
|
||||||
|
expAlert := rule.newAlertAPI(*rule.alerts[0])
|
||||||
|
if !reflect.DeepEqual(alert, expAlert) {
|
||||||
|
t.Errorf("expected %v is equal to %v", alert, expAlert)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("/api/v1/group/1/status", func(t *testing.T) {
|
||||||
|
getResp(ts.URL+"/api/v1/group/1/status", nil, 404)
|
||||||
|
})
|
||||||
|
t.Run("/api/v1/unknown-group/0/status", func(t *testing.T) {
|
||||||
|
getResp(ts.URL+"/api/v1/unknown-group/0/status", nil, 404)
|
||||||
|
})
|
||||||
|
t.Run("/", func(t *testing.T) {
|
||||||
|
getResp(ts.URL, nil, 200)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue