diff options
author | Luke Duncalfe <lduncalfe@eml.cc> | 2019-03-06 03:17:41 +0300 |
---|---|---|
committer | Luke Duncalfe <lduncalfe@eml.cc> | 2019-03-08 06:13:47 +0300 |
commit | 89fb3ca985e403b83945e5bede5d38f815006f43 (patch) | |
tree | cd04edf093ee66f75532ca78108cb5f9bc26f722 | |
parent | 63b99869dc78aae2311b1d01de53a5aa2dbffdf9 (diff) |
Add integration test for custom hook output48132-display-output-from-pre-receive-scripts
-rw-r--r-- | internal/service/operations/custom_hook_integration_test.go | 144 | ||||
-rw-r--r-- | internal/testhelper/hook_script.go | 22 |
2 files changed, 166 insertions, 0 deletions
diff --git a/internal/service/operations/custom_hook_integration_test.go b/internal/service/operations/custom_hook_integration_test.go new file mode 100644 index 000000000..15f28e8ea --- /dev/null +++ b/internal/service/operations/custom_hook_integration_test.go @@ -0,0 +1,144 @@ +package operations + +import ( + "context" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +var testCases = []struct { + scriptContent string + responseContains []string // strings we expect to be contained + responseNotContains []string // strings we should not see in PreReceiveError +}{ + { + "echo \"msg to STDOUT\";", + []string{}, + []string{"msg to STDOUT"}, + }, + { + "echo \"GitLab: msg to STDOUT\";", + []string{"msg to STDOUT"}, + []string{}, + }, + { + "echo \"gitlab: msg to STDOUT\";", // case-insensitive match on prefix + []string{"msg to STDOUT"}, + []string{}, + }, + { + "echo \"GitLab: msg to STDOUT\na second line\";", + []string{"msg to STDOUT"}, + []string{"a second line"}, + }, + { + "echo \"GitLab:msg to STDOUT\";", // no whitespace separation from prefix + []string{"msg to STDOUT"}, + []string{}, + }, + { + "echo \"msg to STDOUT, not prefixed by GitLab:, but containing it\";", + []string{}, + []string{"msg to STDOUT, not prefixed by GitLab:, but containing it"}, + }, + { + "1>&2 echo \"GitLab: msg to STDERR\";", + []string{"msg to STDERR"}, + []string{}, + }, + { + "1>&2 echo \"msg to STDERR, not prefixed by GitLab, but containing it\";", + []string{}, + []string{"msg to STDERR, not prefixed by GitLab:, but containing it"}, + }, +} + +func TestCustomHookResponseWhenHooksFail(t *testing.T) { + testRepo, _, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + + server, serverSocketPath := runOperationServiceServer(t) + defer server.Stop() + + client, conn := newOperationClient(t, serverSocketPath) + defer conn.Close() + + request := &gitalypb.UserCreateBranchRequest{ + Repository: testRepo, + BranchName: []byte("new-branch"), + StartPoint: []byte("c7fbe50c7c7419d9701eebe64b1fdacc3df5b9dd"), + User: user, + } + + for _, testCase := range testCases { + script := testhelper.FailingHookScript(testCase.scriptContent) + + for _, hookName := range gitlabPreHooks { + remove, err := OverrideHooks(hookName, []byte(script)) + + require.NoError(t, err) + defer remove() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + response, err := client.UserCreateBranch(ctx, request) + require.NoError(t, err) + + for _, msg := range testCase.responseContains { + require.Contains(t, response.PreReceiveError, msg) + } + + for _, msg := range testCase.responseNotContains { + require.NotContains(t, response.PreReceiveError, msg) + } + } + } +} + +func TestCustomHookResponseWhenHooksSucceed(t *testing.T) { + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + + server, serverSocketPath := runOperationServiceServer(t) + defer server.Stop() + + client, conn := newOperationClient(t, serverSocketPath) + defer conn.Close() + + branchName := "new-branch" + + request := &gitalypb.UserCreateBranchRequest{ + Repository: testRepo, + BranchName: []byte(branchName), + StartPoint: []byte("c7fbe50c7c7419d9701eebe64b1fdacc3df5b9dd"), + User: user, + } + + for _, testCase := range testCases { + script := testhelper.SuccessfulHookScript(testCase.scriptContent) + + for _, hookName := range gitlabPreHooks { + t.Run(hookName, func(t *testing.T) { + defer exec.Command("git", "-C", testRepoPath, "branch", "-D", branchName).Run() + + remove, err := OverrideHooks(hookName, []byte(script)) + require.NoError(t, err) + defer remove() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + response, err := client.UserCreateBranch(ctx, request) + require.NoError(t, err) + + // response.PreReceiveError is always empty when script exits with success + require.Empty(t, response.PreReceiveError) + }) + } + } +} diff --git a/internal/testhelper/hook_script.go b/internal/testhelper/hook_script.go new file mode 100644 index 000000000..86b1ea60c --- /dev/null +++ b/internal/testhelper/hook_script.go @@ -0,0 +1,22 @@ +package testhelper + +import "fmt" + +const scriptWrapper = ` +#!/bin/sh\ +%s +exit %d` + +// SuccessfulHookScript creates a bash script that will exit with a success code +func SuccessfulHookScript(scriptContent string) string { + return script(scriptContent, 0) +} + +// FailingHookScript creates a bash script that will exit with a failure code +func FailingHookScript(scriptContent string) string { + return script(scriptContent, 1) +} + +func script(scriptContent string, exitCode int8) string { + return fmt.Sprintf(scriptWrapper, scriptContent, exitCode) +} |