diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-17 12:37:46 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-21 08:32:56 +0300 |
commit | 58ef2be67915e8afdaa6d79b99ab69f5413e51fe (patch) | |
tree | 91069dc8ded0ea7c7a285d1867e9e0a8c4e456ae | |
parent | d19c6eab3ecc995ee5b947134657ebd92e35deb8 (diff) |
tests: Convert tests to use Git exec helper
Convert tests which spawn Git commands to use the Git execution helpers
as exposed by the gittest package. Running Git directly without the use
of either those helpers or the Git command factory is not supported
anymore.
-rw-r--r-- | cmd/gitaly-ssh/auth_test.go | 15 | ||||
-rw-r--r-- | internal/gitaly/service/operations/merge_test.go | 4 | ||||
-rw-r--r-- | internal/gitaly/service/repository/calculate_checksum_test.go | 5 | ||||
-rw-r--r-- | internal/gitaly/service/ssh/upload_archive_test.go | 40 |
4 files changed, 22 insertions, 42 deletions
diff --git a/cmd/gitaly-ssh/auth_test.go b/cmd/gitaly-ssh/auth_test.go index 3dd82bea1..94b602f88 100644 --- a/cmd/gitaly-ssh/auth_test.go +++ b/cmd/gitaly-ssh/auth_test.go @@ -4,7 +4,6 @@ import ( "fmt" "net" "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -15,6 +14,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v14/internal/backchannel" "gitlab.com/gitlab-org/gitaly/v14/internal/cache" "gitlab.com/gitlab-org/gitaly/v14/internal/git" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/server" @@ -120,9 +120,7 @@ func TestConnectivity(t *testing.T) { t.Run(testcase.name, func(t *testing.T) { addr, certFile := testcase.addr(t, cfg) - cmd := exec.Command(cfg.Git.BinPath, "ls-remote", "git@localhost:test/test.git", "refs/heads/master") - cmd.Stderr = os.Stderr - cmd.Env = []string{ + env := []string{ fmt.Sprintf("GITALY_PAYLOAD=%s", payload), fmt.Sprintf("GITALY_ADDRESS=%s", addr), fmt.Sprintf("GITALY_WD=%s", cwd), @@ -130,17 +128,16 @@ func TestConnectivity(t *testing.T) { fmt.Sprintf("GIT_SSH_COMMAND=%s upload-pack", filepath.Join(cfg.BinDir, "gitaly-ssh")), fmt.Sprintf("SSL_CERT_FILE=%s", certFile), } - if testcase.proxy { - cmd.Env = append(cmd.Env, + env = append(env, "http_proxy=http://invalid:1234", "https_proxy=https://invalid:1234", ) } - output, err := cmd.Output() - - require.NoError(t, err, "git ls-remote exit status") + output := gittest.ExecOpts(t, cfg, gittest.ExecConfig{ + Env: env, + }, "ls-remote", "git@localhost:test/test.git", "refs/heads/master") require.True(t, strings.HasSuffix(strings.TrimSpace(string(output)), "refs/heads/master")) }) } diff --git a/internal/gitaly/service/operations/merge_test.go b/internal/gitaly/service/operations/merge_test.go index b266bb464..2996390a7 100644 --- a/internal/gitaly/service/operations/merge_test.go +++ b/internal/gitaly/service/operations/merge_test.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "os/exec" "path/filepath" "regexp" "strings" @@ -875,8 +874,7 @@ func TestUserMergeToRef_successful(t *testing.T) { // Writes in existingTargetRef beforeRefreshCommitSha := "a5391128b0ef5d21df5dd23d98557f4ef12fae20" - out, err := exec.Command(cfg.Git.BinPath, "-C", repoPath, "update-ref", string(existingTargetRef), beforeRefreshCommitSha).CombinedOutput() - require.NoError(t, err, "give an existing state to the target ref: %s", out) + gittest.Exec(t, cfg, "-C", repoPath, "update-ref", string(existingTargetRef), beforeRefreshCommitSha) testCases := []struct { desc string diff --git a/internal/gitaly/service/repository/calculate_checksum_test.go b/internal/gitaly/service/repository/calculate_checksum_test.go index c7c32fbcc..6b04e765a 100644 --- a/internal/gitaly/service/repository/calculate_checksum_test.go +++ b/internal/gitaly/service/repository/calculate_checksum_test.go @@ -23,8 +23,9 @@ func TestSuccessfulCalculateChecksum(t *testing.T) { for _, d := range []string{"refs/heads", "refs/tags", "refs/notes"} { require.NoError(t, os.MkdirAll(filepath.Join(repoPath, d), 0o755)) } - require.NoError(t, exec.Command("cp", "testdata/checksum-test-packed-refs", filepath.Join(repoPath, "packed-refs")).Run()) - require.NoError(t, exec.Command(cfg.Git.BinPath, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/feature").Run()) + + testhelper.CopyFile(t, "testdata/checksum-test-packed-refs", filepath.Join(repoPath, "packed-refs")) + gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/feature") request := &gitalypb.CalculateChecksumRequest{Repository: repo} testCtx, cancelCtx := testhelper.Context() diff --git a/internal/gitaly/service/ssh/upload_archive_test.go b/internal/gitaly/service/ssh/upload_archive_test.go index 8c2dc5e7c..1169238d7 100644 --- a/internal/gitaly/service/ssh/upload_archive_test.go +++ b/internal/gitaly/service/ssh/upload_archive_test.go @@ -3,13 +3,12 @@ package ssh import ( "fmt" "os" - "os/exec" "path/filepath" "testing" "time" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" @@ -115,34 +114,19 @@ func TestUploadArchiveSuccess(t *testing.T) { serverSocketPath := runSSHServer(t, cfg) - cmd := exec.Command(cfg.Git.BinPath, "archive", "master", "--remote=git@localhost:test/test.git") - - err := testArchive(t, cfg, serverSocketPath, repo, cmd) - require.NoError(t, err) -} - -func testArchive(t *testing.T, cfg config.Cfg, serverSocketPath string, testRepo *gitalypb.Repository, cmd *exec.Cmd) error { - req := &gitalypb.SSHUploadArchiveRequest{Repository: testRepo} - payload, err := protojson.Marshal(req) - + payload, err := protojson.Marshal(&gitalypb.SSHUploadArchiveRequest{ + Repository: repo, + }) require.NoError(t, err) - cmd.Env = []string{ - fmt.Sprintf("GITALY_ADDRESS=%s", serverSocketPath), - fmt.Sprintf("GITALY_PAYLOAD=%s", payload), - fmt.Sprintf("PATH=%s", ".:"+os.Getenv("PATH")), - fmt.Sprintf(`GIT_SSH_COMMAND=%s upload-archive`, filepath.Join(cfg.BinDir, "gitaly-ssh")), - } - - out, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("%v: %q", err, out) - } - if !cmd.ProcessState.Success() { - return fmt.Errorf("Failed to run `git archive`: %q", out) - } - - return nil + gittest.ExecOpts(t, cfg, gittest.ExecConfig{ + Env: []string{ + fmt.Sprintf("GITALY_ADDRESS=%s", serverSocketPath), + fmt.Sprintf("GITALY_PAYLOAD=%s", payload), + fmt.Sprintf("PATH=%s", ".:"+os.Getenv("PATH")), + fmt.Sprintf(`GIT_SSH_COMMAND=%s upload-archive`, filepath.Join(cfg.BinDir, "gitaly-ssh")), + }, + }, "archive", "master", "--remote=git@localhost:test/test.git") } func testUploadArchiveFailedResponse(t *testing.T, stream gitalypb.SSHService_SSHUploadArchiveClient) error { |