mirror of
https://github.com/grafana/grafana.git
synced 2025-01-13 09:32:12 -06:00
Chore: Remove bus from health api (#44897)
* remove bus from health api * fix health api tests * use db health query as a method * use pointer in sqlstore mock
This commit is contained in:
parent
058e3ffc21
commit
61533a3cb4
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
@ -15,7 +14,7 @@ func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
|
||||
return cached.(bool)
|
||||
}
|
||||
|
||||
healthy := bus.Dispatch(ctx, &models.GetDBHealthQuery{}) == nil
|
||||
healthy := hs.SQLStore.GetDBHealthQuery(ctx, &models.GetDBHealthQuery{}) == nil
|
||||
|
||||
hs.CacheService.Set(cacheKey, healthy, time.Second*5)
|
||||
return healthy
|
||||
|
@ -1,16 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -22,10 +20,6 @@ func TestHealthAPI_Version(t *testing.T) {
|
||||
cfg.BuildCommit = "59906ab1bf"
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
m.ServeHTTP(rec, req)
|
||||
@ -45,10 +39,6 @@ func TestHealthAPI_AnonymousHideVersion(t *testing.T) {
|
||||
m, hs := setupHealthAPITestEnvironment(t)
|
||||
hs.Cfg.AnonymousHideVersion = true
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
m.ServeHTTP(rec, req)
|
||||
@ -68,10 +58,6 @@ func TestHealthAPI_DatabaseHealthy(t *testing.T) {
|
||||
m, hs := setupHealthAPITestEnvironment(t)
|
||||
hs.Cfg.AnonymousHideVersion = true
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
healthy, found := hs.CacheService.Get(cacheKey)
|
||||
require.False(t, found)
|
||||
require.Nil(t, healthy)
|
||||
@ -98,10 +84,7 @@ func TestHealthAPI_DatabaseUnhealthy(t *testing.T) {
|
||||
|
||||
m, hs := setupHealthAPITestEnvironment(t)
|
||||
hs.Cfg.AnonymousHideVersion = true
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return errors.New("bad")
|
||||
})
|
||||
hs.SQLStore.(*mockstore.SQLStoreMock).ExpectedError = errors.New("bad")
|
||||
|
||||
healthy, found := hs.CacheService.Get(cacheKey)
|
||||
require.False(t, found)
|
||||
@ -130,11 +113,6 @@ func TestHealthAPI_DatabaseHealthCached(t *testing.T) {
|
||||
m, hs := setupHealthAPITestEnvironment(t)
|
||||
hs.Cfg.AnonymousHideVersion = true
|
||||
|
||||
// Database is healthy.
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
// Mock unhealthy database in cache.
|
||||
hs.CacheService.Set(cacheKey, false, 5*time.Minute)
|
||||
|
||||
@ -171,9 +149,6 @@ func TestHealthAPI_DatabaseHealthCached(t *testing.T) {
|
||||
func setupHealthAPITestEnvironment(t *testing.T, cbs ...func(*setting.Cfg)) (*web.Mux, *HTTPServer) {
|
||||
t.Helper()
|
||||
|
||||
bus.ClearBusHandlers()
|
||||
t.Cleanup(bus.ClearBusHandlers)
|
||||
|
||||
m := web.New()
|
||||
cfg := setting.NewCfg()
|
||||
for _, cb := range cbs {
|
||||
@ -182,6 +157,7 @@ func setupHealthAPITestEnvironment(t *testing.T, cbs ...func(*setting.Cfg)) (*we
|
||||
hs := &HTTPServer{
|
||||
CacheService: localcache.New(5*time.Minute, 10*time.Minute),
|
||||
Cfg: cfg,
|
||||
SQLStore: mockstore.NewSQLStoreMock(),
|
||||
}
|
||||
|
||||
m.Get("/api/health", hs.apiHealthHandler)
|
||||
|
@ -3,17 +3,12 @@ package sqlstore
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDBHealthQuery)
|
||||
}
|
||||
|
||||
// GetDBHealthQuery executes a query to check
|
||||
// the availability of the database.
|
||||
func GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
func (ss *SQLStore) GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return withDbSession(ctx, x, func(session *DBSession) error {
|
||||
_, err := session.Exec("SELECT 1")
|
||||
return err
|
||||
|
@ -12,9 +12,9 @@ import (
|
||||
)
|
||||
|
||||
func TestGetDBHealthQuery(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
store := InitTestDB(t)
|
||||
|
||||
query := models.GetDBHealthQuery{}
|
||||
err := GetDBHealthQuery(context.Background(), &query)
|
||||
err := store.GetDBHealthQuery(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
@ -584,3 +584,7 @@ func (m *SQLStoreMock) GetTempUserByCode(ctx context.Context, query *models.GetT
|
||||
func (m *SQLStoreMock) ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
@ -82,11 +82,11 @@ func ProvideServiceForTests(migrations registry.DatabaseMigrator) (*SQLStore, er
|
||||
return initTestDB(migrations, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
|
||||
}
|
||||
|
||||
func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bus.Bus, engine *xorm.Engine,
|
||||
func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, b bus.Bus, engine *xorm.Engine,
|
||||
migrations registry.DatabaseMigrator, tracer tracing.Tracer, opts ...InitTestDBOpt) (*SQLStore, error) {
|
||||
ss := &SQLStore{
|
||||
Cfg: cfg,
|
||||
Bus: bus,
|
||||
Bus: b,
|
||||
CacheService: cacheService,
|
||||
log: log.New("sqlstore"),
|
||||
skipEnsureDefaultOrgAndUser: false,
|
||||
@ -133,6 +133,8 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bu
|
||||
ss.addDashboardProvisioningQueryAndCommandHandlers()
|
||||
ss.addOrgQueryAndCommandHandlers()
|
||||
|
||||
bus.AddHandler("sql", ss.GetDBHealthQuery)
|
||||
|
||||
// if err := ss.Reset(); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
@ -147,4 +147,5 @@ type Store interface {
|
||||
GetTempUsersQuery(ctx context.Context, query *models.GetTempUsersQuery) error
|
||||
GetTempUserByCode(ctx context.Context, query *models.GetTempUserByCodeQuery) error
|
||||
ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) error
|
||||
GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user