diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-17 17:54:21 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-01-06 10:28:38 +0300 |
commit | eaa6c1847a77a2d9ed993c84ecf4042bfda75f01 (patch) | |
tree | 2c0199cbb3a3fb4c7dc9ef9cb8b9c02ecec8d89f | |
parent | f0df4548be7783523b18feec17ffe238ef6da98f (diff) |
testhelper: Accept `testing.TB` when starting the server
Currently, `TestServer.Start()` returns an error. Given that it's only
used for tests and never inside of the test's main function, let's
convert it to instead receive a `testing.TB` and use `require`.
18 files changed, 28 insertions, 63 deletions
diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 8ae9b9454..0b415273f 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -644,7 +644,7 @@ func runHookServiceServerWithAPI(t *testing.T, gitlabAPI gitalyhook.GitlabAPI) f gitalypb.RegisterHookServiceServer(server.GrpcServer(), hook.NewServer(config.Config, gitalyhook.NewManager(config.NewLocator(config.Config), gitlabAPI, config.Config))) reflection.Register(server.GrpcServer()) - require.NoError(t, server.Start()) + server.Start(t) return server.Stop } diff --git a/internal/gitaly/service/blob/testhelper_test.go b/internal/gitaly/service/blob/testhelper_test.go index 6702407d5..4452c4b9d 100644 --- a/internal/gitaly/service/blob/testhelper_test.go +++ b/internal/gitaly/service/blob/testhelper_test.go @@ -5,7 +5,6 @@ import ( "testing" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/storage" @@ -46,7 +45,7 @@ func runBlobServer(t *testing.T, locator storage.Locator) (func(), string) { gitalypb.RegisterBlobServiceServer(srv.GrpcServer(), NewServer(rubyServer, locator)) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return srv.Stop, "unix://" + srv.Socket() } diff --git a/internal/gitaly/service/cleanup/testhelper_test.go b/internal/gitaly/service/cleanup/testhelper_test.go index 45125dddd..ed9b2318d 100644 --- a/internal/gitaly/service/cleanup/testhelper_test.go +++ b/internal/gitaly/service/cleanup/testhelper_test.go @@ -4,7 +4,6 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" hookservice "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/hook" @@ -34,7 +33,7 @@ func runCleanupServiceServer(t *testing.T, cfg config.Cfg) (string, func()) { gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(cfg, hook.NewManager(locator, hook.GitlabAPIStub, cfg))) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/conflicts/testhelper_test.go b/internal/gitaly/service/conflicts/testhelper_test.go index ac56960a6..00c72dba6 100644 --- a/internal/gitaly/service/conflicts/testhelper_test.go +++ b/internal/gitaly/service/conflicts/testhelper_test.go @@ -6,7 +6,6 @@ import ( "testing" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" @@ -54,8 +53,7 @@ func runConflictsServer(t *testing.T) (string, func()) { gitalypb.RegisterConflictsServiceServer(srv.GrpcServer(), NewServer(RubyServer, config.Config, locator)) reflection.Register(srv.GrpcServer()) - - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/hook/testhelper_test.go b/internal/gitaly/service/hook/testhelper_test.go index 7cc59858e..7178de876 100644 --- a/internal/gitaly/service/hook/testhelper_test.go +++ b/internal/gitaly/service/hook/testhelper_test.go @@ -4,7 +4,6 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" gitalyhook "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" @@ -48,7 +47,7 @@ func runHooksServerWithAPI(t *testing.T, gitlabAPI gitalyhook.GitlabAPI, cfg con gitalypb.RegisterHookServiceServer(srv.GrpcServer(), NewServer(cfg, gitalyhook.NewManager(config.NewLocator(cfg), gitlabAPI, cfg))) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/operations/branches_test.go b/internal/gitaly/service/operations/branches_test.go index c29f2a4c5..21e8ed80b 100644 --- a/internal/gitaly/service/operations/branches_test.go +++ b/internal/gitaly/service/operations/branches_test.go @@ -146,7 +146,7 @@ func TestUserCreateBranchWithTransaction(t *testing.T) { gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hook.NewServer(config.Config, hookManager)) gitalypb.RegisterRefTransactionServer(srv.GrpcServer(), transactionServer) - require.NoError(t, srv.Start()) + srv.Start(t) defer srv.Stop() go srv.GrpcServer().Serve(internalListener) diff --git a/internal/gitaly/service/operations/testhelper_test.go b/internal/gitaly/service/operations/testhelper_test.go index 2161cdaf1..79e35cc5a 100644 --- a/internal/gitaly/service/operations/testhelper_test.go +++ b/internal/gitaly/service/operations/testhelper_test.go @@ -6,7 +6,6 @@ import ( "testing" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/client" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" @@ -97,7 +96,7 @@ func runOperationServiceServerWithRubyServer(t *testing.T, ruby *rubyserver.Serv gitalypb.RegisterSSHServiceServer(srv.GrpcServer(), ssh.NewServer(locator)) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/operations/update_with_hooks_test.go b/internal/gitaly/service/operations/update_with_hooks_test.go index 1eb91dcb5..39d532372 100644 --- a/internal/gitaly/service/operations/update_with_hooks_test.go +++ b/internal/gitaly/service/operations/update_with_hooks_test.go @@ -114,7 +114,7 @@ func TestUpdateReferenceWithHooks(t *testing.T) { // We need to set up a separate "real" hook service here, as it will be used in // git-update-ref(1) spawned by `updateRefWithHooks()` gitalypb.RegisterHookServiceServer(server.GrpcServer(), hookservice.NewServer(config.Config, hook.NewManager(config.NewLocator(config.Config), hook.GitlabAPIStub, config.Config))) - require.NoError(t, server.Start()) + server.Start(t) user := &gitalypb.User{ GlId: "1234", diff --git a/internal/gitaly/service/ref/testhelper_test.go b/internal/gitaly/service/ref/testhelper_test.go index 4b74da2fa..f12cb80ef 100644 --- a/internal/gitaly/service/ref/testhelper_test.go +++ b/internal/gitaly/service/ref/testhelper_test.go @@ -5,7 +5,6 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" hookservice "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/hook" @@ -49,7 +48,7 @@ func runRefServiceServer(t *testing.T) (func(), string) { gitalypb.RegisterRefServiceServer(srv.GrpcServer(), NewServer(locator)) gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hook.NewManager(locator, hook.GitlabAPIStub, config.Config))) - require.NoError(t, srv.Start()) + srv.Start(t) return srv.Stop, "unix://" + srv.Socket() } diff --git a/internal/gitaly/service/remote/fetch_internal_remote_test.go b/internal/gitaly/service/remote/fetch_internal_remote_test.go index 274047d27..81f53cf4f 100644 --- a/internal/gitaly/service/remote/fetch_internal_remote_test.go +++ b/internal/gitaly/service/remote/fetch_internal_remote_test.go @@ -51,7 +51,7 @@ func TestSuccessfulFetchInternalRemote(t *testing.T) { gitalypb.RegisterSSHServiceServer(gitaly0Server.GrpcServer(), ssh.NewServer(locator)) gitalypb.RegisterRefServiceServer(gitaly0Server.GrpcServer(), ref.NewServer(config.NewLocator(config.Config))) reflection.Register(gitaly0Server.GrpcServer()) - require.NoError(t, gitaly0Server.Start()) + gitaly0Server.Start(t) defer gitaly0Server.Stop() gitaly1Socket, cleanup := remote.RunRemoteServiceServer(t, testhelper.WithStorages([]string{"gitaly-1"})) diff --git a/internal/gitaly/service/remote/testhelper_test.go b/internal/gitaly/service/remote/testhelper_test.go index c269a713b..44cd2bdaa 100644 --- a/internal/gitaly/service/remote/testhelper_test.go +++ b/internal/gitaly/service/remote/testhelper_test.go @@ -5,7 +5,6 @@ import ( "testing" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/testhelper" @@ -42,7 +41,7 @@ func RunRemoteServiceServer(t *testing.T, opts ...testhelper.TestServerOpt) (str gitalypb.RegisterRemoteServiceServer(srv.GrpcServer(), NewServer(RubyServer, config.NewLocator(config.Config))) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/repository/fetch_test.go b/internal/gitaly/service/repository/fetch_test.go index e7176aa03..54ad52713 100644 --- a/internal/gitaly/service/repository/fetch_test.go +++ b/internal/gitaly/service/repository/fetch_test.go @@ -424,7 +424,7 @@ func runFullSecureServer(t *testing.T, locator storage.Locator) (*grpc.Server, s // protected by the same TLS certificate. internalServer := testhelper.NewServer(t, nil, nil, testhelper.WithInternalSocket(config.Config)) gitalypb.RegisterHookServiceServer(internalServer.GrpcServer(), hookservice.NewServer(config.Config, hookManager)) - require.NoError(t, internalServer.Start()) + internalServer.Start(t) go func() { errQ <- server.Serve(listener) }() diff --git a/internal/gitaly/service/repository/testhelper_test.go b/internal/gitaly/service/repository/testhelper_test.go index 3fa641313..9e6d4def4 100644 --- a/internal/gitaly/service/repository/testhelper_test.go +++ b/internal/gitaly/service/repository/testhelper_test.go @@ -9,7 +9,6 @@ import ( log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/client" dcache "gitlab.com/gitlab-org/gitaly/internal/cache" @@ -111,7 +110,7 @@ func runRepoServerWithConfig(t *testing.T, cfg config.Cfg, locator storage.Locat gitalypb.RegisterRepositoryServiceServer(srv.GrpcServer(), NewServer(cfg, RubyServer, locator)) gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hook.NewManager(locator, hook.GitlabAPIStub, cfg))) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/smarthttp/receive_pack_test.go b/internal/gitaly/service/smarthttp/receive_pack_test.go index 740749114..3cc18346f 100644 --- a/internal/gitaly/service/smarthttp/receive_pack_test.go +++ b/internal/gitaly/service/smarthttp/receive_pack_test.go @@ -509,7 +509,7 @@ func testPostReceiveWithTransactionsViaPraefect(t *testing.T, ctx context.Contex gitalypb.RegisterSmartHTTPServiceServer(gitalyServer.GrpcServer(), NewServer(locator)) gitalypb.RegisterHookServiceServer(gitalyServer.GrpcServer(), hook.NewServer(config.Config, gitalyhook.NewManager(locator, gitalyhook.GitlabAPIStub, config.Config))) reflection.Register(gitalyServer.GrpcServer()) - require.NoError(t, gitalyServer.Start()) + gitalyServer.Start(t) defer gitalyServer.Stop() internalSocket := config.Config.GitalyInternalSocketPath() diff --git a/internal/gitaly/service/smarthttp/testhelper_test.go b/internal/gitaly/service/smarthttp/testhelper_test.go index 9aadd9104..1c1d76f19 100644 --- a/internal/gitaly/service/smarthttp/testhelper_test.go +++ b/internal/gitaly/service/smarthttp/testhelper_test.go @@ -6,7 +6,6 @@ import ( "testing" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" diskcache "gitlab.com/gitlab-org/gitaly/internal/cache" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" @@ -61,8 +60,7 @@ func runSmartHTTPServer(t *testing.T, serverOpts ...ServerOpt) (string, func()) locator := config.NewLocator(config.Config) gitalypb.RegisterSmartHTTPServiceServer(srv.GrpcServer(), NewServer(locator, serverOpts...)) gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hook.NewManager(locator, hook.GitlabAPIStub, config.Config))) - - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/ssh/testhelper_test.go b/internal/gitaly/service/ssh/testhelper_test.go index cc3299a2f..8d2097ecd 100644 --- a/internal/gitaly/service/ssh/testhelper_test.go +++ b/internal/gitaly/service/ssh/testhelper_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "testing" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" hookservice "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/hook" @@ -46,7 +45,7 @@ func runSSHServer(t *testing.T, serverOpts ...ServerOpt) (string, func()) { gitalypb.RegisterSSHServiceServer(srv.GrpcServer(), NewServer(locator, serverOpts...)) gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hook.NewManager(locator, hook.GitlabAPIStub, config.Config))) - require.NoError(t, srv.Start()) + srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } diff --git a/internal/gitaly/service/wiki/testhelper_test.go b/internal/gitaly/service/wiki/testhelper_test.go index d469e2674..f3ca45ad9 100644 --- a/internal/gitaly/service/wiki/testhelper_test.go +++ b/internal/gitaly/service/wiki/testhelper_test.go @@ -68,7 +68,7 @@ func runWikiServiceServer(t *testing.T, locator storage.Locator) (func(), string gitalypb.RegisterWikiServiceServer(srv.GrpcServer(), NewServer(rubyServer, locator)) reflection.Register(srv.GrpcServer()) - require.NoError(t, srv.Start()) + srv.Start(t) return srv.Stop, "unix://" + srv.Socket() } diff --git a/internal/testhelper/testserver.go b/internal/testhelper/testserver.go index 163cd7e9a..5bc52cfa4 100644 --- a/internal/testhelper/testserver.go +++ b/internal/testhelper/testserver.go @@ -132,31 +132,24 @@ func (p *TestServer) Socket() string { } // Start will start the grpc server as well as spawn a praefect instance if GITALY_TEST_PRAEFECT_BIN is enabled -func (p *TestServer) Start() error { +func (p *TestServer) Start(t testing.TB) { praefectBinPath, ok := os.LookupEnv("GITALY_TEST_PRAEFECT_BIN") if !ok { gitalyServerSocketPath, err := p.listen() - if err != nil { - return err - } - + require.NoError(t, err) p.socket = gitalyServerSocketPath - return nil + return } tempDir, err := ioutil.TempDir("", "praefect-test-server") - if err != nil { - return err - } + require.NoError(t, err) defer os.RemoveAll(tempDir) praefectServerSocketPath := GetTemporaryGitalySocketFileName() configFilePath := filepath.Join(tempDir, "config.toml") configFile, err := os.Create(configFilePath) - if err != nil { - return err - } + require.NoError(t, err) defer configFile.Close() c := praefectconfig.Config{ @@ -176,9 +169,7 @@ func (p *TestServer) Start() error { for _, storage := range p.storages { gitalyServerSocketPath, err := p.listen() - if err != nil { - return err - } + require.NoError(t, err) c.VirtualStorages = append(c.VirtualStorages, &praefectconfig.VirtualStorage{ Name: storage, @@ -192,13 +183,8 @@ func (p *TestServer) Start() error { }) } - if err := toml.NewEncoder(configFile).Encode(&c); err != nil { - return err - } - if err = configFile.Sync(); err != nil { - return err - } - configFile.Close() + require.NoError(t, toml.NewEncoder(configFile).Encode(&c)) + require.NoError(t, configFile.Close()) cmd := exec.Command(praefectBinPath, "-config", configFilePath) cmd.Stderr = os.Stderr @@ -206,9 +192,7 @@ func (p *TestServer) Start() error { p.socket = praefectServerSocketPath - if err := cmd.Start(); err != nil { - return err - } + require.NoError(t, cmd.Start()) p.waitCh = make(chan struct{}) go func() { @@ -222,19 +206,12 @@ func (p *TestServer) Start() error { } conn, err := grpc.Dial("unix://"+praefectServerSocketPath, opts...) - - if err != nil { - return fmt.Errorf("dial to praefect: %v", err) - } + require.NoError(t, err) defer conn.Close() - if err = WaitHealthy(conn, 3, time.Second); err != nil { - return err - } + require.NoError(t, WaitHealthy(conn, 3, time.Second)) p.process = cmd.Process - - return nil } func (p *TestServer) listen() (string, error) { |