mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Migrate to Wire for dependency injection (#32289)
Fixes #30144 Co-authored-by: dsotirakis <sotirakis.dim@gmail.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com> Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com> Co-authored-by: Will Browne <wbrowne@users.noreply.github.com> Co-authored-by: Leon Sorokin <leeoniya@gmail.com> Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com> Co-authored-by: spinillos <selenepinillos@gmail.com> Co-authored-by: Karl Persson <kalle.persson@grafana.com> Co-authored-by: Leonard Gram <leo@xlson.com>
This commit is contained in:
@@ -3,45 +3,36 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
|
||||
"github.com/grafana/grafana/pkg/server/backgroundsvcs"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type testServiceRegistry struct {
|
||||
services []*registry.Descriptor
|
||||
}
|
||||
|
||||
func (r *testServiceRegistry) GetServices() []*registry.Descriptor {
|
||||
return r.services
|
||||
}
|
||||
|
||||
func (r *testServiceRegistry) IsDisabled(_ registry.Service) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type testService struct {
|
||||
started chan struct{}
|
||||
initErr error
|
||||
runErr error
|
||||
started chan struct{}
|
||||
runErr error
|
||||
isDisabled bool
|
||||
}
|
||||
|
||||
func newTestService(initErr, runErr error) *testService {
|
||||
func newTestService(runErr error, disabled bool) *testService {
|
||||
return &testService{
|
||||
started: make(chan struct{}),
|
||||
initErr: initErr,
|
||||
runErr: runErr,
|
||||
started: make(chan struct{}),
|
||||
runErr: runErr,
|
||||
isDisabled: disabled,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *testService) Init() error {
|
||||
return s.initErr
|
||||
}
|
||||
|
||||
func (s *testService) Run(ctx context.Context) error {
|
||||
if s.isDisabled {
|
||||
return fmt.Errorf("Shouldn't run disabled service")
|
||||
}
|
||||
|
||||
if s.runErr != nil {
|
||||
return s.runErr
|
||||
}
|
||||
@@ -50,8 +41,14 @@ func (s *testService) Run(ctx context.Context) error {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func testServer() *Server {
|
||||
s := newServer(Config{})
|
||||
func (s *testService) IsDisabled() bool {
|
||||
return s.isDisabled
|
||||
}
|
||||
|
||||
func testServer(t *testing.T, services ...registry.BackgroundService) *Server {
|
||||
t.Helper()
|
||||
s, err := newServer(Options{}, setting.NewCfg(), nil, &ossaccesscontrol.OSSAccessControlService{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...))
|
||||
require.NoError(t, err)
|
||||
// Required to skip configuration initialization that causes
|
||||
// DI errors in this test.
|
||||
s.isInitialized = true
|
||||
@@ -59,25 +56,8 @@ func testServer() *Server {
|
||||
}
|
||||
|
||||
func TestServer_Run_Error(t *testing.T) {
|
||||
s := testServer()
|
||||
|
||||
var testErr = errors.New("boom")
|
||||
|
||||
s.serviceRegistry = &testServiceRegistry{
|
||||
services: []*registry.Descriptor{
|
||||
{
|
||||
Name: "TestService1",
|
||||
Instance: newTestService(nil, nil),
|
||||
InitPriority: registry.High,
|
||||
},
|
||||
{
|
||||
Name: "TestService2",
|
||||
Instance: newTestService(nil, testErr),
|
||||
InitPriority: registry.High,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
testErr := errors.New("boom")
|
||||
s := testServer(t, newTestService(nil, false), newTestService(testErr, false))
|
||||
err := s.Run()
|
||||
require.ErrorIs(t, err, testErr)
|
||||
require.NotZero(t, s.ExitCode(err))
|
||||
@@ -86,22 +66,7 @@ func TestServer_Run_Error(t *testing.T) {
|
||||
func TestServer_Shutdown(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
s := testServer()
|
||||
services := []*registry.Descriptor{
|
||||
{
|
||||
Name: "TestService1",
|
||||
Instance: newTestService(nil, nil),
|
||||
InitPriority: registry.High,
|
||||
},
|
||||
{
|
||||
Name: "TestService2",
|
||||
Instance: newTestService(nil, nil),
|
||||
InitPriority: registry.High,
|
||||
},
|
||||
}
|
||||
s.serviceRegistry = &testServiceRegistry{
|
||||
services: services,
|
||||
}
|
||||
s := testServer(t, newTestService(nil, false), newTestService(nil, true))
|
||||
|
||||
ch := make(chan error)
|
||||
|
||||
@@ -109,8 +74,10 @@ func TestServer_Shutdown(t *testing.T) {
|
||||
defer close(ch)
|
||||
|
||||
// Wait until all services launched.
|
||||
for _, svc := range services {
|
||||
<-svc.Instance.(*testService).started
|
||||
for _, svc := range s.backgroundServices {
|
||||
if !svc.(*testService).isDisabled {
|
||||
<-svc.(*testService).started
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user