Chore: Convert background service registry to dskit module (#64062)

* Chore: Add initial support for deployment modes

* revert CLI changes and start modules independently

* add modules to codeowners

* additional comments

* add Engine and Manager interface to fix test issues

* convert background service registry to dskit module

* remove extra context from serviceListener logger

Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>

* Remove whitespace

* fix import

* undo ide changes

* only register All by default

* with registry

* add test

* add comments

* re-add debug log

* fix import

* reorganize arg

* undo kind changes

* add provide service test

* fix import

* rejig systemd calls

* update codeowners

---------

Co-authored-by: Todd Treece <todd.treece@grafana.com>
Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
This commit is contained in:
Will Browne
2023-07-06 14:45:47 +02:00
committed by GitHub
parent 76974009d0
commit 4818568c65
15 changed files with 287 additions and 190 deletions

View File

@@ -3,52 +3,19 @@ package server
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/server/backgroundsvcs"
"github.com/grafana/grafana/pkg/modules"
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
"github.com/grafana/grafana/pkg/setting"
)
type testService struct {
started chan struct{}
runErr error
isDisabled bool
}
func newTestService(runErr error, disabled bool) *testService {
return &testService{
started: make(chan struct{}),
runErr: runErr,
isDisabled: disabled,
}
}
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
}
close(s.started)
<-ctx.Done()
return ctx.Err()
}
func (s *testService) IsDisabled() bool {
return s.isDisabled
}
func testServer(t *testing.T, services ...registry.BackgroundService) *Server {
func testServer(t *testing.T, m *modules.MockModuleEngine) *Server {
t.Helper()
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...), &MockModuleService{})
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, m)
require.NoError(t, err)
// Required to skip configuration initialization that causes
// DI errors in this test.
@@ -58,62 +25,68 @@ func testServer(t *testing.T, services ...registry.BackgroundService) *Server {
func TestServer_Run_Error(t *testing.T) {
testErr := errors.New("boom")
s := testServer(t, newTestService(nil, false), newTestService(testErr, false))
err := s.Run()
require.ErrorIs(t, err, testErr)
t.Run("Modules Run error bubbles up", func(t *testing.T) {
ctx := context.Background()
s := testServer(t, &modules.MockModuleEngine{
RunFunc: func(c context.Context) error {
require.Equal(t, ctx, c)
return testErr
},
})
err := s.Run(ctx)
require.ErrorIs(t, err, testErr)
})
}
func TestServer_Shutdown(t *testing.T) {
ctx := context.Background()
s := testServer(t, newTestService(nil, false), newTestService(nil, true))
modulesShutdown := false
s := testServer(t, &modules.MockModuleEngine{
ShutdownFunc: func(_ context.Context) error {
modulesShutdown = true
return nil
},
})
ch := make(chan error)
go func() {
defer close(ch)
// Wait until all services launched.
for _, svc := range s.backgroundServices {
if !svc.(*testService).isDisabled {
<-svc.(*testService).started
}
}
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
err := s.Shutdown(ctx, "test interrupt")
ch <- err
}()
err := s.Run()
err := s.Run(ctx)
require.NoError(t, err)
err = <-ch
require.NoError(t, err)
}
require.True(t, modulesShutdown)
type MockModuleService struct {
initFunc func(context.Context) error
runFunc func(context.Context) error
shutdownFunc func(context.Context) error
}
t.Run("Modules Shutdown error bubbles up", func(t *testing.T) {
testErr := errors.New("boom")
func (m *MockModuleService) Init(ctx context.Context) error {
if m.initFunc != nil {
return m.initFunc(ctx)
}
return nil
}
s = testServer(t, &modules.MockModuleEngine{
ShutdownFunc: func(_ context.Context) error {
return testErr
},
})
func (m *MockModuleService) Run(ctx context.Context) error {
if m.runFunc != nil {
return m.runFunc(ctx)
}
return nil
}
ch = make(chan error)
go func() {
defer close(ch)
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
err = s.Shutdown(ctx, "test interrupt")
ch <- err
}()
err = s.Run(ctx)
require.NoError(t, err)
func (m *MockModuleService) Shutdown(ctx context.Context) error {
if m.shutdownFunc != nil {
return m.shutdownFunc(ctx)
}
return nil
err = <-ch
require.ErrorIs(t, err, testErr)
})
}