diff options
-rw-r--r-- | cmd/gitaly-hooks/hooks_test.go | 45 | ||||
-rw-r--r-- | internal/gitaly/service/repository/apply_gitattributes_test.go | 36 | ||||
-rw-r--r-- | internal/gitaly/service/repository/fetch_remote_test.go | 16 | ||||
-rw-r--r-- | internal/gitaly/transaction/manager_test.go | 39 |
4 files changed, 55 insertions, 81 deletions
diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 27979cad5..a25837a6a 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/git/gittest" @@ -24,17 +23,17 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/auth" internallog "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/log" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/prometheus" - gitalyhook "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/hook" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/internal/gitlab" gitalylog "gitlab.com/gitlab-org/gitaly/internal/log" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" - "google.golang.org/grpc/reflection" + "google.golang.org/grpc" ) type glHookValues struct { @@ -176,8 +175,7 @@ func testHooksPrePostReceive(t *testing.T, cfg config.Cfg, repo *gitalypb.Reposi gitlabClient, err := gitlab.NewHTTPClient(cfg.Gitlab, cfg.TLS, prometheus.Config{}) require.NoError(t, err) - stop := runHookServiceWithGitlabClient(t, cfg, gitlabClient) - defer stop() + runHookServiceWithGitlabClient(t, cfg, gitlabClient) var stderr, stdout bytes.Buffer stdin := bytes.NewBuffer([]byte(changes)) @@ -262,8 +260,7 @@ func TestHooksUpdate(t *testing.T) { cfg.Gitlab.SecretFile = testhelper.WriteShellSecretFile(t, cfg.GitlabShell.Dir, "the wrong token") - stop := runHookServiceServer(t, cfg) - defer stop() + runHookServiceServer(t, cfg) testHooksUpdate(t, cfg, glHookValues{ GLID: glID, @@ -397,8 +394,7 @@ func TestHooksPostReceiveFailed(t *testing.T) { for _, tc := range testcases { t.Run(tc.desc, func(t *testing.T) { - stop := runHookServiceWithGitlabClient(t, cfg, gitlabClient) - defer stop() + runHookServiceWithGitlabClient(t, cfg, gitlabClient) hooksPayload, err := git.NewHooksPayload( cfg, @@ -469,8 +465,7 @@ func TestHooksNotAllowed(t *testing.T) { gitlabClient, err := gitlab.NewHTTPClient(cfg.Gitlab, cfg.TLS, prometheus.Config{}) require.NoError(t, err) - stop := runHookServiceWithGitlabClient(t, cfg, gitlabClient) - defer stop() + runHookServiceWithGitlabClient(t, cfg, gitlabClient) var stderr, stdout bytes.Buffer @@ -578,8 +573,8 @@ func TestCheckBadCreds(t *testing.T) { require.Regexp(t, `Checking GitLab API access: .* level=error msg="Internal API error" .* error="authorization failed" method=GET status=401 url="http://127.0.0.1:[0-9]+/api/v4/internal/check"\nFAIL`, stdout.String()) } -func runHookServiceServer(t *testing.T, cfg config.Cfg) func() { - return runHookServiceWithGitlabClient(t, cfg, gitlab.NewMockClient()) +func runHookServiceServer(t *testing.T, cfg config.Cfg) { + runHookServiceWithGitlabClient(t, cfg, gitlab.NewMockClient()) } type featureFlagAsserter struct { @@ -617,20 +612,12 @@ func (svc featureFlagAsserter) PackObjectsHook(stream gitalypb.HookService_PackO return svc.wrapped.PackObjectsHook(stream) } -func runHookServiceWithGitlabClient(t *testing.T, cfg config.Cfg, gitlabClient gitlab.Client) func() { - registry := backchannel.NewRegistry() - txManager := transaction.NewManager(cfg, registry) - hookManager := gitalyhook.NewManager(config.NewLocator(cfg), txManager, gitlabClient, cfg) - gitCmdFactory := git.NewExecCommandFactory(cfg) - server := testhelper.NewServerWithAuth(t, nil, nil, cfg.Auth.Token, registry, testhelper.WithInternalSocket(cfg)) - - gitalypb.RegisterHookServiceServer(server.GrpcServer(), featureFlagAsserter{ - t: t, wrapped: hook.NewServer(cfg, hookManager, gitCmdFactory), - }) - reflection.Register(server.GrpcServer()) - server.Start(t) - - return server.Stop +func runHookServiceWithGitlabClient(t *testing.T, cfg config.Cfg, gitlabClient gitlab.Client) { + testserver.RunGitalyServer(t, cfg, nil, func(srv *grpc.Server, deps *service.Dependencies) { + gitalypb.RegisterHookServiceServer(srv, featureFlagAsserter{ + t: t, wrapped: hook.NewServer(deps.GetCfg(), deps.GetHookManager(), deps.GetGitCmdFactory()), + }) + }, testserver.WithGitLabClient(gitlabClient)) } func requireContainsOnce(t *testing.T, s string, contains string) { @@ -692,7 +679,7 @@ func TestGitalyHooksPackObjects(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - defer runHookServiceServer(t, cfg)() + runHookServiceServer(t, cfg) tempDir := testhelper.TempDir(t) diff --git a/internal/gitaly/service/repository/apply_gitattributes_test.go b/internal/gitaly/service/repository/apply_gitattributes_test.go index 49b08beee..b383e5bb2 100644 --- a/internal/gitaly/service/repository/apply_gitattributes_test.go +++ b/internal/gitaly/service/repository/apply_gitattributes_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io/ioutil" - "net" "os" "path/filepath" "testing" @@ -15,13 +14,13 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/transaction" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -90,30 +89,21 @@ func TestApplyGitattributesWithTransaction(t *testing.T) { featureflag.BackchannelVoting, }).Run(t, func(t *testing.T, ctx context.Context) { cfg, repo, repoPath := testcfg.BuildWithRepo(t) - registry := backchannel.NewRegistry() - txManager := transaction.NewManager(cfg, registry) - locator := config.NewLocator(cfg) - transactionServer := &testTransactionServer{} - - logger := testhelper.DiscardTestEntry(t) - srv := testhelper.NewServerWithAuth(t, nil, nil, cfg.Auth.Token, registry) - if featureflag.IsDisabled(ctx, featureflag.BackchannelVoting) { - gitalypb.RegisterRefTransactionServer(srv.GrpcServer(), transactionServer) - } - - gitalypb.RegisterRepositoryServiceServer(srv.GrpcServer(), NewServer(cfg, nil, locator, txManager, git.NewExecCommandFactory(cfg))) - srv.Start(t) - defer srv.Stop() + transactionServer := &testTransactionServer{} + testserver.RunGitalyServer(t, cfg, nil, func(srv *grpc.Server, deps *service.Dependencies) { + gitalypb.RegisterRepositoryServiceServer(srv, NewServer(deps.GetCfg(), deps.GetRubyServer(), deps.GetLocator(), deps.GetTxManager(), deps.GetGitCmdFactory())) + if featureflag.IsDisabled(ctx, featureflag.BackchannelVoting) { + gitalypb.RegisterRefTransactionServer(srv, transactionServer) + } + }) - // We're creating a secondary listener in order to route around + // We're using internal listener in order to route around // Praefect in our tests. Otherwise Praefect would replace our // carefully crafted transaction and server information. - transactionServerListener, err := net.Listen("unix", testhelper.GetTemporaryGitalySocketFileName(t)) - require.NoError(t, err) - go func() { require.NoError(t, srv.GrpcServer().Serve(transactionServerListener)) }() + logger := testhelper.DiscardTestEntry(t) - client := newMuxedRepositoryClient(t, ctx, cfg, "unix://"+transactionServerListener.Addr().String(), + client := newMuxedRepositoryClient(t, ctx, cfg, "unix://"+cfg.GitalyInternalSocketPath(), backchannel.NewClientHandshaker(logger, func() backchannel.Server { srv := grpc.NewServer() @@ -126,7 +116,7 @@ func TestApplyGitattributesWithTransaction(t *testing.T) { ) praefect := metadata.PraefectServer{ - SocketPath: "unix://" + transactionServerListener.Addr().String(), + SocketPath: "unix://" + cfg.GitalyInternalSocketPath(), Token: cfg.Auth.Token, } diff --git a/internal/gitaly/service/repository/fetch_remote_test.go b/internal/gitaly/service/repository/fetch_remote_test.go index 5bcaa07e4..f27a20bd6 100644 --- a/internal/gitaly/service/repository/fetch_remote_test.go +++ b/internal/gitaly/service/repository/fetch_remote_test.go @@ -14,19 +14,21 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" + "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -213,16 +215,12 @@ func (m *mockTxManager) Vote(context.Context, metadata.Transaction, metadata.Pra func TestFetchRemote_transaction(t *testing.T) { sourceCfg, _, sourceRepoPath := testcfg.BuildWithRepo(t) - locator := config.NewLocator(sourceCfg) txManager := &mockTxManager{} - gitCmdFactory := git.NewExecCommandFactory(sourceCfg) + addr := testserver.RunGitalyServer(t, sourceCfg, nil, func(srv *grpc.Server, deps *service.Dependencies) { + gitalypb.RegisterRepositoryServiceServer(srv, NewServer(deps.GetCfg(), deps.GetRubyServer(), deps.GetLocator(), deps.GetTxManager(), deps.GetGitCmdFactory())) + }, testserver.WithTransactionManager(txManager)) - srv := testhelper.NewServerWithAuth(t, nil, nil, sourceCfg.Auth.Token, backchannel.NewRegistry(), testhelper.WithInternalSocket(sourceCfg)) - gitalypb.RegisterRepositoryServiceServer(srv.GrpcServer(), NewServer(sourceCfg, nil, locator, txManager, gitCmdFactory)) - srv.Start(t) - defer srv.Stop() - - client := newRepositoryClient(t, sourceCfg, "unix://"+srv.Socket()) + client := newRepositoryClient(t, sourceCfg, addr) targetCfg, targetRepoProto, targetRepoPath := testcfg.BuildWithRepo(t) port, stopGitServer := gittest.GitServer(t, targetCfg, targetRepoPath, nil) diff --git a/internal/gitaly/transaction/manager_test.go b/internal/gitaly/transaction/manager_test.go index 4e4117ed0..2599fa2d4 100644 --- a/internal/gitaly/transaction/manager_test.go +++ b/internal/gitaly/transaction/manager_test.go @@ -1,4 +1,4 @@ -package transaction +package transaction_test import ( "context" @@ -9,11 +9,15 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/backchannel" "gitlab.com/gitlab-org/gitaly/internal/gitaly/client" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/service" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" + "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -43,8 +47,7 @@ func TestPoolManager_Vote(t *testing.T) { }).Run(t, func(t *testing.T, ctx context.Context) { cfg := testcfg.Build(t) - transactionServer, praefect, stop := runTransactionServer(t, cfg) - defer stop() + transactionServer, praefect := runTransactionServer(t, cfg) registry := backchannel.NewRegistry() if featureflag.IsEnabled(ctx, featureflag.BackchannelVoting) { @@ -54,12 +57,12 @@ func TestPoolManager_Vote(t *testing.T) { praefect = metadata.PraefectServer{BackchannelID: registry.RegisterBackchannel(backchannelConn)} } - manager := NewManager(cfg, registry) + manager := transaction.NewManager(cfg, registry) for _, tc := range []struct { desc string transaction metadata.Transaction - vote Vote + vote transaction.Vote voteFn func(*testing.T, *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) expectedErr error }{ @@ -69,11 +72,11 @@ func TestPoolManager_Vote(t *testing.T) { ID: 1, Node: "node", }, - vote: VoteFromData([]byte("foobar")), + vote: transaction.VoteFromData([]byte("foobar")), voteFn: func(t *testing.T, request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) { require.Equal(t, uint64(1), request.TransactionId) require.Equal(t, "node", request.Node) - require.Equal(t, request.ReferenceUpdatesHash, VoteFromData([]byte("foobar")).Bytes()) + require.Equal(t, request.ReferenceUpdatesHash, transaction.VoteFromData([]byte("foobar")).Bytes()) return &gitalypb.VoteTransactionResponse{ State: gitalypb.VoteTransactionResponse_COMMIT, @@ -124,8 +127,7 @@ func TestPoolManager_Stop(t *testing.T) { }).Run(t, func(t *testing.T, ctx context.Context) { cfg := testcfg.Build(t) - transactionServer, praefect, stop := runTransactionServer(t, cfg) - defer stop() + transactionServer, praefect := runTransactionServer(t, cfg) registry := backchannel.NewRegistry() if featureflag.IsEnabled(ctx, featureflag.BackchannelVoting) { @@ -135,7 +137,7 @@ func TestPoolManager_Stop(t *testing.T) { praefect = metadata.PraefectServer{BackchannelID: registry.RegisterBackchannel(backchannelConn)} } - manager := NewManager(cfg, registry) + manager := transaction.NewManager(cfg, registry) for _, tc := range []struct { desc string @@ -174,20 +176,17 @@ func TestPoolManager_Stop(t *testing.T) { }) } -func runTransactionServer(t *testing.T, cfg config.Cfg) (*testTransactionServer, metadata.PraefectServer, func()) { +func runTransactionServer(t *testing.T, cfg config.Cfg) (*testTransactionServer, metadata.PraefectServer) { transactionServer := &testTransactionServer{} - - server := testhelper.NewServerWithAuth(t, nil, nil, cfg.Auth.Token, backchannel.NewRegistry(), testhelper.WithInternalSocket(cfg)) - gitalypb.RegisterRefTransactionServer(server.GrpcServer(), transactionServer) - server.Start(t) - - listener, address := testhelper.GetLocalhostListener(t) - go func() { require.NoError(t, server.GrpcServer().Serve(listener)) }() + cfg.ListenAddr = ":0" // pushes gRPC to listen on the TCP address + addr := testserver.RunGitalyServer(t, cfg, nil, func(srv *grpc.Server, deps *service.Dependencies) { + gitalypb.RegisterRefTransactionServer(srv, transactionServer) + }, testserver.WithDisablePraefect()) praefect := metadata.PraefectServer{ - ListenAddr: "tcp://" + address, + ListenAddr: addr, Token: cfg.Auth.Token, } - return transactionServer, praefect, server.Stop + return transactionServer, praefect } |