diff options
author | Paul Okstad <pokstad@gitlab.com> | 2020-10-14 11:24:15 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2020-10-14 11:24:15 +0300 |
commit | 4d3b620afbf24c6b9ac16817667a62a8e0144b3e (patch) | |
tree | a387e60090aa7d8359e89344117e32ad3aedc2f4 /internal/testhelper/testserver | |
parent | c0ba6b3b67598563a14ea7be5f538787354d39be (diff) |
Resolve @samihiltunen feedback: Export Gitaly test server code
To support running a Gitaly server for testing purposes, some test code
from Praefect will be exported and moved to a dedicated package. Ideally
this code should live in the `internal/testhelper` package, but that
would create a cyclical dependency due to many service unit tests already
importing that package. This test code instantiates those same services
under test, thus causing the cyclical dependency.
Diffstat (limited to 'internal/testhelper/testserver')
-rw-r--r-- | internal/testhelper/testserver/gitaly.go | 89 |
1 files changed, 89 insertions, 0 deletions
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 +} |