From aaaea1e9591bb5dba035784e9d5e71b297d30046 Mon Sep 17 00:00:00 2001 From: John Cai Date: Mon, 17 Feb 2020 11:19:35 -0800 Subject: [PATCH] Add end to end test that exercises hooks for operations service tests Uses the gitaly ruby directory as the git hooks directory so that git hooks get executed for operations service tests. --- cmd/gitaly-hooks/hooks_test.go | 43 +++--- internal/service/operations/.gitignore | 2 + .../service/operations/apply_patch_test.go | 12 +- internal/service/operations/branches_test.go | 39 ++++-- .../service/operations/cherry_pick_test.go | 51 ++++--- .../service/operations/commit_files_test.go | 63 ++++++--- internal/service/operations/merge_test.go | 24 +++- internal/service/operations/rebase_test.go | 38 ++++-- internal/service/operations/revert_test.go | 56 ++++---- .../service/operations/submodules_test.go | 6 + internal/service/operations/tags_test.go | 42 ++++-- .../service/operations/testhelper_test.go | 54 ++++++-- .../operations/update_branches_test.go | 31 +++-- .../service/smarthttp/receive_pack_test.go | 12 +- internal/service/ssh/receive_pack_test.go | 11 +- internal/testhelper/testserver.go | 128 +++++++++++++----- 16 files changed, 414 insertions(+), 198 deletions(-) create mode 100644 internal/service/operations/.gitignore diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 083b4d7bd5..fa84aa22d8 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -34,7 +35,7 @@ func TestHooksPrePostReceive(t *testing.T) { defer cleanupFn() secretToken := "secret token" - key := 1234 + glID := "key-1234" glRepository := "some_repo" tempGitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) @@ -44,11 +45,11 @@ func TestHooksPrePostReceive(t *testing.T) { gitPushOptions := []string{"gitpushoption1", "gitpushoption2"} - c := testhelper.GitlabServerConfig{ + c := testhelper.GitlabTestServerOptions{ User: "", Password: "", SecretToken: secretToken, - Key: key, + GLID: glID, GLRepository: glRepository, Changes: changes, PostReceiveCounterDecreased: true, @@ -82,7 +83,7 @@ func TestHooksPrePostReceive(t *testing.T) { t, glRepository, tempGitlabShellDir, - key, + glID, gitPushOptions..., ) cmd.Dir = testRepoPath @@ -95,7 +96,7 @@ func TestHooksPrePostReceive(t *testing.T) { } func TestHooksUpdate(t *testing.T) { - key := 1234 + glID := "key-1234" glRepository := "some_repo" tempGitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) @@ -126,7 +127,7 @@ func TestHooksUpdate(t *testing.T) { updateHookPath, err := filepath.Abs("../../ruby/git-hooks/update") require.NoError(t, err) cmd := exec.Command(updateHookPath, refval, oldval, newval) - cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, key) + cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, glID) cmd.Stdout = &stdout cmd.Stderr = &stderr cmd.Dir = testRepoPath @@ -148,8 +149,9 @@ func TestHooksUpdate(t *testing.T) { func TestHooksPostReceiveFailed(t *testing.T) { secretToken := "secret token" - key := 1234 + glID := "key-1234" glRepository := "some_repo" + changes := "oldhead newhead" tempGitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) defer cleanup() @@ -161,13 +163,13 @@ func TestHooksPostReceiveFailed(t *testing.T) { // send back {"reference_counter_increased": false}, indicating something went wrong // with the call - c := testhelper.GitlabServerConfig{ + c := testhelper.GitlabTestServerOptions{ User: "", Password: "", SecretToken: secretToken, - Key: key, + Changes: changes, + GLID: glID, GLRepository: glRepository, - Changes: "", PostReceiveCounterDecreased: false, Protocol: "ssh", } @@ -189,9 +191,10 @@ func TestHooksPostReceiveFailed(t *testing.T) { postReceiveHookPath, err := filepath.Abs("../../ruby/git-hooks/post-receive") require.NoError(t, err) cmd := exec.Command(postReceiveHookPath) - cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, key) + cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, glID) cmd.Stdout = &stdout cmd.Stderr = &stderr + cmd.Stdin = bytes.NewBuffer([]byte(changes)) cmd.Dir = testRepoPath err = cmd.Run() @@ -205,19 +208,20 @@ func TestHooksPostReceiveFailed(t *testing.T) { func TestHooksNotAllowed(t *testing.T) { secretToken := "secret token" - key := 1234 + glID := "key-1234" glRepository := "some_repo" + changes := "oldhead newhead" tempGitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) defer cleanup() - c := testhelper.GitlabServerConfig{ + c := testhelper.GitlabTestServerOptions{ User: "", Password: "", SecretToken: secretToken, - Key: key, + GLID: glID, GLRepository: glRepository, - Changes: "", + Changes: changes, PostReceiveCounterDecreased: true, Protocol: "ssh", } @@ -243,7 +247,8 @@ func TestHooksNotAllowed(t *testing.T) { cmd := exec.Command(preReceiveHookPath) cmd.Stderr = &stderr cmd.Stdout = &stdout - cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, key) + cmd.Stdin = strings.NewReader(changes) + cmd.Env = testhelper.EnvForHooks(t, glRepository, tempGitlabShellDir, glID) cmd.Dir = testRepoPath require.Error(t, cmd.Run()) @@ -254,11 +259,10 @@ func TestHooksNotAllowed(t *testing.T) { func TestCheckOK(t *testing.T) { user, password := "user123", "password321" - c := testhelper.GitlabServerConfig{ + c := testhelper.GitlabTestServerOptions{ User: user, Password: password, SecretToken: "", - Key: 0, GLRepository: "", Changes: "", PostReceiveCounterDecreased: false, @@ -302,11 +306,10 @@ func TestCheckOK(t *testing.T) { func TestCheckBadCreds(t *testing.T) { user, password := "user123", "password321" - c := testhelper.GitlabServerConfig{ + c := testhelper.GitlabTestServerOptions{ User: user, Password: password, SecretToken: "", - Key: 0, GLRepository: "", Changes: "", PostReceiveCounterDecreased: false, diff --git a/internal/service/operations/.gitignore b/internal/service/operations/.gitignore new file mode 100644 index 0000000000..da39105157 --- /dev/null +++ b/internal/service/operations/.gitignore @@ -0,0 +1,2 @@ +/testdata/gitlab-libexec +/testdata/gitlab-shell diff --git a/internal/service/operations/apply_patch_test.go b/internal/service/operations/apply_patch_test.go index fefb2217e9..bd9176caa8 100644 --- a/internal/service/operations/apply_patch_test.go +++ b/internal/service/operations/apply_patch_test.go @@ -19,7 +19,7 @@ import ( ) func TestSuccessfulUserApplyPatch(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -28,6 +28,9 @@ func TestSuccessfulUserApplyPatch(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + ctx, cancel := testhelper.Context() defer cancel() @@ -139,7 +142,7 @@ func TestSuccessfulUserApplyPatch(t *testing.T) { } func TestFailedPatchApplyPatch(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -148,6 +151,9 @@ func TestFailedPatchApplyPatch(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository, "") + defer cleanupSrv() + ctx, cancel := testhelper.Context() defer cancel() @@ -174,7 +180,7 @@ func TestFailedPatchApplyPatch(t *testing.T) { } func TestFailedValidationUserApplyPatch(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) diff --git a/internal/service/operations/branches_test.go b/internal/service/operations/branches_test.go index 4d5f89132c..5d6081bf0c 100644 --- a/internal/service/operations/branches_test.go +++ b/internal/service/operations/branches_test.go @@ -2,7 +2,6 @@ package operations import ( "context" - "os" "os/exec" "testing" @@ -30,6 +29,9 @@ func TestSuccessfulUserCreateBranchRequest(t *testing.T) { startPointCommit, err := log.GetCommit(ctx, testRepo, startPoint) require.NoError(t, err) + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testCases := []struct { desc string branchName string @@ -79,6 +81,9 @@ func TestSuccessfulGitHooksForUserCreateBranchRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + server, serverSocketPath := runOperationServiceServer(t) defer server.Stop() @@ -97,7 +102,8 @@ func TestSuccessfulGitHooksForUserCreateBranchRequest(t *testing.T) { t.Run(hookName, func(t *testing.T) { defer exec.Command("git", "-C", testRepoPath, "branch", "-D", branchName).Run() - hookOutputTempPath := WriteEnvToHook(t, testRepoPath, hookName) + hookOutputTempPath, cleanup := WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() ctx, cancel := testhelper.Context() defer cancel() @@ -107,7 +113,6 @@ func TestSuccessfulGitHooksForUserCreateBranchRequest(t *testing.T) { require.Empty(t, response.PreReceiveError) output := string(testhelper.MustReadFile(t, hookOutputTempPath)) - require.Contains(t, output, "GL_ID="+user.GlId) require.Contains(t, output, "GL_USERNAME="+user.GlUsername) }) } @@ -117,6 +122,9 @@ func TestFailedUserCreateBranchDueToHooks(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + server, serverSocketPath := runOperationServiceServer(t) defer server.Stop() @@ -134,7 +142,7 @@ func TestFailedUserCreateBranchDueToHooks(t *testing.T) { hookContent := []byte("#!/bin/sh\nprintenv | paste -sd ' ' -\nexit 1") for _, hookName := range gitlabPreHooks { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -143,11 +151,7 @@ func TestFailedUserCreateBranchDueToHooks(t *testing.T) { response, err := client.UserCreateBranch(ctx, request) require.Nil(t, err) - require.Contains(t, response.PreReceiveError, "GL_ID="+user.GlId) require.Contains(t, response.PreReceiveError, "GL_USERNAME="+user.GlUsername) - require.Contains(t, response.PreReceiveError, "GL_REPOSITORY="+testRepo.GlRepository) - require.Contains(t, response.PreReceiveError, "GL_PROTOCOL=web") - require.Contains(t, response.PreReceiveError, "PWD="+testRepoPath) } } @@ -161,6 +165,9 @@ func TestFailedUserCreateBranchRequest(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testCases := []struct { desc string branchName string @@ -240,6 +247,9 @@ func TestSuccessfulUserDeleteBranchRequest(t *testing.T) { GlId: "user-123", } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", branchNameInput) request := &gitalypb.UserDeleteBranchRequest{ @@ -275,6 +285,9 @@ func TestSuccessfulGitHooksForUserDeleteBranchRequest(t *testing.T) { GlUsername: "johndoe", } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserDeleteBranchRequest{ Repository: testRepo, BranchName: []byte(branchNameInput), @@ -285,8 +298,8 @@ func TestSuccessfulGitHooksForUserDeleteBranchRequest(t *testing.T) { t.Run(hookName, func(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", branchNameInput) - hookOutputTempPath := WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + hookOutputTempPath, cleanup := WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() ctx, cancel := testhelper.Context() defer cancel() @@ -295,7 +308,6 @@ func TestSuccessfulGitHooksForUserDeleteBranchRequest(t *testing.T) { require.NoError(t, err) output := testhelper.MustReadFile(t, hookOutputTempPath) - require.Contains(t, string(output), "GL_ID=user-123") require.Contains(t, string(output), "GL_USERNAME=johndoe") }) } @@ -380,6 +392,9 @@ func TestFailedUserDeleteBranchDueToHooks(t *testing.T) { GlId: "user-123", } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserDeleteBranchRequest{ Repository: testRepo, BranchName: []byte(branchNameInput), @@ -390,7 +405,7 @@ func TestFailedUserDeleteBranchDueToHooks(t *testing.T) { for _, hookName := range gitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() diff --git a/internal/service/operations/cherry_pick_test.go b/internal/service/operations/cherry_pick_test.go index 6cb026769a..fb3a18cf8f 100644 --- a/internal/service/operations/cherry_pick_test.go +++ b/internal/service/operations/cherry_pick_test.go @@ -2,7 +2,6 @@ package operations_test import ( "net" - "os" "testing" "github.com/stretchr/testify/require" @@ -20,7 +19,7 @@ func TestSuccessfulUserCherryPickRequest(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -47,6 +46,9 @@ func TestSuccessfulUserCherryPickRequest(t *testing.T) { testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testCases := []struct { desc string request *gitalypb.UserCherryPickRequest @@ -129,7 +131,7 @@ func TestSuccessfulGitHooksForUserCherryPickRequest(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -147,6 +149,9 @@ func TestSuccessfulGitHooksForUserCherryPickRequest(t *testing.T) { GlId: "user-123", } + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + cherryPickedCommit, err := log.GetCommit(ctxOuter, testRepo, "8a0f2ee90d940bfb0ba1e14e8214b0649056e4ab") require.NoError(t, err) @@ -158,22 +163,23 @@ func TestSuccessfulGitHooksForUserCherryPickRequest(t *testing.T) { Message: []byte("Cherry-picking " + cherryPickedCommit.Id), } + var hookOutputFiles []string for _, hookName := range operations.GitlabHooks { - t.Run(hookName, func(t *testing.T) { - hookOutputTempPath := operations.WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + hookOutputTempPath, cleanup := operations.WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() + hookOutputFiles = append(hookOutputFiles, hookOutputTempPath) + } - md := testhelper.GitalyServersMetadata(t, serverSocketPath) - ctx := metadata.NewOutgoingContext(ctxOuter, md) + md := testhelper.GitalyServersMetadata(t, serverSocketPath) + ctx := metadata.NewOutgoingContext(ctxOuter, md) - response, err := client.UserCherryPick(ctx, request) - require.NoError(t, err) - require.Empty(t, response.PreReceiveError) + response, err := client.UserCherryPick(ctx, request) + require.NoError(t, err) + require.Empty(t, response.PreReceiveError) - output := string(testhelper.MustReadFile(t, hookOutputTempPath)) - require.Contains(t, output, "GL_ID="+user.GlId) - require.Contains(t, output, "GL_USERNAME="+user.GlUsername) - }) + for _, file := range hookOutputFiles { + output := string(testhelper.MustReadFile(t, file)) + require.Contains(t, output, "GL_USERNAME="+user.GlUsername) } } @@ -181,7 +187,7 @@ func TestFailedUserCherryPickRequestDueToValidations(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -267,7 +273,7 @@ func TestFailedUserCherryPickRequestDueToPreReceiveError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -285,6 +291,9 @@ func TestFailedUserCherryPickRequestDueToPreReceiveError(t *testing.T) { GlId: "user-123", } + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + cherryPickedCommit, err := log.GetCommit(ctxOuter, testRepo, "8a0f2ee90d940bfb0ba1e14e8214b0649056e4ab") require.NoError(t, err) @@ -300,7 +309,7 @@ func TestFailedUserCherryPickRequestDueToPreReceiveError(t *testing.T) { for _, hookName := range operations.GitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := operations.OverrideHooks(hookName, hookContent) + remove, err := operations.WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -318,7 +327,7 @@ func TestFailedUserCherryPickRequestDueToCreateTreeError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -361,7 +370,7 @@ func TestFailedUserCherryPickRequestDueToCommitError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -401,7 +410,7 @@ func TestFailedUserCherryPickRequestDueToCommitError(t *testing.T) { require.Equal(t, "Branch diverged", response.CommitError) } -func runFullServer(t *testing.T) (*grpc.Server, string) { +func runFullServerWithHooks(t *testing.T) (*grpc.Server, string) { server := serverPkg.NewInsecure(operations.RubyServer) serverSocketPath := testhelper.GetTemporaryGitalySocketFileName() diff --git a/internal/service/operations/commit_files_test.go b/internal/service/operations/commit_files_test.go index 213d9e37a4..ad073dfa4a 100644 --- a/internal/service/operations/commit_files_test.go +++ b/internal/service/operations/commit_files_test.go @@ -25,15 +25,18 @@ var ( ) func TestSuccessfulUserCommitFilesRequest(t *testing.T) { - server, serverSocketPath := runFullServer(t) + testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) defer conn.Close() - testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) - defer cleanupFn() - ctxOuter, cancel := testhelper.Context() defer cancel() @@ -134,7 +137,7 @@ func TestSuccessfulUserCommitFilesRequest(t *testing.T) { } func TestSuccessfulUserCommitFilesRequestMove(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -162,6 +165,9 @@ func TestSuccessfulUserCommitFilesRequestMove(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + origFileContent := testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "show", branchName+":"+previousFilePath) md := testhelper.GitalyServersMetadata(t, serverSocketPath) ctx := metadata.NewOutgoingContext(ctxOuter, md) @@ -197,7 +203,7 @@ func TestSuccessfulUserCommitFilesRequestMove(t *testing.T) { } func TestSuccessfulUserCommitFilesRequestForceCommit(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -216,6 +222,9 @@ func TestSuccessfulUserCommitFilesRequestForceCommit(t *testing.T) { targetBranchName := "feature" startBranchName := []byte("master") + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + startBranchCommit, err := log.GetCommit(ctxOuter, testRepo, string(startBranchName)) require.NoError(t, err) @@ -249,7 +258,7 @@ func TestSuccessfulUserCommitFilesRequestForceCommit(t *testing.T) { } func TestSuccessfulUserCommitFilesRequestStartSha(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -271,6 +280,9 @@ func TestSuccessfulUserCommitFilesRequestStartSha(t *testing.T) { headerRequest := headerRequest(testRepo, user, targetBranchName, commitFilesMessage) setStartSha(headerRequest, startCommit.Id) + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + stream, err := client.UserCommitFiles(ctx) require.NoError(t, err) require.NoError(t, stream.Send(headerRequest)) @@ -289,7 +301,7 @@ func TestSuccessfulUserCommitFilesRequestStartSha(t *testing.T) { } func TestSuccessfulUserCommitFilesRequestStartShaRemoteRepository(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -315,6 +327,9 @@ func TestSuccessfulUserCommitFilesRequestStartShaRemoteRepository(t *testing.T) setStartSha(headerRequest, startCommit.Id) setStartRepository(headerRequest, testRepo) + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + stream, err := client.UserCommitFiles(ctx) require.NoError(t, err) require.NoError(t, stream.Send(headerRequest)) @@ -333,7 +348,7 @@ func TestSuccessfulUserCommitFilesRequestStartShaRemoteRepository(t *testing.T) } func TestSuccessfulUserCommitFilesRequestWithSpecialCharactersInSignature(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -342,12 +357,17 @@ func TestSuccessfulUserCommitFilesRequestWithSpecialCharactersInSignature(t *tes testRepo, _, cleanupFn := testhelper.InitBareRepo(t) defer cleanupFn() + glID := "key-123" + ctxOuter, cancel := testhelper.Context() defer cancel() md := testhelper.GitalyServersMetadata(t, serverSocketPath) targetBranchName := "master" + cleanupSrv := operations.SetupAndStartGitlabServer(t, glID, testRepo.GlRepository) + defer cleanupSrv() + testCases := []struct { desc string user *gitalypb.User @@ -355,12 +375,12 @@ func TestSuccessfulUserCommitFilesRequestWithSpecialCharactersInSignature(t *tes }{ { desc: "special characters at start and end", - user: &gitalypb.User{Name: []byte(".,:;<>\"'\nJane Doe.,:;<>'\"\n"), Email: []byte(".,:;<>'\"\njanedoe@gitlab.com.,:;<>'\"\n")}, + user: &gitalypb.User{Name: []byte(".,:;<>\"'\nJane Doe.,:;<>'\"\n"), Email: []byte(".,:;<>'\"\njanedoe@gitlab.com.,:;<>'\"\n"), GlId: glID}, author: &gitalypb.CommitAuthor{Name: []byte("Jane Doe"), Email: []byte("janedoe@gitlab.com")}, }, { desc: "special characters in the middle", - user: &gitalypb.User{Name: []byte("Jaoe"), Email: []byte("ja@gitlab.com")}, + user: &gitalypb.User{Name: []byte("Jaoe"), Email: []byte("ja@gitlab.com"), GlId: glID}, author: &gitalypb.CommitAuthor{Name: []byte("Jane Doe"), Email: []byte("janedoe@gitlab.com")}, }, } @@ -390,13 +410,13 @@ func TestSuccessfulUserCommitFilesRequestWithSpecialCharactersInSignature(t *tes } func TestFailedUserCommitFilesRequestDueToHooks(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) defer conn.Close() - testRepo, _, cleanupFn := testhelper.NewTestRepo(t) + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() ctxOuter, cancel := testhelper.Context() @@ -409,12 +429,14 @@ func TestFailedUserCommitFilesRequestDueToHooks(t *testing.T) { actionsRequest2 := actionContentRequest("My content") hookContent := []byte("#!/bin/sh\nprintenv | paste -sd ' ' -\nexit 1") + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + for _, hookName := range operations.GitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := operations.OverrideHooks(hookName, hookContent) + remove, err := operations.WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() - md := testhelper.GitalyServersMetadata(t, serverSocketPath) ctx := metadata.NewOutgoingContext(ctxOuter, md) stream, err := client.UserCommitFiles(ctx) @@ -433,7 +455,7 @@ func TestFailedUserCommitFilesRequestDueToHooks(t *testing.T) { } func TestFailedUserCommitFilesRequestDueToIndexError(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -442,6 +464,9 @@ func TestFailedUserCommitFilesRequestDueToIndexError(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + ctxOuter, cancel := testhelper.Context() defer cancel() @@ -505,7 +530,7 @@ func TestFailedUserCommitFilesRequestDueToIndexError(t *testing.T) { } func TestFailedUserCommitFilesRequest(t *testing.T) { - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -520,6 +545,10 @@ func TestFailedUserCommitFilesRequest(t *testing.T) { md := testhelper.GitalyServersMetadata(t, serverSocketPath) ctx := metadata.NewOutgoingContext(ctxOuter, md) branchName := "feature" + + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testCases := []struct { desc string req *gitalypb.UserCommitFilesRequest diff --git a/internal/service/operations/merge_test.go b/internal/service/operations/merge_test.go index e2dd5d4732..2074a5abf2 100644 --- a/internal/service/operations/merge_test.go +++ b/internal/service/operations/merge_test.go @@ -3,7 +3,6 @@ package operations import ( "fmt" "io/ioutil" - "os" "os/exec" "strings" "testing" @@ -50,10 +49,14 @@ func TestSuccessfulMerge(t *testing.T) { hooks := GitlabHooks hookTempfiles := make([]string, len(hooks)) for i, h := range hooks { - hookTempfiles[i] = WriteEnvToHook(t, testRepoPath, h) - defer os.Remove(hookTempfiles[i]) + var cleanup func() + hookTempfiles[i], cleanup = WriteEnvToCustomHook(t, testRepoPath, h) + defer cleanup() } + cleanupSrv := SetupAndStartGitlabServer(t, mergeUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + mergeCommitMessage := "Merged by Gitaly" firstRequest := &gitalypb.UserMergeBranchRequest{ Repository: testRepo, @@ -231,11 +234,14 @@ func TestFailedMergeDueToHooks(t *testing.T) { prepareMergeBranch(t, testRepoPath) + cleanupSrv := SetupAndStartGitlabServer(t, mergeUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + hookContent := []byte("#!/bin/sh\necho 'failure'\nexit 1") for _, hookName := range gitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -310,6 +316,9 @@ func TestSuccessfulUserFFBranchRequest(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", "-f", branchName, "6d394385cf567f80a8fd85055db1ab4c5295806f") defer exec.Command("git", "-C", testRepoPath, "branch", "-d", branchName).Run() + cleanupSrv := SetupAndStartGitlabServer(t, mergeUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + resp, err := client.UserFFBranch(ctx, request) require.NoError(t, err) require.Equal(t, expectedResponse, resp) @@ -434,11 +443,14 @@ func TestFailedUserFFBranchDueToHooks(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", "-f", branchName, "6d394385cf567f80a8fd85055db1ab4c5295806f") defer exec.Command("git", "-C", testRepoPath, "branch", "-d", branchName).Run() + cleanupSrv := SetupAndStartGitlabServer(t, mergeUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + hookContent := []byte("#!/bin/sh\necho 'failure'\nexit 1") for _, hookName := range gitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -691,7 +703,7 @@ func TestUserMergeToRefIgnoreHooksRequest(t *testing.T) { for _, hookName := range gitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() diff --git a/internal/service/operations/rebase_test.go b/internal/service/operations/rebase_test.go index d819d1d8bf..7d1a756772 100644 --- a/internal/service/operations/rebase_test.go +++ b/internal/service/operations/rebase_test.go @@ -5,7 +5,6 @@ package operations_test import ( "errors" "fmt" - "os" "strings" "testing" "time" @@ -35,7 +34,7 @@ func TestSuccessfulUserRebaseConfirmableRequest(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -47,6 +46,9 @@ func TestSuccessfulUserRebaseConfirmableRequest(t *testing.T) { testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, rebaseUser.GlId, testRepo.GlRepository, pushOptions...) + defer cleanupSrv() + branchSha := getBranchSha(t, testRepoPath, branchName) md := testhelper.GitalyServersMetadata(t, serverSocketPath) @@ -55,10 +57,10 @@ func TestSuccessfulUserRebaseConfirmableRequest(t *testing.T) { rebaseStream, err := client.UserRebaseConfirmable(ctx) require.NoError(t, err) - preReceiveHookOutputPath := operations.WriteEnvToHook(t, testRepoPath, "pre-receive") - postReceiveHookOutputPath := operations.WriteEnvToHook(t, testRepoPath, "post-receive") - defer os.Remove(preReceiveHookOutputPath) - defer os.Remove(postReceiveHookOutputPath) + preReceiveHookOutputPath, removePreReceive := operations.WriteEnvToCustomHook(t, testRepoPath, "pre-receive") + postReceiveHookOutputPath, removePostReceive := operations.WriteEnvToCustomHook(t, testRepoPath, "post-receive") + defer removePreReceive() + defer removePostReceive() headerRequest := buildHeaderRequest(testRepo, rebaseUser, "1", branchName, branchSha, testRepoCopy, "master") headerRequest.GetHeader().GitPushOptions = pushOptions @@ -101,7 +103,7 @@ func TestFailedRebaseUserRebaseConfirmableRequestDueToInvalidHeader(t *testing.T ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -110,6 +112,9 @@ func TestFailedRebaseUserRebaseConfirmableRequestDueToInvalidHeader(t *testing.T testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, rebaseUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -171,7 +176,7 @@ func TestAbortedUserRebaseConfirmable(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -196,6 +201,9 @@ func TestAbortedUserRebaseConfirmable(t *testing.T) { testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, rebaseUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() @@ -239,7 +247,7 @@ func TestFailedUserRebaseConfirmableDueToApplyBeingFalse(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -251,6 +259,9 @@ func TestFailedUserRebaseConfirmableDueToApplyBeingFalse(t *testing.T) { testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, rebaseUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + branchSha := getBranchSha(t, testRepoPath, branchName) md := testhelper.GitalyServersMetadata(t, serverSocketPath) @@ -285,7 +296,7 @@ func TestFailedUserRebaseConfirmableRequestDueToPreReceiveError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -297,13 +308,16 @@ func TestFailedUserRebaseConfirmableRequestDueToPreReceiveError(t *testing.T) { testRepoCopy, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := operations.SetupAndStartGitlabServer(t, rebaseUser.GlId, testRepo.GlRepository) + defer cleanupSrv() + branchSha := getBranchSha(t, testRepoPath, branchName) hookContent := []byte("#!/bin/sh\necho 'failure'\nexit 1") for i, hookName := range operations.GitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := operations.OverrideHooks(hookName, hookContent) + remove, err := operations.WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err, "set up hooks override") defer remove() @@ -347,7 +361,7 @@ func TestFailedUserRebaseConfirmableDueToGitError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) diff --git a/internal/service/operations/revert_test.go b/internal/service/operations/revert_test.go index 6a96c4313f..716e8f1b36 100644 --- a/internal/service/operations/revert_test.go +++ b/internal/service/operations/revert_test.go @@ -1,7 +1,6 @@ package operations_test import ( - "os" "testing" "github.com/stretchr/testify/require" @@ -17,7 +16,7 @@ func TestSuccessfulUserRevertRequest(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -38,6 +37,9 @@ func TestSuccessfulUserRevertRequest(t *testing.T) { GlId: "user-123", } + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + revertedCommit, err := log.GetCommit(ctxOuter, testRepo, "d59c60028b053793cecfb4022de34602e1a9218e") require.NoError(t, err) @@ -126,7 +128,7 @@ func TestSuccessfulGitHooksForUserRevertRequest(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -139,11 +141,15 @@ func TestSuccessfulGitHooksForUserRevertRequest(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", destinationBranch, "master") user := &gitalypb.User{ - Name: []byte("Ahmad Sherif"), - Email: []byte("ahmad@gitlab.com"), - GlId: "user-123", + Name: []byte("Ahmad Sherif"), + Email: []byte("ahmad@gitlab.com"), + GlId: "user-123", + GlUsername: "username-223", } + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + revertedCommit, err := log.GetCommit(ctxOuter, testRepo, "d59c60028b053793cecfb4022de34602e1a9218e") require.NoError(t, err) @@ -155,22 +161,23 @@ func TestSuccessfulGitHooksForUserRevertRequest(t *testing.T) { Message: []byte("Reverting " + revertedCommit.Id), } + var hookOutputFiles []string for _, hookName := range operations.GitlabHooks { - t.Run(hookName, func(t *testing.T) { - hookOutputTempPath := operations.WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + hookOutputTempPath, cleanup := operations.WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() + hookOutputFiles = append(hookOutputFiles, hookOutputTempPath) + } - md := testhelper.GitalyServersMetadata(t, serverSocketPath) - ctx := metadata.NewOutgoingContext(ctxOuter, md) + md := testhelper.GitalyServersMetadata(t, serverSocketPath) + ctx := metadata.NewOutgoingContext(ctxOuter, md) - response, err := client.UserRevert(ctx, request) - require.NoError(t, err) - require.Empty(t, response.PreReceiveError) + response, err := client.UserRevert(ctx, request) + require.NoError(t, err) + require.Empty(t, response.PreReceiveError) - output := string(testhelper.MustReadFile(t, hookOutputTempPath)) - require.Contains(t, output, "GL_ID="+user.GlId) - require.Contains(t, output, "GL_USERNAME="+user.GlUsername) - }) + for _, file := range hookOutputFiles { + output := string(testhelper.MustReadFile(t, file)) + require.Contains(t, output, "GL_USERNAME="+user.GlUsername) } } @@ -178,7 +185,7 @@ func TestFailedUserRevertRequestDueToValidations(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -264,7 +271,7 @@ func TestFailedUserRevertRequestDueToPreReceiveError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -282,6 +289,9 @@ func TestFailedUserRevertRequestDueToPreReceiveError(t *testing.T) { GlId: "user-123", } + cleanupSrv := operations.SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + revertedCommit, err := log.GetCommit(ctxOuter, testRepo, "d59c60028b053793cecfb4022de34602e1a9218e") require.NoError(t, err) @@ -297,7 +307,7 @@ func TestFailedUserRevertRequestDueToPreReceiveError(t *testing.T) { for _, hookName := range operations.GitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := operations.OverrideHooks(hookName, hookContent) + remove, err := operations.WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -315,7 +325,7 @@ func TestFailedUserRevertRequestDueToCreateTreeError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) @@ -358,7 +368,7 @@ func TestFailedUserRevertRequestDueToCommitError(t *testing.T) { ctxOuter, cancel := testhelper.Context() defer cancel() - server, serverSocketPath := runFullServer(t) + server, serverSocketPath := runFullServerWithHooks(t) defer server.Stop() client, conn := operations.NewOperationClient(t, serverSocketPath) diff --git a/internal/service/operations/submodules_test.go b/internal/service/operations/submodules_test.go index d544477515..71e81b13f4 100644 --- a/internal/service/operations/submodules_test.go +++ b/internal/service/operations/submodules_test.go @@ -23,6 +23,9 @@ func TestSuccessfulUserUpdateSubmoduleRequest(t *testing.T) { testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + commitMessage := []byte("Update Submodule message") testCases := []struct { @@ -272,6 +275,9 @@ func TestFailedUserUpdateSubmoduleRequestDueToSameReference(t *testing.T) { testRepo, _, cleanup := testhelper.NewTestRepo(t) defer cleanup() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserUpdateSubmoduleRequest{ Repository: testRepo, User: user, diff --git a/internal/service/operations/tags_test.go b/internal/service/operations/tags_test.go index aea03ab193..4273d7ccf3 100644 --- a/internal/service/operations/tags_test.go +++ b/internal/service/operations/tags_test.go @@ -1,7 +1,6 @@ package operations import ( - "os" "os/exec" "testing" @@ -38,6 +37,9 @@ func TestSuccessfulUserDeleteTagRequest(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "tag", tagNameInput) + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserDeleteTagRequest{ Repository: testRepo, TagName: []byte(tagNameInput), @@ -61,6 +63,9 @@ func TestSuccessfulGitHooksForUserDeleteTagRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + tagNameInput := "to-be-déleted-soon-tag" defer exec.Command("git", "-C", testRepoPath, "tag", "-d", tagNameInput).Run() @@ -81,8 +86,8 @@ func TestSuccessfulGitHooksForUserDeleteTagRequest(t *testing.T) { t.Run(hookName, func(t *testing.T) { testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "tag", tagNameInput) - hookOutputTempPath := WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + hookOutputTempPath, cleanup := WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() ctx, cancel := testhelper.Context() defer cancel() @@ -91,7 +96,6 @@ func TestSuccessfulGitHooksForUserDeleteTagRequest(t *testing.T) { require.NoError(t, err) output := testhelper.MustReadFile(t, hookOutputTempPath) - require.Contains(t, string(output), "GL_ID=user-123") require.Contains(t, string(output), "GL_USERNAME=johndoe") }) } @@ -155,12 +159,15 @@ func TestSuccessfulUserCreateTagRequest(t *testing.T) { t.Run(testCase.desc, func(t *testing.T) { request := &gitalypb.UserCreateTagRequest{ Repository: testRepo, - TagName: []byte(inputTagName), + TagName: []byte(testCase.tagName), TargetRevision: []byte(testCase.targetRevision), User: user, Message: []byte(testCase.message), } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + ctx, cancel := testhelper.Context() defer cancel() @@ -197,6 +204,10 @@ func TestSuccessfulGitHooksForUserCreateTagRequest(t *testing.T) { GlId: "user-123", GlUsername: "johndoe", } + + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserCreateTagRequest{ Repository: testRepo, TagName: []byte(tagName), @@ -208,8 +219,8 @@ func TestSuccessfulGitHooksForUserCreateTagRequest(t *testing.T) { t.Run(hookName, func(t *testing.T) { defer exec.Command("git", "-C", testRepoPath, "tag", "-d", tagName).Run() - hookOutputTempPath := WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + hookOutputTempPath, cleanup := WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() ctx, cancel := testhelper.Context() defer cancel() @@ -219,7 +230,6 @@ func TestSuccessfulGitHooksForUserCreateTagRequest(t *testing.T) { require.Empty(t, response.PreReceiveError) output := string(testhelper.MustReadFile(t, hookOutputTempPath)) - require.Contains(t, output, "GL_ID="+user.GlId) require.Contains(t, output, "GL_USERNAME="+user.GlUsername) }) } @@ -304,6 +314,9 @@ func TestFailedUserDeleteTagDueToHooks(t *testing.T) { GlId: "user-123", } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserDeleteTagRequest{ Repository: testRepo, TagName: []byte(tagNameInput), @@ -314,7 +327,7 @@ func TestFailedUserDeleteTagDueToHooks(t *testing.T) { for _, hookName := range gitlabPreHooks { t.Run(hookName, func(t *testing.T) { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -338,7 +351,7 @@ func TestFailedUserCreateTagDueToHooks(t *testing.T) { client, conn := newOperationClient(t, serverSocketPath) defer conn.Close() - testRepo, _, cleanupFn := testhelper.NewTestRepo(t) + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() user := &gitalypb.User{ @@ -347,6 +360,10 @@ func TestFailedUserCreateTagDueToHooks(t *testing.T) { GlId: "user-123", GlUsername: "johndoe", } + + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + request := &gitalypb.UserCreateTagRequest{ Repository: testRepo, TagName: []byte("new-tag"), @@ -357,7 +374,7 @@ func TestFailedUserCreateTagDueToHooks(t *testing.T) { hookContent := []byte("#!/bin/sh\necho GL_ID=$GL_ID\nexit 1") for _, hookName := range gitlabPreHooks { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -386,6 +403,9 @@ func TestFailedUserCreateTagRequestDueToTagExistence(t *testing.T) { GlId: "user-123", } + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + testCase := struct { tagName string targetRevision string diff --git a/internal/service/operations/testhelper_test.go b/internal/service/operations/testhelper_test.go index b5d6bb8bf2..38e461f527 100644 --- a/internal/service/operations/testhelper_test.go +++ b/internal/service/operations/testhelper_test.go @@ -5,12 +5,12 @@ import ( "io/ioutil" "net" "os" - "path" + "path/filepath" "testing" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/git/hooks" + "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/rubyserver" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -33,8 +33,7 @@ var ( ) func init() { - copy(GitlabHooks, gitlabPreHooks) - GitlabHooks = append(GitlabHooks, gitlabPostHooks...) + GitlabHooks = append(GitlabHooks, append(gitlabPreHooks, gitlabPostHooks...)...) } func TestMain(m *testing.M) { @@ -44,15 +43,21 @@ func TestMain(m *testing.M) { func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - hookDir, err := ioutil.TempDir("", "gitaly-tmp-hooks") + cwd, err := os.Getwd() if err != nil { log.Fatal(err) } - defer os.RemoveAll(hookDir) + gitlabShellDir := filepath.Join(cwd, "testdata", "gitlab-shell") + os.RemoveAll(gitlabShellDir) - hooks.Override = hookDir + if err := os.MkdirAll(gitlabShellDir, 0755); err != nil { + log.Fatal(err) + } + + config.Config.GitlabShell.Dir = filepath.Join(cwd, "testdata", "gitlab-shell") testhelper.ConfigureGitalySSH() + testhelper.ConfigureGitalyHooksBinary() if err := RubyServer.Start(); err != nil { log.Fatal(err) @@ -95,23 +100,42 @@ var NewOperationClient = newOperationClient // The callee is responsible for clean up of the specific hook, testMain removes // the hook dir -func WriteEnvToHook(t *testing.T, repoPath, hookName string) string { +func WriteEnvToCustomHook(t *testing.T, repoPath, hookName string) (string, func()) { hookOutputTemp, err := ioutil.TempFile("", "") require.NoError(t, err) require.NoError(t, hookOutputTemp.Close()) hookContent := fmt.Sprintf("#!/bin/sh\n/usr/bin/env > %s\n", hookOutputTemp.Name()) - _, err = OverrideHooks(hookName, []byte(hookContent)) + cleanupCustomHook, err := WriteCustomHook(repoPath, hookName, []byte(hookContent)) require.NoError(t, err) - return hookOutputTemp.Name() + return hookOutputTemp.Name(), func() { + cleanupCustomHook() + os.Remove(hookOutputTemp.Name()) + } } -// When testing hooks, the content for one specific hook can be defined, to simulate -// behaviours on different hook content -func OverrideHooks(name string, content []byte) (func(), error) { - fullPath := path.Join(hooks.Path(), name) +// write a hook in the repo/path.git/custom_hooks directory +func WriteCustomHook(repoPath, name string, content []byte) (func(), error) { + fullPath := filepath.Join(repoPath, "custom_hooks", name) + fullpathDir := filepath.Dir(fullPath) + if err := os.MkdirAll(fullpathDir, 0755); err != nil { + return func() {}, err + } + + return func() { + os.RemoveAll(fullpathDir) + }, ioutil.WriteFile(fullPath, content, 0755) +} - return func() { os.Remove(fullPath) }, ioutil.WriteFile(fullPath, content, 0755) +func SetupAndStartGitlabServer(t *testing.T, glID, glRepository string, gitPushOptions ...string) func() { + return testhelper.SetupAndStartGitlabServer(t, &testhelper.GitlabTestServerOptions{ + SecretToken: "secretToken", + GLID: glID, + GLRepository: glRepository, + PostReceiveCounterDecreased: true, + Protocol: "web", + GitPushOptions: gitPushOptions, + }) } diff --git a/internal/service/operations/update_branches_test.go b/internal/service/operations/update_branches_test.go index 95aabfd9ca..d332b9a1a4 100644 --- a/internal/service/operations/update_branches_test.go +++ b/internal/service/operations/update_branches_test.go @@ -2,7 +2,8 @@ package operations import ( "context" - "os" + "crypto/sha1" + "fmt" "testing" "github.com/stretchr/testify/require" @@ -25,6 +26,9 @@ func TestSuccessfulUserUpdateBranchRequest(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + server, serverSocketPath := runOperationServiceServer(t) defer server.Stop() @@ -62,8 +66,11 @@ func TestSuccessfulGitHooksForUserUpdateBranchRequest(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() - hookOutputTempPath := WriteEnvToHook(t, testRepoPath, hookName) - defer os.Remove(hookOutputTempPath) + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + + hookOutputTempPath, cleanup := WriteEnvToCustomHook(t, testRepoPath, hookName) + defer cleanup() ctx, cancel := testhelper.Context() defer cancel() @@ -81,7 +88,6 @@ func TestSuccessfulGitHooksForUserUpdateBranchRequest(t *testing.T) { require.Empty(t, response.PreReceiveError) output := string(testhelper.MustReadFile(t, hookOutputTempPath)) - require.Contains(t, output, "GL_ID="+user.GlId) require.Contains(t, output, "GL_USERNAME="+user.GlUsername) }) } @@ -91,6 +97,9 @@ func TestFailedUserUpdateBranchDueToHooks(t *testing.T) { testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + server, serverSocketPath := runOperationServiceServer(t) defer server.Stop() @@ -109,7 +118,7 @@ func TestFailedUserUpdateBranchDueToHooks(t *testing.T) { hookContent := []byte("#!/bin/sh\nprintenv | paste -sd ' ' -\nexit 1") for _, hookName := range gitlabPreHooks { - remove, err := OverrideHooks(hookName, hookContent) + remove, err := WriteCustomHook(testRepoPath, hookName, hookContent) require.NoError(t, err) defer remove() @@ -118,10 +127,7 @@ func TestFailedUserUpdateBranchDueToHooks(t *testing.T) { response, err := client.UserUpdateBranch(ctx, request) require.Nil(t, err) - require.Contains(t, response.PreReceiveError, "GL_ID="+user.GlId) require.Contains(t, response.PreReceiveError, "GL_USERNAME="+user.GlUsername) - require.Contains(t, response.PreReceiveError, "GL_REPOSITORY="+testRepo.GlRepository) - require.Contains(t, response.PreReceiveError, "GL_PROTOCOL=web") require.Contains(t, response.PreReceiveError, "PWD="+testRepoPath) } } @@ -136,6 +142,11 @@ func TestFailedUserUpdateBranchRequest(t *testing.T) { testRepo, _, cleanupFn := testhelper.NewTestRepo(t) defer cleanupFn() + cleanupSrv := SetupAndStartGitlabServer(t, user.GlId, testRepo.GlRepository) + defer cleanupSrv() + + revDoesntExist := fmt.Sprintf("%x", sha1.Sum([]byte("we need a non existent sha"))) + testCases := []struct { desc string branchName string @@ -187,7 +198,7 @@ func TestFailedUserUpdateBranchRequest(t *testing.T) { { desc: "non-existing newrev", branchName: updateBranchName, - newrev: []byte("i-dont-exist"), + newrev: []byte(revDoesntExist), oldrev: oldrev, user: user, code: codes.FailedPrecondition, @@ -196,7 +207,7 @@ func TestFailedUserUpdateBranchRequest(t *testing.T) { desc: "non-existing oldrev", branchName: updateBranchName, newrev: newrev, - oldrev: []byte("i-dont-exist"), + oldrev: []byte(revDoesntExist), user: user, code: codes.FailedPrecondition, }, diff --git a/internal/service/smarthttp/receive_pack_test.go b/internal/service/smarthttp/receive_pack_test.go index ea84656302..f1074ef063 100644 --- a/internal/service/smarthttp/receive_pack_test.go +++ b/internal/service/smarthttp/receive_pack_test.go @@ -276,7 +276,7 @@ func TestFailedReceivePackRequestDueToValidationError(t *testing.T) { func TestPostReceivePackToHooks(t *testing.T) { secretToken := "secret token" glRepository := "some_repo" - key := 123 + glID := "key-123" server, socket := runSmartHTTPServer(t) defer server.Stop() @@ -302,18 +302,16 @@ func TestPostReceivePackToHooks(t *testing.T) { changes := fmt.Sprintf("%s %s refs/heads/master\n", oldHead, push.newHead) - c := testhelper.GitlabServerConfig{ + ts := testhelper.NewGitlabTestServer(t, testhelper.GitlabTestServerOptions{ User: "", Password: "", SecretToken: secretToken, - Key: key, + GLID: glID, GLRepository: glRepository, Changes: changes, PostReceiveCounterDecreased: true, Protocol: "http", - } - - ts := testhelper.NewGitlabTestServer(t, c) + }) defer ts.Close() testhelper.WriteTemporaryGitlabShellConfigFile(t, tempGitlabShellDir, testhelper.GitlabShellConfig{GitlabURL: ts.URL}) @@ -336,7 +334,7 @@ func TestPostReceivePackToHooks(t *testing.T) { firstRequest := &gitalypb.PostReceivePackRequest{ Repository: repo, - GlId: fmt.Sprintf("key-%d", key), + GlId: glID, GlRepository: glRepository, } diff --git a/internal/service/ssh/receive_pack_test.go b/internal/service/ssh/receive_pack_test.go index 76ec4c82f1..7e4ad6fb32 100644 --- a/internal/service/ssh/receive_pack_test.go +++ b/internal/service/ssh/receive_pack_test.go @@ -208,7 +208,7 @@ func TestObjectPoolRefAdvertisementHidingSSH(t *testing.T) { func TestSSHReceivePackToHooks(t *testing.T) { secretToken := "secret token" glRepository := "some_repo" - key := 123 + glID := "key-123" restore := testhelper.EnableGitProtocolV2Support() defer restore() @@ -229,17 +229,16 @@ func TestSSHReceivePackToHooks(t *testing.T) { cloneDetails, cleanup := setupSSHClone(t) defer cleanup() - c := testhelper.GitlabServerConfig{ + ts := testhelper.NewGitlabTestServer(t, testhelper.GitlabTestServerOptions{ User: "", Password: "", SecretToken: secretToken, - Key: key, + GLID: glID, GLRepository: glRepository, Changes: fmt.Sprintf("%s %s refs/heads/master\n", string(cloneDetails.OldHead), string(cloneDetails.NewHead)), PostReceiveCounterDecreased: true, Protocol: "ssh", - } - ts := testhelper.NewGitlabTestServer(t, c) + }) defer ts.Close() testhelper.WriteTemporaryGitlabShellConfigFile(t, tempGitlabShellDir, testhelper.GitlabShellConfig{GitlabURL: ts.URL}) @@ -256,7 +255,7 @@ func TestSSHReceivePackToHooks(t *testing.T) { lHead, rHead, err := sshPush(t, cloneDetails, serverSocketPath, pushParams{ storageName: testRepo.GetStorageName(), - glID: fmt.Sprintf("key-%d", key), + glID: glID, glRepository: glRepository, gitProtocol: git.ProtocolV2, }) diff --git a/internal/testhelper/testserver.go b/internal/testhelper/testserver.go index 7cc7a7ffb1..6fa2f865fc 100644 --- a/internal/testhelper/testserver.go +++ b/internal/testhelper/testserver.go @@ -11,7 +11,8 @@ import ( "os" "os/exec" "path/filepath" - "strconv" + "regexp" + "strings" "testing" "time" @@ -186,18 +187,50 @@ func NewServer(tb testing.TB, streamInterceptors []grpc.StreamServerInterceptor, )) } -func handleAllowed(t *testing.T, secretToken string, key int, glRepository, changes, protocol string) func(w http.ResponseWriter, r *http.Request) { +var changeLineRegex = regexp.MustCompile("^[a-f0-9]{40} [a-f0-9]{40} refs/[^ ]+$") + +func handleAllowed(t *testing.T, options GitlabTestServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { require.NoError(t, r.ParseForm()) - require.Equal(t, http.MethodPost, r.Method) + require.Equal(t, http.MethodPost, r.Method, "expected http post") require.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) - require.Equal(t, strconv.Itoa(key), r.Form.Get("key_id")) - require.Equal(t, glRepository, r.Form.Get("gl_repository")) - require.Equal(t, protocol, r.Form.Get("protocol")) - require.Equal(t, changes, r.Form.Get("changes")) + + if options.GLID != "" { + glidSplit := strings.SplitN(options.GLID, "-", 2) + require.Len(t, glidSplit, 2, "number of GLID components") + + switch glidSplit[0] { + case "user": + require.Equal(t, glidSplit[1], r.Form.Get("user_id")) + case "key": + require.Equal(t, glidSplit[1], r.Form.Get("key_id")) + case "username": + require.Equal(t, glidSplit[1], r.Form.Get("username")) + default: + t.Fatalf("invalid GLID: %q", options.GLID) + } + } + + require.NotEmpty(t, r.Form.Get("gl_repository"), "gl_repository should not be empty") + if options.GLRepository != "" { + require.Equal(t, options.GLRepository, r.Form.Get("gl_repository"), "expected value of gl_repository should match form") + } + require.NotEmpty(t, r.Form.Get("protocol"), "protocol should not be empty") + if options.Protocol != "" { + require.Equal(t, options.Protocol, r.Form.Get("protocol"), "expected value of options.Protocol should match form") + } + + if options.Changes != "" { + require.Equal(t, options.Changes, r.Form.Get("changes"), "expected value of options.Changes should match form") + } else { + changeLines := strings.Split(strings.TrimSuffix(r.Form.Get("changes"), "\n"), "\n") + for _, line := range changeLines { + require.Regexp(t, changeLineRegex, line) + } + } w.Header().Set("Content-Type", "application/json") - if r.Form.Get("secret_token") == secretToken { + if r.Form.Get("secret_token") == options.SecretToken { w.Write([]byte(`{"status":true}`)) return } @@ -206,13 +239,16 @@ func handleAllowed(t *testing.T, secretToken string, key int, glRepository, chan } } -func handlePreReceive(t *testing.T, secretToken, glRepository string) func(w http.ResponseWriter, r *http.Request) { +func handlePreReceive(t *testing.T, options GitlabTestServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { require.NoError(t, r.ParseForm()) require.Equal(t, http.MethodPost, r.Method) require.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) - require.Equal(t, glRepository, r.Form.Get("gl_repository")) - require.Equal(t, secretToken, r.Form.Get("secret_token")) + require.NotEmpty(t, r.Form.Get("gl_repository"), "gl_repository should not be empty") + if options.GLRepository != "" { + require.Equal(t, options.GLRepository, r.Form.Get("gl_repository"), "expected value of gl_repository should match form") + } + require.Equal(t, options.SecretToken, r.Form.Get("secret_token"), "expected value of secret_token should match form") w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) @@ -220,43 +256,54 @@ func handlePreReceive(t *testing.T, secretToken, glRepository string) func(w htt } } -func handlePostReceive(t *testing.T, secretToken string, key int, glRepository, changes string, counterDecreased bool, gitPushOptions ...string) func(w http.ResponseWriter, r *http.Request) { +func handlePostReceive(t *testing.T, options GitlabTestServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { require.NoError(t, r.ParseForm()) require.Equal(t, http.MethodPost, r.Method) require.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) - require.Equal(t, glRepository, r.Form.Get("gl_repository")) - require.Equal(t, secretToken, r.Form.Get("secret_token")) - require.Equal(t, fmt.Sprintf("key-%d", key), r.Form.Get("identifier")) - require.Equal(t, changes, r.Form.Get("changes")) + require.NotEmpty(t, r.Form.Get("gl_repository")) + if options.GLRepository != "" { + require.Equal(t, options.GLRepository, r.Form.Get("gl_repository"), "expected value of gl_repository should match form") + } + require.Equal(t, options.SecretToken, r.Form.Get("secret_token"), "expected value of gl_repository should match form") - if len(gitPushOptions) > 0 { - require.Equal(t, gitPushOptions, r.Form["push_options[]"]) + require.NotEmpty(t, r.Form.Get("identifier"), "identifier should exist") + if options.GLID != "" { + require.Equal(t, options.GLID, r.Form.Get("identifier"), "identifier should be GLID") + } + + require.NotEmpty(t, r.Form.Get("changes"), "changes should exist") + if options.Changes != "" { + require.Regexp(t, options.Changes, r.Form.Get("changes"), "expected value of changes should match form") + } + + if len(options.GitPushOptions) > 0 { + require.Equal(t, options.GitPushOptions, r.Form["push_options[]"], "expected value of push_options should match form") } - w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf(`{"reference_counter_decreased": %v}`, counterDecreased))) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `{"reference_counter_decreased": %v}`, options.PostReceiveCounterDecreased) } } -func handleCheck(user, password string) func(w http.ResponseWriter, r *http.Request) { +func handleCheck(options GitlabTestServerOptions) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { u, p, ok := r.BasicAuth() - if !ok || u != user || p != password { + if !ok || u != options.User || p != options.Password { http.Error(w, "authorization failed", http.StatusUnauthorized) return } - w.Write([]byte(`{"redis": true}`)) w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, `{"redis": true}`) } } -// GitlabServerConfig is a config for a mock gitlab server -type GitlabServerConfig struct { +// GitlabTestServerOptions is a config for a mock gitlab server containing expected values +type GitlabTestServerOptions struct { User, Password, SecretToken string - Key int + GLID string GLRepository string Changes string PostReceiveCounterDecreased bool @@ -265,12 +312,12 @@ type GitlabServerConfig struct { } // NewGitlabTestServer returns a mock gitlab server that responds to the hook api endpoints -func NewGitlabTestServer(t *testing.T, c GitlabServerConfig) *httptest.Server { +func NewGitlabTestServer(t *testing.T, options GitlabTestServerOptions) *httptest.Server { mux := http.NewServeMux() - mux.Handle("/api/v4/internal/allowed", http.HandlerFunc(handleAllowed(t, c.SecretToken, c.Key, c.GLRepository, c.Changes, c.Protocol))) - mux.Handle("/api/v4/internal/pre_receive", http.HandlerFunc(handlePreReceive(t, c.SecretToken, c.GLRepository))) - mux.Handle("/api/v4/internal/post_receive", http.HandlerFunc(handlePostReceive(t, c.SecretToken, c.Key, c.GLRepository, c.Changes, c.PostReceiveCounterDecreased, c.GitPushOptions...))) - mux.Handle("/api/v4/internal/check", http.HandlerFunc(handleCheck(c.User, c.Password))) + mux.Handle("/api/v4/internal/allowed", http.HandlerFunc(handleAllowed(t, options))) + mux.Handle("/api/v4/internal/pre_receive", http.HandlerFunc(handlePreReceive(t, options))) + mux.Handle("/api/v4/internal/post_receive", http.HandlerFunc(handlePostReceive(t, options))) + mux.Handle("/api/v4/internal/check", http.HandlerFunc(handleCheck(options))) return httptest.NewServer(mux) } @@ -315,19 +362,19 @@ func WriteTemporaryGitalyConfigFile(t *testing.T, tempDir string) (string, func( } // EnvForHooks generates a set of environment variables for gitaly hooks -func EnvForHooks(t *testing.T, glRepo, gitlabShellDir string, key int, gitPushOptions ...string) []string { +func EnvForHooks(t *testing.T, glRepo, gitlabShellDir, glID string, gitPushOptions ...string) []string { rubyDir, err := filepath.Abs("../../ruby") require.NoError(t, err) - return append(append(oldEnv(t, glRepo, gitlabShellDir, key), []string{ + return append(append(oldEnv(t, glRepo, gitlabShellDir, glID), []string{ fmt.Sprintf("GITALY_BIN_DIR=%s", config.Config.BinDir), fmt.Sprintf("GITALY_RUBY_DIR=%s", rubyDir), }...), hooks.GitPushOptions(gitPushOptions)...) } -func oldEnv(t *testing.T, glRepo, gitlabShellDir string, key int) []string { +func oldEnv(t *testing.T, glRepo, gitlabShellDir, glID string) []string { return append([]string{ - fmt.Sprintf("GL_ID=key-%d", key), + fmt.Sprintf("GL_ID=%s", glID), fmt.Sprintf("GL_REPOSITORY=%s", glRepo), "GL_PROTOCOL=ssh", fmt.Sprintf("GITALY_GITLAB_SHELL_DIR=%s", gitlabShellDir), @@ -367,3 +414,14 @@ func NewServerWithHealth(t testing.TB, socketName string) (*grpc.Server, *health return srv, healthSrvr } + +func SetupAndStartGitlabServer(t *testing.T, c *GitlabTestServerOptions) func() { + ts := NewGitlabTestServer(t, *c) + + WriteTemporaryGitlabShellConfigFile(t, config.Config.GitlabShell.Dir, GitlabShellConfig{GitlabURL: ts.URL}) + WriteShellSecretFile(t, config.Config.GitlabShell.Dir, c.SecretToken) + + return func() { + ts.Close() + } +} -- GitLab