diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-01-20 18:51:50 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-02-02 14:24:22 +0300 |
commit | 294bfd9ff69172964a4430a203ce319504ccd25c (patch) | |
tree | d3a60926cb001ce8e96cac93dc9fe5d53ef74fc2 | |
parent | 33701a964fa9cc6939aa94571501e71b29efab76 (diff) |
Create repositories via API in most repository service tests
This commit creates the repositories in tests of repsository service
via API. This enables the tests to work with Praefect without the
metadata creation hack as Praefect can create the metadata when is
proxies the creation calls. The tests converted in this commit are the
ones where it was straighforward replacing, the rest will be done in
more detailed follow up commits.
15 files changed, 68 insertions, 47 deletions
diff --git a/internal/gitaly/service/repository/archive_test.go b/internal/gitaly/service/repository/archive_test.go index f4ea9eb42..e10cf3808 100644 --- a/internal/gitaly/service/repository/archive_test.go +++ b/internal/gitaly/service/repository/archive_test.go @@ -190,8 +190,12 @@ func TestGetArchiveWithLfsSuccess(t *testing.T) { serverSocketPath := runRepositoryServerWithConfig(t, cfg, nil) client := newRepositoryClient(t, cfg, serverSocketPath) + cfg.SocketPath = serverSocketPath - repo, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + ctx := testhelper.Context(t) + repo, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) testcfg.BuildGitalyLFSSmudge(t, cfg) @@ -228,8 +232,6 @@ func TestGetArchiveWithLfsSuccess(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - ctx := testhelper.Context(t) - req := &gitalypb.GetArchiveRequest{ Repository: repo, CommitId: sha, diff --git a/internal/gitaly/service/repository/calculate_checksum_test.go b/internal/gitaly/service/repository/calculate_checksum_test.go index 6d0353095..5b18020ee 100644 --- a/internal/gitaly/service/repository/calculate_checksum_test.go +++ b/internal/gitaly/service/repository/calculate_checksum_test.go @@ -39,10 +39,10 @@ func TestEmptyRepositoryCalculateChecksum(t *testing.T) { t.Parallel() cfg, client := setupRepositoryServiceWithoutRepo(t) - repo, _ := gittest.InitRepo(t, cfg, cfg.Storages[0]) + testCtx := testhelper.Context(t) + repo, _ := gittest.CreateRepository(testCtx, t, cfg) request := &gitalypb.CalculateChecksumRequest{Repository: repo} - testCtx := testhelper.Context(t) response, err := client.CalculateChecksum(testCtx, request) require.NoError(t, err) @@ -53,13 +53,13 @@ func TestBrokenRepositoryCalculateChecksum(t *testing.T) { t.Parallel() cfg, client := setupRepositoryServiceWithoutRepo(t) - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + testCtx := testhelper.Context(t) + repo, repoPath := gittest.CreateRepository(testCtx, t, cfg) // Force an empty HEAD file require.NoError(t, os.Truncate(filepath.Join(repoPath, "HEAD"), 0)) request := &gitalypb.CalculateChecksumRequest{Repository: repo} - testCtx := testhelper.Context(t) _, err := client.CalculateChecksum(testCtx, request) testhelper.RequireGrpcCode(t, err, codes.DataLoss) diff --git a/internal/gitaly/service/repository/cleanup_test.go b/internal/gitaly/service/repository/cleanup_test.go index 1ef522139..4a5dfd314 100644 --- a/internal/gitaly/service/repository/cleanup_test.go +++ b/internal/gitaly/service/repository/cleanup_test.go @@ -45,7 +45,10 @@ func TestCleanupDeletesStaleWorktrees(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + ctx := testhelper.Context(t) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) req := &gitalypb.CleanupRequest{Repository: repo} @@ -55,7 +58,6 @@ func TestCleanupDeletesStaleWorktrees(t *testing.T) { worktreePath := filepath.Join(basePath, "test-worktree") require.NoError(t, os.Chtimes(worktreeCheckoutPath, tc.worktreeTime, tc.worktreeTime)) - ctx := testhelper.Context(t) c, err := client.Cleanup(ctx, req) diff --git a/internal/gitaly/service/repository/config_test.go b/internal/gitaly/service/repository/config_test.go index cf8aace06..18085e933 100644 --- a/internal/gitaly/service/repository/config_test.go +++ b/internal/gitaly/service/repository/config_test.go @@ -20,13 +20,13 @@ func TestGetConfig(t *testing.T) { t.Parallel() cfg, client := setupRepositoryServiceWithoutRepo(t) + ctx := testhelper.Context(t) + getConfig := func( t *testing.T, client gitalypb.RepositoryServiceClient, repo *gitalypb.Repository, ) (string, error) { - ctx := testhelper.Context(t) - stream, err := client.GetConfig(ctx, &gitalypb.GetConfigRequest{ Repository: repo, }) @@ -46,7 +46,9 @@ func TestGetConfig(t *testing.T) { } t.Run("normal repo", func(t *testing.T) { - repo, _ := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) config, err := getConfig(t, client, repo) require.NoError(t, err) @@ -60,7 +62,9 @@ func TestGetConfig(t *testing.T) { }) t.Run("missing config", func(t *testing.T) { - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) configPath := filepath.Join(repoPath, "config") require.NoError(t, os.Remove(configPath)) diff --git a/internal/gitaly/service/repository/create_repository_from_bundle_test.go b/internal/gitaly/service/repository/create_repository_from_bundle_test.go index bcd5772cf..9989aca34 100644 --- a/internal/gitaly/service/repository/create_repository_from_bundle_test.go +++ b/internal/gitaly/service/repository/create_repository_from_bundle_test.go @@ -98,6 +98,9 @@ func TestCreateRepositoryFromBundle_transactional(t *testing.T) { cfg, repoProto, repoPath, client := setupRepositoryService(t, testserver.WithTransactionManager(txManager)) + // Reset the votes casted while creating the test repository. + txManager.Reset() + masterOID := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/master")) // keep-around refs are not cloned in the initial step, but are added via the second call to diff --git a/internal/gitaly/service/repository/fetch_bundle_test.go b/internal/gitaly/service/repository/fetch_bundle_test.go index 41fc58b65..ffc55e107 100644 --- a/internal/gitaly/service/repository/fetch_bundle_test.go +++ b/internal/gitaly/service/repository/fetch_bundle_test.go @@ -35,8 +35,8 @@ func TestServer_FetchBundle_success(t *testing.T) { gittest.Exec(t, cfg, "-C", repoPath, "bundle", "create", bundlePath, "--all") expectedRefs := gittest.Exec(t, cfg, "-C", repoPath, "show-ref", "--head") - targetRepo, targetRepoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) ctx := testhelper.Context(t) + targetRepo, targetRepoPath := gittest.CreateRepository(ctx, t, cfg) stream, err := client.FetchBundle(ctx) require.NoError(t, err) diff --git a/internal/gitaly/service/repository/fetch_test.go b/internal/gitaly/service/repository/fetch_test.go index 6cc145ee8..db35fd4b6 100644 --- a/internal/gitaly/service/repository/fetch_test.go +++ b/internal/gitaly/service/repository/fetch_test.go @@ -21,7 +21,9 @@ func TestFetchSourceBranchSourceRepositorySuccess(t *testing.T) { md := testcfg.GitalyServersMetadataFromCfg(t, cfg) ctx = testhelper.MergeOutgoingMetadata(ctx, md) - targetRepoProto, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + targetRepoProto, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) targetRepo := localrepo.NewTestRepo(t, cfg, targetRepoProto) sourceBranch := "fetch-source-branch-test-branch" @@ -82,7 +84,9 @@ func TestFetchSourceBranchBranchNotFound(t *testing.T) { md := testcfg.GitalyServersMetadataFromCfg(t, cfg) ctx = testhelper.MergeOutgoingMetadata(ctx, md) - sourceRepo, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + sourceRepo, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) sourceBranch := "does-not-exist" targetRef := "refs/tmp/fetch-source-branch-test" diff --git a/internal/gitaly/service/repository/fullpath_test.go b/internal/gitaly/service/repository/fullpath_test.go index fa36c5a63..9ef8bb54d 100644 --- a/internal/gitaly/service/repository/fullpath_test.go +++ b/internal/gitaly/service/repository/fullpath_test.go @@ -32,7 +32,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("missing path", func(t *testing.T) { - repo, _ := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, _ := gittest.CreateRepository(ctx, t, cfg) response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{ Repository: repo, @@ -43,7 +43,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("invalid storage", func(t *testing.T) { - repo, _ := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, _ := gittest.CreateRepository(ctx, t, cfg) repo.StorageName = "" response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{ @@ -77,7 +77,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("normal repo", func(t *testing.T) { - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{ Repository: repo, @@ -91,7 +91,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("missing config", func(t *testing.T) { - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) configPath := filepath.Join(repoPath, "config") require.NoError(t, os.Remove(configPath)) @@ -108,7 +108,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("multiple times", func(t *testing.T) { - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) for i := 0; i < 5; i++ { response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{ @@ -124,7 +124,7 @@ func TestSetFullPath(t *testing.T) { }) t.Run("multiple preexisting paths", func(t *testing.T) { - repo, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) for i := 0; i < 5; i++ { gittest.Exec(t, cfg, "-C", repoPath, "config", "--add", fullPathKey, fmt.Sprintf("foo/%d", i)) diff --git a/internal/gitaly/service/repository/gc_test.go b/internal/gitaly/service/repository/gc_test.go index e0f56d388..340145b6f 100644 --- a/internal/gitaly/service/repository/gc_test.go +++ b/internal/gitaly/service/repository/gc_test.go @@ -212,7 +212,10 @@ func TestGarbageCollectDeletesPackedRefsLock(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + ctx := testhelper.Context(t) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) // Force the packed-refs file to have an old time to test that even // in that case it doesn't get deleted @@ -225,7 +228,6 @@ func TestGarbageCollectDeletesPackedRefsLock(t *testing.T) { if tc.lockTime != nil { mustCreateFileWithTimes(t, lockPath, *tc.lockTime) } - ctx := testhelper.Context(t) c, err := client.GarbageCollect(ctx, req) @@ -308,7 +310,8 @@ func TestGarbageCollectDeletesPackedRefsNew(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + ctx := testhelper.Context(t) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg) req := &gitalypb.GarbageCollectRequest{Repository: repo} packedRefsNewPath := filepath.Join(repoPath, "packed-refs.new") @@ -316,7 +319,6 @@ func TestGarbageCollectDeletesPackedRefsNew(t *testing.T) { if tc.lockTime != nil { mustCreateFileWithTimes(t, packedRefsNewPath, *tc.lockTime) } - ctx := testhelper.Context(t) c, err := client.GarbageCollect(ctx, req) diff --git a/internal/gitaly/service/repository/midx_test.go b/internal/gitaly/service/repository/midx_test.go index add480486..c6814df2f 100644 --- a/internal/gitaly/service/repository/midx_test.go +++ b/internal/gitaly/service/repository/midx_test.go @@ -113,6 +113,9 @@ func TestMidxRepack_transactional(t *testing.T) { cfg, repo, repoPath, client := setupRepositoryService(t, testserver.WithTransactionManager(txManager)) + // Reset the votes after creating the test repository. + txManager.Reset() + ctx, err := txinfo.InjectTransaction(ctx, 1, "node", true) require.NoError(t, err) ctx = peer.NewContext(ctx, &peer.Peer{ @@ -138,8 +141,10 @@ func TestMidxRepackExpire(t *testing.T) { for _, packsAdded := range []int{3, 5, 11, 20} { t.Run(fmt.Sprintf("Test repack expire with %d added packs", packsAdded), func(t *testing.T) { - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) ctx := testhelper.Context(t) + repo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) // add some pack files with different sizes addPackFiles(t, ctx, cfg, client, repo, repoPath, packsAdded, false) diff --git a/internal/gitaly/service/repository/optimize_test.go b/internal/gitaly/service/repository/optimize_test.go index 701bdc38e..0d27d8c55 100644 --- a/internal/gitaly/service/repository/optimize_test.go +++ b/internal/gitaly/service/repository/optimize_test.go @@ -89,7 +89,7 @@ func TestOptimizeRepository(t *testing.T) { require.Equal(t, getNewestPackfileModtime(t, repoPath), newestsPackfileTime, "there should not have been a new packfile created") - testRepoProto, testRepoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) + testRepoProto, testRepoPath := gittest.CreateRepository(ctx, t, cfg) blobs := 10 blobIDs := gittest.WriteBlobs(t, cfg, testRepoPath, blobs) diff --git a/internal/gitaly/service/repository/repack_test.go b/internal/gitaly/service/repository/repack_test.go index 6c7fd0aa9..afd630c8b 100644 --- a/internal/gitaly/service/repository/repack_test.go +++ b/internal/gitaly/service/repository/repack_test.go @@ -129,14 +129,17 @@ func TestRepackFullSuccess(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { + ctx := testhelper.Context(t) + var repoPath string - test.req.Repository, repoPath = gittest.CloneRepo(t, cfg, cfg.Storages[0]) + test.req.Repository, repoPath = gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) // Reset mtime to a long while ago since some filesystems don't have sub-second // precision on `mtime`. packPath := filepath.Join(repoPath, "objects", "pack") testhelper.MustRunCommand(t, nil, "touch", "-t", testTimeString, packPath) testTime := time.Date(2006, 0o1, 0o2, 15, 0o4, 0o5, 0, time.UTC) - ctx := testhelper.Context(t) c, err := client.RepackFull(ctx, test.req) assert.NoError(t, err) assert.NotNil(t, c) diff --git a/internal/gitaly/service/repository/repository_test.go b/internal/gitaly/service/repository/repository_test.go index 01ca142cf..f1150f602 100644 --- a/internal/gitaly/service/repository/repository_test.go +++ b/internal/gitaly/service/repository/repository_test.go @@ -2,7 +2,6 @@ package repository import ( "os" - "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -114,10 +113,8 @@ func TestSuccessfulHasLocalBranches(t *testing.T) { t.Parallel() cfg, repo, _, client := setupRepositoryService(t) - emptyRepoName := "empty-repo.git" - emptyRepoPath := filepath.Join(cfg.Storages[0].Path, emptyRepoName) - gittest.Exec(t, cfg, "init", "--bare", emptyRepoPath) - defer func() { require.NoError(t, os.RemoveAll(emptyRepoPath)) }() + ctx := testhelper.Context(t) + emptyRepo, _ := gittest.CreateRepository(ctx, t, cfg) testCases := []struct { desc string @@ -133,10 +130,7 @@ func TestSuccessfulHasLocalBranches(t *testing.T) { { desc: "repository doesn't have branches", request: &gitalypb.HasLocalBranchesRequest{ - Repository: &gitalypb.Repository{ - StorageName: cfg.Storages[0].Name, - RelativePath: emptyRepoName, - }, + Repository: emptyRepo, }, value: false, }, @@ -144,8 +138,6 @@ func TestSuccessfulHasLocalBranches(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - ctx := testhelper.Context(t) - response, err := client.HasLocalBranches(ctx, tc.request) require.Equal(t, tc.errorCode, helper.GrpcCode(err)) diff --git a/internal/gitaly/service/repository/size_test.go b/internal/gitaly/service/repository/size_test.go index 01f49ac2a..c55134d91 100644 --- a/internal/gitaly/service/repository/size_test.go +++ b/internal/gitaly/service/repository/size_test.go @@ -82,7 +82,9 @@ func TestGetObjectDirectorySize_quarantine(t *testing.T) { ctx := testhelper.Context(t) t.Run("quarantined repo", func(t *testing.T) { - repo, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + repo, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) quarantine, err := quarantine.New(ctx, repo, locator) require.NoError(t, err) @@ -99,11 +101,15 @@ func TestGetObjectDirectorySize_quarantine(t *testing.T) { }) t.Run("quarantined repo with different relative path", func(t *testing.T) { - repo1, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + repo1, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) quarantine1, err := quarantine.New(ctx, repo1, locator) require.NoError(t, err) - repo2, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + repo2, _ := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ + Seed: gittest.SeedGitLabTest, + }) quarantine2, err := quarantine.New(ctx, repo2, locator) require.NoError(t, err) diff --git a/internal/gitaly/service/repository/snapshot_test.go b/internal/gitaly/service/repository/snapshot_test.go index f84cfa39c..7a531dd9f 100644 --- a/internal/gitaly/service/repository/snapshot_test.go +++ b/internal/gitaly/service/repository/snapshot_test.go @@ -161,9 +161,7 @@ func TestGetSnapshot_alternateObjectDirectory(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, client := setupRepositoryServiceWithoutRepo(t) - - repo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + cfg, repo, repoPath, client := setupRepositoryService(t) locator := config.NewLocator(cfg) alternatesFile, err := locator.InfoAlternatesPath(repo) |