diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-02-16 19:16:24 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-02-23 14:20:21 +0300 |
commit | 450056018f4337e293f8e0ff2d1731564e891f71 (patch) | |
tree | ae5922dea25e8f014f8d9419435bdbd8eb04bedb | |
parent | 86c86a2b5d7238d40279216b94fd952fa0bb0c3d (diff) |
Rewrite repository in NewTestRepo
This commit rewrites the repository in NewTestRepo helper. NewTestRepo
instantiates a new localrepo from a Repository proto. Since all of the
call sites are passing in a repository that has not yet been rewritten,
the git operations target wrong directories when the tests are ran with
Praefect in front. Rewriting the repository ensures the operations target
the replica path generated by Praefect rather than the relative path sent
by the client.
As TestWriteCommit in gittest depends on NewTestRepo, this change would
introduce a cyclic dependency between the localrepo and gittest packages.
The test is moved into the external _test package to break this cycle.
-rw-r--r-- | internal/git/gittest/commit_test.go | 43 | ||||
-rw-r--r-- | internal/git/localrepo/repo.go | 12 |
2 files changed, 35 insertions, 20 deletions
diff --git a/internal/git/gittest/commit_test.go b/internal/git/gittest/commit_test.go index a05a70adb..515455252 100644 --- a/internal/git/gittest/commit_test.go +++ b/internal/git/gittest/commit_test.go @@ -1,4 +1,4 @@ -package gittest +package gittest_test import ( "testing" @@ -6,13 +6,16 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v14/internal/git" "gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo" "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" ) func TestWriteCommit(t *testing.T) { - cfg, repoProto, repoPath := setup(t) + cfg, repoProto, repoPath := testcfg.BuildWithRepo(t) + repo := localrepo.NewTestRepo(t, cfg, repoProto) ctx := testhelper.Context(t) @@ -23,8 +26,8 @@ func TestWriteCommit(t *testing.T) { require.NoError(t, err) defaultCommitter := &gitalypb.CommitAuthor{ - Name: []byte(committerName), - Email: []byte(committerEmail), + Name: []byte("Scrooge McDuck"), + Email: []byte("scrooge@mcduck.com"), } defaultParentID := "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863" @@ -40,9 +43,9 @@ func TestWriteCommit(t *testing.T) { for _, tc := range []struct { desc string - opts []WriteCommitOption + opts []gittest.WriteCommitOption expectedCommit *gitalypb.GitCommit - expectedTreeEntries []TreeEntry + expectedTreeEntries []gittest.TreeEntry expectedRevUpdate git.Revision }{ { @@ -60,8 +63,8 @@ func TestWriteCommit(t *testing.T) { }, { desc: "with commit message", - opts: []WriteCommitOption{ - WithMessage("my custom message\n\nfoobar\n"), + opts: []gittest.WriteCommitOption{ + gittest.WithMessage("my custom message\n\nfoobar\n"), }, expectedCommit: &gitalypb.GitCommit{ Author: defaultCommitter, @@ -76,8 +79,8 @@ func TestWriteCommit(t *testing.T) { }, { desc: "with no parents", - opts: []WriteCommitOption{ - WithParents(), + opts: []gittest.WriteCommitOption{ + gittest.WithParents(), }, expectedCommit: &gitalypb.GitCommit{ Author: defaultCommitter, @@ -90,8 +93,8 @@ func TestWriteCommit(t *testing.T) { }, { desc: "with multiple parents", - opts: []WriteCommitOption{ - WithParents(revisions["refs/heads/master"], revisions["refs/heads/master~"]), + opts: []gittest.WriteCommitOption{ + gittest.WithParents(revisions["refs/heads/master"], revisions["refs/heads/master~"]), }, expectedCommit: &gitalypb.GitCommit{ Author: defaultCommitter, @@ -107,8 +110,8 @@ func TestWriteCommit(t *testing.T) { }, { desc: "with branch", - opts: []WriteCommitOption{ - WithBranch("foo"), + opts: []gittest.WriteCommitOption{ + gittest.WithBranch("foo"), }, expectedCommit: &gitalypb.GitCommit{ Author: defaultCommitter, @@ -124,8 +127,8 @@ func TestWriteCommit(t *testing.T) { }, { desc: "with tree entry", - opts: []WriteCommitOption{ - WithTreeEntries(TreeEntry{ + opts: []gittest.WriteCommitOption{ + gittest.WithTreeEntries(gittest.TreeEntry{ Content: "foobar", Mode: "100644", Path: "file", @@ -141,7 +144,7 @@ func TestWriteCommit(t *testing.T) { defaultParentID, }, }, - expectedTreeEntries: []TreeEntry{ + expectedTreeEntries: []gittest.TreeEntry{ { Content: "foobar", Mode: "100644", @@ -151,15 +154,15 @@ func TestWriteCommit(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { - oid := WriteCommit(t, cfg, repoPath, tc.opts...) + oid := gittest.WriteCommit(t, cfg, repoPath, tc.opts...) commit, err := catfile.GetCommit(ctx, objectReader, oid.Revision()) require.NoError(t, err) - CommitEqual(t, tc.expectedCommit, commit) + gittest.CommitEqual(t, tc.expectedCommit, commit) if tc.expectedTreeEntries != nil { - RequireTree(t, cfg, repoPath, oid.String(), tc.expectedTreeEntries) + gittest.RequireTree(t, cfg, repoPath, oid.String(), tc.expectedTreeEntries) } if tc.expectedRevUpdate != "" { diff --git a/internal/git/localrepo/repo.go b/internal/git/localrepo/repo.go index 6fcb32836..7c4eabce8 100644 --- a/internal/git/localrepo/repo.go +++ b/internal/git/localrepo/repo.go @@ -9,9 +9,12 @@ import ( "gitlab.com/gitlab-org/gitaly/v14/internal/command" "gitlab.com/gitlab-org/gitaly/v14/internal/git" "gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v14/internal/git/repository" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/storage" + "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" ) // Repo represents a local Git repository. @@ -37,6 +40,15 @@ func New(locator storage.Locator, gitCmdFactory git.CommandFactory, catfileCache func NewTestRepo(t testing.TB, cfg config.Cfg, repo repository.GitRepo, factoryOpts ...git.ExecCommandFactoryOption) *Repo { t.Helper() + if cfg.SocketPath != "it is a stub to bypass Validate method" { + repo = gittest.RewrittenRepository(testhelper.Context(t), t, cfg, &gitalypb.Repository{ + StorageName: repo.GetStorageName(), + RelativePath: repo.GetRelativePath(), + GitObjectDirectory: repo.GetGitObjectDirectory(), + GitAlternateObjectDirectories: repo.GetGitAlternateObjectDirectories(), + }) + } + gitCmdFactory, cleanup, err := git.NewExecCommandFactory(cfg, factoryOpts...) t.Cleanup(cleanup) require.NoError(t, err) |