diff --git a/app/vmalert/manager.go b/app/vmalert/manager.go index 028d8df24..31e8fe92c 100644 --- a/app/vmalert/manager.go +++ b/app/vmalert/manager.go @@ -205,6 +205,7 @@ func (g *Group) toAPI() APIGroup { Headers: headersToStrings(g.Headers), Labels: g.Labels, } + ag.Rules = make([]APIRule, 0) for _, r := range g.Rules { ag.Rules = append(ag.Rules, r.ToAPI()) } diff --git a/app/vmalert/web_test.go b/app/vmalert/web_test.go index 5b83a58f0..e35535aa0 100644 --- a/app/vmalert/web_test.go +++ b/app/vmalert/web_test.go @@ -147,8 +147,8 @@ func TestHandler(t *testing.T) { } func TestEmptyResponse(t *testing.T) { - rh := &requestHandler{m: &manager{groups: make(map[uint64]*Group)}} - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })) + rhWithNoGroups := &requestHandler{m: &manager{groups: make(map[uint64]*Group)}} + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rhWithNoGroups.handler(w, r) })) defer ts.Close() getResp := func(url string, to interface{}, code int) { @@ -172,7 +172,7 @@ func TestEmptyResponse(t *testing.T) { } } - t.Run("/api/v1/alerts", func(t *testing.T) { + t.Run("no groups /api/v1/alerts", func(t *testing.T) { lr := listAlertsResponse{} getResp(ts.URL+"/api/v1/alerts", &lr, 200) if lr.Data.Alerts == nil { @@ -186,7 +186,7 @@ func TestEmptyResponse(t *testing.T) { } }) - t.Run("/api/v1/rules", func(t *testing.T) { + t.Run("no groups /api/v1/rules", func(t *testing.T) { lr := listGroupsResponse{} getResp(ts.URL+"/api/v1/rules", &lr, 200) if lr.Data.Groups == nil { @@ -199,4 +199,26 @@ func TestEmptyResponse(t *testing.T) { t.Errorf("expected /api/v1/rules response to have non-nil data") } }) + + rhWithEmptyGroup := &requestHandler{m: &manager{groups: map[uint64]*Group{0: {Name: "test"}}}} + ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rhWithEmptyGroup.handler(w, r) }) + + t.Run("empty group /api/v1/rules", func(t *testing.T) { + lr := listGroupsResponse{} + getResp(ts.URL+"/api/v1/rules", &lr, 200) + if lr.Data.Groups == nil { + t.Fatalf("expected /api/v1/rules response to have non-nil data") + } + + lr = listGroupsResponse{} + getResp(ts.URL+"/vmalert/api/v1/rules", &lr, 200) + if lr.Data.Groups == nil { + t.Fatalf("expected /api/v1/rules response to have non-nil data") + } + + group := lr.Data.Groups[0] + if group.Rules == nil { + t.Fatalf("expected /api/v1/rules response to have non-nil rules for group") + } + }) }