mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 05:08:36 +08: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:
@ -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)
|
||||
|
Reference in New Issue
Block a user