diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-10 09:40:45 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-10 11:38:13 +0300 |
commit | 256f96c4a2933d24c2a5924a30c6dfd8e876c4eb (patch) | |
tree | b1f9374690a8202f4991b89742f3a678830466fb | |
parent | 03a82b12c4f30ff3ffce9ad3332223f104ef7c53 (diff) |
gitaly-hooks: Convert test to use Bash instead of Ruby for hooks
One of our tests for gitaly-hooks asserts that both the command line
arguments and environment seen by the process match our expectations.
This is done by writing some custom hooks, where one of the hooks is
currently written in Ruby.
Refactor the hook to use Bash instead in order to reduce our reliance on
Ruby. While at it, convert the test to use `gittest.InitRepo()` instead
of `gittest.InitRepo()`: we don't depend on any references or objects
anyway, so there is not much of a point to use a completely seeded
repository.
-rw-r--r-- | cmd/gitaly-hooks/hooks_test.go | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 798772597..f223e039e 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -5,7 +5,6 @@ package main import ( "bytes" "context" - "encoding/json" "fmt" "os" "os/exec" @@ -27,6 +26,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/service/hook" "gitlab.com/gitlab-org/gitaly/v15/internal/gitlab" + "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" gitalylog "gitlab.com/gitlab-org/gitaly/v15/internal/log" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" @@ -273,30 +273,27 @@ func TestHooksUpdate(t *testing.T) { } func testHooksUpdate(t *testing.T, ctx context.Context, cfg config.Cfg, glValues glHookValues) { - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) refval, oldval, newval := "refval", strings.Repeat("a", 40), strings.Repeat("b", 40) + // Write a custom update hook that dumps all arguments seen by the hook... + customHookArgsPath := filepath.Join(testhelper.TempDir(t), "containsarguments") + testhelper.WriteExecutable(t, + filepath.Join(repoPath, "custom_hooks", "update.d", "dumpargsscript"), + []byte(fmt.Sprintf(`#!/bin/bash + echo "$@" >%q + `, customHookArgsPath)), + ) + + // ... and a second custom hook that dumps the environment variables. + customHookEnvPath := gittest.WriteEnvToCustomHook(t, repoPath, "update") + + var stdout, stderr bytes.Buffer cmd := exec.Command(cfg.BinaryPath("gitaly-hooks")) cmd.Args = []string{"update", refval, oldval, newval} cmd.Env = envForHooks(t, ctx, cfg, repo, glValues, proxyValues{}) cmd.Dir = repoPath - - tempDir := testhelper.TempDir(t) - - customHookArgsPath := filepath.Join(tempDir, "containsarguments") - dumpArgsToTempfileScript := fmt.Sprintf(`#!/usr/bin/env ruby -require 'json' -open('%s', 'w') { |f| f.puts(JSON.dump(ARGV)) } -`, customHookArgsPath) - // write a custom hook to path/to/repo.git/custom_hooks/update.d/dumpargsscript which dumps the args into a tempfile - testhelper.WriteExecutable(t, filepath.Join(repoPath, "custom_hooks", "update.d", "dumpargsscript"), []byte(dumpArgsToTempfileScript)) - - // write a custom hook to path/to/repo.git/custom_hooks/update which dumps the env into a tempfile - customHookOutputPath := gittest.WriteEnvToCustomHook(t, repoPath, "update") - - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout cmd.Stderr = &stderr cmd.Dir = repoPath @@ -305,15 +302,14 @@ open('%s', 'w') { |f| f.puts(JSON.dump(ARGV)) } require.Empty(t, stdout.String()) require.Empty(t, stderr.String()) - require.FileExists(t, customHookArgsPath) - - var inputs []string - - b := testhelper.MustReadFile(t, customHookArgsPath) - require.NoError(t, json.Unmarshal(b, &inputs)) - require.Equal(t, []string{refval, oldval, newval}, inputs) + // Ensure that the hook was executed with the expected arguments... + require.Equal(t, + fmt.Sprintf("%s %s %s", refval, oldval, newval), + text.ChompBytes(testhelper.MustReadFile(t, customHookArgsPath)), + ) - output := string(testhelper.MustReadFile(t, customHookOutputPath)) + // ... and with the expected environment variables. + output := string(testhelper.MustReadFile(t, customHookEnvPath)) require.Contains(t, output, "GL_USERNAME="+glValues.GLUsername) require.Contains(t, output, "GL_ID="+glValues.GLID) require.Contains(t, output, "GL_REPOSITORY="+repo.GetGlRepository()) |