diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2023-07-14 16:08:19 +0300 |
---|---|---|
committer | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2023-07-14 16:08:19 +0300 |
commit | 9b57f4dfe66a3d4da84b38e61a3a4d9270b485cf (patch) | |
tree | ae48b2dca2b2858b9f5ffc265f3c45f39404fcd0 | |
parent | 4c314694734330f910ce5d1409c0d462a02628ff (diff) |
Run hooks in ReceivePack tests
-rw-r--r-- | internal/git/gittest/hooks.go | 29 | ||||
-rw-r--r-- | internal/gitaly/service/smarthttp/receive_pack_test.go | 14 | ||||
-rw-r--r-- | internal/gitaly/service/ssh/receive_pack_test.go | 5 |
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")) |