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

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuang-Minh Nguyen <qmnguyen@gitlab.com>2023-07-14 16:08:21 +0300
committerQuang-Minh Nguyen <qmnguyen@gitlab.com>2023-07-14 16:08:21 +0300
commit9a49509c84ecbee9d68a771985f545f863fa6592 (patch)
tree523c0c1d7adc9d25305f0740266a0b98774e9ece
parent63b50382ad18a30dbf95110d68018d1cc672a28e (diff)
parent9b57f4dfe66a3d4da84b38e61a3a4d9270b485cf (diff)
Merge branch 'smh-run-hooks-receive-pack' into 'master'
Run hooks in ReceivePack tests See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/6061 Merged-by: Quang-Minh Nguyen <qmnguyen@gitlab.com> Approved-by: Quang-Minh Nguyen <qmnguyen@gitlab.com> Reviewed-by: Will Chandler <wchandler@gitlab.com> Reviewed-by: Quang-Minh Nguyen <qmnguyen@gitlab.com> Co-authored-by: Will Chandler <wchandler@gitlab.com> Co-authored-by: Sami Hiltunen <shiltunen@gitlab.com>
-rw-r--r--internal/git/gittest/hooks.go29
-rw-r--r--internal/gitaly/service/smarthttp/receive_pack_test.go14
-rw-r--r--internal/gitaly/service/ssh/receive_pack_test.go5
3 files changed, 30 insertions, 18 deletions
diff --git a/internal/git/gittest/hooks.go b/internal/git/gittest/hooks.go
index 4c9233daa..b93a74741 100644
--- a/internal/git/gittest/hooks.go
+++ b/internal/git/gittest/hooks.go
@@ -2,10 +2,11 @@ package gittest
import (
"fmt"
+ "os"
"path/filepath"
"testing"
- "gitlab.com/gitlab-org/gitaly/v16/internal/git"
+ "github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)
@@ -44,16 +45,28 @@ func WriteCustomHook(tb testing.TB, repoPath, name string, content []byte) strin
return fullPath
}
-// CaptureHookEnv creates a Git command factory which injects a bogus 'update' Git hook to sniff out
-// what environment variables get set for hooks.
-func CaptureHookEnv(tb testing.TB, cfg config.Cfg) (git.CommandFactory, string) {
+// CaptureHookEnv wraps the 'gitaly-hooks' binary with a script that dumps the environment
+// variables the update hook received prior to executing the actual hook. It returns the path
+// to the file that the dump is written to.
+func CaptureHookEnv(tb testing.TB, cfg config.Cfg) string {
+ tb.Helper()
+
hooksPath := testhelper.TempDir(tb)
hookOutputFile := filepath.Join(hooksPath, "hook.env")
- testhelper.WriteExecutable(tb, filepath.Join(hooksPath, "update"), []byte(fmt.Sprintf(
+ binPath := cfg.BinaryPath("gitaly-hooks")
+ originalBinPath := filepath.Join(filepath.Dir(binPath), "gitaly-hooks-original")
+
+ require.NoError(tb, os.Rename(binPath, originalBinPath))
+
+ testhelper.WriteExecutable(tb, binPath, []byte(fmt.Sprintf(
`#!/usr/bin/env bash
- env | grep -e ^GIT -e ^GL_ >%q
- `, hookOutputFile)))
+if [ "$(basename "$0")" == update ]; then
+ env | grep -e ^GIT -e ^GL_ >%q
+fi
+exec -a "$0" %s "$@"
+`, hookOutputFile, originalBinPath,
+ )))
- return NewCommandFactory(tb, cfg, git.WithHooksPath(hooksPath)), hookOutputFile
+ return hookOutputFile
}
diff --git a/internal/gitaly/service/smarthttp/receive_pack_test.go b/internal/gitaly/service/smarthttp/receive_pack_test.go
index a1d0cfff1..16a8b91f8 100644
--- a/internal/gitaly/service/smarthttp/receive_pack_test.go
+++ b/internal/gitaly/service/smarthttp/receive_pack_test.go
@@ -39,11 +39,11 @@ func TestPostReceivePack_successful(t *testing.T) {
cfg := testcfg.Build(t)
cfg.GitlabShell.Dir = "/foo/bar/gitlab-shell"
- gitCmdFactory, hookOutputFile := gittest.CaptureHookEnv(t, cfg)
- server := startSmartHTTPServerWithOptions(t, cfg, nil, []testserver.GitalyServerOpt{
- testserver.WithGitCommandFactory(gitCmdFactory),
- })
+ testcfg.BuildGitalyHooks(t, cfg)
+ hookOutputFile := gittest.CaptureHookEnv(t, cfg)
+
+ server := startSmartHTTPServer(t, cfg)
cfg.SocketPath = server.Address()
repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
@@ -409,10 +409,8 @@ func TestPostReceivePack_invalidObjects(t *testing.T) {
ctx := testhelper.Context(t)
cfg := testcfg.Build(t)
- gitCmdFactory, _ := gittest.CaptureHookEnv(t, cfg)
- server := startSmartHTTPServerWithOptions(t, cfg, nil, []testserver.GitalyServerOpt{
- testserver.WithGitCommandFactory(gitCmdFactory),
- })
+ testcfg.BuildGitalyHooks(t, cfg)
+ server := startSmartHTTPServer(t, cfg)
cfg.SocketPath = server.Address()
client := newSmartHTTPClient(t, server.Address(), cfg.Auth.Token)
diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go
index 7b2ab45f0..53592180e 100644
--- a/internal/gitaly/service/ssh/receive_pack_test.go
+++ b/internal/gitaly/service/ssh/receive_pack_test.go
@@ -123,10 +123,11 @@ func TestReceivePack_success(t *testing.T) {
cfg := testcfg.Build(t)
cfg.GitlabShell.Dir = "/foo/bar/gitlab-shell"
- gitCmdFactory, hookOutputFile := gittest.CaptureHookEnv(t, cfg)
+ testcfg.BuildGitalyHooks(t, cfg)
+ hookOutputFile := gittest.CaptureHookEnv(t, cfg)
testcfg.BuildGitalySSH(t, cfg)
- cfg.SocketPath = runSSHServer(t, cfg, testserver.WithGitCommandFactory(gitCmdFactory))
+ cfg.SocketPath = runSSHServer(t, cfg)
remoteRepo, remoteRepoPath := gittest.CreateRepository(t, ctx, cfg)
gittest.WriteCommit(t, cfg, remoteRepoPath, gittest.WithBranch("main"))