From 0983a83714199f0fb59eba04459aaab44a89de57 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 28 Mar 2022 08:17:26 +0200 Subject: ssh: Refactor tests to not create worktree Our SSH tests need to set up a repository with some new objects so that it can verify that these objects end up on the remote side after the push. To create those objects, we currently use a worktree and then execute git-commit(1). This usage pattern is outdated though: we have a `gittest.WriteCommit()` helper which does not require a worktree. Refactor tests to use this new helper so that we can get rid of using a worktree. --- internal/gitaly/service/ssh/receive_pack_test.go | 53 +++++++----------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'internal/gitaly/service') diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go index cda2fa4aa..266b99844 100644 --- a/internal/gitaly/service/ssh/receive_pack_test.go +++ b/internal/gitaly/service/ssh/receive_pack_test.go @@ -571,18 +571,23 @@ type SSHCloneDetails struct { // setupSSHClone sets up a test clone func setupSSHClone(t *testing.T, cfg config.Cfg, remoteRepo *gitalypb.Repository, remoteRepoPath string) (SSHCloneDetails, func()) { - // Make a non-bare clone of the test repo to act as a local one - _, localRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0], gittest.CloneRepoOpts{ - WithWorktree: true, - }) - - // We need git thinking we're pushing over SSH... - oldHead, newHead, success := makeCommit(t, cfg, localRepoPath) - require.True(t, success) + _, localRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + + oldHead := text.ChompBytes(gittest.Exec(t, cfg, "-C", localRepoPath, "rev-parse", "HEAD")) + newHead := gittest.WriteCommit(t, cfg, localRepoPath, + gittest.WithMessage(fmt.Sprintf("Testing ReceivePack RPC around %d", time.Now().Unix())), + gittest.WithTreeEntries(gittest.TreeEntry{ + Path: "foo.txt", + Mode: "100644", + Content: "foo bar", + }), + gittest.WithBranch("master"), + gittest.WithParents(git.ObjectID(oldHead)), + ) return SSHCloneDetails{ - OldHead: oldHead, - NewHead: newHead, + OldHead: []byte(oldHead), + NewHead: []byte(newHead.String()), LocalRepoPath: localRepoPath, RemoteRepoPath: remoteRepoPath, TempRepo: remoteRepo.GetRelativePath(), @@ -644,34 +649,6 @@ func testCloneAndPush(t *testing.T, cfg config.Cfg, serverSocketPath string, rem return sshPush(t, cfg, cloneDetails, serverSocketPath, params) } -// makeCommit creates a new commit and returns oldHead, newHead, success -func makeCommit(t *testing.T, cfg config.Cfg, localRepoPath string) ([]byte, []byte, bool) { - commitMsg := fmt.Sprintf("Testing ReceivePack RPC around %d", time.Now().Unix()) - committerName := "Scrooge McDuck" - committerEmail := "scrooge@mcduck.com" - newFilePath := localRepoPath + "/foo.txt" - - // Create a tiny file and add it to the index - require.NoError(t, os.WriteFile(newFilePath, []byte("foo bar"), 0o644)) - gittest.Exec(t, cfg, "-C", localRepoPath, "add", ".") - - // The latest commit ID on the remote repo - oldHead := bytes.TrimSpace(gittest.Exec(t, cfg, "-C", localRepoPath, "rev-parse", "master")) - - gittest.Exec(t, cfg, "-C", localRepoPath, - "-c", fmt.Sprintf("user.name=%s", committerName), - "-c", fmt.Sprintf("user.email=%s", committerEmail), - "commit", "-m", commitMsg) - if t.Failed() { - return nil, nil, false - } - - // The commit ID we want to push to the remote repo - newHead := bytes.TrimSpace(gittest.Exec(t, cfg, "-C", localRepoPath, "rev-parse", "master")) - - return oldHead, newHead, true -} - func drainPostReceivePackResponse(stream gitalypb.SSHService_SSHReceivePackClient) error { var err error for err == nil { -- cgit v1.2.3