2021-04-07 02:44:06 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2021-04-30 03:55:59 -05:00
|
|
|
"time"
|
2021-04-07 02:44:06 -05:00
|
|
|
|
2023-01-30 02:26:42 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-07-06 07:45:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/modules"
|
2022-09-05 11:15:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
|
2021-08-25 08:11:22 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-04-07 02:44:06 -05:00
|
|
|
)
|
|
|
|
|
2023-07-06 07:45:47 -05:00
|
|
|
func testServer(t *testing.T, m *modules.MockModuleEngine) *Server {
|
2021-08-25 08:11:22 -05:00
|
|
|
t.Helper()
|
2023-07-18 14:37:25 -05:00
|
|
|
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, m)
|
2021-08-25 08:11:22 -05:00
|
|
|
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")
|
2023-07-06 07:45:47 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2021-04-07 02:44:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_Shutdown(t *testing.T) {
|
2021-04-30 03:55:59 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-07-06 07:45:47 -05:00
|
|
|
modulesShutdown := false
|
|
|
|
s := testServer(t, &modules.MockModuleEngine{
|
|
|
|
ShutdownFunc: func(_ context.Context) error {
|
|
|
|
modulesShutdown = true
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
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)
|
|
|
|
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
|
|
|
}()
|
2023-07-06 07:45:47 -05:00
|
|
|
err := s.Run(ctx)
|
2021-04-07 02:44:06 -05:00
|
|
|
require.NoError(t, err)
|
2021-04-30 03:55:59 -05:00
|
|
|
|
|
|
|
err = <-ch
|
|
|
|
require.NoError(t, err)
|
2023-07-06 07:45:47 -05:00
|
|
|
require.True(t, modulesShutdown)
|
|
|
|
|
|
|
|
t.Run("Modules Shutdown error bubbles up", func(t *testing.T) {
|
|
|
|
testErr := errors.New("boom")
|
|
|
|
|
|
|
|
s = testServer(t, &modules.MockModuleEngine{
|
|
|
|
ShutdownFunc: func(_ context.Context) error {
|
|
|
|
return testErr
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
err = <-ch
|
|
|
|
require.ErrorIs(t, err, testErr)
|
|
|
|
})
|
2023-03-06 13:06:52 -06:00
|
|
|
}
|