Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Okstad <pokstad@gitlab.com>2020-10-06 10:42:07 +0300
committerSami Hiltunen <shiltunen@gitlab.com>2020-10-14 12:00:57 +0300
commit99e95ce5fa91d823ac78196040f536f5ec033fe7 (patch)
tree6422da1f3bca34a4dc0726ba38591aba2b2d93b2
parent4a012d837535e128fe32593e3ddbfba541d1a88c (diff)
Remote repo for resolve refish
-rw-r--r--changelogs/unreleased/po-remote-repo-resolve-refish.yml5
-rw-r--r--internal/git/remote.go58
-rw-r--r--internal/git/repository.go8
-rw-r--r--internal/git/repository_test.go128
-rw-r--r--internal/helper/storage.go16
-rw-r--r--internal/helper/storage_test.go16
-rw-r--r--internal/praefect/helper_test.go67
-rw-r--r--internal/testhelper/testserver/gitaly.go89
8 files changed, 269 insertions, 118 deletions
diff --git a/changelogs/unreleased/po-remote-repo-resolve-refish.yml b/changelogs/unreleased/po-remote-repo-resolve-refish.yml
new file mode 100644
index 000000000..003ec9ebd
--- /dev/null
+++ b/changelogs/unreleased/po-remote-repo-resolve-refish.yml
@@ -0,0 +1,5 @@
+---
+title: Remote repository abstraction for resolving refish
+merge_request: 2629
+author:
+type: other
diff --git a/internal/git/remote.go b/internal/git/remote.go
new file mode 100644
index 000000000..2aba6ff49
--- /dev/null
+++ b/internal/git/remote.go
@@ -0,0 +1,58 @@
+package git
+
+import (
+ "context"
+ "fmt"
+
+ "gitlab.com/gitlab-org/gitaly/client"
+ "gitlab.com/gitlab-org/gitaly/internal/helper"
+ "gitlab.com/gitlab-org/gitaly/internal/storage"
+ "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
+)
+
+// remoteRepository represents a Git repository on a different Gitaly storage
+type remoteRepository struct {
+ UnimplementedRepo
+ repo *gitalypb.Repository
+ server storage.ServerInfo
+ pool *client.Pool
+}
+
+// NewRepository creates a new remote Repository from its protobuf representation.
+func NewRemoteRepository(ctx context.Context, repo *gitalypb.Repository, pool *client.Pool) (Repository, error) {
+ server, err := helper.ExtractGitalyServer(ctx, repo.GetStorageName())
+ if err != nil {
+ return nil, fmt.Errorf("remote repository: %w", err)
+ }
+
+ return remoteRepository{
+ repo: repo,
+ server: server,
+ pool: pool,
+ }, nil
+}
+
+// ResolveRefish will dial to the remote repository and attempt to resolve the
+// refish string via the gRPC interface
+func (rr remoteRepository) ResolveRefish(ctx context.Context, ref string) (string, error) {
+ cc, err := rr.pool.Dial(ctx, rr.server.Address, rr.server.Token)
+ if err != nil {
+ return "", err
+ }
+
+ cli := gitalypb.NewCommitServiceClient(cc)
+ resp, err := cli.FindCommit(ctx, &gitalypb.FindCommitRequest{
+ Repository: rr.repo,
+ Revision: []byte(ref),
+ })
+ if err != nil {
+ return "", err
+ }
+
+ oid := resp.GetCommit().GetId()
+ if oid == "" {
+ return "", ErrReferenceNotFound
+ }
+
+ return oid, nil
+}
diff --git a/internal/git/repository.go b/internal/git/repository.go
index 041266fa9..922254334 100644
--- a/internal/git/repository.go
+++ b/internal/git/repository.go
@@ -110,6 +110,14 @@ func (UnimplementedRepo) UpdateRef(ctx context.Context, reference, newrev, oldre
return ErrUnimplemented
}
+func (UnimplementedRepo) WriteBlob(context.Context, string, io.Reader) (string, error) {
+ return "", ErrUnimplemented
+}
+
+func (UnimplementedRepo) CatFile(context.Context, string) ([]byte, error) {
+ return nil, ErrUnimplemented
+}
+
var _ Repository = UnimplementedRepo{} // compile time assertion
// localRepository represents a local Git repository.
diff --git a/internal/git/repository_test.go b/internal/git/repository_test.go
index 3b6a2e3e0..a22372480 100644
--- a/internal/git/repository_test.go
+++ b/internal/git/repository_test.go
@@ -1,7 +1,8 @@
-package git
+package git_test
import (
"errors"
+ "fmt"
"io"
"io/ioutil"
"os"
@@ -11,7 +12,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/client"
+ "gitlab.com/gitlab-org/gitaly/internal/git"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
+ "gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
+ "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver"
)
const (
@@ -19,12 +25,10 @@ const (
NonexistentID = "ba4f184e126b751d1bffad5897f263108befc780"
)
-func TestLocalRepository_ResolveRefish(t *testing.T) {
+func TestRepository_ResolveRefish(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
-
testcases := []struct {
desc string
refish string
@@ -60,18 +64,38 @@ func TestLocalRepository_ResolveRefish(t *testing.T) {
},
}
- for _, tc := range testcases {
- t.Run(tc.desc, func(t *testing.T) {
- oid, err := repo.ResolveRefish(ctx, tc.refish)
+ _, serverSocketPath, cleanup := testserver.RunInternalGitalyServer(t, config.Config.Storages, config.Config.Auth.Token)
+ defer cleanup()
- if tc.expected == "" {
- require.Error(t, err)
- require.Equal(t, err, ErrReferenceNotFound)
- return
- }
+ for _, repo := range []git.Repository{
+ git.NewRepository(testhelper.TestRepository()),
+ func() git.Repository {
+ ctx, err := helper.InjectGitalyServers(ctx, "default", serverSocketPath, config.Config.Auth.Token)
+ require.NoError(t, err)
+ r, err := git.NewRemoteRepository(
+ helper.OutgoingToIncoming(ctx),
+ testhelper.TestRepository(),
+ client.NewPool(),
+ )
require.NoError(t, err)
- require.Equal(t, tc.expected, oid)
+ return r
+ }(),
+ } {
+ t.Run(fmt.Sprintf("%T", repo), func(t *testing.T) {
+ for _, tc := range testcases {
+ t.Run(tc.desc, func(t *testing.T) {
+ oid, err := repo.ResolveRefish(ctx, tc.refish)
+
+ if tc.expected == "" {
+ require.Equal(t, err, git.ErrReferenceNotFound)
+ return
+ }
+
+ require.NoError(t, err)
+ require.Equal(t, tc.expected, oid)
+ })
+ }
})
}
}
@@ -80,7 +104,7 @@ func TestLocalRepository_ContainsRef(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
testcases := []struct {
desc string
@@ -117,32 +141,32 @@ func TestLocalRepository_GetReference(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
testcases := []struct {
desc string
ref string
- expected Reference
+ expected git.Reference
}{
{
desc: "fully qualified master branch",
ref: "refs/heads/master",
- expected: NewReference("refs/heads/master", MasterID),
+ expected: git.NewReference("refs/heads/master", MasterID),
},
{
desc: "unqualified master branch fails",
ref: "master",
- expected: Reference{},
+ expected: git.Reference{},
},
{
desc: "nonexistent branch",
ref: "refs/heads/nonexistent",
- expected: Reference{},
+ expected: git.Reference{},
},
{
desc: "nonexistent branch",
ref: "nonexistent",
- expected: Reference{},
+ expected: git.Reference{},
},
}
@@ -150,7 +174,7 @@ func TestLocalRepository_GetReference(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
ref, err := repo.GetReference(ctx, tc.ref)
if tc.expected.Name == "" {
- require.True(t, errors.Is(err, ErrReferenceNotFound))
+ require.True(t, errors.Is(err, git.ErrReferenceNotFound))
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, ref)
@@ -163,32 +187,32 @@ func TestLocalRepository_GetBranch(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
testcases := []struct {
desc string
ref string
- expected Reference
+ expected git.Reference
}{
{
desc: "fully qualified master branch",
ref: "refs/heads/master",
- expected: NewReference("refs/heads/master", MasterID),
+ expected: git.NewReference("refs/heads/master", MasterID),
},
{
desc: "half-qualified master branch",
ref: "heads/master",
- expected: NewReference("refs/heads/master", MasterID),
+ expected: git.NewReference("refs/heads/master", MasterID),
},
{
desc: "fully qualified master branch",
ref: "master",
- expected: NewReference("refs/heads/master", MasterID),
+ expected: git.NewReference("refs/heads/master", MasterID),
},
{
desc: "nonexistent branch",
ref: "nonexistent",
- expected: Reference{},
+ expected: git.Reference{},
},
}
@@ -196,7 +220,7 @@ func TestLocalRepository_GetBranch(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
ref, err := repo.GetBranch(ctx, tc.ref)
if tc.expected.Name == "" {
- require.True(t, errors.Is(err, ErrReferenceNotFound))
+ require.True(t, errors.Is(err, git.ErrReferenceNotFound))
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, ref)
@@ -209,40 +233,40 @@ func TestLocalRepository_GetReferences(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
testcases := []struct {
desc string
pattern string
- match func(t *testing.T, refs []Reference)
+ match func(t *testing.T, refs []git.Reference)
}{
{
desc: "master branch",
pattern: "refs/heads/master",
- match: func(t *testing.T, refs []Reference) {
- require.Equal(t, []Reference{
- NewReference("refs/heads/master", MasterID),
+ match: func(t *testing.T, refs []git.Reference) {
+ require.Equal(t, []git.Reference{
+ git.NewReference("refs/heads/master", MasterID),
}, refs)
},
},
{
desc: "all references",
pattern: "",
- match: func(t *testing.T, refs []Reference) {
+ match: func(t *testing.T, refs []git.Reference) {
require.Len(t, refs, 94)
},
},
{
desc: "branches",
pattern: "refs/heads/",
- match: func(t *testing.T, refs []Reference) {
+ match: func(t *testing.T, refs []git.Reference) {
require.Len(t, refs, 91)
},
},
{
desc: "branches",
pattern: "refs/heads/nonexistent",
- match: func(t *testing.T, refs []Reference) {
+ match: func(t *testing.T, refs []git.Reference) {
require.Empty(t, refs)
},
},
@@ -275,7 +299,7 @@ crlf binary
lf text
`), os.ModePerm))
- repo := NewRepository(pbRepo)
+ repo := git.NewRepository(pbRepo)
for _, tc := range []struct {
desc string
@@ -288,7 +312,7 @@ lf text
{
desc: "error reading",
input: ReaderFunc(func([]byte) (int, error) { return 0, assert.AnError }),
- error: errorWithStderr(assert.AnError, nil),
+ error: fmt.Errorf("%w, stderr: %q", assert.AnError, []byte{}),
},
{
desc: "successful empty blob",
@@ -336,7 +360,7 @@ func TestLocalRepository_CatFile(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
for _, tc := range []struct {
desc string
@@ -346,8 +370,8 @@ func TestLocalRepository_CatFile(t *testing.T) {
}{
{
desc: "invalid object",
- oid: NullSHA,
- error: InvalidObjectError(NullSHA),
+ oid: git.NullSHA,
+ error: git.InvalidObjectError(git.NullSHA),
},
{
desc: "valid object",
@@ -368,7 +392,7 @@ func TestLocalRepository_GetBranches(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
- repo := NewRepository(testhelper.TestRepository())
+ repo := git.NewRepository(testhelper.TestRepository())
refs, err := repo.GetBranches(ctx)
require.NoError(t, err)
@@ -382,7 +406,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
testRepo, _, cleanup := testhelper.NewTestRepo(t)
defer cleanup()
- otherRef, err := NewRepository(testRepo).GetReference(ctx, "refs/heads/gitaly-test-ref")
+ otherRef, err := git.NewRepository(testRepo).GetReference(ctx, "refs/heads/gitaly-test-ref")
require.NoError(t, err)
testcases := []struct {
@@ -390,14 +414,14 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref string
newrev string
oldrev string
- verify func(t *testing.T, repo Repository, err error)
+ verify func(t *testing.T, repo git.Repository, err error)
}{
{
desc: "successfully update master",
ref: "refs/heads/master",
newrev: otherRef.Target,
oldrev: MasterID,
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.NoError(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/master")
require.NoError(t, err)
@@ -409,7 +433,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "refs/heads/master",
newrev: otherRef.Target,
oldrev: NonexistentID,
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.Error(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/master")
require.NoError(t, err)
@@ -421,7 +445,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "refs/heads/master",
newrev: NonexistentID,
oldrev: MasterID,
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.Error(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/master")
require.NoError(t, err)
@@ -433,7 +457,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "refs/heads/master",
newrev: otherRef.Target,
oldrev: "",
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.NoError(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/master")
require.NoError(t, err)
@@ -445,7 +469,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "master",
newrev: otherRef.Target,
oldrev: MasterID,
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.Error(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/master")
require.NoError(t, err)
@@ -457,7 +481,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "refs/heads/master",
newrev: strings.Repeat("0", 40),
oldrev: MasterID,
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.NoError(t, err)
_, err = repo.GetReference(ctx, "refs/heads/master")
require.Error(t, err)
@@ -468,7 +492,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
ref: "refs/heads/new",
newrev: MasterID,
oldrev: strings.Repeat("0", 40),
- verify: func(t *testing.T, repo Repository, err error) {
+ verify: func(t *testing.T, repo git.Repository, err error) {
require.NoError(t, err)
ref, err := repo.GetReference(ctx, "refs/heads/new")
require.NoError(t, err)
@@ -483,7 +507,7 @@ func TestLocalRepository_UpdateRef(t *testing.T) {
testRepo, _, cleanup := testhelper.NewTestRepo(t)
defer cleanup()
- repo := NewRepository(testRepo)
+ repo := git.NewRepository(testRepo)
err := repo.UpdateRef(ctx, tc.ref, tc.newrev, tc.oldrev)
tc.verify(t, repo, err)
diff --git a/internal/helper/storage.go b/internal/helper/storage.go
index 254e51fa6..1ae7985ab 100644
--- a/internal/helper/storage.go
+++ b/internal/helper/storage.go
@@ -11,11 +11,15 @@ import (
"google.golang.org/grpc/metadata"
)
+// ErrEmptyMetadata indicates that the gRPC metadata was not found in the
+// context
+var ErrEmptyMetadata = errors.New("empty metadata")
+
// ExtractGitalyServers extracts `storage.GitalyServers` from an incoming context.
func ExtractGitalyServers(ctx context.Context) (gitalyServersInfo storage.GitalyServers, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
- return nil, fmt.Errorf("empty metadata")
+ return nil, ErrEmptyMetadata
}
gitalyServersJSONEncoded := md["gitaly-servers"]
@@ -60,6 +64,16 @@ func IncomingToOutgoing(ctx context.Context) context.Context {
return metadata.NewOutgoingContext(ctx, md)
}
+// OutgoingToIncoming creates an incoming context out of an outgoing context with the same storage metadata
+func OutgoingToIncoming(ctx context.Context) context.Context {
+ md, ok := metadata.FromOutgoingContext(ctx)
+ if !ok {
+ return ctx
+ }
+
+ return metadata.NewIncomingContext(ctx, md)
+}
+
// InjectGitalyServers injects gitaly-servers metadata into an outgoing context
func InjectGitalyServers(ctx context.Context, name, address, token string) (context.Context, error) {
gitalyServers := storage.GitalyServers{
diff --git a/internal/helper/storage_test.go b/internal/helper/storage_test.go
index 44e49b218..91d749e9b 100644
--- a/internal/helper/storage_test.go
+++ b/internal/helper/storage_test.go
@@ -97,3 +97,19 @@ func TestInjectGitalyServers(t *testing.T) {
require.Equal(t, []string{"bar"}, md["foo"])
})
}
+
+func TestOutgoingToIncoming(t *testing.T) {
+ ctx := context.Background()
+ ctx, err := InjectGitalyServers(ctx, "a", "b", "c")
+ require.NoError(t, err)
+
+ _, err = ExtractGitalyServer(ctx, "a")
+ require.Equal(t, ErrEmptyMetadata, err,
+ "server should not be found in the incoming context")
+
+ ctx = OutgoingToIncoming(ctx)
+
+ info, err := ExtractGitalyServer(ctx, "a")
+ require.NoError(t, err)
+ require.Equal(t, storage.ServerInfo{Address: "b", Token: "c"}, info)
+}
diff --git a/internal/praefect/helper_test.go b/internal/praefect/helper_test.go
index 499df18d8..960185cf4 100644
--- a/internal/praefect/helper_test.go
+++ b/internal/praefect/helper_test.go
@@ -14,9 +14,6 @@ import (
gconfig "gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
internalauth "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/auth"
"gitlab.com/gitlab-org/gitaly/internal/gitaly/server/auth"
- "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/internalgitaly"
- "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/repository"
- gitalyserver "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/server"
"gitlab.com/gitlab-org/gitaly/internal/log"
"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/internal/praefect/datastore"
@@ -27,7 +24,7 @@ import (
"gitlab.com/gitlab-org/gitaly/internal/praefect/transactions"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/internal/testhelper/promtest"
- "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
+ "gitlab.com/gitlab-org/gitaly/internal/testhelper/testserver"
correlation "gitlab.com/gitlab-org/labkit/correlation/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
@@ -144,7 +141,7 @@ func flattenVirtualStoragesToStoragePath(virtualStorages []*config.VirtualStorag
func withRealGitalyShared(t testing.TB) func([]*config.VirtualStorage) []testhelper.Cleanup {
return func(virtualStorages []*config.VirtualStorage) []testhelper.Cleanup {
gStorages := flattenVirtualStoragesToStoragePath(virtualStorages, testhelper.GitlabTestStoragePath())
- _, backendAddr, cleanupGitaly := runInternalGitalyServer(t, gStorages, virtualStorages[0].Nodes[0].Token)
+ _, backendAddr, cleanupGitaly := testserver.RunInternalGitalyServer(t, gStorages, virtualStorages[0].Nodes[0].Token)
for _, vs := range virtualStorages {
for i, node := range vs.Nodes {
@@ -263,66 +260,6 @@ func runPraefectServer(t testing.TB, conf config.Config, opt buildOptions) (*grp
return cc, prf, cleanup
}
-// partialGitaly is a subset of Gitaly's behavior needed to properly test
-// Praefect
-type partialGitaly interface {
- gitalypb.ServerServiceServer
- gitalypb.RepositoryServiceServer
- gitalypb.InternalGitalyServer
- healthpb.HealthServer
-}
-
-func registerGitalyServices(server *grpc.Server, pg partialGitaly) {
- gitalypb.RegisterServerServiceServer(server, pg)
- gitalypb.RegisterRepositoryServiceServer(server, pg)
- gitalypb.RegisterInternalGitalyServer(server, pg)
- healthpb.RegisterHealthServer(server, pg)
-}
-
-func realGitaly(storages []gconfig.Storage, authToken, internalSocketPath string) partialGitaly {
- return struct {
- gitalypb.ServerServiceServer
- gitalypb.RepositoryServiceServer
- gitalypb.InternalGitalyServer
- healthpb.HealthServer
- }{
- gitalyserver.NewServer(storages),
- repository.NewServer(RubyServer, gconfig.NewLocator(gconfig.Config), internalSocketPath),
- internalgitaly.NewServer(gconfig.Config.Storages),
- health.NewServer(),
- }
-}
-
-func runInternalGitalyServer(t testing.TB, storages []gconfig.Storage, token string) (*grpc.Server, string, func()) {
- streamInt := []grpc.StreamServerInterceptor{auth.StreamServerInterceptor(internalauth.Config{Token: token})}
- unaryInt := []grpc.UnaryServerInterceptor{auth.UnaryServerInterceptor(internalauth.Config{Token: token})}
-
- server := testhelper.NewTestGrpcServer(t, streamInt, unaryInt)
- serverSocketPath := testhelper.GetTemporaryGitalySocketFileName()
-
- listener, err := net.Listen("unix", serverSocketPath)
- require.NoError(t, err)
-
- internalSocketPath := gconfig.GitalyInternalSocketPath()
- internalListener, err := net.Listen("unix", internalSocketPath)
- require.NoError(t, err)
-
- registerGitalyServices(server, realGitaly(storages, token, internalSocketPath))
-
- errQ := make(chan error)
-
- go func() { errQ <- server.Serve(listener) }()
- go func() { errQ <- server.Serve(internalListener) }()
-
- cleanup := func() {
- server.Stop()
- require.NoError(t, <-errQ)
- require.NoError(t, <-errQ)
- }
-
- return server, "unix://" + serverSocketPath, cleanup
-}
-
func mustLoadProtoReg(t testing.TB) *descriptor.FileDescriptorProto {
gz, _ := (*mock.SimpleRequest)(nil).Descriptor()
fd, err := protoregistry.ExtractFileDescriptor(gz)
diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.go
new file mode 100644
index 000000000..be38eba67
--- /dev/null
+++ b/internal/testhelper/testserver/gitaly.go
@@ -0,0 +1,89 @@
+package testserver
+
+import (
+ "net"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
+ internalauth "gitlab.com/gitlab-org/gitaly/internal/gitaly/config/auth"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/server/auth"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/commit"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/internalgitaly"
+ "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/repository"
+ gitalyserver "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/server"
+ "gitlab.com/gitlab-org/gitaly/internal/testhelper"
+ "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/health"
+ healthpb "google.golang.org/grpc/health/grpc_health_v1"
+)
+
+var RubyServer = &rubyserver.Server{}
+
+// PartialGitaly is a subset of Gitaly's behavior
+type PartialGitaly interface {
+ gitalypb.ServerServiceServer
+ gitalypb.RepositoryServiceServer
+ gitalypb.InternalGitalyServer
+ gitalypb.CommitServiceServer
+ healthpb.HealthServer
+}
+
+func registerGitalyServices(server *grpc.Server, pg PartialGitaly) {
+ gitalypb.RegisterServerServiceServer(server, pg)
+ gitalypb.RegisterRepositoryServiceServer(server, pg)
+ gitalypb.RegisterInternalGitalyServer(server, pg)
+ gitalypb.RegisterCommitServiceServer(server, pg)
+ healthpb.RegisterHealthServer(server, pg)
+}
+
+// RealGitaly instantiates an instance of PartialGitaly that uses the real world
+// services. This is intended to be used in integration tests to validate
+// Gitaly services.
+func RealGitaly(storages []config.Storage, authToken, internalSocketPath string) PartialGitaly {
+ return struct {
+ gitalypb.ServerServiceServer
+ gitalypb.RepositoryServiceServer
+ gitalypb.InternalGitalyServer
+ gitalypb.CommitServiceServer
+ healthpb.HealthServer
+ }{
+ gitalyserver.NewServer(storages),
+ repository.NewServer(RubyServer, config.NewLocator(config.Config), internalSocketPath),
+ internalgitaly.NewServer(config.Config.Storages),
+ commit.NewServer(config.NewLocator(config.Config)),
+ health.NewServer(),
+ }
+}
+
+func RunInternalGitalyServer(t testing.TB, storages []config.Storage, token string) (*grpc.Server, string, func()) {
+ streamInt := []grpc.StreamServerInterceptor{auth.StreamServerInterceptor(internalauth.Config{Token: token})}
+ unaryInt := []grpc.UnaryServerInterceptor{auth.UnaryServerInterceptor(internalauth.Config{Token: token})}
+
+ server := testhelper.NewTestGrpcServer(t, streamInt, unaryInt)
+ serverSocketPath := testhelper.GetTemporaryGitalySocketFileName()
+
+ listener, err := net.Listen("unix", serverSocketPath)
+ require.NoError(t, err)
+
+ internalSocketPath := config.GitalyInternalSocketPath()
+ internalListener, err := net.Listen("unix", internalSocketPath)
+ require.NoError(t, err)
+
+ registerGitalyServices(server, RealGitaly(storages, token, internalSocketPath))
+
+ errQ := make(chan error)
+
+ go func() { errQ <- server.Serve(listener) }()
+ go func() { errQ <- server.Serve(internalListener) }()
+
+ cleanup := func() {
+ server.Stop()
+ require.NoError(t, <-errQ)
+ require.NoError(t, <-errQ)
+ }
+
+ return server, "unix://" + serverSocketPath, cleanup
+}