diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-06-17 13:49:24 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-06-20 17:13:44 +0300 |
commit | 3f5891af6ad846c7f43bc81d6f16e7e4ab6da1e8 (patch) | |
tree | c37df318d9f372071e0860f88e9291cab6b94b16 | |
parent | 6108cf3521b36d440e90e68b7d50dd46baf1b676 (diff) |
smarthttp: Move test helper functions to the bottom
Group together test helper functions at the bottom of our
PostReceivePack tests to make them easier to find.
-rw-r--r-- | internal/gitaly/service/smarthttp/receive_pack_test.go | 114 |
1 files changed, 57 insertions, 57 deletions
diff --git a/internal/gitaly/service/smarthttp/receive_pack_test.go b/internal/gitaly/service/smarthttp/receive_pack_test.go index 6da3cc659..299e5440b 100644 --- a/internal/gitaly/service/smarthttp/receive_pack_test.go +++ b/internal/gitaly/service/smarthttp/receive_pack_test.go @@ -279,55 +279,6 @@ func TestPostReceivePack_rejectViaHooks(t *testing.T) { require.Equal(t, expectedResponse, response) } -func performPush(t *testing.T, stream gitalypb.SmartHTTPService_PostReceivePackClient, firstRequest *gitalypb.PostReceivePackRequest, body io.Reader) string { - require.NoError(t, stream.Send(firstRequest)) - - sw := streamio.NewWriter(func(p []byte) error { - return stream.Send(&gitalypb.PostReceivePackRequest{Data: p}) - }) - _, err := io.Copy(sw, body) - require.NoError(t, err) - require.NoError(t, stream.CloseSend()) - - var response bytes.Buffer - rr := streamio.NewReader(func() ([]byte, error) { - resp, err := stream.Recv() - return resp.GetData(), err - }) - _, err = io.Copy(&response, rr) - require.NoError(t, err) - - return response.String() -} - -func createPushRequest(t *testing.T, cfg config.Cfg) (git.ObjectID, git.ObjectID, io.Reader) { - _, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) - - oldCommitID := git.ObjectID(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "HEAD"))) - newCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(oldCommitID)) - - // ReceivePack request is a packet line followed by a packet flush, then the pack file of the objects we want to push. - // This is explained a bit in https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols#_uploading_data - // We form the packet line the same way git executable does: https://github.com/git/git/blob/d1a13d3fcb252631361a961cb5e2bf10ed467cba/send-pack.c#L524-L527 - var request bytes.Buffer - gittest.WritePktlinef(t, &request, "%s %s refs/heads/master\x00 %s", oldCommitID, newCommitID, uploadPackCapabilities) - gittest.WritePktlinef(t, &request, "%s %s refs/heads/branch", git.ZeroOID, newCommitID) - gittest.WritePktlineFlush(t, &request) - - // We need to get a pack file containing the objects we want to push, so we use git pack-objects - // which expects a list of revisions passed through standard input. The list format means - // pack the objects needed if I have oldHead but not newHead (think of it from the perspective of the remote repo). - // For more info, check the man pages of both `git-pack-objects` and `git-rev-list --objects`. - stdin := strings.NewReader(fmt.Sprintf("^%s\n%s\n", oldCommitID, newCommitID)) - - // The options passed are the same ones used when doing an actual push. - gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: stdin, Stdout: &request}, - "-C", repoPath, "pack-objects", "--stdout", "--revs", "--thin", "--delta-base-offset", "-q", - ) - - return oldCommitID, newCommitID, &request -} - func TestPostReceivePack_requestValidation(t *testing.T) { t.Parallel() @@ -599,14 +550,6 @@ func TestPostReceivePack_fsck(t *testing.T) { require.Contains(t, response, "duplicateEntries: contains duplicate file entries") } -func drainPostReceivePackResponse(stream gitalypb.SmartHTTPService_PostReceivePackClient) error { - var err error - for err == nil { - _, err = stream.Recv() - } - return err -} - func TestPostReceivePack_hooks(t *testing.T) { t.Parallel() @@ -867,3 +810,60 @@ func TestPostReceivePack_notAllowed(t *testing.T) { require.Equal(t, 1, refTransactionServer.called) } + +func createPushRequest(t *testing.T, cfg config.Cfg) (git.ObjectID, git.ObjectID, io.Reader) { + _, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + + oldCommitID := git.ObjectID(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "HEAD"))) + newCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(oldCommitID)) + + // ReceivePack request is a packet line followed by a packet flush, then the pack file of the objects we want to push. + // This is explained a bit in https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols#_uploading_data + // We form the packet line the same way git executable does: https://github.com/git/git/blob/d1a13d3fcb252631361a961cb5e2bf10ed467cba/send-pack.c#L524-L527 + var request bytes.Buffer + gittest.WritePktlinef(t, &request, "%s %s refs/heads/master\x00 %s", oldCommitID, newCommitID, uploadPackCapabilities) + gittest.WritePktlinef(t, &request, "%s %s refs/heads/branch", git.ZeroOID, newCommitID) + gittest.WritePktlineFlush(t, &request) + + // We need to get a pack file containing the objects we want to push, so we use git pack-objects + // which expects a list of revisions passed through standard input. The list format means + // pack the objects needed if I have oldHead but not newHead (think of it from the perspective of the remote repo). + // For more info, check the man pages of both `git-pack-objects` and `git-rev-list --objects`. + stdin := strings.NewReader(fmt.Sprintf("^%s\n%s\n", oldCommitID, newCommitID)) + + // The options passed are the same ones used when doing an actual push. + gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: stdin, Stdout: &request}, + "-C", repoPath, "pack-objects", "--stdout", "--revs", "--thin", "--delta-base-offset", "-q", + ) + + return oldCommitID, newCommitID, &request +} + +func performPush(t *testing.T, stream gitalypb.SmartHTTPService_PostReceivePackClient, firstRequest *gitalypb.PostReceivePackRequest, body io.Reader) string { + require.NoError(t, stream.Send(firstRequest)) + + sw := streamio.NewWriter(func(p []byte) error { + return stream.Send(&gitalypb.PostReceivePackRequest{Data: p}) + }) + _, err := io.Copy(sw, body) + require.NoError(t, err) + require.NoError(t, stream.CloseSend()) + + var response bytes.Buffer + rr := streamio.NewReader(func() ([]byte, error) { + resp, err := stream.Recv() + return resp.GetData(), err + }) + _, err = io.Copy(&response, rr) + require.NoError(t, err) + + return response.String() +} + +func drainPostReceivePackResponse(stream gitalypb.SmartHTTPService_PostReceivePackClient) error { + var err error + for err == nil { + _, err = stream.Recv() + } + return err +} |