2021-04-07 02:44:06 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-08-25 08:11:22 -05:00
|
|
|
"fmt"
|
2021-04-07 02:44:06 -05:00
|
|
|
"testing"
|
2021-04-30 03:55:59 -05:00
|
|
|
"time"
|
2021-04-07 02:44:06 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/server/backgroundsvcs"
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-04-07 02:44:06 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testService struct {
|
2021-08-25 08:11:22 -05:00
|
|
|
started chan struct{}
|
|
|
|
runErr error
|
|
|
|
isDisabled bool
|
2021-04-07 02:44:06 -05:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
func newTestService(runErr error, disabled bool) *testService {
|
2021-04-07 02:44:06 -05:00
|
|
|
return &testService{
|
2021-08-25 08:11:22 -05:00
|
|
|
started: make(chan struct{}),
|
|
|
|
runErr: runErr,
|
|
|
|
isDisabled: disabled,
|
2021-04-07 02:44:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testService) Run(ctx context.Context) error {
|
2021-08-25 08:11:22 -05:00
|
|
|
if s.isDisabled {
|
|
|
|
return fmt.Errorf("Shouldn't run disabled service")
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
if s.runErr != nil {
|
|
|
|
return s.runErr
|
|
|
|
}
|
|
|
|
close(s.started)
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
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)
|
2021-04-07 02:44:06 -05:00
|
|
|
// Required to skip configuration initialization that causes
|
|
|
|
// DI errors in this test.
|
|
|
|
s.isInitialized = true
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_Run_Error(t *testing.T) {
|
2021-08-25 08:11:22 -05:00
|
|
|
testErr := errors.New("boom")
|
|
|
|
s := testServer(t, newTestService(nil, false), newTestService(testErr, false))
|
2021-04-07 02:44:06 -05:00
|
|
|
err := s.Run()
|
|
|
|
require.ErrorIs(t, err, testErr)
|
|
|
|
require.NotZero(t, s.ExitCode(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_Shutdown(t *testing.T) {
|
2021-04-30 03:55:59 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
s := testServer(t, newTestService(nil, false), newTestService(nil, true))
|
2021-04-07 02:44:06 -05:00
|
|
|
|
2021-04-30 03:55:59 -05:00
|
|
|
ch := make(chan error)
|
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
go func() {
|
2021-04-30 03:55:59 -05:00
|
|
|
defer close(ch)
|
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
// Wait until all services launched.
|
2021-08-25 08:11:22 -05:00
|
|
|
for _, svc := range s.backgroundServices {
|
|
|
|
if !svc.(*testService).isDisabled {
|
|
|
|
<-svc.(*testService).started
|
|
|
|
}
|
2021-04-07 02:44:06 -05:00
|
|
|
}
|
2021-04-30 03:55:59 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := s.Shutdown(ctx, "test interrupt")
|
|
|
|
ch <- err
|
2021-04-07 02:44:06 -05:00
|
|
|
}()
|
|
|
|
err := s.Run()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, s.ExitCode(err))
|
2021-04-30 03:55:59 -05:00
|
|
|
|
|
|
|
err = <-ch
|
|
|
|
require.NoError(t, err)
|
2021-04-07 02:44:06 -05:00
|
|
|
}
|