diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2021-03-26 17:58:30 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2021-03-26 17:58:30 +0300 |
commit | a134e059dafa23f29255c1b626887ae16201a819 (patch) | |
tree | 76339828805343547ace64ee2902b9106963618a | |
parent | 720ae57f71081bbcecef9402ad3634715ece41a6 (diff) | |
parent | be3b92b563664c2e324cfcec8840e4cdf0836ab0 (diff) |
Merge branch 'ps-rm-config-ref' into 'master'
Removal of config.Config from ref package
See merge request gitlab-org/gitaly!3298
-rw-r--r-- | internal/gitaly/service/ref/branches_test.go | 26 | ||||
-rw-r--r-- | internal/gitaly/service/ref/delete_refs_test.go | 37 | ||||
-rw-r--r-- | internal/gitaly/service/ref/list_new_blobs_test.go | 14 | ||||
-rw-r--r-- | internal/gitaly/service/ref/list_new_commits_test.go | 14 | ||||
-rw-r--r-- | internal/gitaly/service/ref/pack_refs_test.go | 23 | ||||
-rw-r--r-- | internal/gitaly/service/ref/refexists_test.go | 32 | ||||
-rw-r--r-- | internal/gitaly/service/ref/refname_test.go | 54 | ||||
-rw-r--r-- | internal/gitaly/service/ref/refs_test.go | 349 | ||||
-rw-r--r-- | internal/gitaly/service/ref/remote_branches_test.go | 30 | ||||
-rw-r--r-- | internal/gitaly/service/ref/tag_messages_test.go | 22 | ||||
-rw-r--r-- | internal/gitaly/service/ref/testhelper_test.go | 42 |
11 files changed, 180 insertions, 463 deletions
diff --git a/internal/gitaly/service/ref/branches_test.go b/internal/gitaly/service/ref/branches_test.go index 1e402d96b..20a3fe5c7 100644 --- a/internal/gitaly/service/ref/branches_test.go +++ b/internal/gitaly/service/ref/branches_test.go @@ -5,9 +5,7 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -17,16 +15,9 @@ func TestSuccessfulFindBranchRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, repoProto, _, client := setupRefService(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepoProto, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - repo := localrepo.New(git.NewExecCommandFactory(config.Config), testRepoProto, config.Config) + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) branchesByName := make(map[git.ReferenceName]*gitalypb.Branch) for branchName, revision := range map[git.ReferenceName]git.Revision{ @@ -78,7 +69,7 @@ func TestSuccessfulFindBranchRequest(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { request := &gitalypb.FindBranchRequest{ - Repository: testRepoProto, + Repository: repoProto, Name: []byte(testCase.branchName), } @@ -94,14 +85,7 @@ func TestSuccessfulFindBranchRequest(t *testing.T) { } func TestFailedFindBranchRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) testCases := []struct { desc string @@ -118,7 +102,7 @@ func TestFailedFindBranchRequest(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { request := &gitalypb.FindBranchRequest{ - Repository: testRepo, + Repository: repo, Name: []byte(testCase.branchName), } diff --git a/internal/gitaly/service/ref/delete_refs_test.go b/internal/gitaly/service/ref/delete_refs_test.go index f3af00345..80c16ff89 100644 --- a/internal/gitaly/service/ref/delete_refs_test.go +++ b/internal/gitaly/service/ref/delete_refs_test.go @@ -8,18 +8,13 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" ) func TestSuccessfulDeleteRefs(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + cfg, client := setupRefServiceWithoutRepo(t) testCases := []struct { desc string @@ -41,7 +36,7 @@ func TestSuccessfulDeleteRefs(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { - repo, repoPath, cleanupFn := gittest.CloneRepo(t) + repo, repoPath, cleanupFn := gittest.CloneRepoAtStorage(t, cfg.Storages[0], testCase.desc) defer cleanupFn() testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "update-ref", "refs/delete/a", "b83d6e391c22777fca1ed3012fce84f633d7fed0") @@ -57,7 +52,7 @@ func TestSuccessfulDeleteRefs(t *testing.T) { require.NoError(t, err) // Ensure that the internal refs are gone, but the others still exist - refs, err := localrepo.New(git.NewExecCommandFactory(config.Config), repo, config.Config).GetReferences(ctx, "refs/") + refs, err := localrepo.New(git.NewExecCommandFactory(cfg), repo, cfg).GetReferences(ctx, "refs/") require.NoError(t, err) refNames := make([]string, len(refs)) @@ -75,18 +70,11 @@ func TestSuccessfulDeleteRefs(t *testing.T) { } func TestFailedDeleteRefsRequestDueToGitError(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + _, repo, _, client := setupRefService(t) ctx, cancel := testhelper.Context() defer cancel() - repo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - request := &gitalypb.DeleteRefsRequest{ Repository: repo, Refs: [][]byte{[]byte(`refs\tails\invalid-ref-format`)}, @@ -99,14 +87,7 @@ func TestFailedDeleteRefsRequestDueToGitError(t *testing.T) { } func TestFailedDeleteRefsDueToValidation(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) testCases := []struct { desc string @@ -134,14 +115,14 @@ func TestFailedDeleteRefsDueToValidation(t *testing.T) { { desc: "No prefixes nor refs", request: &gitalypb.DeleteRefsRequest{ - Repository: testRepo, + Repository: repo, }, code: codes.InvalidArgument, }, { desc: "prefixes with refs", request: &gitalypb.DeleteRefsRequest{ - Repository: testRepo, + Repository: repo, ExceptWithPrefix: [][]byte{[]byte("exclude-this")}, Refs: [][]byte{[]byte("delete-this")}, }, @@ -150,7 +131,7 @@ func TestFailedDeleteRefsDueToValidation(t *testing.T) { { desc: "Empty prefix", request: &gitalypb.DeleteRefsRequest{ - Repository: testRepo, + Repository: repo, ExceptWithPrefix: [][]byte{[]byte("exclude-this"), []byte{}}, }, code: codes.InvalidArgument, @@ -158,7 +139,7 @@ func TestFailedDeleteRefsDueToValidation(t *testing.T) { { desc: "Empty ref", request: &gitalypb.DeleteRefsRequest{ - Repository: testRepo, + Repository: repo, Refs: [][]byte{[]byte("delete-this"), []byte{}}, }, code: codes.InvalidArgument, diff --git a/internal/gitaly/service/ref/list_new_blobs_test.go b/internal/gitaly/service/ref/list_new_blobs_test.go index 9eee39248..8093ee9da 100644 --- a/internal/gitaly/service/ref/list_new_blobs_test.go +++ b/internal/gitaly/service/ref/list_new_blobs_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -16,17 +15,10 @@ func TestListNewBlobs(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, repoPath, client := setupRefService(t) oid := "ab2c9622c02288a2bbaaf35d96088cfdff31d9d9" - testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", "-D", "gitaly-diff-stuff") + testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "branch", "-D", "gitaly-diff-stuff") testCases := []struct { revision string @@ -54,7 +46,7 @@ func TestListNewBlobs(t *testing.T) { } for _, tc := range testCases { - request := &gitalypb.ListNewBlobsRequest{Repository: testRepo, CommitId: tc.revision, Limit: 0} + request := &gitalypb.ListNewBlobsRequest{Repository: repo, CommitId: tc.revision, Limit: 0} stream, err := client.ListNewBlobs(ctx, request) require.NoError(t, err) diff --git a/internal/gitaly/service/ref/list_new_commits_test.go b/internal/gitaly/service/ref/list_new_commits_test.go index fdc0cd6b6..ccddf4afc 100644 --- a/internal/gitaly/service/ref/list_new_commits_test.go +++ b/internal/gitaly/service/ref/list_new_commits_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -16,17 +15,10 @@ func TestListNewCommits(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, repoPath, client := setupRefService(t) oid := "0031876facac3f2b2702a0e53a26e89939a42209" - testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "branch", "-D", "few-commits") + testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "branch", "-D", "few-commits") testCases := []struct { revision string @@ -67,7 +59,7 @@ func TestListNewCommits(t *testing.T) { for _, tc := range testCases { t.Run(tc.revision, func(t *testing.T) { - request := &gitalypb.ListNewCommitsRequest{Repository: testRepo, CommitId: tc.revision} + request := &gitalypb.ListNewCommitsRequest{Repository: repo, CommitId: tc.revision} stream, err := client.ListNewCommits(ctx, request) require.NoError(t, err) diff --git a/internal/gitaly/service/ref/pack_refs_test.go b/internal/gitaly/service/ref/pack_refs_test.go index ef7c58c99..5ed920405 100644 --- a/internal/gitaly/service/ref/pack_refs_test.go +++ b/internal/gitaly/service/ref/pack_refs_test.go @@ -12,9 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) @@ -23,18 +21,11 @@ func TestPackRefsSuccessfulRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, repoProto, repoPath, client := setupRefService(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + packedRefs := linesInPackfile(t, repoPath) - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - packedRefs := linesInPackfile(t, testRepoPath) - - repo := localrepo.New(git.NewExecCommandFactory(config.Config), testRepo, config.Config) + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) // creates some new heads newBranches := 10 @@ -43,17 +34,17 @@ func TestPackRefsSuccessfulRequest(t *testing.T) { } // pack all refs - _, err := client.PackRefs(ctx, &gitalypb.PackRefsRequest{Repository: testRepo}) + _, err := client.PackRefs(ctx, &gitalypb.PackRefsRequest{Repository: repoProto}) require.NoError(t, err) - files, err := ioutil.ReadDir(filepath.Join(testRepoPath, "refs/heads")) + files, err := ioutil.ReadDir(filepath.Join(repoPath, "refs/heads")) require.NoError(t, err) assert.Len(t, files, 0, "git pack-refs --all should have packed all refs in refs/heads") - assert.Equal(t, packedRefs+newBranches, linesInPackfile(t, testRepoPath), fmt.Sprintf("should have added %d new lines to the packfile", newBranches)) + assert.Equal(t, packedRefs+newBranches, linesInPackfile(t, repoPath), fmt.Sprintf("should have added %d new lines to the packfile", newBranches)) // ensure all refs are reachable for i := 0; i < newBranches; i++ { - testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "show-ref", fmt.Sprintf("refs/heads/new-ref-%d", i)) + testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "show-ref", fmt.Sprintf("refs/heads/new-ref-%d", i)) } } diff --git a/internal/gitaly/service/ref/refexists_test.go b/internal/gitaly/service/ref/refexists_test.go index 202acb359..962d06d61 100644 --- a/internal/gitaly/service/ref/refexists_test.go +++ b/internal/gitaly/service/ref/refexists_test.go @@ -3,7 +3,6 @@ package ref import ( "testing" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -11,8 +10,7 @@ import ( ) func TestRefExists(t *testing.T) { - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) badRepo := &gitalypb.Repository{StorageName: "invalid", RelativePath: "/etc/"} @@ -23,17 +21,17 @@ func TestRefExists(t *testing.T) { repo *gitalypb.Repository wantErr codes.Code }{ - {"master", "refs/heads/master", true, testRepo, codes.OK}, - {"v1.0.0", "refs/tags/v1.0.0", true, testRepo, codes.OK}, - {"quoted", "refs/heads/'test'", true, testRepo, codes.OK}, - {"unicode exists", "refs/heads/ʕ•ᴥ•ʔ", true, testRepo, codes.OK}, - {"unicode missing", "refs/tags/अस्तित्वहीन", false, testRepo, codes.OK}, - {"spaces", "refs/ /heads", false, testRepo, codes.OK}, - {"haxxors", "refs/; rm -rf /tmp/*", false, testRepo, codes.OK}, - {"dashes", "--", false, testRepo, codes.InvalidArgument}, - {"blank", "", false, testRepo, codes.InvalidArgument}, - {"not tags or branches", "refs/heads/remotes/origin", false, testRepo, codes.OK}, - {"wildcards", "refs/heads/*", false, testRepo, codes.OK}, + {"master", "refs/heads/master", true, repo, codes.OK}, + {"v1.0.0", "refs/tags/v1.0.0", true, repo, codes.OK}, + {"quoted", "refs/heads/'test'", true, repo, codes.OK}, + {"unicode exists", "refs/heads/ʕ•ᴥ•ʔ", true, repo, codes.OK}, + {"unicode missing", "refs/tags/अस्तित्वहीन", false, repo, codes.OK}, + {"spaces", "refs/ /heads", false, repo, codes.OK}, + {"haxxors", "refs/; rm -rf /tmp/*", false, repo, codes.OK}, + {"dashes", "--", false, repo, codes.InvalidArgument}, + {"blank", "", false, repo, codes.InvalidArgument}, + {"not tags or branches", "refs/heads/remotes/origin", false, repo, codes.OK}, + {"wildcards", "refs/heads/*", false, repo, codes.OK}, {"invalid repos", "refs/heads/master", false, badRepo, codes.InvalidArgument}, } @@ -42,12 +40,6 @@ func TestRefExists(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - req := &gitalypb.RefExistsRequest{Repository: tt.repo, Ref: []byte(tt.ref)} got, err := client.RefExists(ctx, req) diff --git a/internal/gitaly/service/ref/refname_test.go b/internal/gitaly/service/ref/refname_test.go index b73bb857a..7342a6536 100644 --- a/internal/gitaly/service/ref/refname_test.go +++ b/internal/gitaly/service/ref/refname_test.go @@ -5,7 +5,6 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -13,17 +12,10 @@ import ( ) func TestFindRefNameSuccess(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) rpcRequest := &gitalypb.FindRefNameRequest{ - Repository: testRepo, + Repository: repo, CommitId: "0b4bc9a49b562e85de7cc9e834518ea6828729b9", Prefix: []byte(`refs/heads/`), } @@ -31,9 +23,7 @@ func TestFindRefNameSuccess(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindRefName(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) response := string(c.GetName()) @@ -43,17 +33,10 @@ func TestFindRefNameSuccess(t *testing.T) { } func TestFindRefNameEmptyCommit(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) rpcRequest := &gitalypb.FindRefNameRequest{ - Repository: testRepo, + Repository: repo, CommitId: "", Prefix: []byte(`refs/heads/`), } @@ -75,11 +58,8 @@ func TestFindRefNameEmptyCommit(t *testing.T) { } func TestFindRefNameInvalidRepo(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() repo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} rpcRequest := &gitalypb.FindRefNameRequest{ Repository: repo, @@ -104,17 +84,10 @@ func TestFindRefNameInvalidRepo(t *testing.T) { } func TestFindRefNameInvalidPrefix(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) rpcRequest := &gitalypb.FindRefNameRequest{ - Repository: testRepo, + Repository: repo, CommitId: "0b4bc9a49b562e85de7cc9e834518ea6828729b9", Prefix: []byte(`refs/nonexistant/`), } @@ -131,17 +104,10 @@ func TestFindRefNameInvalidPrefix(t *testing.T) { } func TestFindRefNameInvalidObject(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) rpcRequest := &gitalypb.FindRefNameRequest{ - Repository: testRepo, + Repository: repo, CommitId: "dead1234dead1234dead1234dead1234dead1234", } diff --git a/internal/gitaly/service/ref/refs_test.go b/internal/gitaly/service/ref/refs_test.go index 72cd54593..015b4b322 100644 --- a/internal/gitaly/service/ref/refs_test.go +++ b/internal/gitaly/service/ref/refs_test.go @@ -19,9 +19,9 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/internal/git/log" "gitlab.com/gitlab-org/gitaly/internal/git/updateref" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" ) @@ -36,16 +36,9 @@ func containsRef(refs [][]byte, ref string) bool { } func TestSuccessfulFindAllBranchNames(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, repo, _, client := setupRefService(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - rpcRequest := &gitalypb.FindAllBranchNamesRequest{Repository: testRepo} + rpcRequest := &gitalypb.FindAllBranchNamesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() @@ -72,19 +65,12 @@ func TestSuccessfulFindAllBranchNames(t *testing.T) { } func TestFindAllBranchNamesVeryLargeResponse(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + cfg, repo, _, client := setupRefService(t) ctx, cancel := testhelper.Context() defer cancel() - updater, err := updateref.New(ctx, config.Config, git.NewExecCommandFactory(config.Config), testRepo) + updater, err := updateref.New(ctx, cfg, git.NewExecCommandFactory(cfg), repo) require.NoError(t, err) // We want to create enough refs to overflow the default bufio.Scanner @@ -106,7 +92,7 @@ func TestFindAllBranchNamesVeryLargeResponse(t *testing.T) { require.NoError(t, updater.Wait()) - rpcRequest := &gitalypb.FindAllBranchNamesRequest{Repository: testRepo} + rpcRequest := &gitalypb.FindAllBranchNamesRequest{Repository: repo} c, err := client.FindAllBranchNames(ctx, rpcRequest) require.NoError(t, err) @@ -128,19 +114,14 @@ func TestFindAllBranchNamesVeryLargeResponse(t *testing.T) { } func TestEmptyFindAllBranchNamesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() rpcRequest := &gitalypb.FindAllBranchNamesRequest{} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllBranchNames(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -153,20 +134,15 @@ func TestEmptyFindAllBranchNamesRequest(t *testing.T) { } func TestInvalidRepoFindAllBranchNamesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() repo := &gitalypb.Repository{StorageName: "default", RelativePath: "made/up/path"} rpcRequest := &gitalypb.FindAllBranchNamesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllBranchNames(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -179,23 +155,14 @@ func TestInvalidRepoFindAllBranchNamesRequest(t *testing.T) { } func TestSuccessfulFindAllTagNames(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, repo, _, client := setupRefService(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - rpcRequest := &gitalypb.FindAllTagNamesRequest{Repository: testRepo} + rpcRequest := &gitalypb.FindAllTagNamesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllTagNames(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var names [][]byte for { @@ -203,9 +170,7 @@ func TestSuccessfulFindAllTagNames(t *testing.T) { if err == io.EOF { break } - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) names = append(names, r.GetNames()...) } @@ -217,19 +182,14 @@ func TestSuccessfulFindAllTagNames(t *testing.T) { } func TestEmptyFindAllTagNamesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() rpcRequest := &gitalypb.FindAllTagNamesRequest{} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllTagNames(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -242,20 +202,15 @@ func TestEmptyFindAllTagNamesRequest(t *testing.T) { } func TestInvalidRepoFindAllTagNamesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() repo := &gitalypb.Repository{StorageName: "default", RelativePath: "made/up/path"} rpcRequest := &gitalypb.FindAllTagNamesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllTagNames(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -268,43 +223,39 @@ func TestInvalidRepoFindAllTagNamesRequest(t *testing.T) { } func TestHeadReference(t *testing.T) { + cfg, repo, _ := testcfg.BuildWithRepo(t) + ctx, cancel := testhelper.Context() defer cancel() - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - headRef, err := headReference(ctx, git.NewExecCommandFactory(config.Config), testRepo) - if err != nil { - t.Fatal(err) - } + headRef, err := headReference(ctx, git.NewExecCommandFactory(cfg), repo) + require.NoError(t, err) require.Equal(t, git.DefaultRef, headRef) } func TestHeadReferenceWithNonExistingHead(t *testing.T) { - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + cfg, repo, repoPath := testcfg.BuildWithRepo(t) // Write bad HEAD - ioutil.WriteFile(testRepoPath+"/HEAD", []byte("ref: refs/heads/nonexisting"), 0644) + ioutil.WriteFile(repoPath+"/HEAD", []byte("ref: refs/heads/nonexisting"), 0644) defer func() { // Restore HEAD - ioutil.WriteFile(testRepoPath+"/HEAD", []byte("ref: refs/heads/master"), 0644) + ioutil.WriteFile(repoPath+"/HEAD", []byte("ref: refs/heads/master"), 0644) }() ctx, cancel := testhelper.Context() defer cancel() - headRef, err := headReference(ctx, git.NewExecCommandFactory(config.Config), testRepo) - if err != nil { - t.Fatal(err) - } + headRef, err := headReference(ctx, git.NewExecCommandFactory(cfg), repo) + require.NoError(t, err) if headRef != nil { t.Fatal("Expected HEAD reference to be nil, got '", string(headRef), "'") } } func TestSetDefaultBranchRef(t *testing.T) { + cfg, repo, _ := testcfg.BuildWithRepo(t) + testCases := []struct { desc string ref string @@ -327,14 +278,11 @@ func TestSetDefaultBranchRef(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - gitCmdFactory := git.NewExecCommandFactory(config.Config) - err := SetDefaultBranchRef(ctx, gitCmdFactory, testRepo, tc.ref, config.Config) + gitCmdFactory := git.NewExecCommandFactory(cfg) + err := SetDefaultBranchRef(ctx, gitCmdFactory, repo, tc.ref, cfg) require.NoError(t, err) - newRef, err := DefaultBranchName(ctx, gitCmdFactory, testRepo) + newRef, err := DefaultBranchName(ctx, gitCmdFactory, repo) require.NoError(t, err) require.Equal(t, tc.expectedRef, string(newRef)) @@ -349,8 +297,7 @@ func TestDefaultBranchName(t *testing.T) { headReference = _headReference }() - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + cfg, repo, _ := testcfg.BuildWithRepo(t) testCases := []struct { desc string @@ -408,10 +355,8 @@ func TestDefaultBranchName(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - defaultBranch, err := DefaultBranchName(ctx, git.NewExecCommandFactory(config.Config), testRepo) - if err != nil { - t.Fatal(err) - } + defaultBranch, err := DefaultBranchName(ctx, git.NewExecCommandFactory(cfg), repo) + require.NoError(t, err) if !bytes.Equal(defaultBranch, testCase.expected) { t.Fatalf("%s: expected %s, got %s instead", testCase.desc, testCase.expected, defaultBranch) } @@ -419,33 +364,19 @@ func TestDefaultBranchName(t *testing.T) { } func TestSuccessfulFindDefaultBranchName(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: testRepo} + _, repo, _, client := setupRefService(t) + rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() r, err := client.FindDefaultBranchName(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) require.Equal(t, r.GetName(), git.DefaultRef) } func TestEmptyFindDefaultBranchNameRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + _, client := setupRefServiceWithoutRepo(t) rpcRequest := &gitalypb.FindDefaultBranchNameRequest{} ctx, cancel := testhelper.Context() @@ -458,12 +389,8 @@ func TestEmptyFindDefaultBranchNameRequest(t *testing.T) { } func TestInvalidRepoFindDefaultBranchNameRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - repo := &gitalypb.Repository{StorageName: "default", RelativePath: "/made/up/path"} + cfg, client := setupRefServiceWithoutRepo(t) + repo := &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: "/made/up/path"} rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo} ctx, cancel := testhelper.Context() @@ -476,12 +403,11 @@ func TestInvalidRepoFindDefaultBranchNameRequest(t *testing.T) { } func TestSuccessfulFindAllTagsRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, client := setupRefServiceWithoutRepo(t) - repoProto, repoPath, cleanupFn := gittest.CloneRepoWithWorktree(t) + repoProto, repoPath, cleanupFn := gittest.CloneRepoWithWorktreeAtStorage(t, cfg.Storages[0]) defer cleanupFn() - repo := localrepo.New(git.NewExecCommandFactory(config.Config), repoProto, config.Config) + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) // reconstruct the v1.1.2 tag from patches to test truncated tag message // with partial PGP block @@ -528,9 +454,6 @@ func TestSuccessfulFindAllTagsRequest(t *testing.T) { // a tag of a tag tagOfTagID := testhelper.CreateTag(t, repoPath, "tag-of-tag", commitTagID, &testhelper.CreateTagOpts{Message: "tag of a tag"}) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - rpcRequest := &gitalypb.FindAllTagsRequest{Repository: repoProto} c, err := client.FindAllTags(ctx, rpcRequest) @@ -678,10 +601,9 @@ func TestSuccessfulFindAllTagsRequest(t *testing.T) { } func TestFindAllTagNestedTags(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, client := setupRefServiceWithoutRepo(t) - testRepoCopy, testRepoCopyPath, cleanupFn := gittest.CloneRepoWithWorktree(t) + testRepoCopy, testRepoCopyPath, cleanupFn := gittest.CloneRepoWithWorktreeAtStorage(t, cfg.Storages[0]) defer cleanupFn() blobID := "faaf198af3a36dbf41961466703cc1d47c61d051" @@ -720,9 +642,9 @@ func TestFindAllTagNestedTags(t *testing.T) { for _, tc := range testCases { t.Run(tc.description, func(t *testing.T) { tags := bytes.NewReader(testhelper.MustRunCommand(t, nil, "git", "-C", testRepoCopyPath, "tag")) - testhelper.MustRunCommand(t, tags, "xargs", config.Config.Git.BinPath, "-C", testRepoCopyPath, "tag", "-d") + testhelper.MustRunCommand(t, tags, "xargs", cfg.Git.BinPath, "-C", testRepoCopyPath, "tag", "-d") - batch, err := catfile.New(ctx, git.NewExecCommandFactory(config.Config), testRepoCopy) + batch, err := catfile.New(ctx, git.NewExecCommandFactory(cfg), testRepoCopy) require.NoError(t, err) info, err := batch.Info(ctx, git.Revision(tc.originalOid)) @@ -759,9 +681,6 @@ func TestFindAllTagNestedTags(t *testing.T) { expectedTags[string(expectedTag.Name)] = expectedTag } - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - rpcRequest := &gitalypb.FindAllTagsRequest{Repository: testRepoCopy} c, err := client.FindAllTags(ctx, rpcRequest) @@ -786,11 +705,8 @@ func TestFindAllTagNestedTags(t *testing.T) { } func TestInvalidFindAllTagsRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() testCases := []struct { desc string request *gitalypb.FindAllTagsRequest @@ -815,9 +731,7 @@ func TestInvalidFindAllTagsRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllTags(ctx, tc.request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -830,23 +744,14 @@ func TestInvalidFindAllTagsRequest(t *testing.T) { } func TestSuccessfulFindLocalBranches(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) - rpcRequest := &gitalypb.FindLocalBranchesRequest{Repository: testRepo} + rpcRequest := &gitalypb.FindLocalBranchesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindLocalBranches(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var branches []*gitalypb.FindLocalBranchResponse for { @@ -855,9 +760,6 @@ func TestSuccessfulFindLocalBranches(t *testing.T) { break } require.NoError(t, err) - if err != nil { - t.Fatal(err) - } branches = append(branches, r.GetBranches()...) } @@ -884,30 +786,21 @@ func TestSuccessfulFindLocalBranches(t *testing.T) { } func TestFindLocalBranchesPagination(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) ctx, cancel := testhelper.Context() defer cancel() limit := 1 rpcRequest := &gitalypb.FindLocalBranchesRequest{ - Repository: testRepo, + Repository: repo, PaginationParams: &gitalypb.PaginationParameter{ Limit: int32(limit), PageToken: "refs/heads/gitaly/squash-test", }, } c, err := client.FindLocalBranches(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var branches []*gitalypb.FindLocalBranchResponse for { @@ -916,9 +809,6 @@ func TestFindLocalBranchesPagination(t *testing.T) { break } require.NoError(t, err) - if err != nil { - t.Fatal(err) - } branches = append(branches, r.GetBranches()...) } @@ -987,25 +877,16 @@ func TestFindLocalBranchesSort(t *testing.T) { }, } - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) for _, testCase := range testCases { t.Run(testCase.desc, func(t *testing.T) { - rpcRequest := &gitalypb.FindLocalBranchesRequest{Repository: testRepo, SortBy: testCase.sortBy} + rpcRequest := &gitalypb.FindLocalBranchesRequest{Repository: repo, SortBy: testCase.sortBy} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindLocalBranches(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var branches []string for { @@ -1013,9 +894,8 @@ func TestFindLocalBranchesSort(t *testing.T) { if err == io.EOF { break } - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + for _, branch := range r.GetBranches() { branches = append(branches, string(branch.Name)) } @@ -1029,19 +909,14 @@ func TestFindLocalBranchesSort(t *testing.T) { } func TestEmptyFindLocalBranchesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() rpcRequest := &gitalypb.FindLocalBranchesRequest{} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindLocalBranches(ctx, rpcRequest) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -1054,8 +929,7 @@ func TestEmptyFindLocalBranchesRequest(t *testing.T) { } func TestSuccessfulFindAllBranchesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, repo, repoPath, client := setupRefService(t) remoteBranch := &gitalypb.FindAllBranchesResponse_Branch{ Name: []byte("refs/remotes/origin/fake-remote-branch"), @@ -1082,21 +956,14 @@ func TestSuccessfulFindAllBranchesRequest(t *testing.T) { }, } - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - - gittest.CreateRemoteBranch(t, testRepoPath, "origin", + gittest.CreateRemoteBranch(t, repoPath, "origin", "fake-remote-branch", remoteBranch.Target.Id) - request := &gitalypb.FindAllBranchesRequest{Repository: testRepo} - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + request := &gitalypb.FindAllBranchesRequest{Repository: repo} ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllBranches(ctx, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) branches := readFindAllBranchesResponsesFromClient(t, c) @@ -1114,15 +981,9 @@ func TestSuccessfulFindAllBranchesRequest(t *testing.T) { } func TestSuccessfulFindAllBranchesRequestWithMergedBranches(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, repoProto, repoPath, client := setupRefService(t) - repoProto, repoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - repo := localrepo.New(git.NewExecCommandFactory(config.Config), repoProto, config.Config) - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) ctx, cancel := testhelper.Context() defer cancel() @@ -1208,11 +1069,8 @@ func TestSuccessfulFindAllBranchesRequestWithMergedBranches(t *testing.T) { } func TestInvalidFindAllBranchesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + _, client := setupRefServiceWithoutRepo(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() testCases := []struct { description string request gitalypb.FindAllBranchesRequest @@ -1237,9 +1095,7 @@ func TestInvalidFindAllBranchesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllBranches(ctx, &tc.request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { @@ -1266,14 +1122,7 @@ func readFindAllBranchesResponsesFromClient(t *testing.T, c gitalypb.RefService_ } func TestListTagNamesContainingCommit(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repoProto, _, client := setupRefService(t) testCases := []struct { description string @@ -1313,7 +1162,7 @@ func TestListTagNamesContainingCommit(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - request := &gitalypb.ListTagNamesContainingCommitRequest{Repository: testRepo, CommitId: tc.commitID} + request := &gitalypb.ListTagNamesContainingCommitRequest{Repository: repoProto, CommitId: tc.commitID} c, err := client.ListTagNamesContainingCommit(ctx, request) require.NoError(t, err) @@ -1343,14 +1192,7 @@ func TestListTagNamesContainingCommit(t *testing.T) { } func TestListBranchNamesContainingCommit(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) testCases := []struct { description string @@ -1407,7 +1249,7 @@ func TestListBranchNamesContainingCommit(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - request := &gitalypb.ListBranchNamesContainingCommitRequest{Repository: testRepo, CommitId: tc.commitID} + request := &gitalypb.ListBranchNamesContainingCommitRequest{Repository: repo, CommitId: tc.commitID} c, err := client.ListBranchNamesContainingCommit(ctx, request) require.NoError(t, err) @@ -1437,12 +1279,9 @@ func TestListBranchNamesContainingCommit(t *testing.T) { } func TestSuccessfulFindTagRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, repoProto, repoPath, client := setupRefService(t) - repoProto, repoPath, cleanupFn := gittest.CloneRepoWithWorktree(t) - defer cleanupFn() - repo := localrepo.New(git.NewExecCommandFactory(config.Config), repoProto, config.Config) + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) blobID := "faaf198af3a36dbf41961466703cc1d47c61d051" commitID := "6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9" @@ -1480,9 +1319,6 @@ func TestSuccessfulFindTagRequest(t *testing.T) { // a tag of a tag tagOfTagID := testhelper.CreateTag(t, repoPath, "tag-of-tag", commitTagID, &testhelper.CreateTagOpts{Message: "tag of a tag"}) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - expectedTags := []*gitalypb.Tag{ { Name: []byte(commitID), @@ -1608,11 +1444,10 @@ func TestSuccessfulFindTagRequest(t *testing.T) { } func TestFindTagNestedTag(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, client := setupRefServiceWithoutRepo(t) - testRepoCopy, testRepoCopyPath, cleanupFn := gittest.CloneRepoWithWorktree(t) - defer cleanupFn() + repo, repoPath, cleanup := gittest.CloneRepoWithWorktreeAtStorage(t, cfg.Storages[0]) + t.Cleanup(cleanup) blobID := "faaf198af3a36dbf41961466703cc1d47c61d051" commitID := "6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9" @@ -1648,14 +1483,11 @@ func TestFindTagNestedTag(t *testing.T) { } for _, tc := range testCases { - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - t.Run(tc.description, func(t *testing.T) { - tags := bytes.NewReader(testhelper.MustRunCommand(t, nil, "git", "-C", testRepoCopyPath, "tag")) - testhelper.MustRunCommand(t, tags, "xargs", config.Config.Git.BinPath, "-C", testRepoCopyPath, "tag", "-d") + tags := bytes.NewReader(testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "tag")) + testhelper.MustRunCommand(t, tags, "xargs", cfg.Git.BinPath, "-C", repoPath, "tag", "-d") - batch, err := catfile.New(ctx, git.NewExecCommandFactory(config.Config), testRepoCopy) + batch, err := catfile.New(ctx, git.NewExecCommandFactory(cfg), repo) require.NoError(t, err) info, err := batch.Info(ctx, git.Revision(tc.originalOid)) @@ -1667,7 +1499,7 @@ func TestFindTagNestedTag(t *testing.T) { for depth := 0; depth < tc.depth; depth++ { tagName = fmt.Sprintf("tag-depth-%d", depth) tagMessage = fmt.Sprintf("a commit %d deep", depth) - tagID = testhelper.CreateTag(t, testRepoCopyPath, tagName, tagID, &testhelper.CreateTagOpts{Message: tagMessage}) + tagID = testhelper.CreateTag(t, repoPath, tagName, tagID, &testhelper.CreateTagOpts{Message: tagMessage}) } expectedTag := &gitalypb.Tag{ Name: []byte(tagName), @@ -1687,7 +1519,7 @@ func TestFindTagNestedTag(t *testing.T) { require.NoError(t, err) expectedTag.TargetCommit = commit } - rpcRequest := &gitalypb.FindTagRequest{Repository: testRepoCopy, TagName: []byte(tagName)} + rpcRequest := &gitalypb.FindTagRequest{Repository: repo, TagName: []byte(tagName)} resp, err := client.FindTag(ctx, rpcRequest) require.NoError(t, err) @@ -1697,14 +1529,7 @@ func TestFindTagNestedTag(t *testing.T) { } func TestInvalidFindTagRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) testCases := []struct { desc string @@ -1726,7 +1551,7 @@ func TestInvalidFindTagRequest(t *testing.T) { { desc: "empty tag name", request: &gitalypb.FindTagRequest{ - Repository: testRepo, + Repository: repo, }, }, } diff --git a/internal/gitaly/service/ref/remote_branches_test.go b/internal/gitaly/service/ref/remote_branches_test.go index 1efa4e501..9f90d4b12 100644 --- a/internal/gitaly/service/ref/remote_branches_test.go +++ b/internal/gitaly/service/ref/remote_branches_test.go @@ -8,7 +8,6 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -18,15 +17,9 @@ func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - stop, serverSocketPath := runRefServiceServer(t) - defer stop() + cfg, repoProto, repoPath, client := setupRefService(t) - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - repoProto, repoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() - repo := localrepo.New(git.NewExecCommandFactory(config.Config), repoProto, config.Config) + repo := localrepo.New(git.NewExecCommandFactory(cfg), repoProto, cfg) remoteName := "my-remote" expectedBranches := map[string]string{ @@ -49,9 +42,7 @@ func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) { request := &gitalypb.FindAllRemoteBranchesRequest{Repository: repoProto, RemoteName: remoteName} c, err := client.FindAllRemoteBranches(ctx, request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) branches := readFindAllRemoteBranchesResponsesFromClient(t, c) require.Len(t, branches, len(expectedBranches)) @@ -82,14 +73,7 @@ func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) { } func TestInvalidFindAllRemoteBranchesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, _, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, _, client := setupRefService(t) testCases := []struct { description string @@ -110,7 +94,7 @@ func TestInvalidFindAllRemoteBranchesRequest(t *testing.T) { }, { description: "Empty remote name", - request: gitalypb.FindAllRemoteBranchesRequest{Repository: testRepo}, + request: gitalypb.FindAllRemoteBranchesRequest{Repository: repo}, }, } @@ -119,9 +103,7 @@ func TestInvalidFindAllRemoteBranchesRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() c, err := client.FindAllRemoteBranches(ctx, &tc.request) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var recvError error for recvError == nil { diff --git a/internal/gitaly/service/ref/tag_messages_test.go b/internal/gitaly/service/ref/tag_messages_test.go index 513acc6e7..81c0b4a9c 100644 --- a/internal/gitaly/service/ref/tag_messages_test.go +++ b/internal/gitaly/service/ref/tag_messages_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -14,14 +13,7 @@ import ( ) func TestSuccessfulGetTagMessagesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() - - testRepo, testRepoPath, cleanupFn := gittest.CloneRepo(t) - defer cleanupFn() + _, repo, repoPath, client := setupRefService(t) ctx, cancel := testhelper.Context() defer cancel() @@ -29,11 +21,11 @@ func TestSuccessfulGetTagMessagesRequest(t *testing.T) { message1 := strings.Repeat("a", helper.MaxCommitOrTagMessageSize*2) message2 := strings.Repeat("b", helper.MaxCommitOrTagMessageSize) - tag1ID := testhelper.CreateTag(t, testRepoPath, "big-tag-1", "master", &testhelper.CreateTagOpts{Message: message1}) - tag2ID := testhelper.CreateTag(t, testRepoPath, "big-tag-2", "master~", &testhelper.CreateTagOpts{Message: message2}) + tag1ID := testhelper.CreateTag(t, repoPath, "big-tag-1", "master", &testhelper.CreateTagOpts{Message: message1}) + tag2ID := testhelper.CreateTag(t, repoPath, "big-tag-2", "master~", &testhelper.CreateTagOpts{Message: message2}) request := &gitalypb.GetTagMessagesRequest{ - Repository: testRepo, + Repository: repo, TagIds: []string{tag1ID, tag2ID}, } @@ -58,11 +50,7 @@ func TestSuccessfulGetTagMessagesRequest(t *testing.T) { } func TestFailedGetTagMessagesRequest(t *testing.T) { - stop, serverSocketPath := runRefServiceServer(t) - defer stop() - - client, conn := newRefServiceClient(t, serverSocketPath) - defer conn.Close() + _, client := setupRefServiceWithoutRepo(t) testCases := []struct { desc string diff --git a/internal/gitaly/service/ref/testhelper_test.go b/internal/gitaly/service/ref/testhelper_test.go index c40da49d1..ab34d701a 100644 --- a/internal/gitaly/service/ref/testhelper_test.go +++ b/internal/gitaly/service/ref/testhelper_test.go @@ -5,13 +5,16 @@ import ( "os" "testing" + "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git" + "gitlab.com/gitlab-org/gitaly/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" hookservice "gitlab.com/gitlab-org/gitaly/internal/gitaly/service/hook" "gitlab.com/gitlab-org/gitaly/internal/gitaly/transaction" "gitlab.com/gitlab-org/gitaly/internal/helper/lines" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc" ) @@ -34,8 +37,6 @@ func testMain(m *testing.M) int { cleanup := testhelper.Configure() defer cleanup() - testhelper.ConfigureGitalyHooksBinary(config.Config.BinDir) - // Force small messages to test that fragmenting the // ref list works correctly lines.ItemsPerMessage = 3 @@ -43,8 +44,32 @@ func testMain(m *testing.M) int { return m.Run() } -func runRefServiceServer(t *testing.T) (func(), string) { - cfg := config.Config +func setupRefService(t testing.TB) (config.Cfg, *gitalypb.Repository, string, gitalypb.RefServiceClient) { + cfg, client := setupRefServiceWithoutRepo(t) + + repo, repoPath, cleanup := gittest.CloneRepoAtStorage(t, cfg.Storages[0], t.Name()) + t.Cleanup(cleanup) + + testhelper.ConfigureGitalyHooksBin(t, cfg) + + return cfg, repo, repoPath, client +} + +func setupRefServiceWithoutRepo(t testing.TB) (config.Cfg, gitalypb.RefServiceClient) { + cfg := testcfg.Build(t) + + testhelper.ConfigureGitalyHooksBin(t, cfg) + + serverSocketPath := runRefServiceServer(t, cfg) + cfg.SocketPath = serverSocketPath + + client, conn := newRefServiceClient(t, serverSocketPath) + t.Cleanup(func() { conn.Close() }) + + return cfg, client +} + +func runRefServiceServer(t testing.TB, cfg config.Cfg) string { locator := config.NewLocator(cfg) txManager := transaction.NewManager(cfg) hookManager := hook.NewManager(locator, txManager, hook.GitlabAPIStub, cfg) @@ -54,18 +79,17 @@ func runRefServiceServer(t *testing.T) (func(), string) { gitalypb.RegisterRefServiceServer(srv.GrpcServer(), NewServer(cfg, locator, gitCmdFactory)) gitalypb.RegisterHookServiceServer(srv.GrpcServer(), hookservice.NewServer(cfg, hookManager, gitCmdFactory)) srv.Start(t) + t.Cleanup(srv.Stop) - return srv.Stop, "unix://" + srv.Socket() + return "unix://" + srv.Socket() } -func newRefServiceClient(t *testing.T, serverSocketPath string) (gitalypb.RefServiceClient, *grpc.ClientConn) { +func newRefServiceClient(t testing.TB, serverSocketPath string) (gitalypb.RefServiceClient, *grpc.ClientConn) { connOpts := []grpc.DialOption{ grpc.WithInsecure(), } conn, err := grpc.Dial(serverSocketPath, connOpts...) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) return gitalypb.NewRefServiceClient(conn), conn } |