diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2022-11-15 12:35:49 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2022-11-15 12:35:49 +0300 |
commit | cdc87c3fefb4386c150cb4d71fcb4530d2b0501b (patch) | |
tree | a2aa7099fbf61e718c7a0d176b09718435211bc7 | |
parent | 04a8caab5e1b6747da6e14470c8f48aa145f42ff (diff) | |
parent | 6af6d880b253118d1f328b447bfb11e2200fb05d (diff) |
Merge branch 'pks-gittest-always-create-bare-repo-names' into 'master'
gittest: Convert `NewRepositoryName()` to always create ".git" suffix
See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/5052
Merged-by: Pavlo Strokov <pstrokov@gitlab.com>
Approved-by: James Fargher <proglottis@gmail.com>
Approved-by: Pavlo Strokov <pstrokov@gitlab.com>
Co-authored-by: Patrick Steinhardt <psteinhardt@gitlab.com>
15 files changed, 70 insertions, 62 deletions
diff --git a/internal/backup/backup_test.go b/internal/backup/backup_test.go index 05a57eb26..a51ae6529 100644 --- a/internal/backup/backup_test.go +++ b/internal/backup/backup_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -45,8 +46,7 @@ func TestManager_Create(t *testing.T) { desc: "no hooks", setup: func(tb testing.TB) (*gitalypb.Repository, string) { noHooksRepo, repoPath := gittest.CreateRepository(tb, ctx, cfg, gittest.CreateRepositoryConfig{ - RelativePath: "no-hooks", - Seed: gittest.SeedGitLabTest, + Seed: gittest.SeedGitLabTest, }) return noHooksRepo, repoPath }, @@ -57,8 +57,7 @@ func TestManager_Create(t *testing.T) { desc: "hooks", setup: func(tb testing.TB) (*gitalypb.Repository, string) { hooksRepo, hooksRepoPath := gittest.CreateRepository(tb, ctx, cfg, gittest.CreateRepositoryConfig{ - RelativePath: "hooks", - Seed: gittest.SeedGitLabTest, + Seed: gittest.SeedGitLabTest, }) require.NoError(tb, os.Mkdir(filepath.Join(hooksRepoPath, "custom_hooks"), os.ModePerm)) require.NoError(tb, os.WriteFile(filepath.Join(hooksRepoPath, "custom_hooks/pre-commit.sample"), []byte("Some hooks"), os.ModePerm)) @@ -82,7 +81,7 @@ func TestManager_Create(t *testing.T) { setup: func(tb testing.TB) (*gitalypb.Repository, string) { emptyRepo, repoPath := gittest.CreateRepository(tb, ctx, cfg) nonexistentRepo := proto.Clone(emptyRepo).(*gitalypb.Repository) - nonexistentRepo.RelativePath = "nonexistent" + nonexistentRepo.RelativePath = gittest.NewRepositoryName(t) return nonexistentRepo, repoPath }, createsBundle: false, @@ -92,15 +91,16 @@ func TestManager_Create(t *testing.T) { } { t.Run(tc.desc, func(t *testing.T) { repo, repoPath := tc.setup(t) - path := testhelper.TempDir(t) - refsPath := filepath.Join(path, repo.RelativePath, backupID, "001.refs") - bundlePath := filepath.Join(path, repo.RelativePath, backupID, "001.bundle") - customHooksPath := filepath.Join(path, repo.RelativePath, backupID, "001.custom_hooks.tar") + backupRoot := testhelper.TempDir(t) + + refsPath := joinBackupPath(t, backupRoot, repo, backupID, "001.refs") + bundlePath := joinBackupPath(t, backupRoot, repo, backupID, "001.bundle") + customHooksPath := joinBackupPath(t, backupRoot, repo, backupID, "001.custom_hooks.tar") pool := client.NewPool() defer testhelper.MustClose(t, pool) - sink := NewFilesystemSink(path) + sink := NewFilesystemSink(backupRoot) locator, err := ResolveLocator("pointer", sink) require.NoError(t, err) @@ -166,8 +166,7 @@ func TestManager_Create_incremental(t *testing.T) { desc: "no previous backup", setup: func(tb testing.TB, backupRoot string) (*gitalypb.Repository, string) { repo, repoPath := gittest.CreateRepository(tb, ctx, cfg, gittest.CreateRepositoryConfig{ - RelativePath: "repo", - Seed: gittest.SeedGitLabTest, + Seed: gittest.SeedGitLabTest, }) return repo, repoPath }, @@ -177,11 +176,10 @@ func TestManager_Create_incremental(t *testing.T) { desc: "previous backup, no updates", setup: func(tb testing.TB, backupRoot string) (*gitalypb.Repository, string) { repo, repoPath := gittest.CreateRepository(tb, ctx, cfg, gittest.CreateRepositoryConfig{ - RelativePath: "repo", - Seed: gittest.SeedGitLabTest, + Seed: gittest.SeedGitLabTest, }) - backupRepoPath := filepath.Join(backupRoot, repo.RelativePath) + backupRepoPath := joinBackupPath(tb, backupRoot, repo) backupPath := filepath.Join(backupRepoPath, backupID) bundlePath := filepath.Join(backupPath, "001.bundle") refsPath := filepath.Join(backupPath, "001.refs") @@ -203,11 +201,10 @@ func TestManager_Create_incremental(t *testing.T) { desc: "previous backup, updates", setup: func(tb testing.TB, backupRoot string) (*gitalypb.Repository, string) { repo, repoPath := gittest.CreateRepository(tb, ctx, cfg, gittest.CreateRepositoryConfig{ - RelativePath: "repo", - Seed: gittest.SeedGitLabTest, + Seed: gittest.SeedGitLabTest, }) - backupRepoPath := filepath.Join(backupRoot, repo.RelativePath) + backupRepoPath := joinBackupPath(tb, backupRoot, repo) backupPath := filepath.Join(backupRepoPath, backupID) bundlePath := filepath.Join(backupPath, "001.bundle") refsPath := filepath.Join(backupPath, "001.refs") @@ -229,16 +226,16 @@ func TestManager_Create_incremental(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { - path := testhelper.TempDir(t) - repo, repoPath := tc.setup(t, path) + backupRoot := testhelper.TempDir(t) + repo, repoPath := tc.setup(t, backupRoot) - refsPath := filepath.Join(path, repo.RelativePath, backupID, tc.expectedIncrement+".refs") - bundlePath := filepath.Join(path, repo.RelativePath, backupID, tc.expectedIncrement+".bundle") + refsPath := joinBackupPath(t, backupRoot, repo, backupID, tc.expectedIncrement+".refs") + bundlePath := joinBackupPath(t, backupRoot, repo, backupID, tc.expectedIncrement+".bundle") pool := client.NewPool() defer testhelper.MustClose(t, pool) - sink := NewFilesystemSink(path) + sink := NewFilesystemSink(backupRoot) locator, err := ResolveLocator("pointer", sink) require.NoError(t, err) @@ -289,7 +286,7 @@ func testManagerRestore(t *testing.T, ctx context.Context) { repo := &gitalypb.Repository{ StorageName: "default", - RelativePath: gittest.NewRepositoryName(tb, false), + RelativePath: gittest.NewRepositoryName(tb), } _, err := repoClient.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{Repository: repo}) @@ -308,7 +305,7 @@ func testManagerRestore(t *testing.T, ctx context.Context) { }) repoChecksum := gittest.ChecksumRepo(t, cfg, repoPath) - path := testhelper.TempDir(t) + backupRoot := testhelper.TempDir(t) for _, tc := range []struct { desc string @@ -324,8 +321,10 @@ func testManagerRestore(t *testing.T, ctx context.Context) { locators: []string{"legacy", "pointer"}, setup: func(tb testing.TB) (*gitalypb.Repository, *git.Checksum) { repo := createRepo(tb) - require.NoError(tb, os.MkdirAll(filepath.Join(path, repo.RelativePath), os.ModePerm)) - bundlePath := filepath.Join(path, repo.RelativePath+".bundle") + + relativePath := stripRelativePath(tb, repo) + require.NoError(tb, os.MkdirAll(filepath.Join(backupRoot, relativePath), os.ModePerm)) + bundlePath := filepath.Join(backupRoot, relativePath+".bundle") gittest.BundleRepo(tb, cfg, repoPath, bundlePath) return repo, repoChecksum @@ -337,9 +336,11 @@ func testManagerRestore(t *testing.T, ctx context.Context) { locators: []string{"legacy", "pointer"}, setup: func(tb testing.TB) (*gitalypb.Repository, *git.Checksum) { repo := createRepo(tb) - bundlePath := filepath.Join(path, repo.RelativePath+".bundle") - customHooksPath := filepath.Join(path, repo.RelativePath, "custom_hooks.tar") - require.NoError(tb, os.MkdirAll(filepath.Join(path, repo.RelativePath), os.ModePerm)) + + relativePath := stripRelativePath(tb, repo) + bundlePath := filepath.Join(backupRoot, relativePath+".bundle") + customHooksPath := filepath.Join(backupRoot, relativePath, "custom_hooks.tar") + require.NoError(tb, os.MkdirAll(filepath.Join(backupRoot, relativePath), os.ModePerm)) gittest.BundleRepo(tb, cfg, repoPath, bundlePath) testhelper.CopyFile(tb, "../gitaly/service/repository/testdata/custom_hooks.tar", customHooksPath) @@ -377,11 +378,12 @@ func testManagerRestore(t *testing.T, ctx context.Context) { setup: func(tb testing.TB) (*gitalypb.Repository, *git.Checksum) { repo := &gitalypb.Repository{ StorageName: "default", - RelativePath: gittest.NewRepositoryName(tb, false), + RelativePath: gittest.NewRepositoryName(tb), } - require.NoError(tb, os.MkdirAll(filepath.Dir(filepath.Join(path, repo.RelativePath)), os.ModePerm)) - bundlePath := filepath.Join(path, repo.RelativePath+".bundle") + relativePath := stripRelativePath(tb, repo) + require.NoError(tb, os.MkdirAll(filepath.Dir(filepath.Join(backupRoot, relativePath)), os.ModePerm)) + bundlePath := filepath.Join(backupRoot, relativePath+".bundle") gittest.BundleRepo(tb, cfg, repoPath, bundlePath) return repo, repoChecksum @@ -394,7 +396,7 @@ func testManagerRestore(t *testing.T, ctx context.Context) { setup: func(tb testing.TB) (*gitalypb.Repository, *git.Checksum) { const backupID = "abc123" repo := createRepo(tb) - repoBackupPath := filepath.Join(path, repo.RelativePath) + repoBackupPath := joinBackupPath(tb, backupRoot, repo) backupPath := filepath.Join(repoBackupPath, backupID) require.NoError(tb, os.MkdirAll(backupPath, os.ModePerm)) require.NoError(tb, os.WriteFile(filepath.Join(repoBackupPath, "LATEST"), []byte(backupID), os.ModePerm)) @@ -416,7 +418,7 @@ func testManagerRestore(t *testing.T, ctx context.Context) { expectedRepoPath := filepath.Join(cfg.Storages[0].Path, expected.RelativePath) repo := createRepo(tb) - repoBackupPath := filepath.Join(path, repo.RelativePath) + repoBackupPath := joinBackupPath(tb, backupRoot, repo) backupPath := filepath.Join(repoBackupPath, backupID) require.NoError(tb, os.MkdirAll(backupPath, os.ModePerm)) require.NoError(tb, os.WriteFile(filepath.Join(repoBackupPath, "LATEST"), []byte(backupID), os.ModePerm)) @@ -474,7 +476,7 @@ func testManagerRestore(t *testing.T, ctx context.Context) { pool := client.NewPool() defer testhelper.MustClose(t, pool) - sink := NewFilesystemSink(path) + sink := NewFilesystemSink(backupRoot) locator, err := ResolveLocator(locatorName, sink) require.NoError(t, err) @@ -644,3 +646,14 @@ func TestResolveLocator(t *testing.T) { }) } } + +func joinBackupPath(tb testing.TB, backupRoot string, repo *gitalypb.Repository, elements ...string) string { + return filepath.Join(append([]string{ + backupRoot, + stripRelativePath(tb, repo), + }, elements...)...) +} + +func stripRelativePath(tb testing.TB, repo *gitalypb.Repository) string { + return strings.TrimSuffix(repo.GetRelativePath(), ".git") +} diff --git a/internal/git/gittest/repo.go b/internal/git/gittest/repo.go index 162183d54..6250f9cc5 100644 --- a/internal/git/gittest/repo.go +++ b/internal/git/gittest/repo.go @@ -60,14 +60,9 @@ func NewObjectPoolName(tb testing.TB) string { } // NewRepositoryName returns a random repository hash -// in format '@hashed/[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{64}(.git)?'. -func NewRepositoryName(tb testing.TB, bare bool) string { - suffix := "" - if bare { - suffix = ".git" - } - - return filepath.Join("@hashed", newDiskHash(tb)+suffix) +// in format '@hashed/[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{64}.git'. +func NewRepositoryName(tb testing.TB) string { + return filepath.Join("@hashed", newDiskHash(tb)+".git") } // newDiskHash generates a random directory path following the Rails app's @@ -136,7 +131,7 @@ func CreateRepository(tb testing.TB, ctx context.Context, cfg config.Cfg, config storage = opts.Storage } - relativePath := NewRepositoryName(tb, true) + relativePath := NewRepositoryName(tb) if opts.RelativePath != "" { relativePath = opts.RelativePath } diff --git a/internal/git/housekeeping/object_pool_test.go b/internal/git/housekeeping/object_pool_test.go index eabbabc62..f534ab7ee 100644 --- a/internal/git/housekeeping/object_pool_test.go +++ b/internal/git/housekeeping/object_pool_test.go @@ -52,7 +52,7 @@ func TestIsPoolRepository(t *testing.T) { { desc: "normal repos dont match", repo: &gitalypb.Repository{ - RelativePath: "@hashed/" + gittest.NewRepositoryName(t, true), + RelativePath: "@hashed/" + gittest.NewRepositoryName(t), }, }, } { diff --git a/internal/git/housekeeping/testhelper_test.go b/internal/git/housekeeping/testhelper_test.go index ccb0e1a28..49499923f 100644 --- a/internal/git/housekeeping/testhelper_test.go +++ b/internal/git/housekeeping/testhelper_test.go @@ -15,7 +15,7 @@ func testRepoAndPool(t *testing.T, desc string, testFunc func(t *testing.T, rela t.Helper() t.Run(desc, func(t *testing.T) { t.Run("normal repository", func(t *testing.T) { - testFunc(t, gittest.NewRepositoryName(t, true)) + testFunc(t, gittest.NewRepositoryName(t)) }) t.Run("object pool", func(t *testing.T) { diff --git a/internal/git/stats/objects_info_test.go b/internal/git/stats/objects_info_test.go index 5282491ff..e63316ad1 100644 --- a/internal/git/stats/objects_info_test.go +++ b/internal/git/stats/objects_info_test.go @@ -116,7 +116,7 @@ func TestLogObjectInfo(t *testing.T) { gittest.WriteCommit(t, cfg, repoPath2, gittest.WithMessage("repo2"), gittest.WithBranch("main")) // clone existing local repo with two alternates - targetRepoName := gittest.NewRepositoryName(t, true) + targetRepoName := gittest.NewRepositoryName(t) targetRepoPath := filepath.Join(storagePath, targetRepoName) gittest.Exec(t, cfg, "clone", "--bare", "--shared", repoPath1, "--reference", repoPath1, "--reference", repoPath2, targetRepoPath) diff --git a/internal/gitaly/service/repository/create_fork_test.go b/internal/gitaly/service/repository/create_fork_test.go index e9767ee62..cd9fbca89 100644 --- a/internal/gitaly/service/repository/create_fork_test.go +++ b/internal/gitaly/service/repository/create_fork_test.go @@ -84,7 +84,7 @@ func TestCreateFork_successful(t *testing.T) { ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) forkedRepo := &gitalypb.Repository{ - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), StorageName: repo.GetStorageName(), } @@ -135,7 +135,7 @@ func TestCreateFork_refs(t *testing.T) { ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) targetRepo := &gitalypb.Repository{ - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), StorageName: sourceRepo.GetStorageName(), } @@ -193,7 +193,7 @@ func TestCreateFork_fsck(t *testing.T) { ) forkedRepo := &gitalypb.Repository{ - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), StorageName: repo.GetStorageName(), } 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 20b40c10d..76e8e5b9f 100644 --- a/internal/gitaly/service/repository/create_repository_from_bundle_test.go +++ b/internal/gitaly/service/repository/create_repository_from_bundle_test.go @@ -132,7 +132,7 @@ func TestCreateRepositoryFromBundle_transactional(t *testing.T) { createdRepo := &gitalypb.Repository{ StorageName: repoProto.GetStorageName(), - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), } require.NoError(t, stream.Send(&gitalypb.CreateRepositoryFromBundleRequest{ diff --git a/internal/gitaly/service/repository/create_repository_from_snapshot_test.go b/internal/gitaly/service/repository/create_repository_from_snapshot_test.go index eae73c6fb..43bbe2e19 100644 --- a/internal/gitaly/service/repository/create_repository_from_snapshot_test.go +++ b/internal/gitaly/service/repository/create_repository_from_snapshot_test.go @@ -162,7 +162,7 @@ func TestCreateRepositoryFromSnapshot_badURL(t *testing.T) { req := &gitalypb.CreateRepositoryFromSnapshotRequest{ Repository: &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), }, HttpUrl: "invalid!scheme://invalid.invalid", } @@ -228,7 +228,7 @@ func TestCreateRepositoryFromSnapshot_invalidArguments(t *testing.T) { req := &gitalypb.CreateRepositoryFromSnapshotRequest{ Repository: &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), }, HttpUrl: srv.URL + tc.url, HttpAuth: tc.auth, @@ -305,7 +305,7 @@ func TestCreateRepositoryFromSnapshot_resolvedAddressSuccess(t *testing.T) { srv := httptest.NewServer(&tarTesthandler{tarData: bytes.NewReader(data), secret: secret, host: host}) defer srv.Close() - repoRelativePath := gittest.NewRepositoryName(t, true) + repoRelativePath := gittest.NewRepositoryName(t) repo := &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, diff --git a/internal/gitaly/service/repository/create_repository_from_url_test.go b/internal/gitaly/service/repository/create_repository_from_url_test.go index fb9c6c1bd..af04a8c92 100644 --- a/internal/gitaly/service/repository/create_repository_from_url_test.go +++ b/internal/gitaly/service/repository/create_repository_from_url_test.go @@ -202,7 +202,7 @@ func TestCreateRepositoryFromURL_fsck(t *testing.T) { ) targetRepoProto := &gitalypb.Repository{ - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), StorageName: cfg.Storages[0].Name, } diff --git a/internal/gitaly/service/repository/create_repository_test.go b/internal/gitaly/service/repository/create_repository_test.go index 1063c56fa..761ece7ce 100644 --- a/internal/gitaly/service/repository/create_repository_test.go +++ b/internal/gitaly/service/repository/create_repository_test.go @@ -106,7 +106,7 @@ func TestCreateRepository_withDefaultBranch(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - repo := &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: gittest.NewRepositoryName(t, true)} + repo := &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: gittest.NewRepositoryName(t)} req := &gitalypb.CreateRepositoryRequest{Repository: repo, DefaultBranch: []byte(tc.defaultBranch)} _, err := client.CreateRepository(ctx, req) diff --git a/internal/gitaly/service/repository/remove_test.go b/internal/gitaly/service/repository/remove_test.go index 2e7e80d1e..079580f9a 100644 --- a/internal/gitaly/service/repository/remove_test.go +++ b/internal/gitaly/service/repository/remove_test.go @@ -25,7 +25,7 @@ func TestRemoveRepository(t *testing.T) { repo := &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), } _, err := client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{ diff --git a/internal/gitaly/service/repository/snapshot_test.go b/internal/gitaly/service/repository/snapshot_test.go index 9f5bf5686..fe52c42a4 100644 --- a/internal/gitaly/service/repository/snapshot_test.go +++ b/internal/gitaly/service/repository/snapshot_test.go @@ -254,7 +254,7 @@ func copyRepoUsingSnapshot(t *testing.T, ctx context.Context, cfg config.Cfg, cl repoCopy := &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), } createRepoReq := &gitalypb.CreateRepositoryFromSnapshotRequest{ diff --git a/internal/gitaly/service/repository/util_test.go b/internal/gitaly/service/repository/util_test.go index 8991ecad8..346d7c64e 100644 --- a/internal/gitaly/service/repository/util_test.go +++ b/internal/gitaly/service/repository/util_test.go @@ -258,7 +258,7 @@ func TestCreateRepository(t *testing.T) { t.Run(tc.desc, func(t *testing.T) { repo := &gitalypb.Repository{ StorageName: cfg.Storages[0].Name, - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), } if tc.transactional { diff --git a/internal/praefect/coordinator_test.go b/internal/praefect/coordinator_test.go index cd6d21e45..88a0b9bdf 100644 --- a/internal/praefect/coordinator_test.go +++ b/internal/praefect/coordinator_test.go @@ -784,7 +784,7 @@ func TestStreamDirector_maintenanceRPCs(t *testing.T) { repository := &gitalypb.Repository{ StorageName: "default", - RelativePath: gittest.NewRepositoryName(t, true), + RelativePath: gittest.NewRepositoryName(t), } primaryRepository := &gitalypb.Repository{ StorageName: primaryStorage, diff --git a/internal/praefect/router_per_repository_test.go b/internal/praefect/router_per_repository_test.go index 905b56531..fa60d9e33 100644 --- a/internal/praefect/router_per_repository_test.go +++ b/internal/praefect/router_per_repository_test.go @@ -459,7 +459,7 @@ func TestPerRepositoryRouter_RouteRepositoryMaintenance(t *testing.T) { var ( virtualStorage = "virtual-storage-1" - relativePath = gittest.NewRepositoryName(t, true) + relativePath = gittest.NewRepositoryName(t) ) configuredStorages := []string{"primary", "secondary-1", "secondary-2"} |