diff --git a/changelogs/unreleased/jc-praefect-test.yml b/changelogs/unreleased/jc-praefect-test.yml new file mode 100644 index 0000000000000000000000000000000000000000..94149b7550ed91805585eb0c942de59c79fdb9ad --- /dev/null +++ b/changelogs/unreleased/jc-praefect-test.yml @@ -0,0 +1,5 @@ +--- +title: Add praefect as a transparent pass through for tests +merge_request: 1736 +author: +type: other diff --git a/internal/praefect/coordinator.go b/internal/praefect/coordinator.go index ae4ebcb61259d3c66da829ab6bf0ab0e1db6eaa3..c0b95b75ae72a07bb388753f642099f5728e475a 100644 --- a/internal/praefect/coordinator.go +++ b/internal/praefect/coordinator.go @@ -62,9 +62,15 @@ func (c *Coordinator) directRepositoryScopedMessage(ctx context.Context, mi prot } return nil, err } + if targetRepo.StorageName == "" || targetRepo.RelativePath == "" { + return nil, status.Error(codes.InvalidArgument, "target repo is invalid") + } shard, err := c.nodeMgr.GetShard(targetRepo.GetStorageName()) if err != nil { + if err == nodes.ErrVirtualStorageNotExist { + return nil, status.Errorf(codes.InvalidArgument, err.Error()) + } return nil, err } @@ -127,6 +133,9 @@ func (c *Coordinator) StreamDirector(ctx context.Context, fullMethodName string, // any RPC that gets proxied through praefect must be repository scoped. shard, err := c.nodeMgr.GetShard(c.conf.VirtualStorages[0].Name) if err != nil { + if err == nodes.ErrVirtualStorageNotExist { + return nil, status.Errorf(codes.InvalidArgument, err.Error()) + } return nil, err } diff --git a/internal/praefect/nodes/manager.go b/internal/praefect/nodes/manager.go index e9f2de58afb74d276cd2d9803ed23fb8e85fef12..e091286d3b392269fa5162cd147d49a2c2d9afc0 100644 --- a/internal/praefect/nodes/manager.go +++ b/internal/praefect/nodes/manager.go @@ -156,11 +156,13 @@ func (n *Mgr) Start(bootstrapInterval, monitorInterval time.Duration) { } } +var ErrVirtualStorageNotExist = errors.New("virtual storage does not exist") + // GetShard retrieves a shard for a virtual storage name func (n *Mgr) GetShard(virtualStorageName string) (Shard, error) { shard, ok := n.shards[virtualStorageName] if !ok { - return nil, errors.New("virtual storage does not exist") + return nil, ErrVirtualStorageNotExist } if n.failoverEnabled { diff --git a/internal/praefect/protoregistry/find_oid.go b/internal/praefect/protoregistry/find_oid.go index 8b73d494315655d9f76cc126c7602a067f5e1c98..961c862d8067927a393daa11fc3dcdf0406a385a 100644 --- a/internal/praefect/protoregistry/find_oid.go +++ b/internal/praefect/protoregistry/find_oid.go @@ -3,6 +3,7 @@ package protoregistry import ( "errors" "fmt" + "math" "reflect" "regexp" "strconv" @@ -22,6 +23,9 @@ var ErrTargetRepoMissing = errors.New("empty Repository") func reflectFindRepoTarget(pbMsg proto.Message, targetOID []int) (*gitalypb.Repository, error) { msgV, e := reflectFindOID(pbMsg, targetOID) if e != nil { + if e == ErrProtoFieldEmpty { + return nil, ErrTargetRepoMissing + } return nil, e } @@ -47,6 +51,9 @@ func reflectFindStorage(pbMsg proto.Message, targetOID []int) (string, error) { return targetRepo, nil } +// ErrProtoFieldEmpty indicates the protobuf field is empty +var ErrProtoFieldEmpty = errors.New("proto field is empty") + // reflectFindOID finds the target repository by using the OID to // navigate the struct tags // Warning: this reflection filled function is full of forbidden dark elf magic @@ -57,10 +64,7 @@ func reflectFindOID(pbMsg proto.Message, targetOID []int) (reflect.Value, error) msgV, err = findProtoField(msgV, fieldNo) if err != nil { - return reflect.Value{}, fmt.Errorf( - "unable to descend OID %+v into message %s: %v", - targetOID, proto.MessageName(pbMsg), err, - ) + return reflect.Value{}, err } } return msgV, nil @@ -74,7 +78,49 @@ const ( protobufTagRegexFieldGroup = 2 ) +func isZero(v reflect.Value) bool { + switch v.Kind() { + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return math.Float64bits(v.Float()) == 0 + case reflect.Complex64, reflect.Complex128: + c := v.Complex() + return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0 + case reflect.Array: + for i := 0; i < v.Len(); i++ { + if !isZero(v.Index(i)) { + return false + } + } + return true + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer: + return v.IsNil() + case reflect.String: + return v.Len() == 0 + case reflect.Struct: + for i := 0; i < v.NumField(); i++ { + if !isZero(v.Field(i)) { + return false + } + } + return true + default: + // This should never happens, but will act as a safeguard for + // later, as a default value doesn't makes sense here. + panic(&reflect.ValueError{"reflect.Value.IsZero", v.Kind()}) + } +} + func findProtoField(msgV reflect.Value, protoField int) (reflect.Value, error) { + if isZero(msgV) { + return reflect.Value{}, ErrProtoFieldEmpty + } + msgV = reflect.Indirect(msgV) for i := 0; i < msgV.NumField(); i++ { field := msgV.Type().Field(i) @@ -115,6 +161,10 @@ func tryNumberedField(field reflect.StructField, protoField int) (bool, error) { } func tryOneOfField(msgV reflect.Value, field reflect.StructField, protoField int) (reflect.Value, bool) { + if isZero(msgV) { + return reflect.Value{}, false + } + oneOfTag := field.Tag.Get(protobufOneOfTag) if oneOfTag == "" { return reflect.Value{}, false // empty tag means this is not a oneOf field diff --git a/internal/service/cleanup/apply_bfg_object_map_stream_test.go b/internal/service/cleanup/apply_bfg_object_map_stream_test.go index 9eb19da788a7749c3d41534a3a3d179c04f689bb..a613f75b2033ab413a229c90e8d464ab69449b97 100644 --- a/internal/service/cleanup/apply_bfg_object_map_stream_test.go +++ b/internal/service/cleanup/apply_bfg_object_map_stream_test.go @@ -17,8 +17,8 @@ import ( ) func TestApplyBfgObjectMapStreamSuccess(t *testing.T) { - server, serverSocketPath := runCleanupServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runCleanupServiceServer(t) + defer stop() client, conn := newCleanupServiceClient(t, serverSocketPath) defer conn.Close() @@ -81,8 +81,8 @@ func requireEntry(t *testing.T, entry *gitalypb.ApplyBfgObjectMapStreamResponse_ } func TestApplyBfgObjectMapStreamFailsOnInvalidInput(t *testing.T) { - server, serverSocketPath := runCleanupServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runCleanupServiceServer(t) + defer stop() client, conn := newCleanupServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/cleanup/testhelper_test.go b/internal/service/cleanup/testhelper_test.go index 1ad6230f22880cf701e489ceaeeb448d8ee82f4b..3d12b9af8a560d2eb48dd435b40c439f542f25d1 100644 --- a/internal/service/cleanup/testhelper_test.go +++ b/internal/service/cleanup/testhelper_test.go @@ -1,30 +1,24 @@ package cleanup import ( - "net" "testing" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) -func runCleanupServiceServer(t *testing.T) (*grpc.Server, string) { - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - grpcServer := testhelper.NewTestGrpcServer(t, nil, nil) +func runCleanupServiceServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterCleanupServiceServer(grpcServer, NewServer()) - reflection.Register(grpcServer) + gitalypb.RegisterCleanupServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - go grpcServer.Serve(listener) + require.NoError(t, srv.Start()) - return grpcServer, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newCleanupServiceClient(t *testing.T, serverSocketPath string) (gitalypb.CleanupServiceClient, *grpc.ClientConn) { diff --git a/internal/service/commit/between_test.go b/internal/service/commit/between_test.go index 78f678aaf813cffb04ec214b81eea3d567bb17d2..9ac2240f3be65b85f0b666d2a3ab1327fb1758b0 100644 --- a/internal/service/commit/between_test.go +++ b/internal/service/commit/between_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulCommitsBetween(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -198,8 +198,8 @@ func TestSuccessfulCommitsBetween(t *testing.T) { } func TestFailedCommitsBetweenRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/commit_messages_test.go b/internal/service/commit/commit_messages_test.go index 7d6b97e0c8447e276b85c3c981e532fc1e7c839c..89c9891f7f744ca6a3745e9caf7695fea2beec49 100644 --- a/internal/service/commit/commit_messages_test.go +++ b/internal/service/commit/commit_messages_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulGetCommitMessagesRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -55,8 +55,8 @@ func TestSuccessfulGetCommitMessagesRequest(t *testing.T) { } func TestFailedGetCommitMessagesRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/commit_signatures_test.go b/internal/service/commit/commit_signatures_test.go index 09493afa1f8b2a7d86f8186ee5bc6a249d3e47e2..1f07d7bcbd8f88f465d40059bb072e83e0790190 100644 --- a/internal/service/commit/commit_signatures_test.go +++ b/internal/service/commit/commit_signatures_test.go @@ -11,8 +11,8 @@ import ( ) func TestSuccessfulGetCommitSignaturesRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -61,8 +61,8 @@ func TestSuccessfulGetCommitSignaturesRequest(t *testing.T) { } func TestFailedGetCommitSignaturesRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/commits_by_message_test.go b/internal/service/commit/commits_by_message_test.go index ad08176adc1ca8138bdf9ded1d11635dd30ce841..bcaedc32f4b4ddf8f1dbb2a344030e931a252089 100644 --- a/internal/service/commit/commits_by_message_test.go +++ b/internal/service/commit/commits_by_message_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulCommitsByMessageRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -155,8 +155,8 @@ func TestSuccessfulCommitsByMessageRequest(t *testing.T) { } func TestFailedCommitsByMessageRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/count_commits_test.go b/internal/service/commit/count_commits_test.go index b609366fb43d9bb2dec4f606c6cbf30e4f5cc1b4..6313d56548ede7cbfda248172bc3e98e76331531 100644 --- a/internal/service/commit/count_commits_test.go +++ b/internal/service/commit/count_commits_test.go @@ -14,8 +14,8 @@ import ( ) func TestSuccessfulCountCommitsRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -163,8 +163,8 @@ func TestSuccessfulCountCommitsRequest(t *testing.T) { } func TestFailedCountCommitsRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/count_diverging_commits_test.go b/internal/service/commit/count_diverging_commits_test.go index 928a9143ab3dc069aaa39172044a04a11ac67bea..a4c844e1d9fa86fcabc1d73fa402632d32e7ed17 100644 --- a/internal/service/commit/count_diverging_commits_test.go +++ b/internal/service/commit/count_diverging_commits_test.go @@ -59,8 +59,8 @@ func createRepoWithDivergentBranches(t *testing.T, leftCommits, rightCommits int } func TestSuccessfulCountDivergentCommitsRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -137,8 +137,8 @@ func TestSuccessfulCountDivergentCommitsRequest(t *testing.T) { } func TestSuccessfulCountDivergentCommitsRequestWithMaxCount(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -186,8 +186,8 @@ func TestSuccessfulCountDivergentCommitsRequestWithMaxCount(t *testing.T) { } func TestFailedCountDivergentCommitsRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/filter_shas_with_signatures.go b/internal/service/commit/filter_shas_with_signatures.go index 8cd2f24fca796fa783d0459b683f02762aa3d2bb..d025196300cc3d780a86424fa8496a0430dbfdc0 100644 --- a/internal/service/commit/filter_shas_with_signatures.go +++ b/internal/service/commit/filter_shas_with_signatures.go @@ -1,9 +1,9 @@ package commit import ( - "errors" "io" + "gitlab.com/gitlab-org/gitaly/internal/errors" "gitlab.com/gitlab-org/gitaly/internal/git/catfile" "gitlab.com/gitlab-org/gitaly/internal/git/log" "gitlab.com/gitlab-org/gitaly/internal/helper" @@ -28,7 +28,7 @@ func (s *server) FilterShasWithSignatures(bidi gitalypb.CommitService_FilterShas func validateFirstFilterShasWithSignaturesRequest(in *gitalypb.FilterShasWithSignaturesRequest) error { if in.Repository == nil { - return errors.New("no repository given") + return errors.ErrEmptyRepository } return nil } diff --git a/internal/service/commit/filter_shas_with_signatures_test.go b/internal/service/commit/filter_shas_with_signatures_test.go index 877a58f5b55c1c41e7f879b7393a76638ac3b33d..3589fa6e1719f6d7e6333638a1cc4e60afff7a9d 100644 --- a/internal/service/commit/filter_shas_with_signatures_test.go +++ b/internal/service/commit/filter_shas_with_signatures_test.go @@ -13,8 +13,8 @@ func TestFilterShasWithSignaturesSuccessful(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -66,7 +66,7 @@ func TestFilterShasWithSignaturesSuccessful(t *testing.T) { func TestFilterShasWithSignaturesValidationError(t *testing.T) { err := validateFirstFilterShasWithSignaturesRequest(&gitalypb.FilterShasWithSignaturesRequest{}) - require.Contains(t, err.Error(), "no repository given") + require.Contains(t, err.Error(), "empty Repository") } func recvFSWS(stream gitalypb.CommitService_FilterShasWithSignaturesClient) ([][]byte, error) { diff --git a/internal/service/commit/find_all_commits_test.go b/internal/service/commit/find_all_commits_test.go index e79b1ae45b1074264c6c878c49735b1cf2ea524f..bdaa442615b1fecace85166e8aad84af617cba9b 100644 --- a/internal/service/commit/find_all_commits_test.go +++ b/internal/service/commit/find_all_commits_test.go @@ -25,8 +25,8 @@ func TestSuccessfulFindAllCommitsRequest(t *testing.T) { }, nil } - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -285,8 +285,8 @@ func TestSuccessfulFindAllCommitsRequest(t *testing.T) { } func TestFailedFindAllCommitsRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/find_commit_test.go b/internal/service/commit/find_commit_test.go index 96367f559901cbcb902b74d86127cc1886f65e3e..bbef71e36e018da239201ab74c91d24521fb77c4 100644 --- a/internal/service/commit/find_commit_test.go +++ b/internal/service/commit/find_commit_test.go @@ -24,8 +24,8 @@ func TestSuccessfulFindCommitRequest(t *testing.T) { windows1251Message, err := ioutil.ReadFile("testdata/commit-c809470461118b7bcab850f6e9a7ca97ac42f8ea-message.txt") require.NoError(t, err) - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -270,8 +270,8 @@ func TestSuccessfulFindCommitRequest(t *testing.T) { } func TestFailedFindCommitRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -320,8 +320,8 @@ func benchmarkFindCommit(withCache bool, b *testing.B) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := startTestServices(b) - defer server.Stop() + stop, serverSocketPath := startTestServices(b) + defer stop() client, conn := newCommitServiceClient(b, serverSocketPath) defer conn.Close() @@ -367,8 +367,8 @@ func TestFindCommitWithCache(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/find_commits_test.go b/internal/service/commit/find_commits_test.go index 29a7eee95d8cdfeac2b61910eb9b1f437df820ca..a22fbf91a606abd74ffdd8a073d3d78270dbbb2d 100644 --- a/internal/service/commit/find_commits_test.go +++ b/internal/service/commit/find_commits_test.go @@ -20,8 +20,8 @@ func TestFindCommitsFields(t *testing.T) { windows1251Message, err := ioutil.ReadFile("testdata/commit-c809470461118b7bcab850f6e9a7ca97ac42f8ea-message.txt") require.NoError(t, err) - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -179,8 +179,8 @@ func TestFindCommitsFields(t *testing.T) { } func TestSuccessfulFindCommitsRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -422,8 +422,8 @@ func TestSuccessfulFindCommitsRequest(t *testing.T) { } func TestSuccessfulFindCommitsRequestWithAltGitObjectDirs(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -492,8 +492,8 @@ func TestSuccessfulFindCommitsRequestWithAltGitObjectDirs(t *testing.T) { } func TestSuccessfulFindCommitsRequestWithAmbiguousRef(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -538,8 +538,8 @@ func TestSuccessfulFindCommitsRequestWithAmbiguousRef(t *testing.T) { } func TestFailureFindCommitsRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -589,8 +589,8 @@ func TestFailureFindCommitsRequest(t *testing.T) { } func TestFindCommitsRequestWithFollowAndOffset(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/isancestor_test.go b/internal/service/commit/isancestor_test.go index b1d22eae96e6358636b312374893b027c4031aec..8b8fed5699d0e6b426ede859ff017d9e7a681aaa 100644 --- a/internal/service/commit/isancestor_test.go +++ b/internal/service/commit/isancestor_test.go @@ -14,8 +14,8 @@ import ( ) func TestCommitIsAncestorFailure(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -80,8 +80,8 @@ func TestCommitIsAncestorFailure(t *testing.T) { } func TestCommitIsAncestorSuccess(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -177,8 +177,8 @@ func TestCommitIsAncestorSuccess(t *testing.T) { } func TestSuccessfulIsAncestorRequestWithAltGitObjectDirs(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/languages_test.go b/internal/service/commit/languages_test.go index eaf526925e6735350722ec93a353ce6c1b1bca1b..c0f9106e2ca5c103934f1b6cf4b091cb427b98b9 100644 --- a/internal/service/commit/languages_test.go +++ b/internal/service/commit/languages_test.go @@ -13,8 +13,8 @@ import ( ) func TestLanguages(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -56,8 +56,8 @@ func TestLanguages(t *testing.T) { } func TestFileCountIsZeroWhenFeatureIsDisabled(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -93,8 +93,8 @@ func requireLanguageEqual(t *testing.T, expected, actual *gitalypb.CommitLanguag } func TestLanguagesEmptyRevision(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -123,8 +123,8 @@ func TestLanguagesEmptyRevision(t *testing.T) { } func TestInvalidCommitLanguagesRequestRevision(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -143,8 +143,8 @@ func TestInvalidCommitLanguagesRequestRevision(t *testing.T) { } func TestAmbiguousRefCommitLanguagesRequestRevision(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/last_commit_for_path_test.go b/internal/service/commit/last_commit_for_path_test.go index 484d0ed9ebb2d5b1ed0af4a060da29ab35f220d0..cfa80149cd3772e623f3c27be21a5a7c633ee0f8 100644 --- a/internal/service/commit/last_commit_for_path_test.go +++ b/internal/service/commit/last_commit_for_path_test.go @@ -12,8 +12,8 @@ import ( ) func TestSuccessfulLastCommitForPathRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -93,8 +93,8 @@ func TestSuccessfulLastCommitForPathRequest(t *testing.T) { } func TestFailedLastCommitForPathRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/list_commits_by_oid_test.go b/internal/service/commit/list_commits_by_oid_test.go index 7b343535033f404c5f865b38852309fcfa1d7419..03033fecc069a81edeed0f93862173277adec38b 100644 --- a/internal/service/commit/list_commits_by_oid_test.go +++ b/internal/service/commit/list_commits_by_oid_test.go @@ -11,8 +11,8 @@ import ( ) func TestSuccessfulListCommitsByOidRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -176,8 +176,8 @@ var masterCommitids = []string{ } func TestSuccessfulListCommitsByOidLargeRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/list_commits_by_ref_name_test.go b/internal/service/commit/list_commits_by_ref_name_test.go index a0a2d3969485ba04f422baeabd985a8d9a50a5f0..f8de19661a10ade58f3d840d02ac57b65d810e22 100644 --- a/internal/service/commit/list_commits_by_ref_name_test.go +++ b/internal/service/commit/list_commits_by_ref_name_test.go @@ -10,8 +10,8 @@ import ( ) func TestSuccessfulListCommitsByRefNameRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -189,8 +189,8 @@ var repositoryRefNames = map[string]string{ } func TestSuccessfulListCommitsByRefNameLargeRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/list_files_test.go b/internal/service/commit/list_files_test.go index 1efcf4428960091f052c075c65c48bbb44af1442..c637ca89f65236297339c09c8faeb1e4c4983172 100644 --- a/internal/service/commit/list_files_test.go +++ b/internal/service/commit/list_files_test.go @@ -49,8 +49,8 @@ func TestListFilesSuccess(t *testing.T) { defaultBranchName = ref.DefaultBranchName }() - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -136,8 +136,8 @@ func TestListFilesSuccess(t *testing.T) { } func TestListFilesFailure(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -186,8 +186,8 @@ func drainListFilesResponse(c gitalypb.CommitService_ListFilesClient) error { } func TestInvalidListFilesRequestRevision(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/list_last_commits_for_tree_test.go b/internal/service/commit/list_last_commits_for_tree_test.go index c7b611dba8fd554953e59255d68f0fc7adb2f3fc..933de977c2aa95928adebbe1f2a94405fcc1debf 100644 --- a/internal/service/commit/list_last_commits_for_tree_test.go +++ b/internal/service/commit/list_last_commits_for_tree_test.go @@ -20,8 +20,8 @@ type commitInfo struct { } func TestSuccessfulListLastCommitsForTreeRequest(t *testing.T) { - server, serverSockerPath := startTestServices(t) - defer server.Stop() + stop, serverSockerPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSockerPath) defer conn.Close() @@ -210,8 +210,8 @@ func TestSuccessfulListLastCommitsForTreeRequest(t *testing.T) { } func TestFailedListLastCommitsForTreeRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -306,8 +306,8 @@ func TestFailedListLastCommitsForTreeRequest(t *testing.T) { } func TestNonUtf8ListLastCommitsForTreeRequest(t *testing.T) { - server, serverSockerPath := startTestServices(t) - defer server.Stop() + stop, serverSockerPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSockerPath) defer conn.Close() diff --git a/internal/service/commit/raw_blame_test.go b/internal/service/commit/raw_blame_test.go index 034b9f1cebae5e1c03b4f6f73be302f30ab71fa9..30182666e3f3a60644d71efdb2f61444e03a4e4e 100644 --- a/internal/service/commit/raw_blame_test.go +++ b/internal/service/commit/raw_blame_test.go @@ -14,8 +14,8 @@ import ( ) func TestSuccessfulRawBlameRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -74,8 +74,8 @@ func TestSuccessfulRawBlameRequest(t *testing.T) { } func TestFailedRawBlameRequest(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/stats_test.go b/internal/service/commit/stats_test.go index 2d923675457f3df568040de027bfe39580feda22..f3eaae7647fe17e18c3dfe69f6154fdece1932b6 100644 --- a/internal/service/commit/stats_test.go +++ b/internal/service/commit/stats_test.go @@ -12,8 +12,8 @@ import ( ) func TestCommitStatsSuccess(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -83,8 +83,8 @@ func TestCommitStatsSuccess(t *testing.T) { } func TestCommitStatsFailure(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/testhelper_test.go b/internal/service/commit/testhelper_test.go index b4621af6ac345a45876a8e6968d46c45d8ea8105..53ada5c9884130cf02b6870119b76cdee3f966ce 100644 --- a/internal/service/commit/testhelper_test.go +++ b/internal/service/commit/testhelper_test.go @@ -1,19 +1,17 @@ package commit import ( - "net" "os" "testing" "github.com/golang/protobuf/ptypes/timestamp" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) -var () - func TestMain(m *testing.M) { os.Exit(testMain(m)) } @@ -24,20 +22,15 @@ func testMain(m *testing.M) int { return m.Run() } -func startTestServices(t testing.TB) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() +func startTestServices(t testing.TB) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal("failed to start server") - } + gitalypb.RegisterCommitServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - gitalypb.RegisterCommitServiceServer(server, NewServer()) - reflection.Register(server) + require.NoError(t, srv.Start()) - go server.Serve(listener) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newCommitServiceClient(t testing.TB, serviceSocketPath string) (gitalypb.CommitServiceClient, *grpc.ClientConn) { diff --git a/internal/service/commit/tree_entries_test.go b/internal/service/commit/tree_entries_test.go index 6c7af65e76b468f5f056a9a2c4fc48cebf02970d..ad597431d7ddef3716188a8188ac86353430d99a 100644 --- a/internal/service/commit/tree_entries_test.go +++ b/internal/service/commit/tree_entries_test.go @@ -17,8 +17,8 @@ import ( ) func TestSuccessfulGetTreeEntriesWithCurlyBraces(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -86,8 +86,8 @@ func TestSuccessfulGetTreeEntries(t *testing.T) { commitID := "d25b6d94034242f3930dfcfeb6d8d9aac3583992" rootOid := "21bdc8af908562ae485ed46d71dd5426c08b084a" - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -410,8 +410,8 @@ func getTreeEntriesFromTreeEntryClient(t *testing.T, client gitalypb.CommitServi } func TestSuccessfulGetTreeEntries_FlatPathMaxDeep_SingleFoldersStructure(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -463,8 +463,8 @@ func TestSuccessfulGetTreeEntries_FlatPathMaxDeep_SingleFoldersStructure(t *test } func TestFailedGetTreeEntriesRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/commit/tree_entry_test.go b/internal/service/commit/tree_entry_test.go index 32a68a97048f8f75aed078afb2b5c6944189ec60..0ffb9d10ac01084d60b7d080cc328c423dbbc419 100644 --- a/internal/service/commit/tree_entry_test.go +++ b/internal/service/commit/tree_entry_test.go @@ -21,8 +21,8 @@ type treeEntry struct { } func TestSuccessfulTreeEntry(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() @@ -160,8 +160,8 @@ func TestSuccessfulTreeEntry(t *testing.T) { } func TestFailedTreeEntryRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := startTestServices(t) - defer server.Stop() + stop, serverSocketPath := startTestServices(t) + defer stop() client, conn := newCommitServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/conflicts/list_conflict_files_test.go b/internal/service/conflicts/list_conflict_files_test.go index 074234caab537ef713cea3423aefe66bf43c8d01..37d19369697f548aab231639d753a380b3c291cf 100644 --- a/internal/service/conflicts/list_conflict_files_test.go +++ b/internal/service/conflicts/list_conflict_files_test.go @@ -17,8 +17,8 @@ type conflictFile struct { } func TestSuccessfulListConflictFilesRequest(t *testing.T) { - server, serverSocketPath := runConflictsServer(t) - defer server.Stop() + stop, serverSocketPath := runConflictsServer(t) + defer stop() client, conn := NewConflictsClient(t, serverSocketPath) defer conn.Close() @@ -93,8 +93,8 @@ end } func TestListConflictFilesFailedPrecondition(t *testing.T) { - server, serverSocketPath := runConflictsServer(t) - defer server.Stop() + stop, serverSocketPath := runConflictsServer(t) + defer stop() client, conn := NewConflictsClient(t, serverSocketPath) defer conn.Close() @@ -158,8 +158,8 @@ func TestListConflictFilesFailedPrecondition(t *testing.T) { } func TestFailedListConflictFilesRequestDueToValidation(t *testing.T) { - server, serverSocketPath := runConflictsServer(t) - defer server.Stop() + stop, serverSocketPath := runConflictsServer(t) + defer stop() client, conn := NewConflictsClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/conflicts/testhelper_test.go b/internal/service/conflicts/testhelper_test.go index f2a05721dfcdd63dcf432bd4fbc54afab4bd52ab..5296055e770fb45fc2b9d6522c0e147b1b6e16ca 100644 --- a/internal/service/conflicts/testhelper_test.go +++ b/internal/service/conflicts/testhelper_test.go @@ -2,11 +2,11 @@ package conflicts import ( "io/ioutil" - "net" "os" "testing" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/rubyserver" @@ -42,21 +42,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runConflictsServer(t *testing.T) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) +func runConflictsServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterConflictsServiceServer(server, NewServer(RubyServer)) - reflection.Register(server) + gitalypb.RegisterConflictsServiceServer(srv.GrpcServer(), NewServer(RubyServer)) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func NewConflictsClient(t *testing.T, serverSocketPath string) (gitalypb.ConflictsServiceClient, *grpc.ClientConn) { diff --git a/internal/service/diff/commit_test.go b/internal/service/diff/commit_test.go index 9953d276a616959545d7b7cf36c920717a39a47b..f187bed7d7fc85ecbe1aacf948eb66940b95be13 100644 --- a/internal/service/diff/commit_test.go +++ b/internal/service/diff/commit_test.go @@ -15,8 +15,8 @@ import ( ) func TestSuccessfulCommitDiffRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -192,8 +192,8 @@ func TestSuccessfulCommitDiffRequest(t *testing.T) { } func TestSuccessfulCommitDiffRequestWithPaths(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -270,8 +270,8 @@ func TestSuccessfulCommitDiffRequestWithPaths(t *testing.T) { } func TestSuccessfulCommitDiffRequestWithTypeChangeDiff(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -321,8 +321,8 @@ func TestSuccessfulCommitDiffRequestWithTypeChangeDiff(t *testing.T) { } func TestSuccessfulCommitDiffRequestWithIgnoreWhitespaceChange(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -435,8 +435,8 @@ func TestSuccessfulCommitDiffRequestWithIgnoreWhitespaceChange(t *testing.T) { } func TestSuccessfulCommitDiffRequestWithLimits(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -657,8 +657,8 @@ func TestSuccessfulCommitDiffRequestWithLimits(t *testing.T) { } func TestFailedCommitDiffRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -692,8 +692,8 @@ func TestFailedCommitDiffRequestDueToValidationError(t *testing.T) { } func TestFailedCommitDiffRequestWithNonExistentCommit(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -717,8 +717,8 @@ func TestFailedCommitDiffRequestWithNonExistentCommit(t *testing.T) { } func TestSuccessfulCommitDeltaRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -840,8 +840,8 @@ func TestSuccessfulCommitDeltaRequest(t *testing.T) { } func TestSuccessfulCommitDeltaRequestWithPaths(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -909,8 +909,8 @@ func TestSuccessfulCommitDeltaRequestWithPaths(t *testing.T) { } func TestFailedCommitDeltaRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -944,8 +944,8 @@ func TestFailedCommitDeltaRequestDueToValidationError(t *testing.T) { } func TestFailedCommitDeltaRequestWithNonExistentCommit(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/diff/numstat_test.go b/internal/service/diff/numstat_test.go index 073e1323cf4dd517e3129180a3af09564289d032..33ead17b25aef5f69849018d69ef17922bd527a2 100644 --- a/internal/service/diff/numstat_test.go +++ b/internal/service/diff/numstat_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulDiffStatsRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -128,8 +128,8 @@ func TestSuccessfulDiffStatsRequest(t *testing.T) { } func TestFailedDiffStatsRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/diff/raw_test.go b/internal/service/diff/raw_test.go index 4e3ce322a81e541fa8e817dffa224d54e20a1084..034bbff61d28c415325c2e96f3d30a950a3051f2 100644 --- a/internal/service/diff/raw_test.go +++ b/internal/service/diff/raw_test.go @@ -12,8 +12,8 @@ import ( ) func TestSuccessfulRawDiffRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -56,8 +56,8 @@ func TestSuccessfulRawDiffRequest(t *testing.T) { } func TestFailedRawDiffRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -111,8 +111,8 @@ func TestFailedRawDiffRequestDueToValidations(t *testing.T) { } func TestSuccessfulRawPatchRequest(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() @@ -148,8 +148,8 @@ func TestSuccessfulRawPatchRequest(t *testing.T) { } func TestFailedRawPatchRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runDiffServer(t) - defer server.Stop() + stop, serverSocketPath := runDiffServer(t) + defer stop() client, conn := newDiffClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/diff/testhelper_test.go b/internal/service/diff/testhelper_test.go index c8bb4219cbe25c4008dc6f485a19a28ac0196745..13ea12fe730e7058f1e1b475d175d2fdbee7d6b9 100644 --- a/internal/service/diff/testhelper_test.go +++ b/internal/service/diff/testhelper_test.go @@ -1,10 +1,10 @@ package diff import ( - "net" "os" "testing" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" @@ -21,21 +21,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runDiffServer(t *testing.T) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) +func runDiffServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterDiffServiceServer(server, NewServer()) - reflection.Register(server) + gitalypb.RegisterDiffServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newDiffClient(t *testing.T, serverSocketPath string) (gitalypb.DiffServiceClient, *grpc.ClientConn) { diff --git a/internal/service/hooks/post_receive_test.go b/internal/service/hooks/post_receive_test.go index 1dfee6948d90399e081fae9eb1b91b1612127864..d68ff1dace3d4cff5d3532ac0a3db86549acda96 100644 --- a/internal/service/hooks/post_receive_test.go +++ b/internal/service/hooks/post_receive_test.go @@ -18,8 +18,8 @@ import ( ) func TestPostReceiveInvalidArgument(t *testing.T) { - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() client, conn := newHooksClient(t, serverSocketPath) defer conn.Close() @@ -45,8 +45,8 @@ func TestPostReceive(t *testing.T) { require.NoError(t, err) config.Config.Ruby.Dir = filepath.Join(cwd, "testdata") - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() diff --git a/internal/service/hooks/pre_receive_test.go b/internal/service/hooks/pre_receive_test.go index 08af19aee6263ec430d9ce4e740b04f10c79db66..4809c26dc857eeab3e151e07b73eb24d09de9e7a 100644 --- a/internal/service/hooks/pre_receive_test.go +++ b/internal/service/hooks/pre_receive_test.go @@ -18,8 +18,8 @@ import ( ) func TestPreReceiveInvalidArgument(t *testing.T) { - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() client, conn := newHooksClient(t, serverSocketPath) defer conn.Close() @@ -45,8 +45,8 @@ func TestPreReceive(t *testing.T) { require.NoError(t, err) config.Config.Ruby.Dir = filepath.Join(cwd, "testdata") - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() diff --git a/internal/service/hooks/testhelper_test.go b/internal/service/hooks/testhelper_test.go index 72367ac7554325133ee253b7e49e3cf6c65cb7c4..e55ab794ff92000fdc6d999d8edcf6253df52e76 100644 --- a/internal/service/hooks/testhelper_test.go +++ b/internal/service/hooks/testhelper_test.go @@ -1,12 +1,11 @@ package hook import ( - "net" "testing" + "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/internal/config" - "gitlab.com/gitlab-org/gitaly/internal/server/auth" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" @@ -26,22 +25,13 @@ func newHooksClient(t *testing.T, serverSocketPath string) (gitalypb.HookService return gitalypb.NewHookServiceClient(conn), conn } -func runHooksServer(t *testing.T) (*grpc.Server, string) { - streamInt := []grpc.StreamServerInterceptor{auth.StreamServerInterceptor(config.Config.Auth)} - unaryInt := []grpc.UnaryServerInterceptor{auth.UnaryServerInterceptor(config.Config.Auth)} +func runHooksServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - server := testhelper.NewTestGrpcServer(t, streamInt, unaryInt) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() + gitalypb.RegisterHookServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterHookServiceServer(server, NewServer()) - reflection.Register(server) - - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } diff --git a/internal/service/hooks/update_test.go b/internal/service/hooks/update_test.go index 7319be1b6a3ed5df1b75d65c90a2aa90a95e6606..f92e52c1370777eadb73212d316ac0a0129bc6b0 100644 --- a/internal/service/hooks/update_test.go +++ b/internal/service/hooks/update_test.go @@ -17,8 +17,8 @@ import ( ) func TestUpdateInvalidArgument(t *testing.T) { - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() client, conn := newHooksClient(t, serverSocketPath) defer conn.Close() @@ -43,8 +43,8 @@ func TestUpdate(t *testing.T) { require.NoError(t, err) config.Config.Ruby.Dir = filepath.Join(cwd, "testdata") - server, serverSocketPath := runHooksServer(t) - defer server.Stop() + stop, serverSocketPath := runHooksServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() diff --git a/internal/service/objectpool/alternates_test.go b/internal/service/objectpool/alternates_test.go index cba9ea7d55ab54c48840dc6d0cd8155017040ad8..5fb34ef4ff6b68560da8bae3cb0ef79d668c4b34 100644 --- a/internal/service/objectpool/alternates_test.go +++ b/internal/service/objectpool/alternates_test.go @@ -14,8 +14,8 @@ import ( ) func TestDisconnectGitAlternates(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -63,8 +63,8 @@ func TestDisconnectGitAlternates(t *testing.T) { } func TestDisconnectGitAlternatesNoAlternates(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -86,8 +86,8 @@ func TestDisconnectGitAlternatesNoAlternates(t *testing.T) { } func TestDisconnectGitAlternatesUnexpectedAlternates(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/objectpool/create_test.go b/internal/service/objectpool/create_test.go index 8b6946fc95626ba94c6fe4e2f7153606710e59cc..7499c19b9aaa91780ac1dcf3fecb7104b8060a28 100644 --- a/internal/service/objectpool/create_test.go +++ b/internal/service/objectpool/create_test.go @@ -15,8 +15,8 @@ import ( ) func TestCreate(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -61,8 +61,8 @@ func TestCreate(t *testing.T) { } func TestUnsuccessfulCreate(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -107,8 +107,8 @@ func TestUnsuccessfulCreate(t *testing.T) { } func TestRemove(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/objectpool/fetch_into_object_pool_test.go b/internal/service/objectpool/fetch_into_object_pool_test.go index 5f31a4159673e15c29ad38a784bec9552fb30eea..771a82c68dce69ed00dd0be4e6b6f88d3400d193 100644 --- a/internal/service/objectpool/fetch_into_object_pool_test.go +++ b/internal/service/objectpool/fetch_into_object_pool_test.go @@ -20,8 +20,8 @@ import ( ) func TestFetchIntoObjectPool_Success(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -78,8 +78,8 @@ func TestFetchIntoObjectPool_CollectLogStatistics(t *testing.T) { defer cancel() ctx = ctxlogrus.ToContext(ctx, log.WithField("test", "logging")) - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -135,32 +135,28 @@ func TestFetchIntoObjectPool_Failure(t *testing.T) { description string request *gitalypb.FetchIntoObjectPoolRequest code codes.Code - errMsg string }{ { - description: "empty origin", + description: "origin and pool do not share the same storage", request: &gitalypb.FetchIntoObjectPoolRequest{ - ObjectPool: pool.ToProto(), + Origin: testRepo, + ObjectPool: poolWithDifferentStorage, }, - code: codes.InvalidArgument, - errMsg: "origin is empty", + code: codes.InvalidArgument, }, { - description: "empty pool", + description: "empty origin", request: &gitalypb.FetchIntoObjectPoolRequest{ - Origin: testRepo, + ObjectPool: pool.ToProto(), }, - code: codes.InvalidArgument, - errMsg: "object pool is empty", + code: codes.InvalidArgument, }, { - description: "origin and pool do not share the same storage", + description: "empty pool", request: &gitalypb.FetchIntoObjectPoolRequest{ - Origin: testRepo, - ObjectPool: poolWithDifferentStorage, + Origin: testRepo, }, - code: codes.InvalidArgument, - errMsg: "origin has different storage than object pool", + code: codes.InvalidArgument, }, } for _, tc := range testCases { @@ -168,7 +164,6 @@ func TestFetchIntoObjectPool_Failure(t *testing.T) { _, err := server.FetchIntoObjectPool(ctx, tc.request) require.Error(t, err) testhelper.RequireGrpcError(t, err, tc.code) - assert.Contains(t, err.Error(), tc.errMsg) }) } } diff --git a/internal/service/objectpool/get_test.go b/internal/service/objectpool/get_test.go index 392758fd3b31f23e74dd061826ac12e106a72177..e6275e45421437b76056aaa3407c8a0ef61a90fe 100644 --- a/internal/service/objectpool/get_test.go +++ b/internal/service/objectpool/get_test.go @@ -13,8 +13,8 @@ import ( ) func TestGetObjectPoolSuccess(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -45,8 +45,8 @@ func TestGetObjectPoolSuccess(t *testing.T) { } func TestGetObjectPoolNoFile(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -66,8 +66,8 @@ func TestGetObjectPoolNoFile(t *testing.T) { } func TestGetObjectPoolBadFile(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/objectpool/link_test.go b/internal/service/objectpool/link_test.go index 9265eaf08db40c2f7a478c9256a53723b6b0f5db..8add03e34ef0b1be074d7c41ef5672acb19ce4ed 100644 --- a/internal/service/objectpool/link_test.go +++ b/internal/service/objectpool/link_test.go @@ -16,8 +16,8 @@ import ( ) func TestLink(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -89,8 +89,8 @@ func TestLink(t *testing.T) { } func TestLinkIdempotent(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -119,8 +119,8 @@ func TestLinkIdempotent(t *testing.T) { } func TestLinkNoClobber(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -158,8 +158,8 @@ func TestLinkNoClobber(t *testing.T) { } func TestLinkNoPool(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -190,8 +190,8 @@ func TestLinkNoPool(t *testing.T) { } func TestUnlink(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() @@ -302,8 +302,8 @@ func TestUnlink(t *testing.T) { } func TestUnlinkIdempotent(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/objectpool/reduplicate_test.go b/internal/service/objectpool/reduplicate_test.go index d1d9f560cdf1169d0223985c4321bbfcb190550a..ae8acb4dc22fc12f8667e3760d554ac34a4274d4 100644 --- a/internal/service/objectpool/reduplicate_test.go +++ b/internal/service/objectpool/reduplicate_test.go @@ -12,8 +12,8 @@ import ( ) func TestReduplicate(t *testing.T) { - server, serverSocketPath := runObjectPoolServer(t) - defer server.Stop() + stop, serverSocketPath := runObjectPoolServer(t) + defer stop() client, conn := newObjectPoolClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/objectpool/testhelper_test.go b/internal/service/objectpool/testhelper_test.go index 448e3d60d327a6c4c9ecfcadda19348110616380..4d5ee3489c66891df0e16b6defae6e5ffdf975fb 100644 --- a/internal/service/objectpool/testhelper_test.go +++ b/internal/service/objectpool/testhelper_test.go @@ -1,11 +1,12 @@ package objectpool import ( - "context" - "net" "os" "testing" + "github.com/stretchr/testify/require" + gitalyauth "gitlab.com/gitlab-org/gitaly/auth" + "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" @@ -22,30 +23,21 @@ func testMain(m *testing.M) int { return m.Run() } -func runObjectPoolServer(t *testing.T) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) +func runObjectPoolServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterObjectPoolServiceServer(server, NewServer()) - reflection.Register(server) + gitalypb.RegisterObjectPoolServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newObjectPoolClient(t *testing.T, serverSocketPath string) (gitalypb.ObjectPoolServiceClient, *grpc.ClientConn) { connOpts := []grpc.DialOption{ grpc.WithInsecure(), - grpc.WithContextDialer(func(ctx context.Context, addr string) (conn net.Conn, err error) { - d := net.Dialer{} - return d.DialContext(ctx, "unix", addr) - }), + grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(config.Config.Auth.Token)), } conn, err := grpc.Dial(serverSocketPath, connOpts...) diff --git a/internal/service/operations/branches_test.go b/internal/service/operations/branches_test.go index 5d6081bf0c9744de274283baeff434fa6d6efeb1..55f6eb1e08b2723fd90498fda7c98a9292f5688e 100644 --- a/internal/service/operations/branches_test.go +++ b/internal/service/operations/branches_test.go @@ -19,8 +19,8 @@ func TestSuccessfulUserCreateBranchRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -84,8 +84,8 @@ func TestSuccessfulGitHooksForUserCreateBranchRequest(t *testing.T) { cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) defer cleanupSrv() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -125,8 +125,8 @@ func TestFailedUserCreateBranchDueToHooks(t *testing.T) { cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) defer cleanupSrv() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -156,8 +156,8 @@ func TestFailedUserCreateBranchDueToHooks(t *testing.T) { } func TestFailedUserCreateBranchRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -231,8 +231,8 @@ func TestSuccessfulUserDeleteBranchRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -269,8 +269,8 @@ func TestSuccessfulGitHooksForUserDeleteBranchRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -314,8 +314,8 @@ func TestSuccessfulGitHooksForUserDeleteBranchRequest(t *testing.T) { } func TestFailedUserDeleteBranchDueToValidation(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -376,8 +376,8 @@ func TestFailedUserDeleteBranchDueToHooks(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/operations/merge_test.go b/internal/service/operations/merge_test.go index 2074a5abf2369c5adcfafdb6c3b567b1424d4714..df1ecff38e1663fd9f1004a04fc0d7b108172f01 100644 --- a/internal/service/operations/merge_test.go +++ b/internal/service/operations/merge_test.go @@ -35,8 +35,8 @@ func TestSuccessfulMerge(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -111,8 +111,8 @@ func TestAbortedMerge(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -182,8 +182,8 @@ func TestFailedMergeConcurrentUpdate(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -226,8 +226,8 @@ func TestFailedMergeDueToHooks(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -288,8 +288,8 @@ func TestSuccessfulUserFFBranchRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -327,8 +327,8 @@ func TestSuccessfulUserFFBranchRequest(t *testing.T) { } func TestFailedUserFFBranchRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -422,8 +422,8 @@ func TestFailedUserFFBranchRequest(t *testing.T) { } func TestFailedUserFFBranchDueToHooks(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -465,8 +465,8 @@ func TestFailedUserFFBranchDueToHooks(t *testing.T) { } func TestSuccessfulUserMergeToRefRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -574,8 +574,8 @@ func TestSuccessfulUserMergeToRefRequest(t *testing.T) { } func TestFailedUserMergeToRefRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -676,8 +676,8 @@ func TestFailedUserMergeToRefRequest(t *testing.T) { } func TestUserMergeToRefIgnoreHooksRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/operations/squash_test.go b/internal/service/operations/squash_test.go index 7115f79778479f39b1ef3c3d79652aad3b3294de..aec3954ba684b2493817798a3c22b8496532cea6 100644 --- a/internal/service/operations/squash_test.go +++ b/internal/service/operations/squash_test.go @@ -30,8 +30,8 @@ func TestSuccessfulUserSquashRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -80,8 +80,8 @@ func TestSuccessfulUserSquashRequestWith3wayMerge(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -128,8 +128,8 @@ func TestSplitIndex(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -159,8 +159,8 @@ func TestSquashRequestWithRenamedFiles(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -221,8 +221,8 @@ func TestSuccessfulUserSquashRequestWithMissingFileOnTargetBranch(t *testing.T) ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -248,8 +248,8 @@ func TestSuccessfulUserSquashRequestWithMissingFileOnTargetBranch(t *testing.T) } func TestFailedUserSquashRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/operations/submodules_test.go b/internal/service/operations/submodules_test.go index 71e81b13f4ee5845e62be459a8a94364321f202d..a37e0c9250da646357f61ec95e8fe8876ae1832a 100644 --- a/internal/service/operations/submodules_test.go +++ b/internal/service/operations/submodules_test.go @@ -14,8 +14,8 @@ import ( ) func TestSuccessfulUserUpdateSubmoduleRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -84,8 +84,8 @@ func TestSuccessfulUserUpdateSubmoduleRequest(t *testing.T) { } func TestFailedUserUpdateSubmoduleRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -212,8 +212,8 @@ func TestFailedUserUpdateSubmoduleRequestDueToInvalidBranch(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -239,8 +239,8 @@ func TestFailedUserUpdateSubmoduleRequestDueToInvalidSubmodule(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -266,8 +266,8 @@ func TestFailedUserUpdateSubmoduleRequestDueToSameReference(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() @@ -299,8 +299,8 @@ func TestFailedUserUpdateSubmoduleRequestDueToRepositoryEmpty(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := NewOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/operations/tags_test.go b/internal/service/operations/tags_test.go index 4273d7ccf325196f4275759554ecd3433c536fbe..ec5c3e5ed23d21b97c720a362eb1a0d7ef500553 100644 --- a/internal/service/operations/tags_test.go +++ b/internal/service/operations/tags_test.go @@ -16,8 +16,8 @@ func TestSuccessfulUserDeleteTagRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -54,8 +54,8 @@ func TestSuccessfulUserDeleteTagRequest(t *testing.T) { } func TestSuccessfulGitHooksForUserDeleteTagRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -105,8 +105,8 @@ func TestSuccessfulUserCreateTagRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -188,8 +188,8 @@ func TestSuccessfulUserCreateTagRequest(t *testing.T) { } func TestSuccessfulGitHooksForUserCreateTagRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -236,8 +236,8 @@ func TestSuccessfulGitHooksForUserCreateTagRequest(t *testing.T) { } func TestFailedUserDeleteTagRequestDueToValidation(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -295,8 +295,8 @@ func TestFailedUserDeleteTagRequestDueToValidation(t *testing.T) { } func TestFailedUserDeleteTagDueToHooks(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -345,8 +345,8 @@ func TestFailedUserDeleteTagDueToHooks(t *testing.T) { } func TestFailedUserCreateTagDueToHooks(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -388,8 +388,8 @@ func TestFailedUserCreateTagDueToHooks(t *testing.T) { } func TestFailedUserCreateTagRequestDueToTagExistence(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -432,8 +432,8 @@ func TestFailedUserCreateTagRequestDueToTagExistence(t *testing.T) { } func TestFailedUserCreateTagRequestDueToValidation(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/operations/testhelper_test.go b/internal/service/operations/testhelper_test.go index 38e461f5272c323163dbf9afc58013417d5e9a9c..81e5093461636422bbac45f6b953147d72762248 100644 --- a/internal/service/operations/testhelper_test.go +++ b/internal/service/operations/testhelper_test.go @@ -3,7 +3,6 @@ package operations import ( "fmt" "io/ioutil" - "net" "os" "path/filepath" "testing" @@ -67,21 +66,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runOperationServiceServer(t *testing.T) (*grpc.Server, string) { - grpcServer := testhelper.NewTestGrpcServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() +func runOperationServiceServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterOperationServiceServer(grpcServer, &server{ruby: RubyServer}) - reflection.Register(grpcServer) + gitalypb.RegisterOperationServiceServer(srv.GrpcServer(), &server{ruby: RubyServer}) + reflection.Register(srv.GrpcServer()) - go grpcServer.Serve(listener) + require.NoError(t, srv.Start()) - return grpcServer, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newOperationClient(t *testing.T, serverSocketPath string) (gitalypb.OperationServiceClient, *grpc.ClientConn) { diff --git a/internal/service/operations/update_branches_test.go b/internal/service/operations/update_branches_test.go index d332b9a1a41e2800c040182ba3860cf1716fc9f7..1b7aafdb4c671128276b14f51e7998101e893b5e 100644 --- a/internal/service/operations/update_branches_test.go +++ b/internal/service/operations/update_branches_test.go @@ -29,8 +29,8 @@ func TestSuccessfulUserUpdateBranchRequest(t *testing.T) { cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) defer cleanupSrv() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -55,8 +55,8 @@ func TestSuccessfulUserUpdateBranchRequest(t *testing.T) { } func TestSuccessfulGitHooksForUserUpdateBranchRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -100,8 +100,8 @@ func TestFailedUserUpdateBranchDueToHooks(t *testing.T) { cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) defer cleanupSrv() - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() @@ -133,8 +133,8 @@ func TestFailedUserUpdateBranchDueToHooks(t *testing.T) { } func TestFailedUserUpdateBranchRequest(t *testing.T) { - server, serverSocketPath := runOperationServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runOperationServiceServer(t) + defer stop() client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/branches_test.go b/internal/service/ref/branches_test.go index b0f6147af8e0342fcba20d643237a11599ed99e9..354cbdf14eb55815956c5be4c26cc766c4af74bb 100644 --- a/internal/service/ref/branches_test.go +++ b/internal/service/ref/branches_test.go @@ -15,8 +15,8 @@ func TestSuccessfulFindBranchRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -78,8 +78,8 @@ func TestSuccessfulFindBranchRequest(t *testing.T) { } func TestFailedFindBranchRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/delete_refs_test.go b/internal/service/ref/delete_refs_test.go index 3c4faab6f7affd9e6d75c8d319e0996091d5a7c2..73a88f7fc3634ba9e10f0b3ac610a97482735568 100644 --- a/internal/service/ref/delete_refs_test.go +++ b/internal/service/ref/delete_refs_test.go @@ -11,8 +11,8 @@ import ( ) func TestSuccessfulDeleteRefs(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -64,8 +64,8 @@ func TestSuccessfulDeleteRefs(t *testing.T) { } func TestFailedDeleteRefsRequestDueToGitError(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -88,8 +88,8 @@ func TestFailedDeleteRefsRequestDueToGitError(t *testing.T) { } func TestFailedDeleteRefsDueToValidation(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/list_new_blobs_test.go b/internal/service/ref/list_new_blobs_test.go index 568053862d5a2d932a628a3300d9df40040c2834..6e16e8c5dac694408880a114a3c0da8811b00bd5 100644 --- a/internal/service/ref/list_new_blobs_test.go +++ b/internal/service/ref/list_new_blobs_test.go @@ -15,8 +15,8 @@ func TestListNewBlobs(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/list_new_commits_test.go b/internal/service/ref/list_new_commits_test.go index f0b5cdffccd9d5cad55d5c808a580e12e4b0c83e..a75ed8c689ad32fca9fef571dc1cce850c60a020 100644 --- a/internal/service/ref/list_new_commits_test.go +++ b/internal/service/ref/list_new_commits_test.go @@ -15,8 +15,8 @@ func TestListNewCommits(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/pack_refs_test.go b/internal/service/ref/pack_refs_test.go index ad479415e3f748dd1212a6933ab5e951f033cd89..6eff598fe52950ad4c8fe831889b497ebc9e66a9 100644 --- a/internal/service/ref/pack_refs_test.go +++ b/internal/service/ref/pack_refs_test.go @@ -19,8 +19,8 @@ func TestPackRefsSuccessfulRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/refexists_test.go b/internal/service/ref/refexists_test.go index 2998461f82cdcddb21375a089103d865f34ee794..557699fda802bd49727dd9aebb7002ba712962f2 100644 --- a/internal/service/ref/refexists_test.go +++ b/internal/service/ref/refexists_test.go @@ -41,8 +41,8 @@ func TestRefExists(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/refname_test.go b/internal/service/ref/refname_test.go index cdc5a61a95736871786c6a49b30b4739737e028f..d69df4b9d6b5cbc3a8370bf50e88f57729b50bc8 100644 --- a/internal/service/ref/refname_test.go +++ b/internal/service/ref/refname_test.go @@ -13,8 +13,8 @@ import ( ) func TestFindRefNameSuccess(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -43,8 +43,8 @@ func TestFindRefNameSuccess(t *testing.T) { } func TestFindRefNameEmptyCommit(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -75,8 +75,8 @@ func TestFindRefNameEmptyCommit(t *testing.T) { } func TestFindRefNameInvalidRepo(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -104,8 +104,8 @@ func TestFindRefNameInvalidRepo(t *testing.T) { } func TestFindRefNameInvalidPrefix(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -131,8 +131,8 @@ func TestFindRefNameInvalidPrefix(t *testing.T) { } func TestFindRefNameInvalidObject(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/refs_test.go b/internal/service/ref/refs_test.go index ae14a2054a5fb175ba0830447c45740dd77b16a8..c71efba880bce329895aa6ee9c5166a4ce8af2eb 100644 --- a/internal/service/ref/refs_test.go +++ b/internal/service/ref/refs_test.go @@ -32,8 +32,8 @@ func containsRef(refs [][]byte, ref string) bool { } func TestSuccessfulFindAllBranchNames(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -68,8 +68,8 @@ func TestSuccessfulFindAllBranchNames(t *testing.T) { } func TestFindAllBranchNamesVeryLargeResponse(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -124,8 +124,8 @@ func TestFindAllBranchNamesVeryLargeResponse(t *testing.T) { } func TestEmptyFindAllBranchNamesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -149,8 +149,8 @@ func TestEmptyFindAllBranchNamesRequest(t *testing.T) { } func TestInvalidRepoFindAllBranchNamesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -175,8 +175,8 @@ func TestInvalidRepoFindAllBranchNamesRequest(t *testing.T) { } func TestSuccessfulFindAllTagNames(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -213,8 +213,8 @@ func TestSuccessfulFindAllTagNames(t *testing.T) { } func TestEmptyFindAllTagNamesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -238,8 +238,8 @@ func TestEmptyFindAllTagNamesRequest(t *testing.T) { } func TestInvalidRepoFindAllTagNamesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -374,8 +374,8 @@ func TestDefaultBranchName(t *testing.T) { } func TestSuccessfulFindDefaultBranchName(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -398,8 +398,8 @@ func TestSuccessfulFindDefaultBranchName(t *testing.T) { } func TestEmptyFindDefaultBranchNameRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -415,8 +415,8 @@ func TestEmptyFindDefaultBranchNameRequest(t *testing.T) { } func TestInvalidRepoFindDefaultBranchNameRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -433,8 +433,8 @@ func TestInvalidRepoFindDefaultBranchNameRequest(t *testing.T) { } func TestSuccessfulFindAllTagsRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() testRepoCopy, testRepoCopyPath, cleanupFn := testhelper.NewTestRepoWithWorktree(t) defer cleanupFn() @@ -671,8 +671,8 @@ func TestSuccessfulFindAllTagsRequest(t *testing.T) { } func TestFindAllTagNestedTags(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() testRepoCopy, testRepoCopyPath, cleanupFn := testhelper.NewTestRepoWithWorktree(t) defer cleanupFn() @@ -779,8 +779,8 @@ func TestFindAllTagNestedTags(t *testing.T) { } func TestInvalidFindAllTagsRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -823,8 +823,8 @@ func TestInvalidFindAllTagsRequest(t *testing.T) { } func TestSuccessfulFindLocalBranches(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -916,8 +916,8 @@ func TestFindLocalBranchesSort(t *testing.T) { }, } - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -958,8 +958,8 @@ func TestFindLocalBranchesSort(t *testing.T) { } func TestEmptyFindLocalBranchesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -983,8 +983,8 @@ func TestEmptyFindLocalBranchesRequest(t *testing.T) { } func TestSuccessfulFindAllBranchesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() remoteBranch := &gitalypb.FindAllBranchesResponse_Branch{ Name: []byte("refs/remotes/origin/fake-remote-branch"), @@ -1042,8 +1042,8 @@ func TestSuccessfulFindAllBranchesRequest(t *testing.T) { } func TestSuccessfulFindAllBranchesRequestWithMergedBranches(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() @@ -1135,8 +1135,8 @@ func TestSuccessfulFindAllBranchesRequestWithMergedBranches(t *testing.T) { } func TestInvalidFindAllBranchesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -1193,8 +1193,8 @@ func readFindAllBranchesResponsesFromClient(t *testing.T, c gitalypb.RefService_ } func TestListTagNamesContainingCommit(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -1270,8 +1270,8 @@ func TestListTagNamesContainingCommit(t *testing.T) { } func TestListBranchNamesContainingCommit(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -1364,8 +1364,8 @@ func TestListBranchNamesContainingCommit(t *testing.T) { } func TestSuccessfulFindTagRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() testRepoCopy, testRepoCopyPath, cleanupFn := testhelper.NewTestRepoWithWorktree(t) defer cleanupFn() @@ -1591,8 +1591,8 @@ func TestSuccessfulFindTagRequest(t *testing.T) { } func TestFindTagNestedTag(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() testRepoCopy, testRepoCopyPath, cleanupFn := testhelper.NewTestRepoWithWorktree(t) defer cleanupFn() @@ -1680,8 +1680,8 @@ func TestFindTagNestedTag(t *testing.T) { } func TestInvalidFindTagRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/remote_branches_test.go b/internal/service/ref/remote_branches_test.go index 2fd0c78ab932f38ae212c603c311711293e4b64e..f62a5f5bb873f3fb632cfd90f0933c2594c56c58 100644 --- a/internal/service/ref/remote_branches_test.go +++ b/internal/service/ref/remote_branches_test.go @@ -16,8 +16,8 @@ func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -79,8 +79,8 @@ func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) { } func TestInvalidFindAllRemoteBranchesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/tag_messages_test.go b/internal/service/ref/tag_messages_test.go index fbd8ebac0df9c6f98b65f5577ab56615835e0996..8c512c912a8ab925bbb25993693f7edc82a983d0 100644 --- a/internal/service/ref/tag_messages_test.go +++ b/internal/service/ref/tag_messages_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulGetTagMessagesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() @@ -55,8 +55,8 @@ func TestSuccessfulGetTagMessagesRequest(t *testing.T) { } func TestFailedGetTagMessagesRequest(t *testing.T) { - server, serverSocketPath := runRefServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRefServiceServer(t) + defer stop() client, conn := newRefServiceClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/ref/testhelper_test.go b/internal/service/ref/testhelper_test.go index 2faeb3d0eb4e12b7e322ba3dcffb99ddc461bd31..0dd3b901897f3fb08f07fad8a9bfd213218d05c6 100644 --- a/internal/service/ref/testhelper_test.go +++ b/internal/service/ref/testhelper_test.go @@ -2,7 +2,6 @@ package ref import ( "bytes" - "net" "os" "testing" @@ -92,21 +91,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runRefServiceServer(t *testing.T) (*grpc.Server, string) { - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - grpcServer := testhelper.NewTestGrpcServer(t, nil, nil) +func runRefServiceServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterRefServiceServer(grpcServer, &server{}) - reflection.Register(grpcServer) + gitalypb.RegisterRefServiceServer(srv.GrpcServer(), &server{}) + reflection.Register(srv.GrpcServer()) - go grpcServer.Serve(listener) + require.NoError(t, srv.Start()) - return grpcServer, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newRefServiceClient(t *testing.T, serverSocketPath string) (gitalypb.RefServiceClient, *grpc.ClientConn) { diff --git a/internal/service/remote/find_remote_root_ref_test.go b/internal/service/remote/find_remote_root_ref_test.go index a8c418abb42ba2bd48a8f3a9f0a726ffd72a6ca8..9d0560174e9e75b754af943b84a8ca521a9ef93c 100644 --- a/internal/service/remote/find_remote_root_ref_test.go +++ b/internal/service/remote/find_remote_root_ref_test.go @@ -10,8 +10,8 @@ import ( ) func TestFindRemoteRootRefSuccess(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -29,8 +29,8 @@ func TestFindRemoteRootRefSuccess(t *testing.T) { } func TestFindRemoteRootRefFailedDueToValidation(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -77,8 +77,8 @@ func TestFindRemoteRootRefFailedDueToValidation(t *testing.T) { } func TestFindRemoteRootRefFailedDueToInvalidRemote(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/remote/remotes_test.go b/internal/service/remote/remotes_test.go index 763be1211e4c47186adf6b36d8e3887f738b89c7..f9bd7330ff88810d8d7e3e1601389150fa3f3fea 100644 --- a/internal/service/remote/remotes_test.go +++ b/internal/service/remote/remotes_test.go @@ -16,8 +16,8 @@ import ( ) func TestSuccessfulAddRemote(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -100,8 +100,8 @@ func TestSuccessfulAddRemote(t *testing.T) { } func TestFailedAddRemoteDueToValidation(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -147,8 +147,8 @@ func TestFailedAddRemoteDueToValidation(t *testing.T) { } func TestSuccessfulRemoveRemote(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -197,8 +197,8 @@ func TestSuccessfulRemoveRemote(t *testing.T) { } func TestFailedRemoveRemoteDueToValidation(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -216,8 +216,8 @@ func TestFailedRemoveRemoteDueToValidation(t *testing.T) { } func TestFindRemoteRepository(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -239,8 +239,8 @@ func TestFindRemoteRepository(t *testing.T) { } func TestFailedFindRemoteRepository(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -272,8 +272,8 @@ func TestFailedFindRemoteRepository(t *testing.T) { } func TestListDifferentPushUrlRemote(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -319,8 +319,8 @@ func TestListDifferentPushUrlRemote(t *testing.T) { } func TestListRemotes(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/remote/testhelper_test.go b/internal/service/remote/testhelper_test.go index 481be3f97a173e69bfc32f1a963dc1ed5bfb5227..4db3f58c699696d98b0306c3e0730c112c20d1ba 100644 --- a/internal/service/remote/testhelper_test.go +++ b/internal/service/remote/testhelper_test.go @@ -2,10 +2,10 @@ package remote import ( "log" - "net" "os" "testing" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -32,21 +32,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runRemoteServiceServer(t *testing.T) (*grpc.Server, string) { - grpcServer := testhelper.NewTestGrpcServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() +func runRemoteServiceServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterRemoteServiceServer(grpcServer, &server{ruby: RubyServer}) - reflection.Register(grpcServer) + gitalypb.RegisterRemoteServiceServer(srv.GrpcServer(), &server{ruby: RubyServer}) + reflection.Register(srv.GrpcServer()) - go grpcServer.Serve(listener) + require.NoError(t, srv.Start()) - return grpcServer, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func NewRemoteClient(t *testing.T, serverSocketPath string) (gitalypb.RemoteServiceClient, *grpc.ClientConn) { diff --git a/internal/service/remote/update_remote_mirror_test.go b/internal/service/remote/update_remote_mirror_test.go index 87e51eb8cdb9165e01b07a5bc0dc0379fa0aeac0..a9e39e15335a7aeaf8d25fe333c76c1fa06b0d81 100644 --- a/internal/service/remote/update_remote_mirror_test.go +++ b/internal/service/remote/update_remote_mirror_test.go @@ -12,8 +12,8 @@ import ( ) func TestSuccessfulUpdateRemoteMirrorRequest(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -96,8 +96,8 @@ func TestSuccessfulUpdateRemoteMirrorRequest(t *testing.T) { } func TestSuccessfulUpdateRemoteMirrorRequestWithWildcards(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -173,8 +173,8 @@ func TestSuccessfulUpdateRemoteMirrorRequestWithWildcards(t *testing.T) { } func TestSuccessfulUpdateRemoteMirrorRequestWithKeepDivergentRefs(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() @@ -229,8 +229,8 @@ func TestSuccessfulUpdateRemoteMirrorRequestWithKeepDivergentRefs(t *testing.T) } func TestFailedUpdateRemoteMirrorRequestDueToValidation(t *testing.T) { - server, serverSocketPath := runRemoteServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runRemoteServiceServer(t) + defer stop() client, conn := NewRemoteClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/apply_gitattributes_test.go b/internal/service/repository/apply_gitattributes_test.go index 0dd242b92f999ab1b422028893b32d6a035a1020..10fc714cd6b5a825407f8108839cc804e0343362 100644 --- a/internal/service/repository/apply_gitattributes_test.go +++ b/internal/service/repository/apply_gitattributes_test.go @@ -14,8 +14,8 @@ import ( ) func TestApplyGitattributesSuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -66,8 +66,8 @@ func TestApplyGitattributesSuccess(t *testing.T) { } func TestApplyGitattributesFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/archive_test.go b/internal/service/repository/archive_test.go index 5e129b8f9088ab3bb3cc1a7f67d8ae57890a02b7..f7c370b0e7adf2ab88cfbbba7822682226bb4778 100644 --- a/internal/service/repository/archive_test.go +++ b/internal/service/repository/archive_test.go @@ -17,8 +17,8 @@ import ( ) func TestGetArchiveSuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -121,8 +121,8 @@ func TestGetArchiveSuccess(t *testing.T) { } func TestGetArchiveFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -236,8 +236,8 @@ func TestGetArchiveFailure(t *testing.T) { } func TestGetArchivePathInjection(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/backup_custom_hooks_test.go b/internal/service/repository/backup_custom_hooks_test.go index abd3df248dab814c4a6c4a60f0c308bc1851a968..1d5f2a5889effb2f91059eee726dec6bcd00faac 100644 --- a/internal/service/repository/backup_custom_hooks_test.go +++ b/internal/service/repository/backup_custom_hooks_test.go @@ -18,8 +18,8 @@ import ( ) func TestSuccessfullBackupCustomHooksRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -67,8 +67,8 @@ func TestSuccessfullBackupCustomHooksRequest(t *testing.T) { } func TestSuccessfullBackupCustomHooksSymlink(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -106,8 +106,8 @@ func TestSuccessfullBackupCustomHooksSymlink(t *testing.T) { } func TestSuccessfullBackupCustomHooksRequestWithNoHooks(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/calculate_checksum_test.go b/internal/service/repository/calculate_checksum_test.go index b29d245dc7c4095f970457b72a1758bd7e819717..386376ff1475862a54df2ec0f524f72d6f527e11 100644 --- a/internal/service/repository/calculate_checksum_test.go +++ b/internal/service/repository/calculate_checksum_test.go @@ -13,8 +13,8 @@ import ( ) func TestSuccessfulCalculateChecksum(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -70,8 +70,8 @@ func TestRefWhitelist(t *testing.T) { } func TestEmptyRepositoryCalculateChecksum(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -89,8 +89,8 @@ func TestEmptyRepositoryCalculateChecksum(t *testing.T) { } func TestBrokenRepositoryCalculateChecksum(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -110,8 +110,8 @@ func TestBrokenRepositoryCalculateChecksum(t *testing.T) { } func TestFailedCalculateChecksum(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -145,8 +145,8 @@ func TestFailedCalculateChecksum(t *testing.T) { } func TestInvalidRefsCalculateChecksum(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/cleanup_test.go b/internal/service/repository/cleanup_test.go index bb0f2e4e106b82be723d4ecea82def49bceeee8c..f30ac32b585d48fb3aae48372840e5b1b196aa03 100644 --- a/internal/service/repository/cleanup_test.go +++ b/internal/service/repository/cleanup_test.go @@ -15,8 +15,8 @@ import ( ) func TestCleanupDeletesRefsLocks(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -58,8 +58,8 @@ func TestCleanupDeletesRefsLocks(t *testing.T) { } func TestCleanupDeletesPackedRefsLock(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -127,8 +127,8 @@ func TestCleanupDeletesPackedRefsLock(t *testing.T) { // TODO: replace emulated rebase RPC with actual // https://gitlab.com/gitlab-org/gitaly/issues/1750 func TestCleanupDeletesStaleWorktrees(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -199,8 +199,8 @@ func TestCleanupDisconnectedWorktrees(t *testing.T) { worktreeAdminDir = "worktrees" ) - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -258,8 +258,8 @@ func TestCleanupDisconnectedWorktrees(t *testing.T) { } func TestCleanupFileLocks(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/config_test.go b/internal/service/repository/config_test.go index de7f099e9e6e9c0f9d9ef8635b65f494ff6874cf..1726aee821f6e6f35a378e4e1a03bb14e72c4f4a 100644 --- a/internal/service/repository/config_test.go +++ b/internal/service/repository/config_test.go @@ -14,8 +14,8 @@ import ( ) func TestDeleteConfig(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -74,8 +74,8 @@ func TestDeleteConfig(t *testing.T) { } func TestSetConfig(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/create_bundle_test.go b/internal/service/repository/create_bundle_test.go index c70373121fbdab49d10761f8e373a7c88031d9a2..256782b2840a3aa27c3484eee1fd24a380fe0052 100644 --- a/internal/service/repository/create_bundle_test.go +++ b/internal/service/repository/create_bundle_test.go @@ -15,8 +15,8 @@ import ( ) func TestSuccessfulCreateBundleRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -53,8 +53,8 @@ func TestSuccessfulCreateBundleRequest(t *testing.T) { } func TestFailedCreateBundleRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/create_from_bundle_test.go b/internal/service/repository/create_from_bundle_test.go index 161fc1c9b02b075d87ce91925e30e8290871fa61..0bdd591431e199e007936beb388b21e609deb520 100644 --- a/internal/service/repository/create_from_bundle_test.go +++ b/internal/service/repository/create_from_bundle_test.go @@ -18,8 +18,8 @@ import ( ) func TestSuccessfulCreateRepositoryFromBundleRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -85,8 +85,8 @@ func TestSuccessfulCreateRepositoryFromBundleRequest(t *testing.T) { } func TestFailedCreateRepositoryFromBundleRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/create_from_snapshot_test.go b/internal/service/repository/create_from_snapshot_test.go index 2abc1aad501bd2c72c7afef40a932d9c09d7a976..cf53d3eb01c13c6f91d6be076b3fd424a5820983 100644 --- a/internal/service/repository/create_from_snapshot_test.go +++ b/internal/service/repository/create_from_snapshot_test.go @@ -55,8 +55,8 @@ func generateTarFile(t *testing.T, path string) ([]byte, []string) { } func createFromSnapshot(t *testing.T, req *gitalypb.CreateRepositoryFromSnapshotRequest) (*gitalypb.CreateRepositoryFromSnapshotResponse, error) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/create_from_url_test.go b/internal/service/repository/create_from_url_test.go index d3f45f672ab599e477b11dc5a3ab903417e5137b..d660d7a21f447dabd0ad8c04bc82b18794860b6e 100644 --- a/internal/service/repository/create_from_url_test.go +++ b/internal/service/repository/create_from_url_test.go @@ -18,8 +18,8 @@ import ( ) func TestSuccessfulCreateRepositoryFromURLRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -86,8 +86,8 @@ func TestCloneRepositoryFromUrlCommand(t *testing.T) { } func TestFailedCreateRepositoryFromURLRequestDueToExistingTarget(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -141,8 +141,8 @@ func TestFailedCreateRepositoryFromURLRequestDueToExistingTarget(t *testing.T) { } func TestPreventingRedirect(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/create_test.go b/internal/service/repository/create_test.go index dcfc09de2e953b52084c1a641c8948f3014518a6..d0c4f319d6cf809832b63bff789ed576883eea46 100644 --- a/internal/service/repository/create_test.go +++ b/internal/service/repository/create_test.go @@ -16,8 +16,8 @@ import ( ) func TestCreateRepositorySuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -48,8 +48,8 @@ func TestCreateRepositorySuccess(t *testing.T) { } func TestCreateRepositoryFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -73,8 +73,8 @@ func TestCreateRepositoryFailure(t *testing.T) { } func TestCreateRepositoryFailureInvalidArgs(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -103,8 +103,8 @@ func TestCreateRepositoryFailureInvalidArgs(t *testing.T) { } func TestCreateRepositoryIdempotent(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/fetch_remote_test.go b/internal/service/repository/fetch_remote_test.go index 5f6cce28de72d7c8dc38af6aec62ee8e3bff46cd..2c75eb5c1746e1f6a0bdd0e3e56f996622b60982 100644 --- a/internal/service/repository/fetch_remote_test.go +++ b/internal/service/repository/fetch_remote_test.go @@ -44,8 +44,8 @@ func TestFetchRemoteSuccess(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, _ := newRepositoryClient(t, serverSocketPath) @@ -74,13 +74,11 @@ func TestFetchRemoteFailure(t *testing.T) { desc string req *gitalypb.FetchRemoteRequest code codes.Code - err string }{ { desc: "invalid storage", req: &gitalypb.FetchRemoteRequest{Repository: &gitalypb.Repository{StorageName: "invalid", RelativePath: "foobar.git"}}, code: codes.InvalidArgument, - err: "Storage can not be found by name 'invalid'", }, } @@ -91,7 +89,6 @@ func TestFetchRemoteFailure(t *testing.T) { resp, err := server.FetchRemote(ctx, tc.req) testhelper.RequireGrpcError(t, err, tc.code) - require.Contains(t, err.Error(), tc.err) assert.Nil(t, resp) }) } @@ -130,8 +127,8 @@ func getRefnames(t *testing.T, repoPath string) []string { } func TestFetchRemoteOverHTTP(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -186,8 +183,8 @@ func TestFetchRemoteOverHTTP(t *testing.T) { } func TestFetchRemoteOverHTTPWithRedirect(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -217,8 +214,8 @@ func TestFetchRemoteOverHTTPWithRedirect(t *testing.T) { } func TestFetchRemoteOverHTTPError(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/fsck_test.go b/internal/service/repository/fsck_test.go index 84057ec9b2a9a5398baf3370dfa0ac07b63f8518..f8739f9079d7f35c7e12f55833daad6d558fc8df 100644 --- a/internal/service/repository/fsck_test.go +++ b/internal/service/repository/fsck_test.go @@ -16,8 +16,8 @@ func TestFsckSuccess(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -35,8 +35,8 @@ func TestFsckFailureSeverelyBrokenRepo(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -61,8 +61,8 @@ func TestFsckFailureSlightlyBrokenRepo(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/gc_test.go b/internal/service/repository/gc_test.go index 9c50e8e84ac18977ddc1c9e88d7c46c826d8e09f..fb86a891732faae77f19ca0a5d93204aa3fd97f6 100644 --- a/internal/service/repository/gc_test.go +++ b/internal/service/repository/gc_test.go @@ -30,8 +30,8 @@ var ( ) func TestGarbageCollectCommitGraph(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -66,8 +66,8 @@ func TestGarbageCollectCommitGraph(t *testing.T) { } func TestGarbageCollectSuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -137,8 +137,8 @@ func TestGarbageCollectLogStatistics(t *testing.T) { defer cancel() ctx = ctxlogrus.ToContext(ctx, log.WithField("test", "logging")) - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -153,8 +153,8 @@ func TestGarbageCollectLogStatistics(t *testing.T) { } func TestGarbageCollectDeletesRefsLocks(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -201,8 +201,8 @@ func TestGarbageCollectDeletesRefsLocks(t *testing.T) { } func TestGarbageCollectFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -231,8 +231,8 @@ func TestGarbageCollectFailure(t *testing.T) { } func TestCleanupInvalidKeepAroundRefs(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -328,8 +328,8 @@ func createFileWithTimes(path string, mTime time.Time) { } func TestGarbageCollectDeltaIslands(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/info_attributes_test.go b/internal/service/repository/info_attributes_test.go index cc867ed6881d77a409c257102e2c4184f606d750..ef8be18d9a5af7b12451534e0287ab23179ece95 100644 --- a/internal/service/repository/info_attributes_test.go +++ b/internal/service/repository/info_attributes_test.go @@ -14,8 +14,8 @@ import ( ) func TestGetInfoAttributesExisting(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -49,8 +49,8 @@ func TestGetInfoAttributesExisting(t *testing.T) { } func TestGetInfoAttributesNonExisting(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/license_test.go b/internal/service/repository/license_test.go index 93d1c6d6a5d50b369e44c26ee49c04da33958b35..65a3888e3d6e1c1e9c14e866d1e2a962c51a9cb7 100644 --- a/internal/service/repository/license_test.go +++ b/internal/service/repository/license_test.go @@ -11,8 +11,8 @@ import ( ) func TestSuccessfulFindLicenseRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -32,8 +32,8 @@ func TestSuccessfulFindLicenseRequest(t *testing.T) { } func TestFindLicenseRequestEmptyRepo(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/merge_base_test.go b/internal/service/repository/merge_base_test.go index 765479520c8bbbb879a7a812bb5393c18ca9c834..3ad0b41ae5ae9d36417bbadecdfef861f37987e9 100644 --- a/internal/service/repository/merge_base_test.go +++ b/internal/service/repository/merge_base_test.go @@ -10,8 +10,8 @@ import ( ) func TestSuccessfulFindFindMergeBaseRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -86,8 +86,8 @@ func TestSuccessfulFindFindMergeBaseRequest(t *testing.T) { } func TestFailedFindMergeBaseRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/raw_changes_test.go b/internal/service/repository/raw_changes_test.go index 2aafe4f4d4c24cb18d58c080672b8439412537f4..9cec23f15075b12e34df805e414f1644946f9a88 100644 --- a/internal/service/repository/raw_changes_test.go +++ b/internal/service/repository/raw_changes_test.go @@ -13,8 +13,8 @@ import ( ) func TestGetRawChanges(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -130,8 +130,8 @@ func TestGetRawChangesSpecialCharacters(t *testing.T) { // This test looks for a specific path known to contain special // characters. - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -174,8 +174,8 @@ func collectChanges(t *testing.T, stream gitalypb.RepositoryService_GetRawChange } func TestGetRawChangesFailures(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -236,8 +236,8 @@ func TestGetRawChangesFailures(t *testing.T) { } func TestGetRawChangesManyFiles(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -264,8 +264,8 @@ func TestGetRawChangesManyFiles(t *testing.T) { } func TestGetRawChangesMappingOperations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -315,8 +315,8 @@ func TestGetRawChangesMappingOperations(t *testing.T) { } func TestGetRawChangesInvalidUTF8Paths(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/rebase_in_progress_test.go b/internal/service/repository/rebase_in_progress_test.go index cddc063d4ee78454fc121d0374a11a1d2f6da33b..9a412f9da66ba26d8e25c86847b01f511807dc5b 100644 --- a/internal/service/repository/rebase_in_progress_test.go +++ b/internal/service/repository/rebase_in_progress_test.go @@ -14,8 +14,8 @@ import ( ) func TestSuccessfulIsRebaseInProgressRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -94,8 +94,8 @@ func TestSuccessfulIsRebaseInProgressRequest(t *testing.T) { } func TestFailedIsRebaseInProgressRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/remove_test.go b/internal/service/repository/remove_test.go index 652bccbe51d2eae94cfe620f812c69c2f2152389..a4aa218691248aa455844e5755b77fddfd6b6d8e 100644 --- a/internal/service/repository/remove_test.go +++ b/internal/service/repository/remove_test.go @@ -9,8 +9,8 @@ import ( ) func TestRemoveRepository(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -28,8 +28,8 @@ func TestRemoveRepository(t *testing.T) { } func TestRemoveRepositoryDoesNotExist(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/rename_test.go b/internal/service/repository/rename_test.go index 70c8c8ea791b76fc39a00935f50ab3967a10bcd3..8b4c98a9d6877697fd79054c9facdc294c2b0c09 100644 --- a/internal/service/repository/rename_test.go +++ b/internal/service/repository/rename_test.go @@ -12,8 +12,8 @@ import ( ) func TestRenameRepositorySuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -45,8 +45,8 @@ func TestRenameRepositorySuccess(t *testing.T) { } func TestRenameRepositoryDestinationExists(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -72,8 +72,8 @@ func TestRenameRepositoryDestinationExists(t *testing.T) { } func TestRenameRepositoryInvalidRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/repack_test.go b/internal/service/repository/repack_test.go index 166b963a4da3085baaff47020a0823de075c31cf..6b2784d69e0ec1453a5551765c02381618840454 100644 --- a/internal/service/repository/repack_test.go +++ b/internal/service/repository/repack_test.go @@ -23,8 +23,8 @@ import ( ) func TestRepackIncrementalSuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -63,8 +63,8 @@ func TestRepackIncrementalCollectLogStatistics(t *testing.T) { defer cancel() ctx = ctxlogrus.ToContext(ctx, log.WithField("test", "logging")) - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -79,8 +79,8 @@ func TestRepackIncrementalCollectLogStatistics(t *testing.T) { } func TestRepackLocal(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -118,8 +118,8 @@ func TestRepackLocal(t *testing.T) { } func TestRepackIncrementalFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -146,8 +146,8 @@ func TestRepackIncrementalFailure(t *testing.T) { } func TestRepackFullSuccess(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -212,8 +212,8 @@ func TestRepackFullCollectLogStatistics(t *testing.T) { defer cancel() ctx = ctxlogrus.ToContext(ctx, log.WithField("test", "logging")) - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -254,8 +254,8 @@ func doBitmapsContainHashCache(t *testing.T, bitmapPaths []string) { } func TestRepackFullFailure(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -282,8 +282,8 @@ func TestRepackFullFailure(t *testing.T) { } func TestRepackFullDeltaIslands(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/replicate_test.go b/internal/service/repository/replicate_test.go index 3e8b5a6f779443de1436e6591e0a2764d182ba3c..c9beb69ae1b303fba4b738ea878585b5a31f4da6 100644 --- a/internal/service/repository/replicate_test.go +++ b/internal/service/repository/replicate_test.go @@ -179,8 +179,8 @@ func TestReplicateRepositoryInvalidArguments(t *testing.T) { }, } - server, serverSocketPath := repository.RunRepoServer(t) - defer server.Stop() + stop, serverSocketPath := repository.RunRepoServer(t) + defer stop() client, conn := repository.NewRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/repository_test.go b/internal/service/repository/repository_test.go index ea6e91763dcb2b84c3434f1950b17aad1ff03324..97fbec499541f71b5fcd786860ba7bb4bcb16e26 100644 --- a/internal/service/repository/repository_test.go +++ b/internal/service/repository/repository_test.go @@ -16,8 +16,8 @@ import ( ) func TestRepositoryExists(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t, testhelper.WithStorages([]string{"default", "other", "broken"})) + defer stop() storageOtherDir, err := ioutil.TempDir("", "gitaly-repository-exists-test") require.NoError(t, err, "tempdir") @@ -122,8 +122,8 @@ func TestRepositoryExists(t *testing.T) { } func TestSuccessfulHasLocalBranches(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -177,8 +177,8 @@ func TestSuccessfulHasLocalBranches(t *testing.T) { } func TestFailedHasLocalBranches(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/restore_custom_hooks_test.go b/internal/service/repository/restore_custom_hooks_test.go index ebfc4c1644ffe901d4085ed451d14e16c9c57ed7..496d5dfbe812307eff20baaf5b5dfca6f817febb 100644 --- a/internal/service/repository/restore_custom_hooks_test.go +++ b/internal/service/repository/restore_custom_hooks_test.go @@ -15,8 +15,8 @@ import ( ) func TestSuccessfullRestoreCustomHooksRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -59,8 +59,8 @@ func TestSuccessfullRestoreCustomHooksRequest(t *testing.T) { } func TestFailedRestoreCustomHooksDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -78,8 +78,8 @@ func TestFailedRestoreCustomHooksDueToValidations(t *testing.T) { } func TestFailedRestoreCustomHooksDueToBadTar(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/search_files_test.go b/internal/service/repository/search_files_test.go index 3aa5204d2e8c5dbb9942b4ea94025a8811d40a90..c88dbe6b568cc63415f7309737f03d257aa9cb0d 100644 --- a/internal/service/repository/search_files_test.go +++ b/internal/service/repository/search_files_test.go @@ -80,8 +80,8 @@ func TestSearchFilesByContentSuccessful(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -152,8 +152,8 @@ func TestSearchFilesByContentLargeFile(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -220,25 +220,21 @@ func TestSearchFilesByContentFailure(t *testing.T) { query string ref string code codes.Code - msg string }{ { desc: "empty request", code: codes.InvalidArgument, - msg: "no query given", }, { desc: "only query given", query: "foo", code: codes.InvalidArgument, - msg: "no ref given", }, { desc: "no repo", query: "foo", ref: "master", code: codes.InvalidArgument, - msg: "empty Repo", }, { desc: "invalid ref argument", @@ -246,7 +242,6 @@ func TestSearchFilesByContentFailure(t *testing.T) { query: ".", ref: "--no-index", code: codes.InvalidArgument, - msg: "invalid ref argument", }, } @@ -259,7 +254,6 @@ func TestSearchFilesByContentFailure(t *testing.T) { }, nil) testhelper.RequireGrpcError(t, err, tc.code) - require.Contains(t, err.Error(), tc.msg) }) } } @@ -268,8 +262,8 @@ func TestSearchFilesByNameSuccessful(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -318,8 +312,8 @@ func TestSearchFilesByNameFailure(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -330,25 +324,21 @@ func TestSearchFilesByNameFailure(t *testing.T) { query string ref string code codes.Code - msg string }{ { desc: "empty request", code: codes.InvalidArgument, - msg: "no query given", }, { desc: "only query given", query: "foo", code: codes.InvalidArgument, - msg: "no ref given", }, { desc: "no repo", query: "foo", ref: "master", code: codes.InvalidArgument, - msg: "empty Repo", }, } @@ -363,7 +353,6 @@ func TestSearchFilesByNameFailure(t *testing.T) { _, err = consumeFilenameByName(stream) testhelper.RequireGrpcError(t, err, tc.code) - require.Contains(t, err.Error(), tc.msg) }) } } diff --git a/internal/service/repository/size_test.go b/internal/service/repository/size_test.go index 9643a43c8522c670391bc7313f7d02bfe02b8ba9..b7caaf463109d117a1a98f0b85c05148f0287d1b 100644 --- a/internal/service/repository/size_test.go +++ b/internal/service/repository/size_test.go @@ -15,8 +15,8 @@ import ( const testRepoMinSizeKB = 10000 func TestSuccessfulRepositorySizeRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -37,8 +37,8 @@ func TestSuccessfulRepositorySizeRequest(t *testing.T) { } func TestFailedRepositorySizeRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -67,8 +67,8 @@ func TestFailedRepositorySizeRequest(t *testing.T) { } func TestSuccessfulGetObjectDirectorySizeRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/snapshot_test.go b/internal/service/repository/snapshot_test.go index 446efcc7e1a6fd5746cd82eb83171c98a6d9e92d..5dba549abccedc29222ab65675f3e96e94230ed3 100644 --- a/internal/service/repository/snapshot_test.go +++ b/internal/service/repository/snapshot_test.go @@ -25,8 +25,8 @@ import ( ) func getSnapshot(t *testing.T, req *gitalypb.GetSnapshotRequest) ([]byte, error) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/squash_in_progress_test.go b/internal/service/repository/squash_in_progress_test.go index 9dffb44be34705d1d6d0153df1607eeaf641bffc..23238fa9b6685c7523ef9333144e78d555148525 100644 --- a/internal/service/repository/squash_in_progress_test.go +++ b/internal/service/repository/squash_in_progress_test.go @@ -11,8 +11,8 @@ import ( ) func TestSuccessfulIsSquashInProgressRequest(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -62,8 +62,8 @@ func TestSuccessfulIsSquashInProgressRequest(t *testing.T) { } func TestFailedIsSquashInProgressRequestDueToValidations(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/repository/testhelper_test.go b/internal/service/repository/testhelper_test.go index d377ceac299fb48a726b4cbdab39ee36a917659b..2f7cf6718a23da98098f4dff458076dc8738be27 100644 --- a/internal/service/repository/testhelper_test.go +++ b/internal/service/repository/testhelper_test.go @@ -3,13 +3,13 @@ package repository import ( "crypto/x509" "log" - "net" "os" "path/filepath" "testing" "time" "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" @@ -17,7 +17,6 @@ import ( mcache "gitlab.com/gitlab-org/gitaly/internal/middleware/cache" "gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry" "gitlab.com/gitlab-org/gitaly/internal/rubyserver" - "gitlab.com/gitlab-org/gitaly/internal/server/auth" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" @@ -37,8 +36,9 @@ var ( func newRepositoryClient(t *testing.T, serverSocketPath string) (gitalypb.RepositoryServiceClient, *grpc.ClientConn) { connOpts := []grpc.DialOption{ grpc.WithInsecure(), - grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(testhelper.RepositoryAuthToken)), + grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(config.Config.Auth.Token)), } + conn, err := grpc.Dial(serverSocketPath, connOpts...) if err != nil { t.Fatal(err) @@ -53,7 +53,7 @@ var RunRepoServer = runRepoServer func newSecureRepoClient(t *testing.T, serverSocketPath string, pool *x509.CertPool) (gitalypb.RepositoryServiceClient, *grpc.ClientConn) { connOpts := []grpc.DialOption{ grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(pool, "")), - grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(testhelper.RepositoryAuthToken)), + grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(config.Config.Auth.Token)), } conn, err := client.Dial(serverSocketPath, connOpts) @@ -66,35 +66,27 @@ func newSecureRepoClient(t *testing.T, serverSocketPath string, pool *x509.CertP var NewSecureRepoClient = newSecureRepoClient -func runRepoServer(t *testing.T) (*grpc.Server, string) { +func runRepoServer(t *testing.T, opts ...testhelper.TestServerOpt) (func(), string) { streamInt := []grpc.StreamServerInterceptor{ - auth.StreamServerInterceptor(config.Config.Auth), mcache.StreamInvalidator(dcache.LeaseKeyer{}, protoregistry.GitalyProtoPreregistered), } unaryInt := []grpc.UnaryServerInterceptor{ - auth.UnaryServerInterceptor(config.Config.Auth), mcache.UnaryInvalidator(dcache.LeaseKeyer{}, protoregistry.GitalyProtoPreregistered), } - server := testhelper.NewTestGrpcServer(t, streamInt, unaryInt) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } + srv := testhelper.NewServerWithAuth(t, streamInt, unaryInt, config.Config.Auth.Token, opts...) - gitalypb.RegisterRepositoryServiceServer(server, NewServer(RubyServer, config.GitalyInternalSocketPath())) - reflection.Register(server) + gitalypb.RegisterRepositoryServiceServer(srv.GrpcServer(), NewServer(RubyServer, config.GitalyInternalSocketPath())) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func TestRepoNoAuth(t *testing.T) { - srv, path := runRepoServer(t) - defer srv.Stop() + stop, path := runRepoServer(t) + defer stop() connOpts := []grpc.DialOption{ grpc.WithInsecure(), diff --git a/internal/service/repository/write_ref_test.go b/internal/service/repository/write_ref_test.go index 905515a7ecddd548b23f9893fa85f86064e8aea2..df240b8d6c8567790edd25e2a702a94039e9f533 100644 --- a/internal/service/repository/write_ref_test.go +++ b/internal/service/repository/write_ref_test.go @@ -12,8 +12,8 @@ import ( ) func TestWriteRefSuccessful(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -78,8 +78,8 @@ func TestWriteRefSuccessful(t *testing.T) { } func TestWriteRefValidationError(t *testing.T) { - server, serverSocketPath := runRepoServer(t) - defer server.Stop() + stop, serverSocketPath := runRepoServer(t) + defer stop() client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/smarthttp/inforefs_test.go b/internal/service/smarthttp/inforefs_test.go index 9cbb1fcaabcc4dc7b6a4cc9c933c67af1bf6208c..0e0c4efe12d8261223088dd71be5a4c44da0e678 100644 --- a/internal/service/smarthttp/inforefs_test.go +++ b/internal/service/smarthttp/inforefs_test.go @@ -26,8 +26,8 @@ import ( ) func TestSuccessfulInfoRefsUploadPack(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() @@ -43,8 +43,8 @@ func TestSuccessfulInfoRefsUploadPack(t *testing.T) { } func TestSuccessfulInfoRefsUploadPackWithGitConfigOptions(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() @@ -65,8 +65,8 @@ func TestSuccessfulInfoRefsUploadPackWithGitProtocol(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() @@ -116,8 +116,8 @@ func makeInfoRefsUploadPackRequest(ctx context.Context, t *testing.T, serverSock } func TestSuccessfulInfoRefsReceivePack(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, serverSocketPath) defer conn.Close() @@ -149,8 +149,8 @@ func TestSuccessfulInfoRefsReceivePack(t *testing.T) { } func TestObjectPoolRefAdvertisementHiding(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, serverSocketPath) defer conn.Close() @@ -186,8 +186,8 @@ func TestObjectPoolRefAdvertisementHiding(t *testing.T) { } func TestFailureRepoNotFoundInfoRefsReceivePack(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, serverSocketPath) defer conn.Close() @@ -208,8 +208,8 @@ func TestFailureRepoNotFoundInfoRefsReceivePack(t *testing.T) { } func TestFailureRepoNotSetInfoRefsReceivePack(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, serverSocketPath) defer conn.Close() @@ -250,8 +250,8 @@ func assertGitRefAdvertisement(t *testing.T, rpc, responseBody string, firstLine func TestCacheInfoRefsUploadPack(t *testing.T) { clearCache(t) - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() diff --git a/internal/service/smarthttp/receive_pack_test.go b/internal/service/smarthttp/receive_pack_test.go index f1074ef063ac276e615cf240e87136b3d5080fea..1eb49876bb6c14917c094cd7319425fb276e2183 100644 --- a/internal/service/smarthttp/receive_pack_test.go +++ b/internal/service/smarthttp/receive_pack_test.go @@ -31,8 +31,8 @@ func TestSuccessfulReceivePackRequest(t *testing.T) { hookOutputFile, cleanup := testhelper.CaptureHookEnv(t) defer cleanup() - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -73,8 +73,8 @@ func TestSuccessfulReceivePackRequestWithGitProtocol(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -102,8 +102,8 @@ func TestSuccessfulReceivePackRequestWithGitProtocol(t *testing.T) { } func TestFailedReceivePackRequestWithGitOpts(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() repo, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -138,8 +138,8 @@ func TestFailedReceivePackRequestDueToHooksFailure(t *testing.T) { hookContent := []byte("#!/bin/sh\nexit 1") ioutil.WriteFile(path.Join(hooks.Path(), "pre-receive"), hookContent, 0755) - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() repo, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -244,8 +244,8 @@ func createCommit(t *testing.T, repoPath string, fileContents []byte) (oldHead s } func TestFailedReceivePackRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, serverSocketPath) defer conn.Close() @@ -278,8 +278,8 @@ func TestPostReceivePackToHooks(t *testing.T) { glRepository := "some_repo" glID := "key-123" - server, socket := runSmartHTTPServer(t) - defer server.Stop() + stop, socket := runSmartHTTPServer(t) + defer stop() client, conn := newSmartHTTPClient(t, "unix://"+socket) defer conn.Close() diff --git a/internal/service/smarthttp/testhelper_test.go b/internal/service/smarthttp/testhelper_test.go index ed5eff8d34d3218006d8e3e5155caa7dde2bdfe1..aa300b59fd1d4e85523569f783a7d9c03bcdde34 100644 --- a/internal/service/smarthttp/testhelper_test.go +++ b/internal/service/smarthttp/testhelper_test.go @@ -1,10 +1,10 @@ package smarthttp import ( - "net" "os" "testing" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -28,21 +28,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runSmartHTTPServer(t *testing.T) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) +func runSmartHTTPServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterSmartHTTPServiceServer(server, NewServer()) - reflection.Register(server) + gitalypb.RegisterSmartHTTPServiceServer(srv.GrpcServer(), NewServer()) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newSmartHTTPClient(t *testing.T, serverSocketPath string) (gitalypb.SmartHTTPServiceClient, *grpc.ClientConn) { diff --git a/internal/service/smarthttp/upload_pack_test.go b/internal/service/smarthttp/upload_pack_test.go index 808ace220a0eb3c6e6501106f236b0b67d2ab268..6a32d4f589aaa16c4462aab21144ae5e1c1e402b 100644 --- a/internal/service/smarthttp/upload_pack_test.go +++ b/internal/service/smarthttp/upload_pack_test.go @@ -29,8 +29,8 @@ const ( ) func TestSuccessfulUploadPackRequest(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() ctx, cancel := testhelper.Context() defer cancel() @@ -95,8 +95,8 @@ func TestSuccessfulUploadPackRequest(t *testing.T) { } func TestUploadPackRequestWithGitConfigOptions(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() ctx, cancel := testhelper.Context() defer cancel() @@ -167,8 +167,8 @@ func TestUploadPackRequestWithGitProtocol(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() ctx, cancel := testhelper.Context() defer cancel() @@ -213,8 +213,8 @@ func TestUploadPackRequestWithGitProtocol(t *testing.T) { // on 'deepen' requests even though the request is being handled just // fine from the client perspective. func TestSuccessfulUploadPackDeepenRequest(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() ctx, cancel := testhelper.Context() defer cancel() @@ -235,8 +235,8 @@ func TestSuccessfulUploadPackDeepenRequest(t *testing.T) { } func TestFailedUploadPackRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() rpcRequests := []gitalypb.PostUploadPackRequest{ {Repository: &gitalypb.Repository{StorageName: "fake", RelativePath: "path"}}, // Repository doesn't exist @@ -324,8 +324,8 @@ func extractPackDataFromResponse(t *testing.T, buf *bytes.Buffer) ([]byte, int, } func TestUploadPackRequestForPartialCloneSuccess(t *testing.T) { - server, serverSocketPath := runSmartHTTPServer(t) - defer server.Stop() + stop, serverSocketPath := runSmartHTTPServer(t) + defer stop() testRepo := testhelper.TestRepository() testRepoPath, err := helper.GetRepoPath(testRepo) diff --git a/internal/service/ssh/receive_pack_test.go b/internal/service/ssh/receive_pack_test.go index 7e4ad6fb329efd33616b440a66dad8e0818ac934..dc1784cd357ca6aa8e781395265f04b8831185ab 100644 --- a/internal/service/ssh/receive_pack_test.go +++ b/internal/service/ssh/receive_pack_test.go @@ -27,8 +27,8 @@ import ( ) func TestFailedReceivePackRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -84,8 +84,8 @@ func TestReceivePackPushSuccess(t *testing.T) { hookOutputFile, cleanup := testhelper.CaptureHookEnv(t) defer cleanup() - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() glRepository := "project-456" @@ -112,8 +112,8 @@ func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() lHead, rHead, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testRepo.GetStorageName(), glID: "1", gitProtocol: git.ProtocolV2}) if err != nil { @@ -129,8 +129,8 @@ func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { } func TestReceivePackPushFailure(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() _, _, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: "foobar", glID: "1"}) require.Error(t, err, "local and remote head equal. push did not fail") @@ -140,8 +140,8 @@ func TestReceivePackPushFailure(t *testing.T) { } func TestReceivePackPushHookFailure(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() hookDir, err := ioutil.TempDir("", "gitaly-tmp-hooks") require.NoError(t, err) @@ -161,8 +161,8 @@ func TestReceivePackPushHookFailure(t *testing.T) { } func TestObjectPoolRefAdvertisementHidingSSH(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -213,8 +213,8 @@ func TestSSHReceivePackToHooks(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() tempGitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) defer cleanup() diff --git a/internal/service/ssh/testhelper_test.go b/internal/service/ssh/testhelper_test.go index e368ba877b88805917187be6ab066ed6ad55b420..4cb52617997a4e362685b1bf30c3e7a16ae98a37 100644 --- a/internal/service/ssh/testhelper_test.go +++ b/internal/service/ssh/testhelper_test.go @@ -1,12 +1,12 @@ package ssh import ( - "net" "os" "path" "testing" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/testhelper" @@ -58,21 +58,15 @@ func mustGetCwd() string { return wd } -func runSSHServer(t *testing.T, serverOpts ...ServerOpt) (*grpc.Server, string) { - server := testhelper.NewTestGrpcServer(t, nil, nil) +func runSSHServer(t *testing.T, serverOpts ...ServerOpt) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterSSHServiceServer(server, NewServer(serverOpts...)) - reflection.Register(server) + gitalypb.RegisterSSHServiceServer(srv.GrpcServer(), NewServer(serverOpts...)) + reflection.Register(srv.GrpcServer()) - go server.Serve(listener) + require.NoError(t, srv.Start()) - return server, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newSSHClient(t *testing.T, serverSocketPath string) (gitalypb.SSHServiceClient, *grpc.ClientConn) { diff --git a/internal/service/ssh/upload_archive_test.go b/internal/service/ssh/upload_archive_test.go index 7a512bc3f8f78668e7a6513010fbcbf4bc8c6342..0df7581724f232fef2749f644290d092399e53f0 100644 --- a/internal/service/ssh/upload_archive_test.go +++ b/internal/service/ssh/upload_archive_test.go @@ -16,8 +16,8 @@ import ( ) func TestFailedUploadArchiveRequestDueToTimeout(t *testing.T) { - server, serverSocketPath := runSSHServer(t, WithArchiveRequestTimeout(100*time.Microsecond)) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t, WithArchiveRequestTimeout(100*time.Microsecond)) + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -50,8 +50,8 @@ func TestFailedUploadArchiveRequestDueToTimeout(t *testing.T) { } func TestFailedUploadArchiveRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -99,8 +99,8 @@ func TestFailedUploadArchiveRequestDueToValidationError(t *testing.T) { } func TestUploadArchiveSuccess(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() cmd := exec.Command("git", "archive", "master", "--remote=git@localhost:test/test.git") diff --git a/internal/service/ssh/upload_pack_test.go b/internal/service/ssh/upload_pack_test.go index d3936461dae7636d1263da07068de44f43fef8d0..67e5b366a5f1a18bc009195ada9d701e511d2074 100644 --- a/internal/service/ssh/upload_pack_test.go +++ b/internal/service/ssh/upload_pack_test.go @@ -21,9 +21,9 @@ import ( ) func TestFailedUploadPackRequestDueToTimeout(t *testing.T) { - server, serverSocketPath := runSSHServer(t, WithUploadPackRequestTimeout(10*time.Microsecond)) + stop, serverSocketPath := runSSHServer(t, WithUploadPackRequestTimeout(10*time.Microsecond)) - defer server.Stop() + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -77,8 +77,8 @@ func requireFailedSSHStream(t *testing.T, recv func() (int32, error)) { } func TestFailedUploadPackRequestDueToValidationError(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() @@ -126,8 +126,8 @@ func TestFailedUploadPackRequestDueToValidationError(t *testing.T) { } func TestUploadPackCloneSuccess(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() localRepoPath := path.Join(testRepoRoot, "gitlab-test-upload-pack-local") @@ -158,8 +158,8 @@ func TestUploadPackCloneSuccessWithGitProtocol(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support() defer restore() - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() localRepoPath := path.Join(testRepoRoot, "gitlab-test-upload-pack-local") @@ -192,8 +192,8 @@ func TestUploadPackCloneSuccessWithGitProtocol(t *testing.T) { } func TestUploadPackCloneHideTags(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() localRepoPath := path.Join(testRepoRoot, "gitlab-test-upload-pack-local-hide-tags") @@ -213,8 +213,8 @@ func TestUploadPackCloneHideTags(t *testing.T) { } func TestUploadPackCloneFailure(t *testing.T) { - server, serverSocketPath := runSSHServer(t) - defer server.Stop() + stop, serverSocketPath := runSSHServer(t) + defer stop() localRepoPath := path.Join(testRepoRoot, "gitlab-test-upload-pack-local-failure") diff --git a/internal/service/wiki/delete_page_test.go b/internal/service/wiki/delete_page_test.go index 0ffac0eb0d575151825fa046ff32a8e02b73a20e..428ba4b7df08a3d500511838567031c4788dc5f4 100644 --- a/internal/service/wiki/delete_page_test.go +++ b/internal/service/wiki/delete_page_test.go @@ -17,8 +17,8 @@ func TestSuccessfulWikiDeletePageRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -85,8 +85,8 @@ func TestFailedWikiDeletePageDueToValidations(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/find_file_test.go b/internal/service/wiki/find_file_test.go index 9908686a0a1f5e707862d985e14fb1d816722cfb..1ff13871fd4f72bc773b9da4fba1f66618a22698 100644 --- a/internal/service/wiki/find_file_test.go +++ b/internal/service/wiki/find_file_test.go @@ -18,8 +18,8 @@ func TestSuccessfulWikiFindFileRequest(t *testing.T) { _, wikiRepoPath, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -156,8 +156,8 @@ func TestFailedWikiFindFileDueToValidation(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/find_page_test.go b/internal/service/wiki/find_page_test.go index fd7e262f6028ce44e69cff3e3c1570da62cdf26b..4d5ab7249fcd0ad0d09ebbf0d54209943a55d38c 100644 --- a/internal/service/wiki/find_page_test.go +++ b/internal/service/wiki/find_page_test.go @@ -14,8 +14,8 @@ func TestSuccessfulWikiFindPageRequest(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -190,8 +190,8 @@ func TestSuccessfulWikiFindPageSameTitleDifferentPathRequest(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -306,8 +306,8 @@ func TestFailedWikiFindPageDueToValidation(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -372,8 +372,8 @@ func readFullWikiPageFromWikiFindPageClient(t *testing.T, c gitalypb.WikiService } func TestInvalidWikiFindPageRequestRevision(t *testing.T) { - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/get_all_pages_test.go b/internal/service/wiki/get_all_pages_test.go index c14acbd46b6d3bbaee9045ebb0fcc3fa399f1398..7952915c2ab1174df535e1a188335a7fdb182cd7 100644 --- a/internal/service/wiki/get_all_pages_test.go +++ b/internal/service/wiki/get_all_pages_test.go @@ -14,8 +14,8 @@ func TestSuccessfulWikiGetAllPagesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -69,8 +69,8 @@ func TestWikiGetAllPagesSorting(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -177,8 +177,8 @@ func TestWikiGetAllPagesSorting(t *testing.T) { } func TestFailedWikiGetAllPagesDueToValidation(t *testing.T) { - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/get_page_versions_test.go b/internal/service/wiki/get_page_versions_test.go index 6456146d3bece44d74f295a8c14cfc90658e69ea..09a8ad15a5e7b2b3dafcd49dfcdb347334f9eaa0 100644 --- a/internal/service/wiki/get_page_versions_test.go +++ b/internal/service/wiki/get_page_versions_test.go @@ -19,8 +19,8 @@ func TestWikiGetPageVersionsRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -109,8 +109,8 @@ func TestWikiGetPageVersionsPaginationParams(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/list_pages_test.go b/internal/service/wiki/list_pages_test.go index 4bb9d699509bff5a37feef8facfda3e2c3c770ef..b2e0f164b644b62469921917096aaab70d5e0b8f 100644 --- a/internal/service/wiki/list_pages_test.go +++ b/internal/service/wiki/list_pages_test.go @@ -13,8 +13,8 @@ func TestSuccessfulWikiListPagesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -70,8 +70,8 @@ func TestWikiListPagesSorting(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/testhelper_test.go b/internal/service/wiki/testhelper_test.go index 5de3ac199ea2bb1187b433f2fa3ecb17fd95ccb8..efad5506380983c20a8c24e2747c47cc4aa19ec0 100644 --- a/internal/service/wiki/testhelper_test.go +++ b/internal/service/wiki/testhelper_test.go @@ -3,7 +3,6 @@ package wiki import ( "bytes" "io/ioutil" - "net" "os" "path" "strings" @@ -58,21 +57,15 @@ func testMain(m *testing.M) int { return m.Run() } -func runWikiServiceServer(t *testing.T) (*grpc.Server, string) { - grpcServer := testhelper.NewTestGrpcServer(t, nil, nil) - serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() +func runWikiServiceServer(t *testing.T) (func(), string) { + srv := testhelper.NewServer(t, nil, nil) - listener, err := net.Listen("unix", serverSocketPath) - if err != nil { - t.Fatal(err) - } - - gitalypb.RegisterWikiServiceServer(grpcServer, &server{ruby: rubyServer}) - reflection.Register(grpcServer) + gitalypb.RegisterWikiServiceServer(srv.GrpcServer(), &server{ruby: rubyServer}) + reflection.Register(srv.GrpcServer()) - go grpcServer.Serve(listener) + require.NoError(t, srv.Start()) - return grpcServer, "unix://" + serverSocketPath + return srv.Stop, "unix://" + srv.Socket() } func newWikiClient(t *testing.T, serverSocketPath string) (gitalypb.WikiServiceClient, *grpc.ClientConn) { diff --git a/internal/service/wiki/update_page_test.go b/internal/service/wiki/update_page_test.go index f4bbbbc78229969693d16cdd3ae8c37fec2a46c6..094628826ee0925a26355f0d8bb3c1139dd5e5ef 100644 --- a/internal/service/wiki/update_page_test.go +++ b/internal/service/wiki/update_page_test.go @@ -18,8 +18,8 @@ func TestSuccessfulWikiUpdatePageRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -114,8 +114,8 @@ func TestFailedWikiUpdatePageDueToValidations(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/service/wiki/write_page_test.go b/internal/service/wiki/write_page_test.go index 53577ebc7e283f99d24e48bd283fd56331534de6..37b1ce86da4bc21e9555e935fe2b5f34086b28e1 100644 --- a/internal/service/wiki/write_page_test.go +++ b/internal/service/wiki/write_page_test.go @@ -18,8 +18,8 @@ func TestSuccessfulWikiWritePageRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -117,8 +117,8 @@ func TestFailedWikiWritePageDueToDuplicatePage(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -162,8 +162,8 @@ func TestFailedWikiWritePageInPathDueToDuplicatePage(t *testing.T) { wikiRepo, _, cleanupFunc := setupWikiRepo(t) defer cleanupFunc() - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() @@ -206,8 +206,8 @@ func TestFailedWikiWritePageInPathDueToDuplicatePage(t *testing.T) { func TestFailedWikiWritePageDueToValidations(t *testing.T) { wikiRepo := &gitalypb.Repository{} - server, serverSocketPath := runWikiServiceServer(t) - defer server.Stop() + stop, serverSocketPath := runWikiServiceServer(t) + defer stop() client, conn := newWikiClient(t, serverSocketPath) defer conn.Close() diff --git a/internal/testhelper/testserver.go b/internal/testhelper/testserver.go index 6fa2f865fc808d06944b4fbd2ef15ee7bd45f89f..d9104392364fe0621287afef81d15163e1231c29 100644 --- a/internal/testhelper/testserver.go +++ b/internal/testhelper/testserver.go @@ -22,11 +22,14 @@ import ( grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" + gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/internal/config" + "gitlab.com/gitlab-org/gitaly/internal/config/auth" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/helper/fieldextractors" praefectconfig "gitlab.com/gitlab-org/gitaly/internal/praefect/config" "gitlab.com/gitlab-org/gitaly/internal/praefect/models" + serverauth "gitlab.com/gitlab-org/gitaly/internal/server/auth" "google.golang.org/grpc" "google.golang.org/grpc/health" "google.golang.org/grpc/health/grpc_health_v1" @@ -34,11 +37,62 @@ import ( "gopkg.in/yaml.v2" ) +// PraefectEnabled returns whether or not tests should use a praefect proxy +func PraefectEnabled() bool { + _, ok := os.LookupEnv("GITALY_TEST_PRAEFECT_BIN") + return ok +} + +// TestServerOpt is an option for TestServer +type TestServerOpt func(t *TestServer) + +// WithToken is a TestServerOpt that provides a security token +func WithToken(token string) TestServerOpt { + return func(t *TestServer) { + t.token = token + } +} + +// WithStorages is a TestServerOpt that sets the storages for a TestServer +func WithStorages(storages []string) TestServerOpt { + return func(t *TestServer) { + t.storages = storages + } +} + // NewTestServer instantiates a new TestServer -func NewTestServer(srv *grpc.Server) *TestServer { - return &TestServer{ +func NewTestServer(srv *grpc.Server, opts ...TestServerOpt) *TestServer { + ts := &TestServer{ grpcServer: srv, } + + for _, opt := range opts { + opt(ts) + } + + if len(ts.storages) == 0 { + ts.storages = []string{"default"} + } + + return ts +} + +// NewServerWithAuth creates a new test server with authentication +func NewServerWithAuth(tb testing.TB, streamInterceptors []grpc.StreamServerInterceptor, unaryInterceptors []grpc.UnaryServerInterceptor, token string, opts ...TestServerOpt) *TestServer { + if token != "" { + if PraefectEnabled() { + opts = append(opts, WithToken(token)) + } + streamInterceptors = append(streamInterceptors, serverauth.StreamServerInterceptor(auth.Config{Token: token})) + unaryInterceptors = append(unaryInterceptors, serverauth.UnaryServerInterceptor(auth.Config{Token: token})) + } + + return NewServer( + tb, + streamInterceptors, + unaryInterceptors, + opts..., + ) } // TestServer wraps a grpc Server and handles automatically putting a praefect in front of a gitaly instance @@ -47,6 +101,8 @@ type TestServer struct { grpcServer *grpc.Server socket string process *os.Process + token string + storages []string } // GrpcServer returns the underlying grpc.Server @@ -101,18 +157,23 @@ func (p *TestServer) Start() error { c := praefectconfig.Config{ SocketPath: praefectServerSocketPath, - VirtualStorages: []*praefectconfig.VirtualStorage{ - { - Name: "default", - Nodes: []*models.Node{ - { - Storage: "default", - Address: "unix:/" + gitalyServerSocketPath, - DefaultPrimary: true, - }, + Auth: auth.Config{ + Token: p.token, + }, + } + + for _, storage := range p.storages { + c.VirtualStorages = append(c.VirtualStorages, &praefectconfig.VirtualStorage{ + Name: storage, + Nodes: []*models.Node{ + { + Storage: storage, + Address: "unix:/" + gitalyServerSocketPath, + DefaultPrimary: true, + Token: p.token, }, }, - }, + }) } if err := toml.NewEncoder(configFile).Encode(&c); err != nil { @@ -134,7 +195,12 @@ func (p *TestServer) Start() error { } go cmd.Wait() - conn, err := grpc.Dial("unix://"+praefectServerSocketPath, grpc.WithInsecure()) + opts := []grpc.DialOption{grpc.WithInsecure()} + if p.token != "" { + opts = append(opts, grpc.WithPerRPCCredentials(gitalyauth.RPCCredentials(p.token))) + } + + conn, err := grpc.Dial("unix://"+praefectServerSocketPath, opts...) if err != nil { return fmt.Errorf("dial: %v", err) @@ -169,7 +235,7 @@ func waitForPraefectStartup(conn *grpc.ClientConn) error { } // NewServer creates a Server for testing purposes -func NewServer(tb testing.TB, streamInterceptors []grpc.StreamServerInterceptor, unaryInterceptors []grpc.UnaryServerInterceptor) *TestServer { +func NewServer(tb testing.TB, streamInterceptors []grpc.StreamServerInterceptor, unaryInterceptors []grpc.UnaryServerInterceptor, opts ...TestServerOpt) *TestServer { logger := NewTestLogger(tb) logrusEntry := log.NewEntry(logger).WithField("test", tb.Name()) @@ -184,7 +250,9 @@ func NewServer(tb testing.TB, streamInterceptors []grpc.StreamServerInterceptor, grpc.NewServer( grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptors...)), grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)), - )) + ), + opts..., + ) } var changeLineRegex = regexp.MustCompile("^[a-f0-9]{40} [a-f0-9]{40} refs/[^ ]+$")