Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-20 16:10:17 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-26 17:01:45 +0300
commit8180ffdd8867cdb75b68439781b3a6201ea3821c (patch)
tree713e81bd76e6fafda4ee73ade0b350f2cc59bb79
parent0b225ce4a128ed70b761e0e49decd4e2294c9b81 (diff)
tests: Get rid of plain temporary directories and files
Get rid of plain temporary directories and files and instead use the `testhelper.TempDir()` helper function to create them.
-rw-r--r--cmd/gitaly-wrapper/main_test.go15
-rw-r--r--cmd/gitaly-wrapper/testhelper_test.go11
-rw-r--r--internal/gitaly/service/operations/merge_test.go13
-rw-r--r--internal/gitaly/service/repository/archive_test.go17
-rw-r--r--internal/gitaly/service/repository/create_bundle_from_ref_list_test.go3
-rw-r--r--internal/gitaly/storage/metadata_test.go13
-rw-r--r--internal/gitaly/storage/testhelper_test.go11
-rw-r--r--internal/streamcache/pipe_test.go8
8 files changed, 50 insertions, 41 deletions
diff --git a/cmd/gitaly-wrapper/main_test.go b/cmd/gitaly-wrapper/main_test.go
index 8014037bb..abca0aa09 100644
--- a/cmd/gitaly-wrapper/main_test.go
+++ b/cmd/gitaly-wrapper/main_test.go
@@ -4,6 +4,7 @@ import (
"errors"
"os"
"os/exec"
+ "path/filepath"
"strconv"
"testing"
@@ -14,21 +15,23 @@ import (
// TestStolenPid tests for regressions in https://gitlab.com/gitlab-org/gitaly/issues/1661
func TestStolenPid(t *testing.T) {
- defer func(oldValue string) {
- require.NoError(t, os.Setenv(bootstrap.EnvPidFile, oldValue))
- }(os.Getenv(bootstrap.EnvPidFile))
+ tempDir := testhelper.TempDir(t)
- pidFile, err := os.CreateTemp("", "pidfile")
+ pidFile, err := os.Create(filepath.Join(tempDir, "pidfile"))
require.NoError(t, err)
- defer func() { require.NoError(t, os.Remove(pidFile.Name())) }()
- require.NoError(t, os.Setenv(bootstrap.EnvPidFile, pidFile.Name()))
+ cleanup := testhelper.ModifyEnvironment(t, bootstrap.EnvPidFile, pidFile.Name())
+ defer cleanup()
ctx, cancel := testhelper.Context()
defer cancel()
cmd := exec.CommandContext(ctx, "tail", "-f")
require.NoError(t, cmd.Start())
+ defer func() {
+ cancel()
+ _ = cmd.Wait()
+ }()
_, err = pidFile.WriteString(strconv.Itoa(cmd.Process.Pid))
require.NoError(t, err)
diff --git a/cmd/gitaly-wrapper/testhelper_test.go b/cmd/gitaly-wrapper/testhelper_test.go
new file mode 100644
index 000000000..64d8edae5
--- /dev/null
+++ b/cmd/gitaly-wrapper/testhelper_test.go
@@ -0,0 +1,11 @@
+package main
+
+import (
+ "testing"
+
+ "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
+)
+
+func TestMain(m *testing.M) {
+ testhelper.Run(m)
+}
diff --git a/internal/gitaly/service/operations/merge_test.go b/internal/gitaly/service/operations/merge_test.go
index c60b3b236..a191bfa2d 100644
--- a/internal/gitaly/service/operations/merge_test.go
+++ b/internal/gitaly/service/operations/merge_test.go
@@ -5,8 +5,8 @@ import (
"errors"
"fmt"
"io"
- "os"
"os/exec"
+ "path/filepath"
"regexp"
"strings"
"testing"
@@ -56,18 +56,17 @@ func TestUserMergeBranch_successful(t *testing.T) {
gittest.Exec(t, cfg, "-C", repoPath, "branch", mergeBranchName, mergeBranchHeadBefore)
+ tempDir := testhelper.TempDir(t)
+
hooks := GitlabHooks
hookTempfiles := make([]string, len(hooks))
for i, hook := range hooks {
- outputFile, err := os.CreateTemp("", "")
- require.NoError(t, err)
- require.NoError(t, outputFile.Close())
- defer func() { require.NoError(t, os.Remove(outputFile.Name())) }()
+ outputFile := filepath.Join(tempDir, hook)
- script := fmt.Sprintf("#!/bin/sh\n(cat && env) >%s \n", outputFile.Name())
+ script := fmt.Sprintf("#!/bin/sh\n(cat && env) >%s \n", outputFile)
gittest.WriteCustomHook(t, repoPath, hook, []byte(script))
- hookTempfiles[i] = outputFile.Name()
+ hookTempfiles[i] = outputFile
}
mergeCommitMessage := "Merged by Gitaly"
diff --git a/internal/gitaly/service/repository/archive_test.go b/internal/gitaly/service/repository/archive_test.go
index b80e16992..83b7258d7 100644
--- a/internal/gitaly/service/repository/archive_test.go
+++ b/internal/gitaly/service/repository/archive_test.go
@@ -147,9 +147,8 @@ func TestGetArchiveSuccess(t *testing.T) {
data, err := consumeArchive(stream)
require.NoError(t, err)
- archiveFile, err := os.CreateTemp("", "")
+ archiveFile, err := os.Create(filepath.Join(testhelper.TempDir(t), "archive"))
require.NoError(t, err)
- defer func() { require.NoError(t, os.Remove(archiveFile.Name())) }()
_, err = archiveFile.Write(data)
require.NoError(t, err)
@@ -475,17 +474,9 @@ func TestGetArchivePathInjection(t *testing.T) {
func TestGetArchiveEnv(t *testing.T) {
t.Parallel()
- tmpFile, err := os.CreateTemp("", "archive.sh")
- require.NoError(t, err)
- defer func() { require.NoError(t, os.Remove(tmpFile.Name())) }()
-
- err = tmpFile.Chmod(0o755)
- require.NoError(t, err)
- _, err = tmpFile.Write([]byte(`#!/bin/sh
-env | grep -E "^GL_|CORRELATION|GITALY_"`))
- require.NoError(t, err)
- require.NoError(t, tmpFile.Close())
+ scriptPath := filepath.Join(testhelper.TempDir(t), "archive.sh")
+ require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\nenv | grep -E '^GL_|CORRELATION|GITALY_'"), 0o755))
cfg := testcfg.Build(t)
@@ -495,7 +486,7 @@ env | grep -E "^GL_|CORRELATION|GITALY_"`))
// This replacement only needs to be done for the configuration used to invoke git commands.
// Other operations should use actual path to the git binary to work properly.
spyGitCfg := cfg
- spyGitCfg.Git.BinPath = tmpFile.Name()
+ spyGitCfg.Git.BinPath = scriptPath
serverSocketPath := runRepositoryServerWithConfig(t, spyGitCfg, nil)
cfg.SocketPath = serverSocketPath
diff --git a/internal/gitaly/service/repository/create_bundle_from_ref_list_test.go b/internal/gitaly/service/repository/create_bundle_from_ref_list_test.go
index 382fce605..ad787c9bf 100644
--- a/internal/gitaly/service/repository/create_bundle_from_ref_list_test.go
+++ b/internal/gitaly/service/repository/create_bundle_from_ref_list_test.go
@@ -55,9 +55,8 @@ func TestCreateBundleFromRefList_success(t *testing.T) {
return response.GetData(), err
})
- bundle, err := os.CreateTemp("", "bundle")
+ bundle, err := os.Create(filepath.Join(testhelper.TempDir(t), "bundle"))
require.NoError(t, err)
- defer func() { require.NoError(t, os.Remove(bundle.Name())) }()
_, err = io.Copy(bundle, reader)
require.NoError(t, err)
diff --git a/internal/gitaly/storage/metadata_test.go b/internal/gitaly/storage/metadata_test.go
index a70846045..628480592 100644
--- a/internal/gitaly/storage/metadata_test.go
+++ b/internal/gitaly/storage/metadata_test.go
@@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func readFilesystemID(t *testing.T, path string) string {
@@ -22,22 +23,14 @@ func readFilesystemID(t *testing.T, path string) string {
}
func TestWriteMetdataFile(t *testing.T) {
- tempDir, err := os.MkdirTemp("", t.Name())
- require.NoError(t, err)
- defer func() {
- require.NoError(t, os.RemoveAll(tempDir))
- }()
+ tempDir := testhelper.TempDir(t)
require.NoError(t, WriteMetadataFile(tempDir))
require.NotEmpty(t, readFilesystemID(t, tempDir))
}
func TestWriteMetadataFile_AlreadyExists(t *testing.T) {
- tempDir, err := os.MkdirTemp("", t.Name())
- require.NoError(t, err)
- defer func() {
- require.NoError(t, os.RemoveAll(tempDir))
- }()
+ tempDir := testhelper.TempDir(t)
metadataPath := filepath.Join(tempDir, ".gitaly-metadata")
metadataFile, err := os.Create(metadataPath)
diff --git a/internal/gitaly/storage/testhelper_test.go b/internal/gitaly/storage/testhelper_test.go
new file mode 100644
index 000000000..61b45bdb5
--- /dev/null
+++ b/internal/gitaly/storage/testhelper_test.go
@@ -0,0 +1,11 @@
+package storage
+
+import (
+ "testing"
+
+ "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
+)
+
+func TestMain(m *testing.M) {
+ testhelper.Run(m)
+}
diff --git a/internal/streamcache/pipe_test.go b/internal/streamcache/pipe_test.go
index 17c73f9fb..311e23415 100644
--- a/internal/streamcache/pipe_test.go
+++ b/internal/streamcache/pipe_test.go
@@ -5,18 +5,20 @@ import (
"io"
"math/rand"
"os"
+ "path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func createPipe(t *testing.T) (*pipeReader, *pipe) {
t.Helper()
- f, err := os.CreateTemp("", "gitaly-streamcache-test")
+ f, err := os.Create(filepath.Join(testhelper.TempDir(t), "gitaly-streamcache-test"))
require.NoError(t, err)
pr, p, err := newPipe(f)
@@ -200,7 +202,7 @@ func (cs *closeSpy) Close() error {
// Closing the last reader _before_ closing the writer is a failure
// condition. After this happens, opening new readers should fail.
func TestPipe_closeWrongOrder(t *testing.T) {
- f, err := os.CreateTemp("", "gitaly-streamcache-test")
+ f, err := os.Create(filepath.Join(testhelper.TempDir(t), "gitaly-streamcache-test"))
require.NoError(t, err)
cs := &closeSpy{namedWriteCloser: f}
@@ -233,7 +235,7 @@ func TestPipe_closeWrongOrder(t *testing.T) {
// Closing last reader after closing the writer is the happy path. After
// this happens, opening new readers should work.
func TestPipe_closeOrderHappy(t *testing.T) {
- f, err := os.CreateTemp("", "gitaly-streamcache-test")
+ f, err := os.Create(filepath.Join(testhelper.TempDir(t), "gitaly-streamcache-test"))
require.NoError(t, err)
cs := &closeSpy{namedWriteCloser: f}