mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -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"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,7 +14,7 @@ func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
|
|||||||
return cached.(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)
|
hs.CacheService.Set(cacheKey, healthy, time.Second*5)
|
||||||
return healthy
|
return healthy
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
"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/setting"
|
||||||
"github.com/grafana/grafana/pkg/web"
|
"github.com/grafana/grafana/pkg/web"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -22,10 +20,6 @@ func TestHealthAPI_Version(t *testing.T) {
|
|||||||
cfg.BuildCommit = "59906ab1bf"
|
cfg.BuildCommit = "59906ab1bf"
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
m.ServeHTTP(rec, req)
|
m.ServeHTTP(rec, req)
|
||||||
@ -45,10 +39,6 @@ func TestHealthAPI_AnonymousHideVersion(t *testing.T) {
|
|||||||
m, hs := setupHealthAPITestEnvironment(t)
|
m, hs := setupHealthAPITestEnvironment(t)
|
||||||
hs.Cfg.AnonymousHideVersion = true
|
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)
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
m.ServeHTTP(rec, req)
|
m.ServeHTTP(rec, req)
|
||||||
@ -68,10 +58,6 @@ func TestHealthAPI_DatabaseHealthy(t *testing.T) {
|
|||||||
m, hs := setupHealthAPITestEnvironment(t)
|
m, hs := setupHealthAPITestEnvironment(t)
|
||||||
hs.Cfg.AnonymousHideVersion = true
|
hs.Cfg.AnonymousHideVersion = true
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
healthy, found := hs.CacheService.Get(cacheKey)
|
healthy, found := hs.CacheService.Get(cacheKey)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
require.Nil(t, healthy)
|
require.Nil(t, healthy)
|
||||||
@ -98,10 +84,7 @@ func TestHealthAPI_DatabaseUnhealthy(t *testing.T) {
|
|||||||
|
|
||||||
m, hs := setupHealthAPITestEnvironment(t)
|
m, hs := setupHealthAPITestEnvironment(t)
|
||||||
hs.Cfg.AnonymousHideVersion = true
|
hs.Cfg.AnonymousHideVersion = true
|
||||||
|
hs.SQLStore.(*mockstore.SQLStoreMock).ExpectedError = errors.New("bad")
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDBHealthQuery) error {
|
|
||||||
return errors.New("bad")
|
|
||||||
})
|
|
||||||
|
|
||||||
healthy, found := hs.CacheService.Get(cacheKey)
|
healthy, found := hs.CacheService.Get(cacheKey)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
@ -130,11 +113,6 @@ func TestHealthAPI_DatabaseHealthCached(t *testing.T) {
|
|||||||
m, hs := setupHealthAPITestEnvironment(t)
|
m, hs := setupHealthAPITestEnvironment(t)
|
||||||
hs.Cfg.AnonymousHideVersion = true
|
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.
|
// Mock unhealthy database in cache.
|
||||||
hs.CacheService.Set(cacheKey, false, 5*time.Minute)
|
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) {
|
func setupHealthAPITestEnvironment(t *testing.T, cbs ...func(*setting.Cfg)) (*web.Mux, *HTTPServer) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
bus.ClearBusHandlers()
|
|
||||||
t.Cleanup(bus.ClearBusHandlers)
|
|
||||||
|
|
||||||
m := web.New()
|
m := web.New()
|
||||||
cfg := setting.NewCfg()
|
cfg := setting.NewCfg()
|
||||||
for _, cb := range cbs {
|
for _, cb := range cbs {
|
||||||
@ -182,6 +157,7 @@ func setupHealthAPITestEnvironment(t *testing.T, cbs ...func(*setting.Cfg)) (*we
|
|||||||
hs := &HTTPServer{
|
hs := &HTTPServer{
|
||||||
CacheService: localcache.New(5*time.Minute, 10*time.Minute),
|
CacheService: localcache.New(5*time.Minute, 10*time.Minute),
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
|
SQLStore: mockstore.NewSQLStoreMock(),
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Get("/api/health", hs.apiHealthHandler)
|
m.Get("/api/health", hs.apiHealthHandler)
|
||||||
|
@ -3,17 +3,12 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
bus.AddHandler("sql", GetDBHealthQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDBHealthQuery executes a query to check
|
// GetDBHealthQuery executes a query to check
|
||||||
// the availability of the database.
|
// 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 {
|
return withDbSession(ctx, x, func(session *DBSession) error {
|
||||||
_, err := session.Exec("SELECT 1")
|
_, err := session.Exec("SELECT 1")
|
||||||
return err
|
return err
|
||||||
|
@ -12,9 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGetDBHealthQuery(t *testing.T) {
|
func TestGetDBHealthQuery(t *testing.T) {
|
||||||
InitTestDB(t)
|
store := InitTestDB(t)
|
||||||
|
|
||||||
query := models.GetDBHealthQuery{}
|
query := models.GetDBHealthQuery{}
|
||||||
err := GetDBHealthQuery(context.Background(), &query)
|
err := store.GetDBHealthQuery(context.Background(), &query)
|
||||||
require.NoError(t, err)
|
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 {
|
func (m *SQLStoreMock) ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) error {
|
||||||
return m.ExpectedError
|
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})
|
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) {
|
migrations registry.DatabaseMigrator, tracer tracing.Tracer, opts ...InitTestDBOpt) (*SQLStore, error) {
|
||||||
ss := &SQLStore{
|
ss := &SQLStore{
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
Bus: bus,
|
Bus: b,
|
||||||
CacheService: cacheService,
|
CacheService: cacheService,
|
||||||
log: log.New("sqlstore"),
|
log: log.New("sqlstore"),
|
||||||
skipEnsureDefaultOrgAndUser: false,
|
skipEnsureDefaultOrgAndUser: false,
|
||||||
@ -133,6 +133,8 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bu
|
|||||||
ss.addDashboardProvisioningQueryAndCommandHandlers()
|
ss.addDashboardProvisioningQueryAndCommandHandlers()
|
||||||
ss.addOrgQueryAndCommandHandlers()
|
ss.addOrgQueryAndCommandHandlers()
|
||||||
|
|
||||||
|
bus.AddHandler("sql", ss.GetDBHealthQuery)
|
||||||
|
|
||||||
// if err := ss.Reset(); err != nil {
|
// if err := ss.Reset(); err != nil {
|
||||||
// return nil, err
|
// return nil, err
|
||||||
// }
|
// }
|
||||||
|
@ -147,4 +147,5 @@ type Store interface {
|
|||||||
GetTempUsersQuery(ctx context.Context, query *models.GetTempUsersQuery) error
|
GetTempUsersQuery(ctx context.Context, query *models.GetTempUsersQuery) error
|
||||||
GetTempUserByCode(ctx context.Context, query *models.GetTempUserByCodeQuery) error
|
GetTempUserByCode(ctx context.Context, query *models.GetTempUserByCodeQuery) error
|
||||||
ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) 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