diff options
author | Toon Claes <toon@gitlab.com> | 2020-11-19 22:05:56 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2020-11-19 22:05:56 +0300 |
commit | 2ba10355e529759ce808d1c452c27b93b2e8dc97 (patch) | |
tree | d974889ed1203d2781276d264e7ac353d558d145 | |
parent | f77008aeff9d669fdc9744c75e15825178a259e4 (diff) | |
parent | c7edf9ae83fa39fd1152a27a52151d18704f97cb (diff) |
Merge branch 'pks-tests-storage-in-tmpfs' into 'master'
testhelper: Move storage to temporary directory
See merge request gitlab-org/gitaly!2805
36 files changed, 293 insertions, 205 deletions
diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index bb81ec32a..bbe14e87f 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -535,11 +535,8 @@ func TestCheckOK(t *testing.T) { serverURL, cleanup := testhelper.NewGitlabTestServer(t, c) defer cleanup() - tempDir, err := ioutil.TempDir("", t.Name()) - require.NoError(t, err) - defer func() { - os.RemoveAll(tempDir) - }() + tempDir, cleanup := testhelper.TempDir(t) + defer cleanup() gitlabShellDir := filepath.Join(tempDir, "gitlab-shell") require.NoError(t, os.MkdirAll(gitlabShellDir, 0755)) @@ -554,7 +551,7 @@ func TestCheckOK(t *testing.T) { cmd.Stderr = &stderr cmd.Stdout = &stdout - err = cmd.Run() + err := cmd.Run() require.NoError(t, err) require.Empty(t, stderr.String()) @@ -583,11 +580,8 @@ func TestCheckBadCreds(t *testing.T) { serverURL, cleanup := testhelper.NewGitlabTestServer(t, c) defer cleanup() - tempDir, err := ioutil.TempDir("", t.Name()) - require.NoError(t, err) - defer func() { - os.RemoveAll(tempDir) - }() + tempDir, cleanup := testhelper.TempDir(t) + defer cleanup() gitlabShellDir := filepath.Join(tempDir, "gitlab-shell") require.NoError(t, os.MkdirAll(gitlabShellDir, 0755)) diff --git a/cmd/gitaly-lfs-smudge/lfs_smudge_test.go b/cmd/gitaly-lfs-smudge/lfs_smudge_test.go index 244cd16d5..a84cf204f 100644 --- a/cmd/gitaly-lfs-smudge/lfs_smudge_test.go +++ b/cmd/gitaly-lfs-smudge/lfs_smudge_test.go @@ -91,9 +91,8 @@ func TestSuccessfulLfsSmudge(t *testing.T) { }) require.NoError(t, err) - tmpDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) + tmpDir, cleanup := testhelper.TempDir(t) + defer cleanup() env := map[string]string{ "GL_REPOSITORY": "project-1", @@ -186,9 +185,8 @@ func TestUnsuccessfulLfsSmudge(t *testing.T) { tlsCfg, err := json.Marshal(tc.tlsCfg) require.NoError(t, err) - tmpDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) + tmpDir, cleanup := testhelper.TempDir(t) + defer cleanup() env := map[string]string{ "GL_REPOSITORY": "project-1", diff --git a/cmd/gitaly-ssh/auth_test.go b/cmd/gitaly-ssh/auth_test.go index ef13bd2fb..c22122097 100644 --- a/cmd/gitaly-ssh/auth_test.go +++ b/cmd/gitaly-ssh/auth_test.go @@ -34,7 +34,8 @@ func TestConnectivity(t *testing.T) { certPoolPath := filepath.Join(cwd, "testdata", "certs") - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() socketPath := testhelper.GetTemporaryGitalySocketFileName() diff --git a/internal/bootstrap/bootstrap_test.go b/internal/bootstrap/bootstrap_test.go index de824fc2b..bf8230c93 100644 --- a/internal/bootstrap/bootstrap_test.go +++ b/internal/bootstrap/bootstrap_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" ) type mockUpgrader struct { @@ -59,7 +60,10 @@ func (s *testServer) slowRequest(duration time.Duration) <-chan error { } func TestCreateUnixListener(t *testing.T) { - socketPath := filepath.Join(os.TempDir(), "gitaly-test-unix-socket") + tempDir, cleanup := testhelper.TempDir(t) + defer cleanup() + + socketPath := filepath.Join(tempDir, "gitaly-test-unix-socket") if err := os.Remove(socketPath); err != nil { require.True(t, os.IsNotExist(err), "cannot delete dangling socket: %v", err) } @@ -288,9 +292,12 @@ func makeBootstrap(t *testing.T) (*Bootstrap, *testServer) { } } + tempDir, cleanup := testhelper.TempDir(t) + defer cleanup() + for network, address := range map[string]string{ "tcp": "127.0.0.1:0", - "unix": filepath.Join(os.TempDir(), "gitaly-test-unix-socket"), + "unix": filepath.Join(tempDir, "gitaly-test-unix-socket"), } { b.RegisterStarter(start(network, address)) } diff --git a/internal/bootstrap/testhelper_test.go b/internal/bootstrap/testhelper_test.go new file mode 100644 index 000000000..ad7c28be0 --- /dev/null +++ b/internal/bootstrap/testhelper_test.go @@ -0,0 +1,18 @@ +package bootstrap + +import ( + "os" + "testing" + + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + cleanup := testhelper.Configure() + defer cleanup() + return m.Run() +} diff --git a/internal/cache/cachedb_test.go b/internal/cache/cachedb_test.go index d18e0355f..161e24981 100644 --- a/internal/cache/cachedb_test.go +++ b/internal/cache/cachedb_test.go @@ -134,8 +134,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) { func injectTempStorage(t testing.TB) (string, testhelper.Cleanup) { oldStorages := config.Config.Storages - tmpDir, err := ioutil.TempDir("", t.Name()) - require.NoError(t, err) + tmpDir, cleanup := testhelper.TempDir(t) name := filepath.Base(tmpDir) config.Config.Storages = append(config.Config.Storages, config.Storage{ @@ -143,12 +142,10 @@ func injectTempStorage(t testing.TB) (string, testhelper.Cleanup) { Path: tmpDir, }) - cleanup := func() { + return name, func() { config.Config.Storages = oldStorages - require.NoError(t, os.RemoveAll(tmpDir)) + cleanup() } - - return name, cleanup } func TestLoserCount(t *testing.T) { diff --git a/internal/cache/walker_internal_test.go b/internal/cache/walker_internal_test.go index bd5d3e768..438978f77 100644 --- a/internal/cache/walker_internal_test.go +++ b/internal/cache/walker_internal_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" ) func TestCleanWalkDirNotExists(t *testing.T) { @@ -19,9 +20,8 @@ func TestCleanWalkDirNotExists(t *testing.T) { } func TestCleanWalkEmptyDirs(t *testing.T) { - tmp, err := ioutil.TempDir("", t.Name()) - require.NoError(t, err) - defer func() { require.NoError(t, os.RemoveAll(tmp)) }() + tmp, cleanup := testhelper.TempDir(t) + defer cleanup() for _, tt := range []struct { path string diff --git a/internal/cache/walker_test.go b/internal/cache/walker_test.go index a469ffbd1..d4c341299 100644 --- a/internal/cache/walker_test.go +++ b/internal/cache/walker_test.go @@ -93,8 +93,7 @@ func TestDiskCacheInitialClear(t *testing.T) { } func setupDiskCacheWalker(t testing.TB) func() { - tmpPath, err := ioutil.TempDir("", t.Name()) - require.NoError(t, err) + tmpPath, cleanup := testhelper.TempDir(t) oldStorages := config.Config.Storages config.Config.Storages = []config.Storage{ @@ -104,12 +103,10 @@ func setupDiskCacheWalker(t testing.TB) func() { }, } - cleanup := func() { + return func() { config.Config.Storages = oldStorages - require.NoError(t, os.RemoveAll(tmpPath)) + cleanup() } - - return cleanup } func pollCountersUntil(t testing.TB, expectRemovals int) { diff --git a/internal/git/catfile/catfile_test.go b/internal/git/catfile/catfile_test.go index da1ab8059..14cc4316d 100644 --- a/internal/git/catfile/catfile_test.go +++ b/internal/git/catfile/catfile_test.go @@ -15,6 +15,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/metadata" ) @@ -22,7 +23,10 @@ func TestInfo(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) testCases := []struct { @@ -55,7 +59,10 @@ func TestBlob(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) gitignoreBytes, err := ioutil.ReadFile("testdata/blob-dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82") @@ -119,7 +126,10 @@ func TestCommit(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) commitBytes, err := ioutil.ReadFile("testdata/commit-e63f41fe459e62e1228fcef60d7189127aeba95a") @@ -154,7 +164,10 @@ func TestTag(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) tagBytes, err := ioutil.ReadFile("testdata/tag-a509fa67c27202a2bc9dd5e014b4af7e6063ac76") @@ -218,7 +231,10 @@ func TestTree(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) treeBytes, err := ioutil.ReadFile("testdata/tree-7e2f26d033ee47cd0745649d1a28277c56197921") @@ -282,7 +298,10 @@ func TestRepeatedCalls(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - c, err := New(ctx, testhelper.TestRepository()) + testRepository, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + c, err := New(ctx, testRepository) require.NoError(t, err) treeOid := "7e2f26d033ee47cd0745649d1a28277c56197921" @@ -344,8 +363,11 @@ func TestSpawnFailure(t *testing.T) { ctx1, cancel1 := testhelper.Context() defer cancel1() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + injectSpawnErrors = false - _, err := catfileWithFreshSessionID(ctx1) + _, err := catfileWithFreshSessionID(ctx1, testRepo) require.NoError(t, err, "catfile spawn should succeed in normal circumstances") require.Equal(t, 2, numGitChildren(t), "there should be 2 git child processes") @@ -373,7 +395,7 @@ func TestSpawnFailure(t *testing.T) { defer cancel2() injectSpawnErrors = true - _, err = catfileWithFreshSessionID(ctx2) + _, err = catfileWithFreshSessionID(ctx2, testRepo) require.Error(t, err, "expect simulated error") require.IsType(t, &simulatedBatchSpawnError{}, err) @@ -384,7 +406,7 @@ func TestSpawnFailure(t *testing.T) { ) } -func catfileWithFreshSessionID(ctx context.Context) (*Batch, error) { +func catfileWithFreshSessionID(ctx context.Context, repo *gitalypb.Repository) (*Batch, error) { id, err := text.RandomHex(4) if err != nil { return nil, err @@ -394,7 +416,7 @@ func catfileWithFreshSessionID(ctx context.Context) (*Batch, error) { SessionIDField: id, }) - return New(metadata.NewIncomingContext(ctx, md), testhelper.TestRepository()) + return New(metadata.NewIncomingContext(ctx, md), repo) } func waitTrue(callback func() bool) bool { diff --git a/internal/git/command_test.go b/internal/git/command_test.go index 746baf856..ce29dc35d 100644 --- a/internal/git/command_test.go +++ b/internal/git/command_test.go @@ -1,7 +1,6 @@ package git import ( - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -27,9 +26,8 @@ func TestGitCommandProxy(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - dir, err := ioutil.TempDir("", "test-clone") - require.NoError(t, err) - defer os.RemoveAll(dir) + dir, cleanup := testhelper.TempDir(t) + defer cleanup() cmd, err := unsafeCmdWithoutRepo(ctx, CmdStream{}, "clone", "http://gitlab.com/bogus-repo", dir) require.NoError(t, err) diff --git a/internal/git/remote_test.go b/internal/git/remote_test.go index 2f7fda939..a50e2e49d 100644 --- a/internal/git/remote_test.go +++ b/internal/git/remote_test.go @@ -62,6 +62,11 @@ func TestRepositoryRemote_Add(t *testing.T) { repo, repoPath, cleanup := testhelper.NewTestRepo(t) defer cleanup() + _, remoteRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "remote", "remove", "origin") + ctx, cancel := testhelper.Context() defer cancel() @@ -96,14 +101,12 @@ func TestRepositoryRemote_Add(t *testing.T) { }) t.Run("fetch", func(t *testing.T) { - require.NoError(t, remote.Add(ctx, "first", testhelper.GitlabTestStoragePath()+"/gitlab-test.git", RemoteAddOpts{Fetch: true})) + require.NoError(t, remote.Add(ctx, "first", remoteRepoPath, RemoteAddOpts{Fetch: true})) remotes := text.ChompBytes(testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "remote", "--verbose")) require.Equal(t, - "first "+testhelper.GitlabTestStoragePath()+"/gitlab-test.git (fetch)\n"+ - "first "+testhelper.GitlabTestStoragePath()+"/gitlab-test.git (push)\n"+ - "origin "+testhelper.GitlabTestStoragePath()+"/gitlab-test.git (fetch)\n"+ - "origin "+testhelper.GitlabTestStoragePath()+"/gitlab-test.git (push)", + "first "+remoteRepoPath+" (fetch)\n"+ + "first "+remoteRepoPath+" (push)", remotes, ) latestSHA := text.ChompBytes(testhelper.MustRunCommand(t, nil, "git", "-C", repoPath, "rev-parse", "refs/remotes/first/master")) diff --git a/internal/git/remoterepo/repository_test.go b/internal/git/remoterepo/repository_test.go index b88a55413..15408e0d5 100644 --- a/internal/git/remoterepo/repository_test.go +++ b/internal/git/remoterepo/repository_test.go @@ -23,12 +23,15 @@ func TestRepository(t *testing.T) { ctx, err := helper.InjectGitalyServers(ctx, "default", serverSocketPath, config.Config.Auth.Token) require.NoError(t, err) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + git.TestRepository(t, func(t testing.TB, pbRepo *gitalypb.Repository) git.Repository { t.Helper() r, err := New( helper.OutgoingToIncoming(ctx), - testhelper.TestRepository(), + testRepo, client.NewPool(), ) require.NoError(t, err) diff --git a/internal/git/repository_suite.go b/internal/git/repository_suite.go index 616530fb6..dc5c3a4e0 100644 --- a/internal/git/repository_suite.go +++ b/internal/git/repository_suite.go @@ -20,7 +20,10 @@ func TestRepository(t *testing.T, getRepository func(testing.TB, *gitalypb.Repos }, } { t.Run(tc.desc, func(t *testing.T) { - tc.test(t, getRepository(t, testhelper.TestRepository())) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + tc.test(t, getRepository(t, testRepo)) }) } } diff --git a/internal/git/repository_test.go b/internal/git/repository_test.go index 79af68e96..86f3f1920 100644 --- a/internal/git/repository_test.go +++ b/internal/git/repository_test.go @@ -35,7 +35,10 @@ func TestLocalRepository_ContainsRef(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) testcases := []struct { desc string @@ -72,7 +75,10 @@ func TestLocalRepository_GetReference(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) testcases := []struct { desc string @@ -118,7 +124,10 @@ func TestLocalRepository_GetBranch(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) testcases := []struct { desc string @@ -164,7 +173,10 @@ func TestLocalRepository_GetReferences(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) testcases := []struct { desc string @@ -291,7 +303,10 @@ func TestLocalRepository_ReadObject(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) for _, tc := range []struct { desc string @@ -323,7 +338,10 @@ func TestLocalRepository_GetBranches(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := NewRepository(testhelper.TestRepository()) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + repo := NewRepository(testRepo) refs, err := repo.GetBranches(ctx) require.NoError(t, err) @@ -450,12 +468,15 @@ func TestLocalRepository_FetchRemote(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() + _, remoteRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + initBareWithRemote := func(t *testing.T, remote string) (*LocalRepository, string, testhelper.Cleanup) { t.Helper() testRepo, testRepoPath, cleanup := testhelper.InitBareRepo(t) - cmd := exec.Command(command.GitPath(), "-C", testRepoPath, "remote", "add", remote, testhelper.GitlabTestStoragePath()+"/gitlab-test.git") + cmd := exec.Command(command.GitPath(), "-C", testRepoPath, "remote", "add", remote, remoteRepoPath) err := cmd.Run() if err != nil { cleanup() diff --git a/internal/gitaly/rubyserver/proxy_test.go b/internal/gitaly/rubyserver/proxy_test.go index 6076a7ad0..d21b006ca 100644 --- a/internal/gitaly/rubyserver/proxy_test.go +++ b/internal/gitaly/rubyserver/proxy_test.go @@ -16,6 +16,9 @@ func TestSetHeadersBlocksUnknownMetadata(t *testing.T) { otherValue := "test-value" inCtx := metadata.NewIncomingContext(ctx, metadata.Pairs(otherKey, otherValue)) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + outCtx, err := SetHeaders(inCtx, testhelper.DefaultLocator(), testRepo) require.NoError(t, err) @@ -34,6 +37,9 @@ func TestSetHeadersPreservesAllowlistedMetadata(t *testing.T) { value := "test-value" inCtx := metadata.NewIncomingContext(ctx, metadata.Pairs(key, value)) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + outCtx, err := SetHeaders(inCtx, testhelper.DefaultLocator(), testRepo) require.NoError(t, err) @@ -51,6 +57,9 @@ func TestRubyFeatureHeaders(t *testing.T) { value := "true" inCtx := metadata.NewIncomingContext(ctx, metadata.Pairs(key, value)) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + outCtx, err := SetHeaders(inCtx, testhelper.DefaultLocator(), testRepo) require.NoError(t, err) diff --git a/internal/gitaly/rubyserver/rubyserver_test.go b/internal/gitaly/rubyserver/rubyserver_test.go index c0a3a5553..f1635b5df 100644 --- a/internal/gitaly/rubyserver/rubyserver_test.go +++ b/internal/gitaly/rubyserver/rubyserver_test.go @@ -28,6 +28,9 @@ func TestSetHeaders(t *testing.T) { locator := testhelper.DefaultLocator() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + testCases := []struct { desc string repo *gitalypb.Repository diff --git a/internal/gitaly/rubyserver/testhelper_test.go b/internal/gitaly/rubyserver/testhelper_test.go index bdd36f0eb..304f73e0d 100644 --- a/internal/gitaly/rubyserver/testhelper_test.go +++ b/internal/gitaly/rubyserver/testhelper_test.go @@ -5,11 +5,6 @@ import ( "testing" "gitlab.com/gitlab-org/gitaly/internal/testhelper" - "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" -) - -var ( - testRepo *gitalypb.Repository ) func TestMain(m *testing.M) { @@ -21,7 +16,6 @@ func testMain(m *testing.M) int { cleanup := testhelper.Configure() defer cleanup() - testRepo = testhelper.TestRepository() return m.Run() } diff --git a/internal/gitaly/service/operations/tags_test.go b/internal/gitaly/service/operations/tags_test.go index f9a1c54ba..65ffefa7f 100644 --- a/internal/gitaly/service/operations/tags_test.go +++ b/internal/gitaly/service/operations/tags_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io/ioutil" - "os" "os/exec" "path/filepath" "testing" @@ -108,15 +107,12 @@ unless out.chomp == ARGV[0] abort "error: expected #{ARGV[0]} object, got #{out}" end`, command.GitPath()) - dir, err := ioutil.TempDir("", "gitaly-temp-dir-*") - require.NoError(t, err) + dir, cleanup := testhelper.TempDir(t) hookPath := filepath.Join(dir, "pre-receive") require.NoError(t, ioutil.WriteFile(hookPath, []byte(hook), 0755)) - return hookPath, func() { - os.RemoveAll(dir) - } + return hookPath, cleanup } func writeAssertObjectTypeUpdateHook(t *testing.T) (string, func()) { @@ -136,15 +132,12 @@ unless out.chomp == expected_object_type abort "error: expected #{expected_object_type} object, got #{out}" end`, command.GitPath()) - dir, err := ioutil.TempDir("", "gitaly-temp-dir-*") - require.NoError(t, err) + dir, cleanup := testhelper.TempDir(t) hookPath := filepath.Join(dir, "pre-receive") require.NoError(t, ioutil.WriteFile(hookPath, []byte(hook), 0755)) - return hookPath, func() { - os.RemoveAll(dir) - } + return hookPath, cleanup } func TestSuccessfulUserCreateTagRequest(t *testing.T) { diff --git a/internal/gitaly/service/repository/archive_test.go b/internal/gitaly/service/repository/archive_test.go index 1188d3344..79a8a5942 100644 --- a/internal/gitaly/service/repository/archive_test.go +++ b/internal/gitaly/service/repository/archive_test.go @@ -180,13 +180,22 @@ func TestGetArchiveWithLfsSuccess(t *testing.T) { LfsBody: lfsBody, } + gitlabShellDir, cleanup := testhelper.TempDir(t) + defer cleanup() + + defer func(cfg config.Cfg) { + config.Config = cfg + }(config.Config) + + config.Config.GitlabShell.Dir = gitlabShellDir + config.Config.Gitlab.SecretFile = filepath.Join(config.Config.GitlabShell.Dir, ".gitlab_shell_secret") + url, cleanup := testhelper.SetupAndStartGitlabServer(t, &defaultOptions) defer cleanup() - cfg := config.Config - cfg.Gitlab.URL = url - cfg.Gitlab.SecretFile = filepath.Join(cfg.GitlabShell.Dir, ".gitlab_shell_secret") - serverSocketPath, stop := runRepoServerWithConfig(t, cfg, config.NewLocator(cfg)) + config.Config.Gitlab.URL = url + + serverSocketPath, stop := runRepoServerWithConfig(t, config.Config, config.NewLocator(config.Config)) defer stop() client, conn := newRepositoryClient(t, serverSocketPath) @@ -429,9 +438,8 @@ func TestGetArchivePathInjection(t *testing.T) { defer cancel() // Adding a temp directory representing the .ssh directory - sshDirectory, err := ioutil.TempDir("", ".ssh") - require.NoError(t, err) - require.NoError(t, os.MkdirAll(sshDirectory, os.ModeDir|0755)) + sshDirectory, cleanup := testhelper.TempDir(t) + defer cleanup() // Adding an empty authorized_keys file authorizedKeysPath := filepath.Join(sshDirectory, "authorized_keys") diff --git a/internal/gitaly/service/repository/fetch_remote_test.go b/internal/gitaly/service/repository/fetch_remote_test.go index 5ec877126..b2a0d7520 100644 --- a/internal/gitaly/service/repository/fetch_remote_test.go +++ b/internal/gitaly/service/repository/fetch_remote_test.go @@ -72,7 +72,8 @@ func TestFetchRemoteSuccess(t *testing.T) { } func TestFetchRemoteFailure(t *testing.T) { - repo := testhelper.TestRepository() + repo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() serverSocketPath, stop := runRepoServer(t, config.NewLocator(config.Config)) defer stop() diff --git a/internal/gitaly/service/repository/repack_test.go b/internal/gitaly/service/repository/repack_test.go index 08b1fa01b..39360ab92 100644 --- a/internal/gitaly/service/repository/repack_test.go +++ b/internal/gitaly/service/repository/repack_test.go @@ -136,7 +136,7 @@ func TestRepackIncrementalFailure(t *testing.T) { {desc: "nil repo", repo: nil, code: codes.InvalidArgument}, {desc: "invalid storage name", repo: &gitalypb.Repository{StorageName: "foo"}, code: codes.InvalidArgument}, {desc: "no storage name", repo: &gitalypb.Repository{RelativePath: "bar"}, code: codes.InvalidArgument}, - {desc: "non-existing repo", repo: &gitalypb.Repository{StorageName: testhelper.TestRepository().GetStorageName(), RelativePath: "bar"}, code: codes.NotFound}, + {desc: "non-existing repo", repo: &gitalypb.Repository{StorageName: testhelper.DefaultStorageName, RelativePath: "bar"}, code: codes.NotFound}, } for _, test := range tests { @@ -275,7 +275,7 @@ func TestRepackFullFailure(t *testing.T) { {desc: "nil repo", repo: nil, code: codes.InvalidArgument}, {desc: "invalid storage name", repo: &gitalypb.Repository{StorageName: "foo"}, code: codes.InvalidArgument}, {desc: "no storage name", repo: &gitalypb.Repository{RelativePath: "bar"}, code: codes.InvalidArgument}, - {desc: "non-existing repo", repo: &gitalypb.Repository{StorageName: testhelper.TestRepository().GetStorageName(), RelativePath: "bar"}, code: codes.NotFound}, + {desc: "non-existing repo", repo: &gitalypb.Repository{StorageName: testhelper.DefaultStorageName, RelativePath: "bar"}, code: codes.NotFound}, } for _, test := range tests { diff --git a/internal/gitaly/service/repository/repository_test.go b/internal/gitaly/service/repository/repository_test.go index 156350b91..18a5a932b 100644 --- a/internal/gitaly/service/repository/repository_test.go +++ b/internal/gitaly/service/repository/repository_test.go @@ -1,7 +1,6 @@ package repository import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -15,9 +14,8 @@ import ( ) func TestRepositoryExists(t *testing.T) { - storageOtherDir, err := ioutil.TempDir("", "gitaly-repository-exists-test") - require.NoError(t, err, "tempdir") - defer os.Remove(storageOtherDir) + storageOtherDir, cleanup := testhelper.TempDir(t) + defer cleanup() // Setup storage paths testStorages := []config.Storage{ @@ -35,6 +33,9 @@ func TestRepositoryExists(t *testing.T) { serverSocketPath, stop := runRepoServer(t, locator, testhelper.WithStorages([]string{"default", "other", "broken"})) defer stop() + testRepo, _, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() @@ -56,7 +57,7 @@ func TestRepositoryExists(t *testing.T) { request: &gitalypb.RepositoryExistsRequest{ Repository: &gitalypb.Repository{ StorageName: "", - RelativePath: testhelper.TestRelativePath, + RelativePath: testRepo.GetRelativePath(), }, }, errorCode: codes.InvalidArgument, @@ -65,7 +66,7 @@ func TestRepositoryExists(t *testing.T) { desc: "relative path empty", request: &gitalypb.RepositoryExistsRequest{ Repository: &gitalypb.Repository{ - StorageName: "default", + StorageName: testRepo.GetStorageName(), RelativePath: "", }, }, @@ -75,8 +76,8 @@ func TestRepositoryExists(t *testing.T) { desc: "exists true", request: &gitalypb.RepositoryExistsRequest{ Repository: &gitalypb.Repository{ - StorageName: "default", - RelativePath: testhelper.TestRelativePath, + StorageName: testRepo.GetStorageName(), + RelativePath: testRepo.GetRelativePath(), }, }, exists: true, @@ -86,7 +87,7 @@ func TestRepositoryExists(t *testing.T) { request: &gitalypb.RepositoryExistsRequest{ Repository: &gitalypb.Repository{ StorageName: "other", - RelativePath: testhelper.TestRelativePath, + RelativePath: testRepo.GetRelativePath(), }, }, exists: false, diff --git a/internal/gitaly/service/repository/size_test.go b/internal/gitaly/service/repository/size_test.go index 2e0f0668c..9ba171edb 100644 --- a/internal/gitaly/service/repository/size_test.go +++ b/internal/gitaly/service/repository/size_test.go @@ -22,7 +22,8 @@ func TestSuccessfulRepositorySizeRequest(t *testing.T) { client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() - repo := testhelper.TestRepository() + repo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() request := &gitalypb.RepositorySizeRequest{Repository: repo} @@ -76,7 +77,9 @@ func TestSuccessfulGetObjectDirectorySizeRequest(t *testing.T) { client, conn := newRepositoryClient(t, serverSocketPath) defer conn.Close() - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + testRepo.GitObjectDirectory = "objects/" request := &gitalypb.GetObjectDirectorySizeRequest{ diff --git a/internal/gitaly/service/smarthttp/inforefs_test.go b/internal/gitaly/service/smarthttp/inforefs_test.go index a9ff321bf..e77d4f26c 100644 --- a/internal/gitaly/service/smarthttp/inforefs_test.go +++ b/internal/gitaly/service/smarthttp/inforefs_test.go @@ -54,7 +54,8 @@ func TestSuccessfulInfoRefsUploadWithPartialClone(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() request := &gitalypb.InfoRefsRequest{ Repository: testRepo, diff --git a/internal/gitaly/service/smarthttp/receive_pack_test.go b/internal/gitaly/service/smarthttp/receive_pack_test.go index a939c98d1..9c719ee74 100644 --- a/internal/gitaly/service/smarthttp/receive_pack_test.go +++ b/internal/gitaly/service/smarthttp/receive_pack_test.go @@ -138,9 +138,8 @@ func TestFailedReceivePackRequestWithGitOpts(t *testing.T) { } func TestFailedReceivePackRequestDueToHooksFailure(t *testing.T) { - hookDir, err := ioutil.TempDir("", "gitaly-tmp-hooks") - require.NoError(t, err) - defer os.RemoveAll(hookDir) + hookDir, cleanup := testhelper.TempDir(t) + defer cleanup() defer func(override string) { hooks.Override = override diff --git a/internal/gitaly/service/smarthttp/upload_pack_test.go b/internal/gitaly/service/smarthttp/upload_pack_test.go index cdd2757fd..ebbc6d341 100644 --- a/internal/gitaly/service/smarthttp/upload_pack_test.go +++ b/internal/gitaly/service/smarthttp/upload_pack_test.go @@ -38,8 +38,8 @@ func TestSuccessfulUploadPackRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo := testhelper.TestRepository() - testRepoPath := filepath.Join(testhelper.GitlabTestStoragePath(), testRepo.RelativePath) + _, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() storagePath := testhelper.GitlabTestStoragePath() remoteRepoRelativePath := "gitlab-test-remote" @@ -107,8 +107,8 @@ func TestUploadPackRequestWithGitConfigOptions(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo := testhelper.TestRepository() - testRepoPath := filepath.Join(testhelper.GitlabTestStoragePath(), testRepo.RelativePath) + _, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() storagePath := testhelper.GitlabTestStoragePath() ourRepoRelativePath := "gitlab-test-remote" @@ -173,8 +173,8 @@ func TestUploadPackRequestWithGitProtocol(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo := testhelper.TestRepository() - testRepoPath := filepath.Join(testhelper.GitlabTestStoragePath(), testRepo.RelativePath) + _, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() storagePath := testhelper.GitlabTestStoragePath() relativePath, err := filepath.Rel(storagePath, testRepoPath) @@ -218,7 +218,8 @@ func TestSuccessfulUploadPackDeepenRequest(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() requestBody := &bytes.Buffer{} pktline.WriteString(requestBody, fmt.Sprintf("want e63f41fe459e62e1228fcef60d7189127aeba95a %s\n", clientCapabilities)) @@ -330,8 +331,8 @@ func TestUploadPackRequestForPartialCloneSuccess(t *testing.T) { ) defer stop() - testRepo := testhelper.TestRepository() - testRepoPath := filepath.Join(testhelper.GitlabTestStoragePath(), testRepo.RelativePath) + _, testRepoPath, cleanup := testhelper.NewTestRepo(t) + defer cleanup() storagePath := testhelper.GitlabTestStoragePath() remoteRepoRelativePath := "gitlab-test-remote" diff --git a/internal/gitaly/service/ssh/receive_pack_test.go b/internal/gitaly/service/ssh/receive_pack_test.go index 9647e74a4..8fdae975d 100644 --- a/internal/gitaly/service/ssh/receive_pack_test.go +++ b/internal/gitaly/service/ssh/receive_pack_test.go @@ -32,6 +32,9 @@ func TestFailedReceivePackRequestDueToValidationError(t *testing.T) { client, conn := newSSHClient(t, serverSocketPath) defer conn.Close() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + tests := []struct { Desc string Req *gitalypb.SSHReceivePackRequest @@ -89,7 +92,7 @@ func TestReceivePackPushSuccess(t *testing.T) { glRepository := "project-456" glProjectPath := "project/path" - lHead, rHead, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testRepo.GetStorageName(), glID: "user-123", glRepository: glRepository, glProjectPath: glProjectPath}) + lHead, rHead, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testhelper.DefaultStorageName, glID: "user-123", glRepository: glRepository, glProjectPath: glProjectPath}) if err != nil { t.Fatal(err) } @@ -116,7 +119,7 @@ func TestReceivePackPushSuccessWithGitProtocol(t *testing.T) { serverSocketPath, stop := runSSHServer(t) defer stop() - lHead, rHead, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testRepo.GetStorageName(), glID: "1", gitProtocol: git.ProtocolV2}) + lHead, rHead, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testhelper.DefaultStorageName, glID: "1", gitProtocol: git.ProtocolV2}) if err != nil { t.Fatal(err) } @@ -136,7 +139,7 @@ func TestReceivePackPushFailure(t *testing.T) { _, _, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: "foobar", glID: "1"}) require.Error(t, err, "local and remote head equal. push did not fail") - _, _, err = testCloneAndPush(t, serverSocketPath, pushParams{storageName: testRepo.GetStorageName(), glID: ""}) + _, _, err = testCloneAndPush(t, serverSocketPath, pushParams{storageName: testhelper.DefaultStorageName, glID: ""}) require.Error(t, err, "local and remote head equal. push did not fail") } @@ -144,9 +147,8 @@ func TestReceivePackPushHookFailure(t *testing.T) { serverSocketPath, stop := runSSHServer(t) defer stop() - hookDir, err := ioutil.TempDir("", "gitaly-tmp-hooks") - require.NoError(t, err) - defer os.RemoveAll(hookDir) + hookDir, cleanup := testhelper.TempDir(t) + defer cleanup() defer func(old string) { hooks.Override = old }(hooks.Override) hooks.Override = hookDir @@ -156,7 +158,7 @@ func TestReceivePackPushHookFailure(t *testing.T) { hookContent := []byte("#!/bin/sh\nexit 1") ioutil.WriteFile(filepath.Join(hooks.Path(), "pre-receive"), hookContent, 0755) - _, _, err = testCloneAndPush(t, serverSocketPath, pushParams{storageName: testRepo.GetStorageName(), glID: "1"}) + _, _, err := testCloneAndPush(t, serverSocketPath, pushParams{storageName: testhelper.DefaultStorageName, glID: "1"}) require.Error(t, err) require.Contains(t, err.Error(), "(pre-receive hook declined)") } @@ -251,7 +253,7 @@ func TestSSHReceivePackToHooks(t *testing.T) { defer cleanup() lHead, rHead, err := sshPush(t, cloneDetails, serverSocketPath, pushParams{ - storageName: testRepo.GetStorageName(), + storageName: testhelper.DefaultStorageName, glID: glID, glRepository: glRepository, gitProtocol: git.ProtocolV2, @@ -274,6 +276,9 @@ type SSHCloneDetails struct { // setupSSHClone sets up a test clone func setupSSHClone(t *testing.T) (SSHCloneDetails, func()) { + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + storagePath := testhelper.GitlabTestStoragePath() tempRepo := "gitlab-test-ssh-receive-pack.git" testRepoPath := filepath.Join(storagePath, testRepo.GetRelativePath()) diff --git a/internal/gitaly/service/ssh/testhelper_test.go b/internal/gitaly/service/ssh/testhelper_test.go index da2d79083..1d805651b 100644 --- a/internal/gitaly/service/ssh/testhelper_test.go +++ b/internal/gitaly/service/ssh/testhelper_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "testing" - log "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" @@ -14,13 +13,7 @@ import ( "google.golang.org/grpc/reflection" ) -const ( - testPath = "testdata/scratch" - testRepoRoot = testPath + "/data" -) - var ( - testRepo *gitalypb.Repository gitalySSHPath string ) @@ -39,15 +32,6 @@ func testMain(m *testing.M) int { defer cleanup() config.Config.Ruby.Dir = filepath.Join("../../../ruby", "git-hooks") - - err := os.RemoveAll(testPath) - if err != nil { - log.Error(err) - return 1 - } - - testRepo = testhelper.TestRepository() - testhelper.ConfigureGitalyHooksBinary() testhelper.ConfigureGitalySSH() gitalySSHPath = filepath.Join(config.Config.BinDir, "gitaly-ssh") diff --git a/internal/gitaly/service/ssh/upload_archive_test.go b/internal/gitaly/service/ssh/upload_archive_test.go index d39f636f2..21a601e38 100644 --- a/internal/gitaly/service/ssh/upload_archive_test.go +++ b/internal/gitaly/service/ssh/upload_archive_test.go @@ -25,6 +25,9 @@ func TestFailedUploadArchiveRequestDueToTimeout(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + stream, err := client.SSHUploadArchive(ctx) require.NoError(t, err) @@ -104,6 +107,9 @@ func TestUploadArchiveSuccess(t *testing.T) { cmd := exec.Command(command.GitPath(), "archive", "master", "--remote=git@localhost:test/test.git") + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + err := testArchive(t, serverSocketPath, testRepo, cmd) require.NoError(t, err) } diff --git a/internal/gitaly/service/ssh/upload_pack_test.go b/internal/gitaly/service/ssh/upload_pack_test.go index b0d5ce0dd..b544f278d 100644 --- a/internal/gitaly/service/ssh/upload_pack_test.go +++ b/internal/gitaly/service/ssh/upload_pack_test.go @@ -76,6 +76,9 @@ func (cmd cloneCommand) test(t *testing.T, localRepoPath string) (string, string err := cmd.execute(t) require.NoError(t, err) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + storagePath := testhelper.GitlabTestStoragePath() testRepoPath := filepath.Join(storagePath, testRepo.GetRelativePath()) @@ -101,6 +104,9 @@ func TestFailedUploadPackRequestDueToTimeout(t *testing.T) { stream, err := client.SSHUploadPack(ctx) require.NoError(t, err) + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + // The first request is not limited by timeout, but also not under attacker control require.NoError(t, stream.Send(&gitalypb.SSHUploadPackRequest{Repository: testRepo})) @@ -200,7 +206,8 @@ func TestUploadPackCloneSuccess(t *testing.T) { ) defer stop() - localRepoPath := filepath.Join(testRepoRoot, "gitlab-test-upload-pack-local") + localRepoPath, cleanup := testhelper.TempDir(t) + defer cleanup() tests := []struct { cmd *exec.Cmd @@ -219,6 +226,9 @@ func TestUploadPackCloneSuccess(t *testing.T) { }, } + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { cmd := cloneCommand{ @@ -249,6 +259,9 @@ func TestUploadPackWithoutSideband(t *testing.T) { pktline.WriteFlush(negotiation) pktline.WriteString(negotiation, "done") + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + request := &gitalypb.SSHUploadPackRequest{ Repository: testRepo, } @@ -306,12 +319,17 @@ func TestUploadPackCloneWithPartialCloneFilter(t *testing.T) { }, } + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { // Run the clone with filtering enabled in both runs. The only // difference is that in the first run, we have the // UploadPackFilter flag disabled. - localPath := filepath.Join(testRepoRoot, fmt.Sprintf("gitlab-test-upload-pack-local-%s", tc.desc)) + localPath, cleanup := testhelper.TempDir(t) + defer cleanup() + cmd := cloneCommand{ repository: testRepo, command: exec.Command(command.GitPath(), append(tc.cloneArgs, localPath)...), @@ -328,7 +346,8 @@ func TestUploadPackCloneWithPartialCloneFilter(t *testing.T) { } func TestUploadPackCloneSuccessWithGitProtocol(t *testing.T) { - localRepoPath := filepath.Join(testRepoRoot, "gitlab-test-upload-pack-local") + localRepoPath, cleanup := testhelper.TempDir(t) + defer cleanup() tests := []struct { cmd *exec.Cmd @@ -344,6 +363,9 @@ func TestUploadPackCloneSuccessWithGitProtocol(t *testing.T) { }, } + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { restore := testhelper.EnableGitProtocolV2Support(t) @@ -374,7 +396,11 @@ func TestUploadPackCloneHideTags(t *testing.T) { serverSocketPath, stop := runSSHServer(t) defer stop() - localRepoPath := filepath.Join(testRepoRoot, "gitlab-test-upload-pack-local-hide-tags") + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + localRepoPath, cleanup := testhelper.TempDir(t) + defer cleanup() cmd := cloneCommand{ repository: testRepo, @@ -396,7 +422,11 @@ func TestUploadPackCloneFailure(t *testing.T) { serverSocketPath, stop := runSSHServer(t) defer stop() - localRepoPath := filepath.Join(testRepoRoot, "gitlab-test-upload-pack-local-failure") + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + + localRepoPath, cleanup := testhelper.TempDir(t) + defer cleanup() cmd := cloneCommand{ repository: &gitalypb.Repository{ diff --git a/internal/helper/repo_test.go b/internal/helper/repo_test.go index 9cd28c874..ba8648ee3 100644 --- a/internal/helper/repo_test.go +++ b/internal/helper/repo_test.go @@ -79,7 +79,9 @@ func TestGetRepoPath(t *testing.T) { config.Config.Storages = oldStorages }(config.Config.Storages) - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + repoPath, err := GetRepoPath(testRepo) if err != nil { t.Fatal(err) @@ -101,12 +103,12 @@ func TestGetRepoPath(t *testing.T) { { desc: "storages configured", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: testhelper.TestRelativePath}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath()}, path: repoPath, }, { desc: "no storage config, storage name provided", - repo: &gitalypb.Repository{StorageName: "does not exist", RelativePath: testhelper.TestRelativePath}, + repo: &gitalypb.Repository{StorageName: "does not exist", RelativePath: testRepo.GetRelativePath()}, err: codes.InvalidArgument, }, { @@ -127,55 +129,55 @@ func TestGetRepoPath(t *testing.T) { { desc: "non existing repo", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "made/up/path.git"}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "made/up/path.git"}, err: codes.NotFound, }, { desc: "non existing storage", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "does not exists", RelativePath: testhelper.TestRelativePath}, + repo: &gitalypb.Repository{StorageName: "does not exists", RelativePath: testRepo.GetRelativePath()}, err: codes.InvalidArgument, }, { desc: "storage defined but storage dir does not exist", - storages: []config.Storage{{Name: "default", Path: "/does/not/exist"}}, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "foobar.git"}, + storages: []config.Storage{{Name: testRepo.GetStorageName(), Path: "/does/not/exist"}}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "foobar.git"}, err: codes.Internal, }, { desc: "relative path with directory traversal", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "../bazqux.git"}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "../bazqux.git"}, err: codes.InvalidArgument, }, { desc: "valid path with ..", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "foo../bazqux.git"}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "foo../bazqux.git"}, err: codes.NotFound, // Because the directory doesn't exist }, { desc: "relative path with sneaky directory traversal", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "/../bazqux.git"}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "/../bazqux.git"}, err: codes.InvalidArgument, }, { desc: "relative path with traversal outside storage", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: testhelper.TestRelativePath + "/../.."}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath() + "/../../../../.."}, err: codes.InvalidArgument, }, { desc: "relative path with traversal outside storage with trailing slash", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: testhelper.TestRelativePath + "/../../"}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath() + "/../../../../../"}, err: codes.InvalidArgument, }, { desc: "relative path with deep traversal at the end", storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "default", RelativePath: "bazqux.git/../.."}, + repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "bazqux.git/../.."}, err: codes.InvalidArgument, }, } @@ -214,7 +216,9 @@ func assertInvalidRepoWithoutFile(t *testing.T, repo *gitalypb.Repository, repoP } func TestGetRepoPathWithCorruptedRepo(t *testing.T) { - testRepo := testhelper.TestRepository() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + testRepoStoragePath := testhelper.GitlabTestStoragePath() testRepoPath := filepath.Join(testRepoStoragePath, testRepo.RelativePath) diff --git a/internal/middleware/commandstatshandler/commandstatshandler_test.go b/internal/middleware/commandstatshandler/commandstatshandler_test.go index 0fbf34954..bc6ecbca2 100644 --- a/internal/middleware/commandstatshandler/commandstatshandler_test.go +++ b/internal/middleware/commandstatshandler/commandstatshandler_test.go @@ -67,6 +67,9 @@ func TestInterceptor(t *testing.T) { require.NoError(t, err) }() + testRepo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() + tests := []struct { name string performRPC func(ctx context.Context, client gitalypb.RefServiceClient) @@ -75,8 +78,7 @@ func TestInterceptor(t *testing.T) { { name: "Unary", performRPC: func(ctx context.Context, client gitalypb.RefServiceClient) { - repo := testhelper.TestRepository() - req := &gitalypb.RefExistsRequest{Repository: repo, Ref: []byte("refs/foo")} + req := &gitalypb.RefExistsRequest{Repository: testRepo, Ref: []byte("refs/foo")} _, err := client.RefExists(ctx, req) require.NoError(t, err) @@ -86,8 +88,7 @@ func TestInterceptor(t *testing.T) { { name: "Stream", performRPC: func(ctx context.Context, client gitalypb.RefServiceClient) { - repo := testhelper.TestRepository() - req := &gitalypb.FindAllBranchNamesRequest{Repository: repo} + req := &gitalypb.FindAllBranchNamesRequest{Repository: testRepo} stream, err := client.FindAllBranchNames(ctx, req) require.NoError(t, err) diff --git a/internal/praefect/replicator_test.go b/internal/praefect/replicator_test.go index 114cfbe14..2629cad84 100644 --- a/internal/praefect/replicator_test.go +++ b/internal/praefect/replicator_test.go @@ -2,7 +2,6 @@ package praefect import ( "context" - "io/ioutil" "net" "os" "path/filepath" @@ -82,10 +81,9 @@ func testMain(m *testing.M) int { func TestReplMgr_ProcessBacklog(t *testing.T) { backupStorageName := "backup" - backupDir, err := ioutil.TempDir(testhelper.GitlabTestStoragePath(), backupStorageName) - require.NoError(t, err) + backupDir, cleanup := testhelper.TempDir(t) + defer cleanup() - defer func() { os.RemoveAll(backupDir) }() defer func(oldStorages []gitaly_config.Storage) { gitaly_config.Config.Storages = oldStorages }(gitaly_config.Config.Storages) gitaly_config.Config.Storages = append(gitaly_config.Config.Storages, @@ -537,9 +535,8 @@ func TestConfirmReplication(t *testing.T) { func TestProcessBacklog_FailedJobs(t *testing.T) { backupStorageName := "backup" - backupDir, err := ioutil.TempDir(testhelper.GitlabTestStoragePath(), backupStorageName) - require.NoError(t, err) - defer os.RemoveAll(backupDir) + backupDir, cleanup := testhelper.TempDir(t) + defer cleanup() defer func(oldStorages []gitaly_config.Storage) { gitaly_config.Config.Storages = oldStorages }(gitaly_config.Config.Storages) gitaly_config.Config.Storages = append(gitaly_config.Config.Storages, gitaly_config.Storage{ @@ -668,9 +665,8 @@ func TestProcessBacklog_FailedJobs(t *testing.T) { func TestProcessBacklog_Success(t *testing.T) { defer func(oldStorages []gitaly_config.Storage) { gitaly_config.Config.Storages = oldStorages }(gitaly_config.Config.Storages) - backupDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(backupDir) + backupDir, cleanup := testhelper.TempDir(t) + defer cleanup() gitaly_config.Config.Storages = append(gitaly_config.Config.Storages, gitaly_config.Storage{ Name: "backup", @@ -740,7 +736,7 @@ func TestProcessBacklog_Success(t *testing.T) { }, } - _, err = queueInterceptor.Enqueue(ctx, eventType1) + _, err := queueInterceptor.Enqueue(ctx, eventType1) require.NoError(t, err) _, err = queueInterceptor.Enqueue(ctx, eventType1) diff --git a/internal/tempdir/tempdir_test.go b/internal/tempdir/tempdir_test.go index 44d8fba36..16e52bdcf 100644 --- a/internal/tempdir/tempdir_test.go +++ b/internal/tempdir/tempdir_test.go @@ -18,7 +18,9 @@ import ( func TestNewAsRepositorySuccess(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - repo := testhelper.TestRepository() + + repo, _, cleanup := testhelper.NewTestRepo(t) + defer cleanup() tempRepo, tempDir, err := NewAsRepository(ctx, repo, config.NewLocator(config.Config)) require.NoError(t, err) diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index df5528bb8..cdfbec1f5 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -47,9 +47,7 @@ import ( "google.golang.org/grpc/status" ) -// TestRelativePath is the path inside its storage of the gitlab-test repo const ( - TestRelativePath = "gitlab-test.git" RepositoryAuthToken = "the-secret-token" DefaultStorageName = "default" testGitEnv = "testdata/git-env" @@ -91,6 +89,10 @@ func Configure() func() { config.Config.Storages = []config.Storage{ {Name: "default", Path: GitlabTestStoragePath()}, } + if err := os.Mkdir(config.Config.Storages[0].Path, 0755); err != nil { + os.RemoveAll(testDirectory) + log.Fatal(err) + } config.Config.SocketPath = "/bogus" config.Config.GitlabShell.Dir = "/" @@ -138,11 +140,10 @@ func MustReadFile(t testing.TB, filename string) []byte { // GitlabTestStoragePath returns the storage path to the gitlab-test repo. func GitlabTestStoragePath() string { - _, currentFile, _, ok := runtime.Caller(0) - if !ok { - log.Fatal("Could not get caller info") + if testDirectory == "" { + log.Fatal("you must call testhelper.Configure() before GitlabTestStoragePath()") } - return filepath.Join(filepath.Dir(currentFile), "testdata", "data") + return filepath.Join(testDirectory, "storage") } // GitalyServersMetadata returns a metadata pair for gitaly-servers to be used in @@ -192,25 +193,6 @@ func isValidRepoPath(absolutePath string) bool { return true } -// TestRepository returns the `Repository` object for the gitlab-test repo. -// Tests should be calling this function instead of cloning the repo themselves. -// Tests that involve modifications to the repo should copy/clone the repo -// via the `Repository` returned from this function. -func TestRepository() *gitalypb.Repository { - repo := &gitalypb.Repository{ - StorageName: "default", - RelativePath: TestRelativePath, - GlRepository: GlRepository, - } - - storagePath, _ := config.Config.StoragePath(repo.GetStorageName()) - if !isValidRepoPath(filepath.Join(storagePath, repo.RelativePath)) { - panic("Test repo not found, did you run `make prepare-tests`?") - } - - return repo -} - // RequireGrpcError asserts the passed err is of the same code as expectedCode. func RequireGrpcError(t testing.TB, err error, expectedCode codes.Code) { t.Helper() @@ -519,7 +501,8 @@ func Context(opts ...ContextOpt) (context.Context, func()) { // CreateRepo creates a temporary directory for a repo, without initializing it func CreateRepo(t testing.TB, storagePath, relativePath string) *gitalypb.Repository { - require.NoError(t, os.MkdirAll(filepath.Dir(storagePath), 0755), "making repo parent dir") + repoPath := filepath.Join(storagePath, relativePath, "..") + require.NoError(t, os.MkdirAll(repoPath, 0755), "making repo parent dir") return &gitalypb.Repository{ StorageName: "default", RelativePath: relativePath, @@ -578,7 +561,12 @@ func NewTestRepoWithWorktree(t testing.TB) (repo *gitalypb.Repository, repoPath // testRepositoryPath returns the absolute path of local 'gitlab-org/gitlab-test.git' clone. // It is cloned under the path by the test preparing step of make. func testRepositoryPath(t testing.TB) string { - path := filepath.Join(GitlabTestStoragePath(), TestRelativePath) + _, currentFile, _, ok := runtime.Caller(0) + if !ok { + log.Fatal("could not get caller info") + } + + path := filepath.Join(filepath.Dir(currentFile), "testdata", "data", "gitlab-test.git") if !isValidRepoPath(path) { t.Fatalf("local clone of 'gitlab-org/gitlab-test.git' not found in %q, did you run `make prepare-tests`?", path) } diff --git a/internal/testhelper/testserver.go b/internal/testhelper/testserver.go index b20415bc8..7b5f8285e 100644 --- a/internal/testhelper/testserver.go +++ b/internal/testhelper/testserver.go @@ -885,11 +885,8 @@ func startSocketHTTPServer(t FatalLogger, mux *http.ServeMux, tlsCfg *tls.Config // CreateTemporaryGitlabShellDir creates a temporary gitlab shell directory. It returns the path to the directory // and a cleanup function func CreateTemporaryGitlabShellDir(t testing.TB) (string, func()) { - tempDir, err := ioutil.TempDir("", "gitlab-shell") - require.NoError(t, err) - return tempDir, func() { - require.NoError(t, os.RemoveAll(tempDir)) - } + tempDir, cleanup := TempDir(t) + return tempDir, cleanup } // WriteTemporaryGitlabShellConfigFile writes a gitlab shell config.yml in a temporary directory. It returns the path |