Chore: Sql store split for legacy alerting (#52901)

Moves ~20 sqlstore methods for legacy alerting out of sqlstore (sqlstore.Store interface) and into alerting.
This commit is contained in:
Kyle Brandt
2022-08-03 11:17:26 -04:00
committed by GitHub
parent 46b7ca12e1
commit 643d2bc890
39 changed files with 483 additions and 474 deletions
+7 -7
View File
@@ -28,7 +28,7 @@ func (hs *HTTPServer) ValidateOrgAlert(c *models.ReqContext) {
}
query := models.GetAlertByIdQuery{Id: id}
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
c.JsonApiErr(404, "Alert not found", nil)
return
}
@@ -60,7 +60,7 @@ func (hs *HTTPServer) GetAlertStatesForDashboard(c *models.ReqContext) response.
DashboardId: c.QueryInt64("dashboardId"),
}
if err := hs.SQLStore.GetAlertStatesForDashboard(c.Req.Context(), &query); err != nil {
if err := hs.AlertEngine.AlertStore.GetAlertStatesForDashboard(c.Req.Context(), &query); err != nil {
return response.Error(500, "Failed to fetch alert states", err)
}
@@ -141,7 +141,7 @@ func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
query.State = states
}
if err := hs.SQLStore.HandleAlertsQuery(c.Req.Context(), &query); err != nil {
if err := hs.AlertEngine.AlertStore.HandleAlertsQuery(c.Req.Context(), &query); err != nil {
return response.Error(500, "List alerts failed", err)
}
@@ -223,7 +223,7 @@ func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response {
}
query := models.GetAlertByIdQuery{Id: id}
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
return response.Error(500, "List alerts failed", err)
}
@@ -689,7 +689,7 @@ func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.Req
result["alertId"] = alertID
query := models.GetAlertByIdQuery{Id: alertID}
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
if err := hs.AlertEngine.AlertStore.GetAlertById(c.Req.Context(), &query); err != nil {
return response.Error(500, "Get Alert failed", err)
}
@@ -719,7 +719,7 @@ func (hs *HTTPServer) PauseAlert(legacyAlertingEnabled *bool) func(c *models.Req
Paused: dto.Paused,
}
if err := hs.SQLStore.PauseAlert(c.Req.Context(), &cmd); err != nil {
if err := hs.AlertEngine.AlertStore.PauseAlert(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "", err)
}
@@ -764,7 +764,7 @@ func (hs *HTTPServer) PauseAllAlerts(legacyAlertingEnabled *bool) func(c *models
Paused: dto.Paused,
}
if err := hs.SQLStore.PauseAllAlerts(c.Req.Context(), &updateCmd); err != nil {
if err := hs.AlertEngine.AlertStore.PauseAllAlerts(c.Req.Context(), &updateCmd); err != nil {
return response.Error(500, "Failed to pause alerts", err)
}
-157
View File
@@ -1,157 +0,0 @@
package api
import (
"context"
"fmt"
"testing"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
)
var (
viewerRole = models.ROLE_VIEWER
editorRole = models.ROLE_EDITOR
)
type setUpConf struct {
aclMockResp []*models.DashboardACLInfoDTO
}
type mockSearchService struct{ ExpectedResult models.HitList }
func (mss *mockSearchService) SearchHandler(_ context.Context, q *search.Query) error {
q.Result = mss.ExpectedResult
return nil
}
func (mss *mockSearchService) SortOptions() []models.SortOption { return nil }
func setUp(confs ...setUpConf) *HTTPServer {
singleAlert := &models.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
store := mockstore.NewSQLStoreMock()
hs := &HTTPServer{SQLStore: store, SearchService: &mockSearchService{}}
store.ExpectedAlert = singleAlert
aclMockResp := []*models.DashboardACLInfoDTO{}
for _, c := range confs {
if c.aclMockResp != nil {
aclMockResp = c.aclMockResp
}
}
store.ExpectedTeamsByUser = []*models.TeamDTO{}
dashSvc := &dashboards.FakeDashboardService{}
dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Run(func(args mock.Arguments) {
q := args.Get(1).(*models.GetDashboardACLInfoListQuery)
q.Result = aclMockResp
}).Return(nil)
guardian.InitLegacyGuardian(store, dashSvc)
return hs
}
func TestAlertingAPIEndpoint(t *testing.T) {
t.Run("When user is editor and not in the ACL", func(t *testing.T) {
hs := setUp()
cmd := dtos.PauseAlertCommand{
AlertId: 1,
Paused: true,
}
postAlertScenario(t, hs, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
setUp()
callPauseAlert(sc)
assert.Equal(t, 403, sc.resp.Code)
})
})
t.Run("When user is editor and dashboard has default ACL", func(t *testing.T) {
hs := setUp()
cmd := dtos.PauseAlertCommand{
AlertId: 1,
Paused: true,
}
postAlertScenario(t, hs, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
setUp(setUpConf{
aclMockResp: []*models.DashboardACLInfoDTO{
{Role: &viewerRole, Permission: models.PERMISSION_VIEW},
{Role: &editorRole, Permission: models.PERMISSION_EDIT},
},
})
callPauseAlert(sc)
assert.Equal(t, 200, sc.resp.Code)
})
})
t.Run("When calling GET", func(t *testing.T) {
hs := setUp()
loggedInUserScenarioWithRole(t, "When calling GET on", "GET",
"/api/alerts?dashboardId=1&dashboardId=2&folderId=3&dashboardTag=abc&dashboardQuery=dbQuery&limit=5&query=alertQuery",
"/api/alerts", models.ROLE_EDITOR, func(sc *scenarioContext) {
hs.SearchService.(*mockSearchService).ExpectedResult = models.HitList{
&models.Hit{ID: 1},
&models.Hit{ID: 2},
}
sc.handlerFunc = hs.GetAlerts
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
getAlertsQuery := hs.SQLStore.(*mockstore.SQLStoreMock).LastGetAlertsQuery
require.NotNil(t, getAlertsQuery)
assert.Equal(t, int64(1), getAlertsQuery.DashboardIDs[0])
assert.Equal(t, int64(2), getAlertsQuery.DashboardIDs[1])
assert.Equal(t, int64(5), getAlertsQuery.Limit)
assert.Equal(t, "alertQuery", getAlertsQuery.Query)
}, hs.SQLStore)
})
t.Run("When calling GET on alert-notifications", func(t *testing.T) {
hs := setUp()
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/alert-notifications/1",
"/alert-notifications/:notificationId", models.ROLE_ADMIN, func(sc *scenarioContext) {
sc.handlerFunc = hs.GetAlertNotificationByID
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
assert.Equal(t, 404, sc.resp.Code)
}, hs.SQLStore)
})
}
func callPauseAlert(sc *scenarioContext) {
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
}
func postAlertScenario(t *testing.T, hs *HTTPServer, desc string, url string, routePattern string, role models.RoleType,
cmd dtos.PauseAlertCommand, fn scenarioFunc) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
sc := setupScenarioContext(t, url)
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
c.Req.Body = mockRequestBody(cmd)
c.Req.Header.Add("Content-Type", "application/json")
sc.context = c
sc.context.UserId = testUserID
sc.context.OrgId = testOrgID
sc.context.OrgRole = role
legacyAlertingEnabled := new(bool)
*legacyAlertingEnabled = true
return hs.PauseAlert(legacyAlertingEnabled)(c)
})
sc.m.Post(routePattern, sc.defaultHandler)
fn(sc)
})
}
+43
View File
@@ -11,6 +11,7 @@ import (
"path/filepath"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/response"
@@ -33,6 +34,7 @@ import (
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/service"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/ldap"
"github.com/grafana/grafana/pkg/services/licensing"
"github.com/grafana/grafana/pkg/services/login/loginservice"
@@ -40,9 +42,11 @@ import (
"github.com/grafana/grafana/pkg/services/preference/preftest"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/searchusers"
"github.com/grafana/grafana/pkg/services/searchusers/filters"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
@@ -468,3 +472,42 @@ func SetupAPITestServer(t *testing.T, opts ...APITestServerOption) *webtest.Serv
s := webtest.NewServer(t, hs.RouteRegister)
return s
}
var (
viewerRole = models.ROLE_VIEWER
editorRole = models.ROLE_EDITOR
)
type setUpConf struct {
aclMockResp []*models.DashboardACLInfoDTO
}
type mockSearchService struct{ ExpectedResult models.HitList }
func (mss *mockSearchService) SearchHandler(_ context.Context, q *search.Query) error {
q.Result = mss.ExpectedResult
return nil
}
func (mss *mockSearchService) SortOptions() []models.SortOption { return nil }
func setUp(confs ...setUpConf) *HTTPServer {
singleAlert := &models.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
store := mockstore.NewSQLStoreMock()
hs := &HTTPServer{SQLStore: store, SearchService: &mockSearchService{}}
store.ExpectedAlert = singleAlert
aclMockResp := []*models.DashboardACLInfoDTO{}
for _, c := range confs {
if c.aclMockResp != nil {
aclMockResp = c.aclMockResp
}
}
store.ExpectedTeamsByUser = []*models.TeamDTO{}
dashSvc := &dashboards.FakeDashboardService{}
dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Run(func(args mock.Arguments) {
q := args.Get(1).(*models.GetDashboardACLInfoListQuery)
q.Result = aclMockResp
}).Return(nil)
guardian.InitLegacyGuardian(store, dashSvc)
return hs
}