diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2021-02-21 14:14:24 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2021-02-21 14:14:24 +0300 |
commit | 51927cd256cb36426c9414b51397f7c11f8c8c1e (patch) | |
tree | 47d25b59b1f144a7dba8dd1a668ffd0583026cdc | |
parent | a33a5c088e0b87b85e6d198ff7e4401ac44a4521 (diff) | |
parent | b61c9c340ee3a1149e3682eaafe7f7730b58398f (diff) |
Merge branch 'ps-config-removal-smarthttp' into 'master'
Removal of config.Config from test code: EnableGitProtocolV2Support
See merge request gitlab-org/gitaly!3155
-rw-r--r-- | internal/gitaly/service/smarthttp/inforefs_test.go | 51 | ||||
-rw-r--r-- | internal/gitaly/service/smarthttp/receive_pack_test.go | 53 | ||||
-rw-r--r-- | internal/gitaly/service/smarthttp/testhelper_test.go | 27 | ||||
-rw-r--r-- | internal/gitaly/service/smarthttp/upload_pack_test.go | 41 | ||||
-rw-r--r-- | internal/gitaly/service/ssh/receive_pack_test.go | 8 | ||||
-rw-r--r-- | internal/gitaly/service/ssh/upload_pack_test.go | 4 | ||||
-rw-r--r-- | internal/testhelper/git_protocol.go | 8 |
7 files changed, 103 insertions, 89 deletions
diff --git a/internal/gitaly/service/smarthttp/inforefs_test.go b/internal/gitaly/service/smarthttp/inforefs_test.go index ffdcc6250..d678a5057 100644 --- a/internal/gitaly/service/smarthttp/inforefs_test.go +++ b/internal/gitaly/service/smarthttp/inforefs_test.go @@ -28,7 +28,7 @@ import ( ) func TestSuccessfulInfoRefsUploadPack(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -39,7 +39,7 @@ func TestSuccessfulInfoRefsUploadPack(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, rpcRequest) + response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest) require.NoError(t, err) assertGitRefAdvertisement(t, "InfoRefsUploadPack", string(response), "001e# service=git-upload-pack", "0000", []string{ "003ef4e6814c3e4e7a0de82a9e7cd20c626cc963a2f8 refs/tags/v1.0.0", @@ -48,7 +48,7 @@ func TestSuccessfulInfoRefsUploadPack(t *testing.T) { } func TestSuccessfulInfoRefsUploadWithPartialClone(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() ctx, cancel := testhelper.Context() @@ -61,7 +61,7 @@ func TestSuccessfulInfoRefsUploadWithPartialClone(t *testing.T) { Repository: testRepo, } - partialResponse, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, request) + partialResponse, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, request) require.NoError(t, err) partialRefs := stats.Get{} err = partialRefs.Parse(bytes.NewReader(partialResponse)) @@ -73,7 +73,7 @@ func TestSuccessfulInfoRefsUploadWithPartialClone(t *testing.T) { } func TestSuccessfulInfoRefsUploadPackWithGitConfigOptions(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -89,16 +89,19 @@ func TestSuccessfulInfoRefsUploadPackWithGitConfigOptions(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, rpcRequest) + response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest) require.NoError(t, err) assertGitRefAdvertisement(t, "InfoRefsUploadPack", string(response), "001e# service=git-upload-pack", "0000", []string{}) } func TestSuccessfulInfoRefsUploadPackWithGitProtocol(t *testing.T) { - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -109,7 +112,7 @@ func TestSuccessfulInfoRefsUploadPackWithGitProtocol(t *testing.T) { GitProtocol: git.ProtocolV2, } - client, _ := newSmartHTTPClient(t, serverSocketPath) + client, _ := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) ctx, cancel := testhelper.Context() defer cancel() @@ -129,8 +132,8 @@ func TestSuccessfulInfoRefsUploadPackWithGitProtocol(t *testing.T) { require.Contains(t, envData, fmt.Sprintf("GIT_PROTOCOL=%s\n", git.ProtocolV2)) } -func makeInfoRefsUploadPackRequest(ctx context.Context, t *testing.T, serverSocketPath string, rpcRequest *gitalypb.InfoRefsRequest) ([]byte, error) { - client, conn := newSmartHTTPClient(t, serverSocketPath) +func makeInfoRefsUploadPackRequest(ctx context.Context, t *testing.T, serverSocketPath, token string, rpcRequest *gitalypb.InfoRefsRequest) ([]byte, error) { + client, conn := newSmartHTTPClient(t, serverSocketPath, token) defer conn.Close() ctx, cancel := context.WithCancel(ctx) @@ -147,10 +150,10 @@ func makeInfoRefsUploadPackRequest(ctx context.Context, t *testing.T, serverSock } func TestSuccessfulInfoRefsReceivePack(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -180,10 +183,10 @@ func TestSuccessfulInfoRefsReceivePack(t *testing.T) { } func TestObjectPoolRefAdvertisementHiding(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() repo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -217,10 +220,10 @@ func TestObjectPoolRefAdvertisementHiding(t *testing.T) { } func TestFailureRepoNotFoundInfoRefsReceivePack(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() repo := &gitalypb.Repository{StorageName: "default", RelativePath: "testdata/scratch/another_repo"} rpcRequest := &gitalypb.InfoRefsRequest{Repository: repo} @@ -239,10 +242,10 @@ func TestFailureRepoNotFoundInfoRefsReceivePack(t *testing.T) { } func TestFailureRepoNotSetInfoRefsReceivePack(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() rpcRequest := &gitalypb.InfoRefsRequest{} @@ -293,7 +296,7 @@ func (ms mockStreamer) PutStream(ctx context.Context, repo *gitalypb.Repository, func TestCacheInfoRefsUploadPack(t *testing.T) { clearCache(t) - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() testRepo, _, cleanupFn := testhelper.NewTestRepo(t) @@ -308,7 +311,7 @@ func TestCacheInfoRefsUploadPack(t *testing.T) { ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, rpcRequest) + response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest) require.NoError(t, err) assertGitRefAdvertisement(t, "InfoRefsUploadPack", string(response), @@ -332,7 +335,7 @@ func TestCacheInfoRefsUploadPack(t *testing.T) { // replace cached response file to prove the info-ref uses the cache replaceCachedResponse(t, ctx, rpcRequest, strings.Join(replacedContents, "\n")) - response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, rpcRequest) + response, err := makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest) require.NoError(t, err) assertGitRefAdvertisement(t, "InfoRefsUploadPack", string(response), replacedContents[0], replacedContents[3], replacedContents[1:3], @@ -359,7 +362,7 @@ func TestCacheInfoRefsUploadPack(t *testing.T) { invalidRepoCleanup := createInvalidRepo(t, filepath.Join(testhelper.GitlabTestStoragePath(), invalidReq.Repository.RelativePath)) defer invalidRepoCleanup() - _, err = makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, invalidReq) + _, err = makeInfoRefsUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, invalidReq) testhelper.RequireGrpcError(t, err, codes.Internal) testhelper.AssertPathNotExists(t, pathToCachedResponse(t, ctx, invalidReq)) @@ -376,7 +379,7 @@ func TestCacheInfoRefsUploadPack(t *testing.T) { }) stop() - serverSocketPath, stop = runSmartHTTPServer(t, withInfoRefCache(mockInfoRefCache)) + serverSocketPath, stop = runSmartHTTPServer(t, config.Config, withInfoRefCache(mockInfoRefCache)) defer stop() invalidateCacheForRepo() diff --git a/internal/gitaly/service/smarthttp/receive_pack_test.go b/internal/gitaly/service/smarthttp/receive_pack_test.go index 0dae7d785..d5dc4ca61 100644 --- a/internal/gitaly/service/smarthttp/receive_pack_test.go +++ b/internal/gitaly/service/smarthttp/receive_pack_test.go @@ -47,13 +47,13 @@ func TestSuccessfulReceivePackRequest(t *testing.T) { hookOutputFile, cleanup := testhelper.CaptureHookEnv(t) defer cleanup() - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() ctx, cancel := testhelper.Context() @@ -108,16 +108,19 @@ func TestSuccessfulReceivePackRequest(t *testing.T) { } func TestSuccessfulReceivePackRequestWithGitProtocol(t *testing.T) { - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() ctx, cancel := testhelper.Context() @@ -138,13 +141,13 @@ func TestSuccessfulReceivePackRequestWithGitProtocol(t *testing.T) { } func TestFailedReceivePackRequestWithGitOpts(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() ctx, cancel := testhelper.Context() @@ -175,13 +178,13 @@ func TestFailedReceivePackRequestDueToHooksFailure(t *testing.T) { hookContent := []byte("#!/bin/sh\nexit 1") ioutil.WriteFile(filepath.Join(hooks.Path(config.Config), "pre-receive"), hookContent, 0755) - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() ctx, cancel := testhelper.Context() @@ -283,10 +286,10 @@ func createCommit(t *testing.T, repoPath string, fileContents []byte) (oldHead s } func TestFailedReceivePackRequestDueToValidationError(t *testing.T) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() - client, conn := newSmartHTTPClient(t, serverSocketPath) + client, conn := newSmartHTTPClient(t, serverSocketPath, config.Config.Auth.Token) defer conn.Close() rpcRequests := []gitalypb.PostReceivePackRequest{ @@ -339,13 +342,13 @@ func TestInvalidTimezone(t *testing.T) { _, cleanup := testhelper.CaptureHookEnv(t) defer cleanup() - socket, stop := runSmartHTTPServer(t) + socket, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() - client, conn := newSmartHTTPClient(t, socket) + client, conn := newSmartHTTPClient(t, socket, config.Config.Auth.Token) defer conn.Close() ctx, cancel := testhelper.Context() @@ -384,10 +387,10 @@ func TestPostReceivePackToHooks(t *testing.T) { config.Config.Auth.Token = "abc123" - server, socket := runSmartHTTPHookServiceServer(t) + server, socket := runSmartHTTPHookServiceServer(t, config.Config) defer server.Stop() - client, conn := newSmartHTTPClient(t, "unix://"+socket) + client, conn := newSmartHTTPClient(t, "unix://"+socket, config.Config.Auth.Token) defer conn.Close() tempGitlabShellDir, cleanup := testhelper.TempDir(t) @@ -442,7 +445,7 @@ func TestPostReceivePackToHooks(t *testing.T) { require.Equal(t, io.EOF, drainPostReceivePackResponse(stream)) } -func runSmartHTTPHookServiceServer(t *testing.T) (*grpc.Server, string) { +func runSmartHTTPHookServiceServer(t *testing.T, cfg config.Cfg) (*grpc.Server, string) { server := testhelper.NewTestGrpcServer(t, nil, nil) serverSocketPath := testhelper.GetTemporaryGitalySocketFileName(t) @@ -450,18 +453,18 @@ func runSmartHTTPHookServiceServer(t *testing.T) (*grpc.Server, string) { if err != nil { t.Fatal(err) } - internalListener, err := net.Listen("unix", config.Config.GitalyInternalSocketPath()) + internalListener, err := net.Listen("unix", cfg.GitalyInternalSocketPath()) if err != nil { t.Fatal(err) } - locator := config.NewLocator(config.Config) - txManager := transaction.NewManager(config.Config) - hookManager := gitalyhook.NewManager(locator, txManager, gitalyhook.GitlabAPIStub, config.Config) - gitCmdFactory := git.NewExecCommandFactory(config.Config) + locator := config.NewLocator(cfg) + txManager := transaction.NewManager(cfg) + hookManager := gitalyhook.NewManager(locator, txManager, gitalyhook.GitlabAPIStub, cfg) + gitCmdFactory := git.NewExecCommandFactory(cfg) - gitalypb.RegisterSmartHTTPServiceServer(server, NewServer(config.Config, locator, gitCmdFactory)) - gitalypb.RegisterHookServiceServer(server, hook.NewServer(config.Config, hookManager, gitCmdFactory)) + gitalypb.RegisterSmartHTTPServiceServer(server, NewServer(cfg, locator, gitCmdFactory)) + gitalypb.RegisterHookServiceServer(server, hook.NewServer(cfg, hookManager, gitCmdFactory)) reflection.Register(server) go server.Serve(listener) @@ -533,7 +536,7 @@ func testPostReceiveWithTransactionsViaPraefect(t *testing.T, ctx context.Contex gitalyServer.GrpcServer().Serve(internalListener) }() - client, conn := newSmartHTTPClient(t, "unix://"+gitalyServer.Socket()) + client, conn := newSmartHTTPClient(t, "unix://"+gitalyServer.Socket(), config.Config.Auth.Token) defer conn.Close() stream, err := client.PostReceivePack(ctx) @@ -585,7 +588,7 @@ func TestPostReceiveWithReferenceTransactionHook(t *testing.T) { go gitalyServer.Serve(internalListener) defer gitalyServer.Stop() - client, conn := newSmartHTTPClient(t, "unix://"+gitalySocketPath) + client, conn := newSmartHTTPClient(t, "unix://"+gitalySocketPath, config.Config.Auth.Token) defer conn.Close() // As we ain't got a Praefect server setup, we instead hooked up the diff --git a/internal/gitaly/service/smarthttp/testhelper_test.go b/internal/gitaly/service/smarthttp/testhelper_test.go index 9962760d6..1a59c9a6e 100644 --- a/internal/gitaly/service/smarthttp/testhelper_test.go +++ b/internal/gitaly/service/smarthttp/testhelper_test.go @@ -6,6 +6,7 @@ import ( "testing" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" diskcache "gitlab.com/gitlab-org/gitaly/internal/cache" "gitlab.com/gitlab-org/gitaly/internal/git" @@ -47,12 +48,12 @@ func testMain(m *testing.M) int { return m.Run() } -func runSmartHTTPServer(t *testing.T, serverOpts ...ServerOpt) (string, func()) { - locator := config.NewLocator(config.Config) +func runSmartHTTPServer(t *testing.T, cfg config.Cfg, serverOpts ...ServerOpt) (string, func()) { + locator := config.NewLocator(cfg) keyer := diskcache.NewLeaseKeyer(locator) - txManager := transaction.NewManager(config.Config) - hookManager := hook.NewManager(locator, txManager, hook.GitlabAPIStub, config.Config) - gitCmdFactory := git.NewExecCommandFactory(config.Config) + txManager := transaction.NewManager(cfg) + hookManager := hook.NewManager(locator, txManager, hook.GitlabAPIStub, cfg) + gitCmdFactory := git.NewExecCommandFactory(cfg) srv := testhelper.NewServer(t, []grpc.StreamServerInterceptor{ @@ -61,24 +62,24 @@ func runSmartHTTPServer(t *testing.T, serverOpts ...ServerOpt) (string, func()) []grpc.UnaryServerInterceptor{ cache.UnaryInvalidator(keyer, protoregistry.GitalyProtoPreregistered), }, - testhelper.WithInternalSocket(config.Config)) + testhelper.WithInternalSocket(cfg)) - gitalypb.RegisterSmartHTTPServiceServer(srv.GrpcServer(), NewServer(config.Config, locator, gitCmdFactory, serverOpts...)) - gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(config.Config, hookManager, gitCmdFactory)) + gitalypb.RegisterSmartHTTPServiceServer(srv.GrpcServer(), NewServer(cfg, locator, gitCmdFactory, serverOpts...)) + gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(cfg, hookManager, gitCmdFactory)) srv.Start(t) return "unix://" + srv.Socket(), srv.Stop } -func newSmartHTTPClient(t *testing.T, serverSocketPath string) (gitalypb.SmartHTTPServiceClient, *grpc.ClientConn) { +func newSmartHTTPClient(t *testing.T, serverSocketPath, token string) (gitalypb.SmartHTTPServiceClient, *grpc.ClientConn) { + t.Helper() + connOpts := []grpc.DialOption{ grpc.WithInsecure(), - grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(config.Config.Auth.Token)), + grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(token)), } conn, err := grpc.Dial(serverSocketPath, connOpts...) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) return gitalypb.NewSmartHTTPServiceClient(conn), conn } diff --git a/internal/gitaly/service/smarthttp/upload_pack_test.go b/internal/gitaly/service/smarthttp/upload_pack_test.go index 161a3c0bd..938758973 100644 --- a/internal/gitaly/service/smarthttp/upload_pack_test.go +++ b/internal/gitaly/service/smarthttp/upload_pack_test.go @@ -39,7 +39,7 @@ func testSuccessfulUploadPackRequest(t *testing.T, ctx context.Context) { negotiationMetrics := prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"feature"}) serverSocketPath, stop := runSmartHTTPServer( - t, WithPackfileNegotiationMetrics(negotiationMetrics), + t, config.Config, WithPackfileNegotiationMetrics(negotiationMetrics), ) defer stop() @@ -87,7 +87,7 @@ func testSuccessfulUploadPackRequest(t *testing.T, ctx context.Context) { RelativePath: filepath.Join(remoteRepoRelativePath, ".git"), }, } - responseBuffer, err := makePostUploadPackRequest(ctx, t, serverSocketPath, req, requestBuffer) + responseBuffer, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, req, requestBuffer) require.NoError(t, err) // There's no git command we can pass it this response and do the work for us (extracting pack file, ...), @@ -112,7 +112,7 @@ func TestUploadPackRequestWithGitConfigOptions(t *testing.T) { } func testUploadPackRequestWithGitConfigOptions(t *testing.T, ctx context.Context) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() _, testRepoPath, cleanup := testhelper.NewTestRepo(t) @@ -153,7 +153,7 @@ func testUploadPackRequestWithGitConfigOptions(t *testing.T, ctx context.Context } // The ref is successfully requested as it is not hidden - response, err := makePostUploadPackRequest(ctx, t, serverSocketPath, rpcRequest, requestBody) + response, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest, requestBody) require.NoError(t, err) _, _, count := extractPackDataFromResponse(t, response) assert.Equal(t, 5, count, "pack should have 5 entries") @@ -164,7 +164,7 @@ func testUploadPackRequestWithGitConfigOptions(t *testing.T, ctx context.Context // we need to set uploadpack.allowAnySHA1InWant=false, because if it's true then we won't encounter an error from setting // uploadpack.hideRefs=refs/hidden. We are setting uploadpack.allowAnySHA1InWant=true in the RPC to enable partial clones rpcRequest.GitConfigOptions = []string{"uploadpack.hideRefs=refs/hidden", "uploadpack.allowAnySHA1InWant=false"} - response, err = makePostUploadPackRequest(ctx, t, serverSocketPath, rpcRequest, requestBodyCopy) + response, err = makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest, requestBodyCopy) testhelper.RequireGrpcError(t, err, codes.Unavailable) expected := fmt.Sprintf("0049ERR upload-pack: not our ref %v", want) @@ -178,10 +178,13 @@ func TestUploadPackRequestWithGitProtocol(t *testing.T) { } func testUploadPackRequestWithGitProtocol(t *testing.T, ctx context.Context) { - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() _, testRepoPath, cleanup := testhelper.NewTestRepo(t) @@ -210,7 +213,7 @@ func testUploadPackRequestWithGitProtocol(t *testing.T, ctx context.Context) { } // The ref is successfully requested as it is not hidden - _, err = makePostUploadPackRequest(ctx, t, serverSocketPath, rpcRequest, requestBody) + _, err = makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest, requestBody) require.NoError(t, err) envData := testhelper.GetGitEnvData(t) @@ -227,7 +230,7 @@ func TestSuccessfulUploadPackDeepenRequest(t *testing.T) { } func testSuccessfulUploadPackDeepenRequest(t *testing.T, ctx context.Context) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() testRepo, _, cleanup := testhelper.NewTestRepo(t) @@ -239,7 +242,7 @@ func testSuccessfulUploadPackDeepenRequest(t *testing.T, ctx context.Context) { pktline.WriteFlush(requestBody) rpcRequest := &gitalypb.PostUploadPackRequest{Repository: testRepo} - response, err := makePostUploadPackRequest(ctx, t, serverSocketPath, rpcRequest, requestBody) + response, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, rpcRequest, requestBody) // This assertion is the main reason this test exists. assert.NoError(t, err) @@ -269,7 +272,7 @@ func TestUploadPackWithPackObjectsHook(t *testing.T) { cleanup = testhelper.WriteExecutable(t, filepath.Join(hookDir, "gitaly-hooks"), []byte(hookScript)) defer cleanup() - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() repo, repoPath, cleanup := testhelper.NewTestRepo(t) @@ -287,7 +290,7 @@ func TestUploadPackWithPackObjectsHook(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - _, err := makePostUploadPackRequest(ctx, t, serverSocketPath, &gitalypb.PostUploadPackRequest{ + _, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, &gitalypb.PostUploadPackRequest{ Repository: repo, }, requestBuffer) require.NoError(t, err) @@ -303,7 +306,7 @@ func TestFailedUploadPackRequestDueToValidationError(t *testing.T) { } func testFailedUploadPackRequestDueToValidationError(t *testing.T, ctx context.Context) { - serverSocketPath, stop := runSmartHTTPServer(t) + serverSocketPath, stop := runSmartHTTPServer(t, config.Config) defer stop() rpcRequests := []gitalypb.PostUploadPackRequest{ @@ -314,14 +317,14 @@ func testFailedUploadPackRequestDueToValidationError(t *testing.T, ctx context.C for _, rpcRequest := range rpcRequests { t.Run(fmt.Sprintf("%v", rpcRequest), func(t *testing.T) { - _, err := makePostUploadPackRequest(ctx, t, serverSocketPath, &rpcRequest, bytes.NewBuffer(nil)) + _, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, &rpcRequest, bytes.NewBuffer(nil)) testhelper.RequireGrpcError(t, err, codes.InvalidArgument) }) } } -func makePostUploadPackRequest(ctx context.Context, t *testing.T, serverSocketPath string, in *gitalypb.PostUploadPackRequest, body io.Reader) (*bytes.Buffer, error) { - client, conn := newSmartHTTPClient(t, serverSocketPath) +func makePostUploadPackRequest(ctx context.Context, t *testing.T, serverSocketPath, token string, in *gitalypb.PostUploadPackRequest, body io.Reader) (*bytes.Buffer, error) { + client, conn := newSmartHTTPClient(t, serverSocketPath, token) defer conn.Close() stream, err := client.PostUploadPack(ctx) @@ -398,7 +401,7 @@ func testUploadPackRequestForPartialCloneSuccess(t *testing.T, ctx context.Conte negotiationMetrics := prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"feature"}) serverSocketPath, stop := runSmartHTTPServer( - t, WithPackfileNegotiationMetrics(negotiationMetrics), + t, config.Config, WithPackfileNegotiationMetrics(negotiationMetrics), ) defer stop() @@ -448,7 +451,7 @@ func testUploadPackRequestForPartialCloneSuccess(t *testing.T, ctx context.Conte }, } - responseBuffer, err := makePostUploadPackRequest(ctx, t, serverSocketPath, req, &requestBuffer) + responseBuffer, err := makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, req, &requestBuffer) require.NoError(t, err) pack, version, entries := extractPackDataFromResponse(t, responseBuffer) @@ -480,7 +483,7 @@ func testUploadPackRequestForPartialCloneSuccess(t *testing.T, ctx context.Conte pktline.WriteFlush(&requestBuffer) pktline.WriteFlush(&requestBuffer) - _, err = makePostUploadPackRequest(ctx, t, serverSocketPath, req, &requestBuffer) + _, err = makePostUploadPackRequest(ctx, t, serverSocketPath, config.Config.Auth.Token, req, &requestBuffer) require.NoError(t, err) metric, err := negotiationMetrics.GetMetricWithLabelValues("filter") diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go index de18fc0de..f58368d3f 100644 --- a/internal/gitaly/service/ssh/receive_pack_test.go +++ b/internal/gitaly/service/ssh/receive_pack_test.go @@ -140,8 +140,10 @@ func TestReceivePackPushSuccess(t *testing.T) { } func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg serverSocketPath, stop := runSSHServer(t) defer stop() @@ -241,8 +243,10 @@ func TestSSHReceivePackToHooks(t *testing.T) { glRepository := "some_repo" glID := "key-123" - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg serverSocketPath, stop := runSSHServer(t) defer stop() diff --git a/internal/gitaly/service/ssh/upload_pack_test.go b/internal/gitaly/service/ssh/upload_pack_test.go index 16944d384..8518accc5 100644 --- a/internal/gitaly/service/ssh/upload_pack_test.go +++ b/internal/gitaly/service/ssh/upload_pack_test.go @@ -431,8 +431,10 @@ func TestUploadPackCloneSuccessWithGitProtocol(t *testing.T) { for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - restore := testhelper.EnableGitProtocolV2Support(t) + defer func(old config.Cfg) { config.Config = old }(config.Config) + cfg, restore := testhelper.EnableGitProtocolV2Support(t, config.Config) defer restore() + config.Config = cfg serverSocketPath, stop := runSSHServer(t) defer stop() diff --git a/internal/testhelper/git_protocol.go b/internal/testhelper/git_protocol.go index d0043303d..fb6fe5805 100644 --- a/internal/testhelper/git_protocol.go +++ b/internal/testhelper/git_protocol.go @@ -14,7 +14,7 @@ import ( // wrapper that allows the protocol to be tested. It returns a function that // restores the given settings as well as an array of environment variables // which need to be set when invoking Git with this setup. -func EnableGitProtocolV2Support(t testing.TB) func() { +func EnableGitProtocolV2Support(t testing.TB, cfg config.Cfg) (config.Cfg, Cleanup) { envPath := filepath.Join(testDirectory, "git-env") script := fmt.Sprintf(`#!/bin/sh @@ -28,11 +28,9 @@ exec "%s" "$@" cleanupExe := WriteExecutable(t, path, []byte(script)) - oldGitBinPath := config.Config.Git.BinPath - config.Config.Git.BinPath = path - return func() { + cfg.Git.BinPath = path + return cfg, func() { assert.NoError(t, os.Remove(envPath)) - config.Config.Git.BinPath = oldGitBinPath cleanupExe() cleanupDir() } |